From eaf77dac534931cfdaf8f08c53dff740582c5418 Mon Sep 17 00:00:00 2001 From: Tony Tung Date: Tue, 12 Nov 2019 22:38:48 -0800 Subject: [PATCH] [easy] Test for Codebook.to_json / open_json This verifies that the two operations are symmetric. Test plan: this is the test. --- starfish/core/codebook/codebook.py | 5 +++-- starfish/core/codebook/test/test_to_json.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 starfish/core/codebook/test/test_to_json.py diff --git a/starfish/core/codebook/codebook.py b/starfish/core/codebook/codebook.py index e8cddb714..4ab640bfd 100644 --- a/starfish/core/codebook/codebook.py +++ b/starfish/core/codebook/codebook.py @@ -1,5 +1,6 @@ import json import uuid +from pathlib import Path from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, TypeVar, Union import numpy as np @@ -388,13 +389,13 @@ def open_json( return cls.from_code_array(codebook_doc[DocumentKeys.MAPPINGS_KEY], n_round, n_channel) - def to_json(self, filename: str) -> None: + def to_json(self, filename: Union[str, Path]) -> None: """ Save a codebook to json using SpaceTx Format. Parameters ---------- - filename : str + filename : Union[str, Path] The name of the file in which to save the codebook. """ diff --git a/starfish/core/codebook/test/test_to_json.py b/starfish/core/codebook/test/test_to_json.py new file mode 100644 index 000000000..893b623bc --- /dev/null +++ b/starfish/core/codebook/test/test_to_json.py @@ -0,0 +1,14 @@ +import os + +from .factories import simple_codebook_array +from ..codebook import Codebook + + +def test_to_json(tmp_path): + code_array = simple_codebook_array() + codebook = Codebook.from_code_array(code_array) + codebook_path = tmp_path / "codebook.json" + codebook.to_json(codebook_path) + + loaded_codebook = Codebook.open_json(os.fspath(codebook_path)) + assert codebook.equals(loaded_codebook)