Skip to content

Commit

Permalink
JSON loader built
Browse files Browse the repository at this point in the history
  • Loading branch information
abradner committed Jun 18, 2024
1 parent 477a6af commit c1265ce
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
23 changes: 23 additions & 0 deletions json_mason/models.py
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
20 changes: 20 additions & 0 deletions json_mason/tests/test_mason.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ def test_decompress(self):
self.assertIsInstance(decompressed_data, list)
# Add more assertions to verify the content of decompressed_data

def test_parse_json():
json_input = '{"name": "JSONMason", "active": true}'
expected_output = {"name": "JSONMason", "active": True}
assert parse_json(json_input) == expected_output

def test_find_common_structures():
data = [
{"name": "JSONMason", "active": True},
{"name": "JSONMason", "active": False}
]
# Assuming find_common_structures returns a list of common keys
assert 'name' in find_common_structures(data)
assert 'active' in find_common_structures(data)

def test_build_blocks_integration():
source_data = ['{"name": "JSONMason", "active": true}', '{"name": "JSONMason", "active": false}']
blocks = JSONMason.build_blocks(source_data)
# Test if blocks contain the correct structures
assert 'name' in blocks.keys()


if __name__ == '__main__':
unittest.main()
63 changes: 63 additions & 0 deletions json_mason/tests/test_models.py
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()

0 comments on commit c1265ce

Please sign in to comment.