-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 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,5 +1,28 @@ | ||
import json | ||
from typing import List, Union, IO | ||
|
||
|
||
class BuildingBlocks: | ||
def __init__(self): | ||
self.blocks = {} | ||
self.hash_map = {} | ||
|
||
|
||
class JSONBundle: | ||
def __init__(self, source_data: List[Union[str, bytes, IO[str]]]): | ||
self.data = [self.load_json(element) for element in source_data] | ||
|
||
@staticmethod | ||
def load_json(element: Union[str, bytes, IO[str]]) -> dict: | ||
if isinstance(element, str): | ||
return json.loads(element) | ||
elif isinstance(element, bytes): | ||
return json.loads(element.decode('utf-8')) | ||
else: | ||
return json.loads(element.read()) | ||
|
||
@classmethod | ||
def from_preprocessed(cls, preprocessed_data: List[dict]): | ||
instance = cls.__new__(cls) | ||
instance.data = preprocessed_data | ||
return instance |
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,63 @@ | ||
import io | ||
import json | ||
import unittest | ||
|
||
from json_mason.models import JSONBundle | ||
|
||
|
||
class TestJSONBundle(unittest.TestCase): | ||
def setUp(self): | ||
self.preprocessed_data = [ | ||
{ | ||
"int": 1, | ||
"float": 1.1, | ||
"string": "test", | ||
"bool": True, | ||
"array": [1, 1.1, "test", True], | ||
"object": { | ||
"int": 2, | ||
"float": 2.2, | ||
"string": "test2", | ||
"bool": False | ||
} | ||
}, | ||
{ | ||
"int": 3, | ||
"float": 3.3, | ||
"string": "test3", | ||
"bool": True, | ||
"array": [3, 3.3, "test3", True], | ||
"object": { | ||
"int": 4, | ||
"float": 4.4, | ||
"string": "test4", | ||
"bool": False | ||
} | ||
} | ||
] | ||
self.str_data = [json.dumps(data) for data in self.preprocessed_data] | ||
self.bytes_data = [data.encode('utf-8') for data in self.str_data] | ||
self.file_like_data = [io.StringIO(data) for data in self.str_data] | ||
|
||
def test_init_with_str_data(self): | ||
bundle = JSONBundle(self.str_data) | ||
self.assertEqual(bundle.data, self.preprocessed_data) | ||
|
||
def test_init_with_bytes_data(self): | ||
bundle = JSONBundle(self.bytes_data) | ||
self.assertEqual(bundle.data, self.preprocessed_data) | ||
|
||
def test_init_with_file_like_data(self): | ||
bundle = JSONBundle(self.file_like_data) | ||
self.assertEqual(bundle.data, self.preprocessed_data) | ||
|
||
def test_init_with_mixed_data(self): | ||
bundle = JSONBundle(self.str_data + self.bytes_data + self.file_like_data) | ||
self.assertEqual(bundle.data, self.preprocessed_data * 3) | ||
|
||
def test_from_preprocessed(self): | ||
bundle = JSONBundle.from_preprocessed(self.preprocessed_data) | ||
self.assertEqual(bundle.data, self.preprocessed_data) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |