Skip to content

Commit

Permalink
test: Test array serialization as lists
Browse files Browse the repository at this point in the history
  • Loading branch information
alecandido committed Nov 1, 2024
1 parent 8ea0ba3 commit 41f1d3d
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from pydantic import BaseModel, ConfigDict

from qibolab._core.serialize import NdArray, eq
from qibolab._core.serialize import ArrayList, NdArray, eq


class ArrayModel(BaseModel):
Expand All @@ -10,12 +10,43 @@ class ArrayModel(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)


def test_equality():
assert eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=np.arange(10)))
assert not eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=np.arange(11)))
ar = np.arange(10)
ar[5:] = 42
assert not eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=ar))
class TestNdArray:
def test_equality(self):
assert eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=np.arange(10)))
assert not eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=np.arange(11)))
ar = np.arange(10)
ar[5:] = 42
assert not eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=ar))

assert not eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=np.ones((10, 2))))
assert eq(ArrayModel(ar=np.ones((10, 2))), ArrayModel(ar=np.ones((10, 2))))
assert not eq(ArrayModel(ar=np.arange(10)), ArrayModel(ar=np.ones((10, 2))))
assert eq(ArrayModel(ar=np.ones((10, 2))), ArrayModel(ar=np.ones((10, 2))))


class ArrayListModel(BaseModel):
ar: ArrayList

model_config = ConfigDict(arbitrary_types_allowed=True)


class TestArrayList:
def test_equality(self):
assert eq(ArrayListModel(ar=np.arange(10)), ArrayListModel(ar=np.arange(10)))
assert not eq(
ArrayListModel(ar=np.arange(10)), ArrayListModel(ar=np.arange(11))
)
ar = np.arange(10)
ar[5:] = 42
assert not eq(ArrayListModel(ar=np.arange(10)), ArrayListModel(ar=ar))

assert not eq(
ArrayListModel(ar=np.arange(10)), ArrayListModel(ar=np.ones((10, 2)))
)
assert eq(
ArrayListModel(ar=np.ones((10, 2))), ArrayListModel(ar=np.ones((10, 2)))
)

def test_serde(self):
lst = ArrayListModel(ar=np.arange(10)).model_dump()["ar"]
assert isinstance(lst, list)
assert isinstance(lst[0], int)
assert len(lst) == 10

0 comments on commit 41f1d3d

Please sign in to comment.