Skip to content

Commit

Permalink
Move Pegasus.json and Pegasus.braindump to pegasus-common package. Fi…
Browse files Browse the repository at this point in the history
…x minor issue with Pegasus.json.load_all. Add more test cases
  • Loading branch information
mayani committed Jul 10, 2020
1 parent 9e2b216 commit 4d5011c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/pegasus-common/setup.py
Expand Up @@ -7,6 +7,7 @@
home_dir = os.path.abspath(os.path.join(src_dir, "../.."))

install_requires = [
"attrs",
"PyYAML",
]

Expand Down
Expand Up @@ -71,7 +71,7 @@ def load_all(s, *args, **kwargs) -> Iterator:
else:
raise TypeError("s must either be a string or an open text file")

for d in fp.readline():
for d in fp.readlines():
yield loads(d.strip(), *args, **kwargs)


Expand Down
@@ -1,8 +1,25 @@
import io
from enum import Enum
from pathlib import Path
from uuid import UUID

import pytest

from Pegasus.json import dump_all, dumps, loads
from Pegasus.json import dump_all, dumps, load_all, loads


class _Color(Enum):
RED = 1


class _Html:
def __html__(self):
return "html"


class _Json:
def __json__(self):
return "json"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -37,10 +54,26 @@ def test_dumps(obj, expected):
assert dumps(obj) == expected


@pytest.mark.parametrize(
"obj, expected", [('{"key": 1}\n{"key": 2}', [{"key": 1}, {"key": 2}])],
)
def test_load_all(obj, expected):
"""Test :meth:`Pegasus.json.load_all`."""
assert list(load_all(obj)) == expected
assert list(load_all(io.StringIO(obj))) == expected


@pytest.mark.parametrize(
"obj, expected",
[
({"key": 1}, '{"key": 1}\n'),
({"key": _Color.RED}, '{"key": "RED"}\n'),
(
{"key": UUID("{12345678-1234-5678-1234-567812345678}")},
'{"key": "12345678-1234-5678-1234-567812345678"}\n',
),
({"key": _Html()}, '{"key": "html"}\n'),
({"key": _Json()}, '{"key": "json"}\n'),
({"key": "2018-10-10"}, '{"key": "2018-10-10"}\n'),
({"key": "yes"}, '{"key": "yes"}\n'),
({"key": True}, '{"key": true}\n'),
Expand All @@ -51,3 +84,11 @@ def test_dumps(obj, expected):
def test_dump_all(obj, expected):
"""Test :meth:`Pegasus.json.dumps`."""
assert dump_all([obj]) == expected

out = io.StringIO()
dump_all([obj], out)
assert out.getvalue() == expected

with pytest.raises(TypeError) as e:
dump_all([obj], 1)
assert "s must either be None or an open text file" in str(e.value)
3 changes: 0 additions & 3 deletions packages/pegasus-python/setup.py
Expand Up @@ -8,9 +8,6 @@

install_requires = [
# Utils
# TODO: Replace attrs with the dataclasses module, when min Python version is >= 3.6
"attrs",
# 'dataclasses;python_version=="3.6"',
# DAX/Workflow
"PyYAML",
# pegasus-init
Expand Down

0 comments on commit 4d5011c

Please sign in to comment.