Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure an empty config file load as an empty dict
- Loading branch information
1 parent
c82b323
commit 1346f3f
Showing
8 changed files
with
208 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
from json import dump # noqa | ||
from json import load # noqa | ||
from json import dump as _dump | ||
from json import load as _load | ||
from json.decoder import JSONDecodeError | ||
|
||
|
||
def dump(content, f): | ||
return _dump(content, f) | ||
|
||
|
||
def load(f): | ||
try: | ||
content = _load(f) | ||
except JSONDecodeError: | ||
content = {} | ||
return content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from io import StringIO | ||
|
||
|
||
class BaseTestLoad: | ||
module = None | ||
|
||
def test_empty(self): | ||
stream = StringIO("") | ||
loaded = self.module.load(stream) | ||
assert loaded == {}, "Blank file should load as an empty dict" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from io import StringIO | ||
|
||
import pytest | ||
|
||
from resconfig.io import json | ||
|
||
from .bases import BaseTestLoad | ||
|
||
content = """ | ||
{ | ||
"str": "str", | ||
"int": 10, | ||
"bool": true, | ||
"null": null, | ||
"array": [0, 1, 2], | ||
"nested": { | ||
"str": "mystr", | ||
"int": 10 | ||
} | ||
} | ||
""" | ||
|
||
|
||
@pytest.fixture | ||
def stream(): | ||
yield StringIO(content) | ||
|
||
|
||
@pytest.fixture | ||
def loaded(stream): | ||
yield json.load(stream) | ||
|
||
|
||
@pytest.fixture | ||
def dumped(loaded, stream): | ||
json.dump(loaded, stream) | ||
stream.seek(0) | ||
yield stream.read() | ||
|
||
|
||
class TestLoad(BaseTestLoad): | ||
module = json | ||
|
||
def test_integer(self, loaded): | ||
assert loaded["int"] == 10 | ||
|
||
def test_string(self, loaded): | ||
assert loaded["str"] == "str" | ||
|
||
def test_bool(self, loaded): | ||
assert loaded["bool"] is True | ||
|
||
def test_null(self, loaded): | ||
assert loaded["null"] is None | ||
|
||
def test_array(self, loaded): | ||
assert loaded["array"] == [0, 1, 2] | ||
|
||
def test_nested(self, loaded): | ||
assert loaded["nested"]["int"] == 10 | ||
|
||
|
||
class TestDump: | ||
def test_string(self, dumped): | ||
assert '"str": "str"' in dumped | ||
|
||
def test_integer(self, dumped): | ||
assert '"int": 10' in dumped | ||
|
||
def test_bool(self, dumped): | ||
assert '"bool": true' in dumped | ||
|
||
def test_null(self, dumped): | ||
assert '"null": null' in dumped | ||
|
||
def test_array(self, dumped): | ||
assert '"array": [0, 1, 2]' in dumped | ||
|
||
def test_nested(self, dumped): | ||
assert '"nested": {' in dumped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from io import StringIO | ||
|
||
import pytest | ||
|
||
from resconfig.io import yaml | ||
|
||
from .bases import BaseTestLoad | ||
|
||
content = """ | ||
--- | ||
str: str | ||
int: 10 | ||
bool: true | ||
null: null | ||
array: [0, 1, 2] | ||
nested: | ||
str: mystr | ||
int: 10 | ||
""" | ||
|
||
|
||
@pytest.fixture | ||
def stream(): | ||
yield StringIO(content) | ||
|
||
|
||
@pytest.fixture | ||
def loaded(stream): | ||
yield yaml.load(stream) | ||
|
||
|
||
@pytest.fixture | ||
def dumped(loaded, stream): | ||
yaml.dump(loaded, stream) | ||
stream.seek(0) | ||
yield stream.read() | ||
|
||
|
||
class TestLoad(BaseTestLoad): | ||
module = yaml | ||
|
||
def test_integer(self, loaded): | ||
assert loaded["int"] == 10 | ||
|
||
def test_string(self, loaded): | ||
assert loaded["str"] == "str" | ||
|
||
def test_bool(self, loaded): | ||
assert loaded["bool"] is True | ||
|
||
def test_null(self, loaded): | ||
assert loaded[None] is None | ||
|
||
def test_array(self, loaded): | ||
assert loaded["array"] == [0, 1, 2] | ||
|
||
def test_nested(self, loaded): | ||
assert loaded["nested"]["int"] == 10 | ||
|
||
|
||
class TestDump: | ||
def test_string(self, dumped): | ||
assert "str: str" in dumped | ||
|
||
def test_integer(self, dumped): | ||
assert "int: 10" in dumped | ||
|
||
def test_bool(self, dumped): | ||
assert "bool: true" in dumped | ||
|
||
def test_null(self, dumped): | ||
assert "null: null" in dumped | ||
|
||
def test_array(self, dumped): | ||
assert "array:\n- 0\n- 1\n- 2\n" in dumped | ||
|
||
def test_nested(self, dumped): | ||
assert "nested:" in dumped |