def test_codebook_serialization(): """ Test that codebook can be saved to disk and recovered, and that the recovered codebook is identical to the one that it was serialized from. """ # Create a codebook codebook_array = codebook_json_data_factory() codebook = Codebook.from_code_array(codebook_array) # Dump it to a temporary file with tempfile.TemporaryDirectory() as directory: json_codebook = os.path.join(directory, 'codebook.json') codebook.to_json(json_codebook) # Retrieve it and test that the data it contains has not changed codebook_reloaded = Codebook.from_json(json_codebook) assert codebook_reloaded.equals(codebook)
def test_codebook_loads_from_local_file() -> None: """ dumps the codebook data to a temporary json file and reads it back into a Codebook, verifying that the data has not changed. """ # dump codebook to disk codebook_data: List = codebook_json_data_factory() with tempfile.TemporaryDirectory() as directory: codebook_json: str = os.path.join(directory, 'simple_codebook.json') with open(codebook_json, 'w') as f: json.dump(codebook_data, f) # load the codebook codebook = Codebook.from_json(codebook_json) assert codebook.sizes[Indices.ROUND] == 2 assert codebook.sizes[Indices.CH] == 3 assert codebook.sizes[Features.TARGET] == 2