Coverage for tests\test_zanj_populate_nested.py: 100%

1 from __future__ import annotations

2

3 import typing

4 from pathlib import Path

5

6 import numpy as np

7 from muutils.json_serialize import (

8 SerializableDataclass,

9 serializable_dataclass,

10 serializable_field,

11)

12

13 from zanj import ZANJ

14

15 np.random.seed(0)

16

17# pylint: disable=missing-function-docstring,missing-class-docstring

18

19 TEST_DATA_PATH: Path = Path("tests/junk_data")

20

21

22@serializable_dataclass

23 class InnerClassWithArray(SerializableDataclass):

24 some_string: str

25 arr_numbers: np.ndarray

26

27

28@serializable_dataclass

29 class OuterClassWithNestedList(SerializableDataclass):

30 name: str

31 lst_basic: typing.List[InnerClassWithArray] = serializable_field(

32 serialization_fn=lambda x: [b.serialize() for b in x],

33 loading_fn=lambda x: [InnerClassWithArray.load(b) for b in x["lst_basic"]],

34 )

35

36

37 def test_nested_populate():

38 instance = OuterClassWithNestedList(

39 name="hello",

40 lst_basic=[

41 InnerClassWithArray(

42 some_string=f"hello_{i}",

43 arr_numbers=np.random.rand(20),

44 )

45 for i in range(20)

46 ],

47 )

48

49 z = ZANJ(

50 external_array_threshold=10,

51 external_list_threshold=10,

52 )

53 path = TEST_DATA_PATH / "test_nested_populate.zanj"

54 path.unlink(missing_ok=True)

55 z.save(instance, path)

56 recovered = z.read(path)

57 assert instance == recovered