Skip to content

Commit cbd50d6

Browse files
authored
Merge pull request #57 from DistributedTaskScheduling/unit-tests
Unit tests
2 parents b82edbf + 6eb9b13 commit cbd50d6

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
language: python
22
python: 3.6
33

4-
install: "pip3 install flake8 mypy"
4+
install: "pip3 install flake8 mypy PyYAML"
55
script:
66
- flake8 src/ --ignore F821 --max-line-length=120
77
- ./.travis/check_init_py.sh src/
88
- MYPYPATH=$(pwd)/src mypy -p ja --no-strict-optional --strict
9+
- MYPYPATH=$(pwd)/src mypy -p test --no-strict-optional --strict
10+
- cd src && ./setup.py test

src/ja/common/message/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
from abc import ABC, abstractmethod
77
from typing import Dict
8+
import yaml
89

910

1011
class Serializable(ABC):
@@ -36,6 +37,9 @@ def __str__(self) -> str:
3637
"""!
3738
@return A YAML document (string) representation of this object.
3839
"""
40+
as_yaml = yaml.dump(self.to_dict())
41+
assert isinstance(as_yaml, str)
42+
return as_yaml
3943

4044
@classmethod
4145
def from_string(cls, yaml_string: str) -> "Serializable":
@@ -45,6 +49,9 @@ def from_string(cls, yaml_string: str) -> "Serializable":
4549
@return A new Serializable object based on the properties encoded in
4650
the YAML document.
4751
"""
52+
as_dict = yaml.load(yaml_string, Loader=yaml.SafeLoader)
53+
assert isinstance(as_dict, dict)
54+
return cls.from_dict(as_dict)
4855

4956

5057
class Message(Serializable, ABC):

src/setup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
5+
from setuptools import find_packages, setup
6+
7+
8+
def discover_ja_tests():
9+
_tl = unittest.TestLoader()
10+
_ts = _tl.discover('test', '*.py')
11+
return _ts
12+
13+
14+
setup(
15+
name='jobadder',
16+
version="0.0.1",
17+
description='JobAdder: a package for priority-based scheduling of Docker jobs to mwork machines',
18+
long_description='',
19+
author='Johannes Gäßler, ', # TODO add everyone else
20+
author_email='johannesg@5d6.de, ', # TODO add everyone else
21+
url='https://github.com/DistributedTaskScheduling/JobAdder',
22+
packages=find_packages(),
23+
package_data={},
24+
scripts=[],
25+
test_suite='setup.discover_ja_tests',
26+
keywords=[],
27+
license='GPL3',
28+
install_requires=[],
29+
classifiers=[],
30+
)

src/test/__init__.py

Whitespace-only changes.

src/test/serializable/__init__.py

Whitespace-only changes.

src/test/serializable/base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from ja.common.message.base import Serializable
2+
from typing import Dict
3+
from unittest import TestCase
4+
5+
6+
class SimpleSerializable(Serializable):
7+
def __init__(self, x: int) -> None:
8+
self.x = x
9+
10+
def to_dict(self) -> Dict[str, object]:
11+
return {'x': self.x}
12+
13+
@classmethod
14+
def from_dict(cls, property_dict: Dict[str, object]) -> "SimpleSerializable":
15+
assert len(property_dict) == 1
16+
assert 'x' in property_dict
17+
assert isinstance(property_dict['x'], int)
18+
return SimpleSerializable(property_dict['x'])
19+
20+
def __eq__(self, other: object) -> bool:
21+
if not isinstance(other, SimpleSerializable):
22+
return False
23+
24+
return self.x == other.x
25+
26+
27+
class SerializableTest(TestCase):
28+
def test_simple_serializable(self) -> None:
29+
simple5 = SimpleSerializable(5)
30+
self.assertEqual(simple5, SimpleSerializable.from_string(str(simple5)))
31+
32+
string_repr = 'x: 5'
33+
self.assertEqual(SimpleSerializable.from_string(string_repr), simple5)

0 commit comments

Comments
 (0)