Skip to content

Commit

Permalink
test: Adapt tests to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
James Harr committed Nov 20, 2023
1 parent 2c7d924 commit 35f8ee1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
12 changes: 6 additions & 6 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ def create(cls, diffsync: Adapter, ids: Dict, attrs: Dict):
return None # non-fatal error
return super().create(diffsync, ids, attrs) # type: ignore

def update(self, attrs: Dict):
def update(self, diffsync: Adapter, attrs: Dict):
"""As DiffSyncModel.update(), but periodically throw exceptions."""
# pylint: disable=protected-access
self.__class__._counter += 1
if not self.__class__._counter % 5:
raise ObjectNotUpdated("Random update error!")
if not self.__class__._counter % 4:
return None # non-fatal error
return super().update(attrs) # type: ignore
return super().update(diffsync, attrs) # type: ignore

def delete(self):
def delete(self, diffsync: Adapter):
"""As DiffSyncModel.delete(), but periodically throw exceptions."""
# pylint: disable=protected-access
self.__class__._counter += 1
if not self.__class__._counter % 5:
raise ObjectNotDeleted("Random deletion error!")
if not self.__class__._counter % 4:
return None # non-fatal error
return super().delete() # type: ignore
return super().delete(diffsync) # type: ignore


class ExceptionModelMixin:
Expand All @@ -73,11 +73,11 @@ def create(cls, diffsync: Adapter, ids: Dict, attrs: Dict):
"""As DiffSyncModel.create(), but always throw exceptions."""
raise NotImplementedError

def update(self, attrs: Dict):
def update(self, diffsync: Adapter, attrs: Dict):
"""As DiffSyncModel.update(), but always throw exceptions."""
raise NotImplementedError

def delete(self):
def delete(self, diffsync: Adapter):
"""As DiffSyncModel.delete(), but always throw exceptions."""
raise NotImplementedError

Expand Down
18 changes: 8 additions & 10 deletions tests/unit/test_diffsync_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_diffsync_model_json_with_no_data(generic_diffsync_model):


def test_diffsync_model_str_with_no_data(generic_diffsync_model):
assert generic_diffsync_model.str() == "diffsyncmodel: : {}"
assert generic_diffsync_model.str(diffsync=None) == "diffsyncmodel: : {}"


def test_diffsync_model_subclass_getters(make_site, make_device, make_interface):
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_diffsync_model_json_with_data(make_interface):
def test_diffsync_model_str_with_data(make_interface):
intf = make_interface()
# str() only includes _attributes
assert intf.str() == "interface: device1__eth0: {'interface_type': 'ethernet', 'description': None}"
assert intf.str(diffsync=None) == "interface: device1__eth0: {'interface_type': 'ethernet', 'description': None}"


def test_diffsync_model_subclass_add_remove(make_site, make_device, make_interface):
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_diffsync_model_str_with_children(generic_diffsync, make_site, make_devi
generic_diffsync.add(device1)

assert (
site1.str()
site1.str(diffsync=generic_diffsync)
== """\
site: site1: {}
devices
Expand All @@ -207,7 +207,7 @@ def test_diffsync_model_str_with_children(generic_diffsync, make_site, make_devi
)

assert (
site1.str(include_children=False)
site1.str(diffsync=generic_diffsync, include_children=False)
== """\
site: site1: {}
devices: ['device1']\
Expand All @@ -219,7 +219,6 @@ def test_diffsync_model_subclass_crud(generic_diffsync):
"""Test basic CRUD operations on generic DiffSyncModel subclasses."""
device1 = Device.create(generic_diffsync, {"name": "device1"}, {"role": "spine"})
assert isinstance(device1, Device)
assert device1.diffsync == generic_diffsync
assert device1.name == "device1"
assert device1.role == "spine"

Expand All @@ -229,29 +228,28 @@ def test_diffsync_model_subclass_crud(generic_diffsync):
{"description": "some description"},
)
assert isinstance(device1_eth0, Interface)
assert device1_eth0.diffsync == generic_diffsync
assert device1_eth0.name == "eth0"
assert device1_eth0.device_name == "device1"
assert device1_eth0.description == "some description"

device1 = device1.update({"site_name": "site1", "role": "leaf"})
device1 = device1.update(generic_diffsync, {"site_name": "site1", "role": "leaf"})
assert isinstance(device1, Device)
assert device1.name == "device1"
assert device1.site_name == "site1"
assert device1.role == "leaf"

device1_eth0 = device1_eth0.update({"description": ""})
device1_eth0 = device1_eth0.update(generic_diffsync, {"description": ""})
assert isinstance(device1_eth0, Interface)
assert device1_eth0.name == "eth0"
assert device1_eth0.device_name == "device1"
assert device1_eth0.description == ""

# TODO: negative tests - try to update identifiers with update(), for example

device1 = device1.delete()
device1 = device1.delete(generic_diffsync)
assert isinstance(device1, Device)

device1_eth0.delete()
device1_eth0.delete(generic_diffsync)
assert isinstance(device1_eth0, Interface)


Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_diffsync_model_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ class TestModelChild(DiffSyncModel): # pylint: disable=missing-class-docstring

name: str

def delete(self):
def delete(self, diffsync: Adapter):
call_order.append(self.name)
return super().delete()
return super().delete(diffsync)

class TestModelParent(DiffSyncModel): # pylint: disable=missing-class-docstring
_modelname = "parent"
Expand All @@ -137,9 +137,9 @@ class TestModelParent(DiffSyncModel): # pylint: disable=missing-class-docstring
name: str
children: List[TestModelChild] = []

def delete(self):
def delete(self, diffsync: Adapter):
call_order.append(self.name)
return super().delete()
return super().delete(diffsync)

class TestBackend(Adapter): # pylint: disable=missing-class-docstring
top_level = ["parent"]
Expand Down

0 comments on commit 35f8ee1

Please sign in to comment.