Skip to content

Unit tests #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: python
python: 3.6

install: "pip3 install flake8 mypy"
install: "pip3 install flake8 mypy PyYAML"
script:
- flake8 src/ --ignore F821 --max-line-length=120
- ./.travis/check_init_py.sh src/
- MYPYPATH=$(pwd)/src mypy -p ja --no-strict-optional --strict
- MYPYPATH=$(pwd)/src mypy -p test --no-strict-optional --strict
- cd src && ./setup.py test
7 changes: 7 additions & 0 deletions src/ja/common/message/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
from abc import ABC, abstractmethod
from typing import Dict
import yaml


class Serializable(ABC):
Expand Down Expand Up @@ -36,6 +37,9 @@ def __str__(self) -> str:
"""!
@return A YAML document (string) representation of this object.
"""
as_yaml = yaml.dump(self.to_dict())
assert isinstance(as_yaml, str)
return as_yaml

@classmethod
def from_string(cls, yaml_string: str) -> "Serializable":
Expand All @@ -45,6 +49,9 @@ def from_string(cls, yaml_string: str) -> "Serializable":
@return A new Serializable object based on the properties encoded in
the YAML document.
"""
as_dict = yaml.load(yaml_string, Loader=yaml.SafeLoader)
assert isinstance(as_dict, dict)
return cls.from_dict(as_dict)


class Message(Serializable, ABC):
Expand Down
30 changes: 30 additions & 0 deletions src/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3

import unittest

from setuptools import find_packages, setup


def discover_ja_tests():
_tl = unittest.TestLoader()
_ts = _tl.discover('test', '*.py')
return _ts


setup(
name='jobadder',
version="0.0.1",
description='JobAdder: a package for priority-based scheduling of Docker jobs to mwork machines',
long_description='',
author='Johannes Gäßler, ', # TODO add everyone else
author_email='johannesg@5d6.de, ', # TODO add everyone else
url='https://github.com/DistributedTaskScheduling/JobAdder',
packages=find_packages(),
package_data={},
scripts=[],
test_suite='setup.discover_ja_tests',
keywords=[],
license='GPL3',
install_requires=[],
classifiers=[],
)
Empty file added src/test/__init__.py
Empty file.
Empty file.
33 changes: 33 additions & 0 deletions src/test/serializable/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from ja.common.message.base import Serializable
from typing import Dict
from unittest import TestCase


class SimpleSerializable(Serializable):
def __init__(self, x: int) -> None:
self.x = x

def to_dict(self) -> Dict[str, object]:
return {'x': self.x}

@classmethod
def from_dict(cls, property_dict: Dict[str, object]) -> "SimpleSerializable":
assert len(property_dict) == 1
assert 'x' in property_dict
assert isinstance(property_dict['x'], int)
return SimpleSerializable(property_dict['x'])

def __eq__(self, other: object) -> bool:
if not isinstance(other, SimpleSerializable):
return False

return self.x == other.x


class SerializableTest(TestCase):
def test_simple_serializable(self) -> None:
simple5 = SimpleSerializable(5)
self.assertEqual(simple5, SimpleSerializable.from_string(str(simple5)))

string_repr = 'x: 5'
self.assertEqual(SimpleSerializable.from_string(string_repr), simple5)