diff --git a/.travis.yml b/.travis.yml index 43269f1c..e8a8fabf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/ja/common/message/base.py b/src/ja/common/message/base.py index aacd9a9b..40be8d5a 100644 --- a/src/ja/common/message/base.py +++ b/src/ja/common/message/base.py @@ -5,6 +5,7 @@ """ from abc import ABC, abstractmethod from typing import Dict +import yaml class Serializable(ABC): @@ -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": @@ -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): diff --git a/src/setup.py b/src/setup.py new file mode 100755 index 00000000..5262b6b7 --- /dev/null +++ b/src/setup.py @@ -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=[], +) diff --git a/src/test/__init__.py b/src/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/test/serializable/__init__.py b/src/test/serializable/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/test/serializable/base.py b/src/test/serializable/base.py new file mode 100644 index 00000000..b2341db7 --- /dev/null +++ b/src/test/serializable/base.py @@ -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)