feat: adds tests for serialisation to and from json and shows can be done
This commit is contained in:
parent
9278d4cd40
commit
0a51d12cc0
@ -6,14 +6,19 @@ from enum import Enum
|
||||
import logging
|
||||
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
# stuff for generating random dipoles from parameters
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
class Orientation(Enum):
|
||||
XY = 1
|
||||
Z = 2
|
||||
RANDOM = 3
|
||||
|
||||
class Orientation(str, Enum):
|
||||
# Enum for orientation, making string for json serialisation purposes
|
||||
#
|
||||
# Note that htis might not be infinitely extensible?
|
||||
# https://stackoverflow.com/questions/75040733/is-there-a-way-to-use-strenum-in-earlier-python-versions
|
||||
XY = "XY"
|
||||
Z = "Z"
|
||||
RANDOM = "RANDOM"
|
||||
|
||||
|
||||
# A description of the parameters needed to generate random dipoles
|
||||
|
@ -0,0 +1,22 @@
|
||||
# serializer version: 1
|
||||
# name: test_deserialise_json_to_generation_config
|
||||
DipoleGenerationConfig(x_min=-5, x_max=5, y_min=-4.2, y_max=39, z_min=1.2, z_max=80, mag=1000, w_log_min=-5.5, w_log_max=6.6, orientation='RANDOM', dipole_count=15, generation_seed=1234)
|
||||
# ---
|
||||
# name: test_serialise_generation_config_to_json
|
||||
'''
|
||||
{
|
||||
"x_min": -10,
|
||||
"x_max": 10,
|
||||
"y_min": -5,
|
||||
"y_max": 5,
|
||||
"z_min": 4,
|
||||
"z_max": 8,
|
||||
"mag": 100,
|
||||
"w_log_min": -3,
|
||||
"w_log_max": 0,
|
||||
"orientation": "XY",
|
||||
"dipole_count": 5,
|
||||
"generation_seed": 1234
|
||||
}
|
||||
'''
|
||||
# ---
|
@ -1,6 +1,7 @@
|
||||
from tantri.dipoles.generation import DipoleGenerationConfig, make_dipoles, Orientation
|
||||
import numpy
|
||||
|
||||
|
||||
def test_generation_simple_xy_override_rng(snapshot):
|
||||
|
||||
rng = numpy.random.default_rng(1234)
|
||||
@ -16,12 +17,13 @@ def test_generation_simple_xy_override_rng(snapshot):
|
||||
w_log_max=0,
|
||||
orientation=Orientation.XY,
|
||||
dipole_count=5,
|
||||
generation_seed=999999, # should not be used
|
||||
generation_seed=999999, # should not be used
|
||||
)
|
||||
|
||||
dipoles = make_dipoles(config, rng)
|
||||
assert dipoles == snapshot
|
||||
|
||||
|
||||
def test_generation_simple_xy(snapshot):
|
||||
|
||||
config = DipoleGenerationConfig(
|
||||
@ -42,6 +44,7 @@ def test_generation_simple_xy(snapshot):
|
||||
dipoles = make_dipoles(config)
|
||||
assert dipoles == snapshot
|
||||
|
||||
|
||||
def test_generation_simple_z(snapshot):
|
||||
|
||||
config = DipoleGenerationConfig(
|
||||
@ -62,6 +65,7 @@ def test_generation_simple_z(snapshot):
|
||||
dipoles = make_dipoles(config)
|
||||
assert dipoles == snapshot
|
||||
|
||||
|
||||
def test_generation_simple_random(snapshot):
|
||||
|
||||
config = DipoleGenerationConfig(
|
||||
@ -81,4 +85,3 @@ def test_generation_simple_random(snapshot):
|
||||
|
||||
dipoles = make_dipoles(config)
|
||||
assert dipoles == snapshot
|
||||
|
||||
|
69
tests/dipoles/generation/test_serialization_dipole_generation.py
Executable file
69
tests/dipoles/generation/test_serialization_dipole_generation.py
Executable file
@ -0,0 +1,69 @@
|
||||
import json
|
||||
from tantri.dipoles.generation import DipoleGenerationConfig, Orientation
|
||||
import dataclasses
|
||||
|
||||
|
||||
def test_serialise_generation_config_to_json(snapshot):
|
||||
|
||||
config = DipoleGenerationConfig(
|
||||
x_min=-10,
|
||||
x_max=10,
|
||||
y_min=-5,
|
||||
y_max=5,
|
||||
z_min=4,
|
||||
z_max=8,
|
||||
mag=100,
|
||||
w_log_min=-3,
|
||||
w_log_max=0,
|
||||
orientation=Orientation.XY,
|
||||
dipole_count=5,
|
||||
generation_seed=1234,
|
||||
)
|
||||
|
||||
config_json = json.dumps(dataclasses.asdict(config), indent="\t")
|
||||
assert config_json == snapshot
|
||||
|
||||
|
||||
def test_deserialise_json_to_generation_config(snapshot):
|
||||
config_json_string = """
|
||||
{
|
||||
"x_min": -5,
|
||||
"x_max": 5,
|
||||
"y_min": -4.2,
|
||||
"y_max": 39,
|
||||
"z_min": 1.2,
|
||||
"z_max": 80,
|
||||
"mag": 1000,
|
||||
"w_log_min": -5.5,
|
||||
"w_log_max": 6.6,
|
||||
"orientation": "RANDOM",
|
||||
"dipole_count": 15,
|
||||
"generation_seed": 1234
|
||||
}
|
||||
"""
|
||||
|
||||
data = json.loads(config_json_string)
|
||||
config = DipoleGenerationConfig(**data)
|
||||
|
||||
assert config == snapshot
|
||||
|
||||
|
||||
def test_serialise_deserialise_dipole_generation_config_back_and_forth():
|
||||
config = DipoleGenerationConfig(
|
||||
x_min=-10,
|
||||
x_max=10,
|
||||
y_min=-5,
|
||||
y_max=5,
|
||||
z_min=4,
|
||||
z_max=8,
|
||||
mag=100,
|
||||
w_log_min=-3,
|
||||
w_log_max=0,
|
||||
orientation=Orientation.XY,
|
||||
dipole_count=5,
|
||||
generation_seed=1234,
|
||||
)
|
||||
|
||||
config_json = json.dumps(dataclasses.asdict(config), indent="\t")
|
||||
data = json.loads(config_json)
|
||||
assert config == DipoleGenerationConfig(**data)
|
Loading…
x
Reference in New Issue
Block a user