Skip to content

Commit

Permalink
Add tests for SerializationStrategy init_subclass validations
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-janidlo committed Feb 28, 2025
1 parent 1275cb0 commit abc1a22
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions compute_sdk/tests/unit/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,55 @@ def deserialize(self, payload):
assert "is not a known serialization strategy" in str(e)

SerializationStrategy._CACHE.pop(NewStrategy.identifier)


@pytest.mark.parametrize("id", ["", "1", "12", "1234"])
def test_init_subclass_requires_correct_length(id):
with pytest.raises(ValueError) as pyt_exc:

class NewStrategy(SerializationStrategy):
identifier = id
for_code = True

def serialize(self, data):
pass

def deserialize(self, payload):
pass

assert "must be 3 characters long" in str(pyt_exc.value)


def test_init_subclass_requires_newline():
with pytest.raises(ValueError) as pyt_exc:

class NewStrategy(SerializationStrategy):
identifier = "123"
for_code = True

def serialize(self, data):
pass

def deserialize(self, payload):
pass

assert "must end with a newline character" in str(pyt_exc.value)


@pytest.mark.parametrize(
"id", [s.identifier for s in SerializationStrategy._CACHE.values()]
)
def test_init_subclass_requires_unique_identifier(id):
with pytest.raises(ValueError) as pyt_exc:

class NewStrategy(SerializationStrategy):
identifier = id
for_code = True

def serialize(self, data):
pass

def deserialize(self, payload):
pass

assert f"{id!r} is already used by" in str(pyt_exc.value)

0 comments on commit abc1a22

Please sign in to comment.