Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Jul 5, 2024
1 parent 59780db commit 5db5f1c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
55 changes: 54 additions & 1 deletion llamazure/azrest/azrest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

# pylint: disable=redefined-outer-name
import pytest
from pydantic import BaseModel, ValidationError

from llamazure.azrest.azrest import AzRest
from llamazure.azrest.models import AzList, AzureError, BatchReq, Req
from llamazure.azrest.models import AzList, AzureError, BatchReq, Req, cast_as, ensure


@pytest.fixture
Expand Down Expand Up @@ -89,3 +90,55 @@ def test_custom_names(self, azr, it_info):
assert isinstance(res, AzList)

assert len(batch_res["test-req-0"].value) > 0


# Example Pydantic models
class Foo(BaseModel):
id: int
name: str


class FooUpdateParameters(BaseModel):
name: str


class TestCastAs:
def test_cast_foo_to_fooupdateparameters(self):
foo = Foo(id=1, name="test")
foo_update = cast_as(foo, FooUpdateParameters)
assert foo_update.name == foo.name
assert not hasattr(foo_update, "id")

def test_cast_fooupdateparameters_to_foo(self):
foo_update = FooUpdateParameters(name="updated_name")
with pytest.raises(ValidationError):
cast_as(foo_update, Foo) # This should raise an error since 'id' is missing

def test_cast_with_additional_fields(self):
class Bar(BaseModel):
name: str
extra_field: int

foo = Foo(id=1, name="test")
with pytest.raises(ValueError):
cast_as(foo, Bar) # This should raise an error since 'extra_field' is missing

def test_cast_with_missing_fields(self):
class FooPartial(BaseModel):
id: int

foo = Foo(id=1, name="test")
foo_partial = cast_as(foo, FooPartial)
assert foo_partial.id == foo.id
assert not hasattr(foo_partial, "name")


class TestEnsure:
def test_ensure_not_none(self):
assert ensure(5) == 5
assert ensure("test") == "test"
assert ensure([1, 2, 3]) == [1, 2, 3]

def test_ensure_none_raises(self):
with pytest.raises(TypeError, match="value was None"):
ensure(None)
11 changes: 7 additions & 4 deletions llamazure/rbac/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ def assert_role_assigned():
# explicitly make a `put` that already exists
retry(assert_role_assigned, AzureError)

l.info("deleting RoleAssignment")
ras.DeleteById(asn.rid)
def cleanup():
l.info("deleting RoleAssignment")
ras.DeleteById(asn.rid)

Check warning on line 81 in llamazure/rbac/integration_test.py

View check run for this annotation

Codecov / codecov/patch

llamazure/rbac/integration_test.py#L80-L81

Added lines #L80 - L81 were not covered by tests

l.info("cleanup role")
retry(lambda: rds.delete_by_name(role.properties.roleName), AzureError)
l.info("cleanup role")
rds.delete_by_name(role.properties.roleName)

Check warning on line 84 in llamazure/rbac/integration_test.py

View check run for this annotation

Codecov / codecov/patch

llamazure/rbac/integration_test.py#L83-L84

Added lines #L83 - L84 were not covered by tests

retry(lambda: cleanup, AzureError)

@pytest.mark.integration
@pytest.mark.admin
Expand Down

0 comments on commit 5db5f1c

Please sign in to comment.