def serialize_as_dict(stage: Stage) -> Dict[str, Any]: data = transform.as_dict(stage) return { **data, "added_files": [file.serialize_as_dict(x) for x in stage.added_files], "updated_files": [file.serialize_as_dict(x) for x in stage.updated_files], "removed_files": [file.serialize_as_dict(x) for x in stage.removed_files], }
def serialize_as_dict(differences: Differences) -> Dict[str, Any]: data = transform.as_dict(differences) return { **data, "added_files": [file.serialize_as_dict(x) for x in differences.added_files], "updated_files": [file.serialize_as_dict(x) for x in differences.updated_files], "removed_files": [file.serialize_as_dict(x) for x in differences.removed_files], }
def test_transform_as_list(self): file_a_instance = File(Path("file_a")) file_b_instance = File(Path("foobar/file_b")) directory_instance = Directory( {"foobar": Directory({}, {"file_b": file_b_instance})}, {"file_a": file_a_instance}, ) expected_result = [ file.serialize_as_dict(file_b_instance), file.serialize_as_dict(file_a_instance), ] result = [ file.serialize_as_dict(x) for x in directory.transform_as_list(Path(), directory_instance) ] self.assertListEqual(result, expected_result)
def serialize_as_dict(directory: Directory) -> Dict[str, Any]: return { **transform.as_dict(directory), "directories": { key: serialize_as_dict(value) for key, value in directory.directories.items() }, "files": { key: file.serialize_as_dict(value) for key, value in directory.files.items() }, }
def test_load_from_blob(self): file_hashid = "" with tempfile.TemporaryDirectory() as tmpdirname: file_hashid = file.store_as_blob(Path(tmpdirname), file_instance) result_file_instance = file.load_from_blob(Path(tmpdirname), file_hashid) blob_path = Path(tmpdirname).joinpath( transform.hashid_as_path(file_hashid)) expected_result_file_instance = File( blob_path, True, blob_path, file_hashid, ) self.assertEqual( file.serialize_as_dict(result_file_instance), file.serialize_as_dict(expected_result_file_instance), )
def test_transform_from_list(self): file_instance = File(Path("test/foobar")) data = [file_instance] expected_result = { "directories": { "test": { "directories": {}, "files": { "foobar": file.serialize_as_dict(file_instance) }, } }, "files": {}, } result = directory.serialize_as_dict( directory.transform_from_list(Path(), data)) self.assertEqual(result, expected_result)
def test_load_from_directory_path(self): directory_instance = Directory() fake_file_path = Path() with tempfile.TemporaryDirectory() as tmpdirname: ignore_file_path = Path(tmpdirname).joinpath(".ignore") # add ignore file with open(ignore_file_path, "w") as f: f.write("\n".join(["*", "^*.c4d"])) # create subdirectory "test" test_directory_path = Path(tmpdirname).joinpath("test") os.makedirs(test_directory_path) # create fake binary file fake_file_path = test_directory_path.joinpath("foo.c4d") with open(fake_file_path, "wb") as f: f.write(b"fake binary data") directory_instance = directory.load_from_directory_path( Path(tmpdirname)) expected_result = { "directories": { "test": { "directories": {}, "files": { "foo.c4d": file.serialize_as_dict(File(fake_file_path)) }, } }, "files": {}, } result = directory.serialize_as_dict(directory_instance) self.assertDictEqual(result, expected_result)
def test_deserialize_from_dict(self): result = file.deserialize_from_dict(expected_result) self.assertEqual(file.serialize_as_dict(result), expected_result)
def test_serialize_as_dict(self): self.assertEqual(file.serialize_as_dict(file_instance), expected_result)
def test_file_datatype(self): self.assertDictEqual( file.serialize_as_dict(file_instance), expected_result, )