From 1c488f1ed4a5fc4bf05e24488447480f64892521 Mon Sep 17 00:00:00 2001 From: Leo Kirchner Date: Tue, 23 Jan 2024 09:04:02 +0100 Subject: [PATCH] renames 'diffsync' fields and variables to 'adapter' --- diffsync/__init__.py | 89 +++++-- diffsync/helpers.py | 2 +- diffsync/store/__init__.py | 12 +- diffsync/store/local.py | 4 +- diffsync/store/redis.py | 6 +- .../getting_started/01-getting-started.md | 4 +- docs/source/upgrading/01-upgrading-to-2.0.md | 2 + examples/03-remote-system/nautobot_models.py | 14 +- .../05-nautobot-peeringdb/adapter_nautobot.py | 24 +- examples/06-ip-prefixes/adapter_ipam_a.py | 12 +- examples/06-ip-prefixes/adapter_ipam_b.py | 12 +- tests/unit/conftest.py | 12 +- tests/unit/test_diffsync.py | 246 +++++++++--------- tests/unit/test_diffsync_model.py | 46 ++-- 14 files changed, 263 insertions(+), 222 deletions(-) diff --git a/diffsync/__init__.py b/diffsync/__init__.py index 46194b97..acaf732d 100644 --- a/diffsync/__init__.py +++ b/diffsync/__init__.py @@ -16,7 +16,18 @@ """ import sys from inspect import isclass -from typing import Callable, ClassVar, Dict, List, Optional, Tuple, Type, Union, Any, Set +from typing import ( + Callable, + ClassVar, + Dict, + List, + Optional, + Tuple, + Type, + Union, + Any, + Set, +) import warnings from pydantic import ConfigDict, BaseModel, PrivateAttr @@ -24,7 +35,12 @@ from diffsync.diff import Diff from diffsync.enum import DiffSyncModelFlags, DiffSyncFlags, DiffSyncStatus -from diffsync.exceptions import DiffClassMismatch, ObjectAlreadyExists, ObjectStoreWrongType, ObjectNotFound +from diffsync.exceptions import ( + DiffClassMismatch, + ObjectAlreadyExists, + ObjectStoreWrongType, + ObjectNotFound, +) from diffsync.helpers import DiffSyncDiffer, DiffSyncSyncer from diffsync.store import BaseStore from diffsync.store.local import LocalStore @@ -97,15 +113,17 @@ class DiffSyncModel(BaseModel): Can be set as a class attribute or an instance attribute as needed. """ - diffsync: Optional["Adapter"] = None - """Optional: the DiffSync instance that owns this model instance.""" + adapter: Optional["Adapter"] = None + """Optional: the Adapter instance that owns this model instance.""" _status: DiffSyncStatus = PrivateAttr(DiffSyncStatus.SUCCESS) """Status of the last attempt at creating/updating/deleting this model.""" _status_message: str = PrivateAttr("") """Message, if any, associated with the create/update/delete status value.""" + model_config = ConfigDict(arbitrary_types_allowed=True) + """Pydantic-specific configuration to allow arbitrary types on this class.""" @classmethod def __pydantic_init_subclass__(cls, **kwargs: Any) -> None: @@ -145,15 +163,15 @@ def __str__(self) -> str: return self.get_unique_id() def dict(self, **kwargs: Any) -> Dict: - """Convert this DiffSyncModel to a dict, excluding the diffsync field by default as it is not serializable.""" + """Convert this DiffSyncModel to a dict, excluding the adapter field by default as it is not serializable.""" if "exclude" not in kwargs: - kwargs["exclude"] = {"diffsync"} + kwargs["exclude"] = {"adapter"} return super().model_dump(**kwargs) def json(self, **kwargs: Any) -> StrType: - """Convert this DiffSyncModel to a JSON string, excluding the diffsync field by default as it is not serializable.""" + """Convert this DiffSyncModel to a JSON string, excluding the adapter field by default as it is not serializable.""" if "exclude" not in kwargs: - kwargs["exclude"] = {"diffsync"} + kwargs["exclude"] = {"adapter"} if "exclude_defaults" not in kwargs: kwargs["exclude_defaults"] = True return super().model_dump_json(**kwargs) @@ -167,12 +185,12 @@ def str(self, include_children: bool = True, indent: int = 0) -> StrType: child_ids = getattr(self, fieldname) if not child_ids: output += ": []" - elif not self.diffsync or not include_children: + elif not self.adapter or not include_children: output += f": {child_ids}" else: for child_id in child_ids: try: - child = self.diffsync.get(modelname, child_id) + child = self.adapter.get(modelname, child_id) output += "\n" + child.str(include_children=include_children, indent=indent + 4) except ObjectNotFound: output += f"\n{margin} {child_id} (ERROR: details unavailable)" @@ -184,32 +202,32 @@ def set_status(self, status: DiffSyncStatus, message: StrType = "") -> None: self._status_message = message @classmethod - def create_base(cls, diffsync: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]: + def create_base(cls, adapter: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]: """Instantiate this class, along with any platform-specific data creation. This method is not meant to be subclassed, users should redefine create() instead. Args: - diffsync: The master data store for other DiffSyncModel instances that we might need to reference + adapter: The master data store for other DiffSyncModel instances that we might need to reference ids: Dictionary of unique-identifiers needed to create the new object attrs: Dictionary of additional attributes to set on the new object Returns: DiffSyncModel: instance of this class. """ - model = cls(**ids, diffsync=diffsync, **attrs) + model = cls(**ids, adapter=adapter, **attrs) model.set_status(DiffSyncStatus.SUCCESS, "Created successfully") return model @classmethod - def create(cls, diffsync: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]: + def create(cls, adapter: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]: """Instantiate this class, along with any platform-specific data creation. Subclasses must call `super().create()` or `self.create_base()`; they may wish to then override the default status information by calling `set_status()` to provide more context (such as details of any interactions with underlying systems). Args: - diffsync: The master data store for other DiffSyncModel instances that we might need to reference + adapter: The master data store for other DiffSyncModel instances that we might need to reference ids: Dictionary of unique-identifiers needed to create the new object attrs: Dictionary of additional attributes to set on the new object @@ -220,7 +238,7 @@ def create(cls, diffsync: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]: Raises: ObjectNotCreated: if an error occurred. """ - return cls.create_base(diffsync=diffsync, ids=ids, attrs=attrs) + return cls.create_base(adapter=adapter, ids=ids, attrs=attrs) def update_base(self, attrs: Dict) -> Optional[Self]: """Base Update method to update the attributes of this instance, along with any platform-specific data updates. @@ -376,7 +394,10 @@ def add_child(self, child: "DiffSyncModel") -> None: attr_name = self._children[child_type] childs = getattr(self, attr_name) if child.get_unique_id() in childs: - raise ObjectAlreadyExists(f"Already storing a {child_type} with unique_id {child.get_unique_id()}", child) + raise ObjectAlreadyExists( + f"Already storing a {child_type} with unique_id {child.get_unique_id()}", + child, + ) childs.append(child.get_unique_id()) def remove_child(self, child: "DiffSyncModel") -> None: @@ -417,7 +438,9 @@ class Adapter: # pylint: disable=too-many-public-methods """List of top-level modelnames to begin from when diffing or synchronizing.""" def __init__( - self, name: Optional[str] = None, internal_storage_engine: Union[Type[BaseStore], BaseStore] = LocalStore + self, + name: Optional[str] = None, + internal_storage_engine: Union[Type[BaseStore], BaseStore] = LocalStore, ) -> None: """Generic initialization function. @@ -426,9 +449,9 @@ def __init__( if isinstance(internal_storage_engine, BaseStore): self.store = internal_storage_engine - self.store.diffsync = self + self.store.adapter = self else: - self.store = internal_storage_engine(diffsync=self) + self.store = internal_storage_engine(adapter=self) # If the type is not defined, use the name of the class as the default value if self.type is None: @@ -565,7 +588,13 @@ def sync_from( # pylint: disable=too-many-arguments # Generate the diff if an existing diff was not provided if not diff: diff = self.diff_from(source, diff_class=diff_class, flags=flags, callback=callback) - syncer = DiffSyncSyncer(diff=diff, src_diffsync=source, dst_diffsync=self, flags=flags, callback=callback) + syncer = DiffSyncSyncer( + diff=diff, + src_diffsync=source, + dst_diffsync=self, + flags=flags, + callback=callback, + ) result = syncer.perform_sync() if result: self.sync_complete(source, diff, flags, syncer.base_logger) @@ -639,7 +668,11 @@ def diff_from( calculation of the diff proceeds. """ differ = DiffSyncDiffer( - src_diffsync=source, dst_diffsync=self, flags=flags, diff_class=diff_class, callback=callback + src_diffsync=source, + dst_diffsync=self, + flags=flags, + diff_class=diff_class, + callback=callback, ) return differ.calculate_diffs() @@ -674,7 +707,9 @@ def get_all_model_names(self) -> Set[StrType]: return self.store.get_all_model_names() def get( - self, obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]], identifier: Union[StrType, Dict] + self, + obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]], + identifier: Union[StrType, Dict], ) -> DiffSyncModel: """Get one object from the data store based on its unique id. @@ -689,7 +724,9 @@ def get( return self.store.get(model=obj, identifier=identifier) def get_or_none( - self, obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]], identifier: Union[StrType, Dict] + self, + obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]], + identifier: Union[StrType, Dict], ) -> Optional[DiffSyncModel]: """Get one object from the data store based on its unique id or get a None @@ -720,7 +757,9 @@ def get_all(self, obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]]) -> Li return self.store.get_all(model=obj) def get_by_uids( - self, uids: List[StrType], obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]] + self, + uids: List[StrType], + obj: Union[StrType, DiffSyncModel, Type[DiffSyncModel]], ) -> List[DiffSyncModel]: """Get multiple objects from the store by their unique IDs/Keys and type. diff --git a/diffsync/helpers.py b/diffsync/helpers.py index 6ed7bae6..60c61f27 100644 --- a/diffsync/helpers.py +++ b/diffsync/helpers.py @@ -425,7 +425,7 @@ def sync_model( # pylint: disable=too-many-branches, unused-argument if self.action == DiffSyncActions.CREATE: if dst_model is not None: raise ObjectNotCreated(f"Failed to create {self.model_class.get_type()} {ids} - it already exists!") - dst_model = self.model_class.create(diffsync=self.dst_diffsync, ids=ids, attrs=attrs) + dst_model = self.model_class.create(adapter=self.dst_diffsync, ids=ids, attrs=attrs) elif self.action == DiffSyncActions.UPDATE: if dst_model is None: raise ObjectNotUpdated(f"Failed to update {self.model_class.get_type()} {ids} - not found!") diff --git a/diffsync/store/__init__.py b/diffsync/store/__init__.py index 3dacf330..0b18b766 100644 --- a/diffsync/store/__init__.py +++ b/diffsync/store/__init__.py @@ -15,12 +15,12 @@ class BaseStore: def __init__( self, # pylint: disable=unused-argument *args: Any, # pylint: disable=unused-argument - diffsync: Optional["Adapter"] = None, + adapter: Optional["Adapter"] = None, name: str = "", **kwargs: Any, # pylint: disable=unused-argument ) -> None: """Init method for BaseStore.""" - self.diffsync = diffsync + self.adapter = adapter self.name = name or self.__class__.__name__ self._log = structlog.get_logger().new(store=self) @@ -95,8 +95,8 @@ def remove(self, *, obj: "DiffSyncModel", remove_children: bool = False) -> None self.remove_item(modelname, uid) - if obj.diffsync: - obj.diffsync = None + if obj.adapter: + obj.adapter = None if remove_children: for child_type, child_fieldname in obj.get_children_mapping().items(): @@ -243,9 +243,9 @@ def _get_object_class_and_model( """Get object class and model name for a model.""" if isinstance(model, str): modelname = model - if not hasattr(self.diffsync, model): + if not hasattr(self.adapter, model): return None, modelname - object_class = getattr(self.diffsync, model) + object_class = getattr(self.adapter, model) else: object_class = model modelname = model.get_type() diff --git a/diffsync/store/local.py b/diffsync/store/local.py index 82bb69ba..a56d1dff 100644 --- a/diffsync/store/local.py +++ b/diffsync/store/local.py @@ -108,8 +108,8 @@ def add(self, *, obj: "DiffSyncModel") -> None: # Return so we don't have to change anything on the existing object and underlying data return - if not obj.diffsync: - obj.diffsync = self.diffsync + if not obj.adapter: + obj.adapter = self.adapter self._data[modelname][uid] = obj diff --git a/diffsync/store/redis.py b/diffsync/store/redis.py index 927dbf5e..ab534dd1 100644 --- a/diffsync/store/redis.py +++ b/diffsync/store/redis.py @@ -65,7 +65,7 @@ def _get_object_from_redis_key(self, key: str) -> "DiffSyncModel": pickled_object = self._store.get(key) if pickled_object: obj_result = loads(pickled_object) # nosec - obj_result.diffsync = self.diffsync + obj_result.adapter = self.adapter return obj_result raise ObjectNotFound(f"{key} not present in Cache") @@ -178,7 +178,7 @@ def add(self, *, obj: "DiffSyncModel") -> None: # Remove the diffsync object before sending to Redis obj_copy = copy.copy(obj) - obj_copy.diffsync = None + obj_copy.adapter = None self._store.set(object_key, dumps(obj_copy)) @@ -193,7 +193,7 @@ def update(self, *, obj: "DiffSyncModel") -> None: object_key = self._get_key_for_object(modelname, uid) obj_copy = copy.copy(obj) - obj_copy.diffsync = None + obj_copy.adapter = None self._store.set(object_key, dumps(obj_copy)) diff --git a/docs/source/getting_started/01-getting-started.md b/docs/source/getting_started/01-getting-started.md index 9b99cc35..3696dd73 100644 --- a/docs/source/getting_started/01-getting-started.md +++ b/docs/source/getting_started/01-getting-started.md @@ -181,10 +181,10 @@ class Device(DiffSyncModel): [...] @classmethod - def create(cls, diffsync, ids, attrs): + def create(cls, adapter, ids, attrs): ## TODO add your own logic here to create the device on the remote system # Call the super().create() method to create the in-memory DiffSyncModel instance - return super().create(ids=ids, diffsync=diffsync, attrs=attrs) + return super().create(ids=ids, adapter=adapter, attrs=attrs) def update(self, attrs): ## TODO add your own logic here to update the device on the remote system diff --git a/docs/source/upgrading/01-upgrading-to-2.0.md b/docs/source/upgrading/01-upgrading-to-2.0.md index 55e896ba..34c7bfef 100644 --- a/docs/source/upgrading/01-upgrading-to-2.0.md +++ b/docs/source/upgrading/01-upgrading-to-2.0.md @@ -6,6 +6,8 @@ With diffsync 2.0, there a couple of breaking changes. What they are and how to The main diffsync class `diffsync.Diffsync` has been renamed to `diffsync.Adapter` as we have found that this is the verbiage that is most often used by users and explains the intent of the class clearer. The old name will still be around until 2.1, but is considered deprecated at this point. +As a consequence, a lot of fields have been renamed all across diffsync. To the end user, this will most prominently appear in the signature of the `create` method, where you will have to rename the `diffsync` parameter to `adapter`. + ## Upgrade to Pydantic's major version 2 A [migration guide](https://docs.pydantic.dev/latest/migration/) is available in the Pydantic documentation. Here are the key things that may apply to your usage of diffsync: diff --git a/examples/03-remote-system/nautobot_models.py b/examples/03-remote-system/nautobot_models.py index 2baba778..e2daf55b 100644 --- a/examples/03-remote-system/nautobot_models.py +++ b/examples/03-remote-system/nautobot_models.py @@ -30,11 +30,11 @@ class NautobotCountry(Country): """Store the nautobot uuid in the object to allow update and delete of existing object.""" @classmethod - def create(cls, diffsync: Adapter, ids: dict, attrs: dict): + def create(cls, adapter: Adapter, ids: dict, attrs: dict): """Create a country object in Nautobot. Args: - diffsync: The master data store for other DiffSyncModel instances that we might need to reference + adapter: The master data store for other DiffSyncModel instances that we might need to reference ids: Dictionary of unique-identifiers needed to create the new object attrs: Dictionary of additional attributes to set on the new object @@ -43,11 +43,11 @@ def create(cls, diffsync: Adapter, ids: dict, attrs: dict): """ # Retrieve the parent region in internal cache to access its UUID # because the UUID is required to associate the object to its parent region in Nautobot - region = diffsync.get(diffsync.region, attrs.get("region")) + region = adapter.get(adapter.region, attrs.get("region")) # Create the new country in Nautobot and attach it to its parent try: - country = diffsync.nautobot.dcim.regions.create( + country = adapter.nautobot.dcim.regions.create( slug=ids.get("slug"), name=attrs.get("name"), custom_fields=dict(population=attrs.get("population")), @@ -61,7 +61,7 @@ def create(cls, diffsync: Adapter, ids: dict, attrs: dict): # Add the newly created remote_id and create the internal object for this resource. attrs["remote_id"] = country.id - item = super().create(ids=ids, diffsync=diffsync, attrs=attrs) + item = super().create(ids=ids, adapter=adapter, attrs=attrs) return item def update(self, attrs: dict): @@ -78,7 +78,7 @@ def update(self, attrs: dict): ObjectNotUpdated: if an error occurred. """ # Retrive the pynautobot object from Nautobot since we only have the UUID internally - remote = self.diffsync.nautobot.dcim.regions.get(self.remote_id) + remote = self.adapter.nautobot.dcim.regions.get(self.remote_id) # Convert the internal attrs to Nautobot format if "population" in attrs: @@ -98,7 +98,7 @@ def delete(self): NautobotCountry: DiffSync object """ # Retrieve the pynautobot object and delete the object in Nautobot - remote = self.diffsync.nautobot.dcim.regions.get(self.remote_id) + remote = self.adapter.nautobot.dcim.regions.get(self.remote_id) remote.delete() super().delete() diff --git a/examples/05-nautobot-peeringdb/adapter_nautobot.py b/examples/05-nautobot-peeringdb/adapter_nautobot.py index 84be2d69..51b6f8af 100644 --- a/examples/05-nautobot-peeringdb/adapter_nautobot.py +++ b/examples/05-nautobot-peeringdb/adapter_nautobot.py @@ -9,11 +9,11 @@ class RegionNautobotModel(RegionModel): """Implementation of Region create/update/delete methods for updating remote Nautobot data.""" @classmethod - def create(cls, diffsync, ids, attrs): + def create(cls, adapter, ids, attrs): """Create a new Region record in remote Nautobot. Args: - diffsync (NautobotRemote): DiffSync adapter owning this Region + adapter (NautobotRemote): DiffSync adapter owning this Region ids (dict): Initial values for this model's _identifiers attrs (dict): Initial values for this model's _attributes """ @@ -24,11 +24,11 @@ def create(cls, diffsync, ids, attrs): if attrs["description"]: data["description"] = attrs["description"] if attrs["parent_name"]: - data["parent"] = str(diffsync.get(diffsync.region, attrs["parent_name"]).pk) + data["parent"] = str(adapter.get(adapter.region, attrs["parent_name"]).pk) - diffsync.nautobot_api.dcim.regions.create(**data) + adapter.nautobot_api.dcim.regions.create(**data) - return super().create(diffsync, ids=ids, attrs=attrs) + return super().create(adapter, ids=ids, attrs=attrs) def update(self, attrs): """Update an existing Region record in remote Nautobot. @@ -36,7 +36,7 @@ def update(self, attrs): Args: attrs (dict): Updated values for this record's _attributes """ - region = self.diffsync.nautobot_api.dcim.regions.get(name=self.name) + region = self.adapter.nautobot_api.dcim.regions.get(name=self.name) data = {} if "slug" in attrs: data["slug"] = attrs["slug"] @@ -44,7 +44,7 @@ def update(self, attrs): data["description"] = attrs["description"] if "parent_name" in attrs: if attrs["parent_name"]: - data["parent"] = str(self.diffsync.get(self.diffsync.region, attrs["parent_name"]).name) + data["parent"] = str(self.adapter.get(self.adapter.region, attrs["parent_name"]).name) else: data["parent"] = None @@ -62,15 +62,15 @@ class SiteNautobotModel(SiteModel): """Implementation of Site create/update/delete methods for updating remote Nautobot data.""" @classmethod - def create(cls, diffsync, ids, attrs): + def create(cls, adapter, ids, attrs): """Create a new Site in remote Nautobot. Args: - diffsync (NautobotRemote): DiffSync adapter owning this Site + adapter (NautobotRemote): DiffSync adapter owning this Site ids (dict): Initial values for this model's _identifiers attrs (dict): Initial values for this model's _attributes """ - diffsync.nautobot_api.dcim.sites.create( + adapter.nautobot_api.dcim.sites.create( name=ids["name"], slug=attrs["slug"], description=attrs["description"], @@ -79,7 +79,7 @@ def create(cls, diffsync, ids, attrs): latitude=attrs["latitude"], longitude=attrs["longitude"], ) - return super().create(diffsync, ids=ids, attrs=attrs) + return super().create(adapter, ids=ids, attrs=attrs) def update(self, attrs): """Update an existing Site record in remote Nautobot. @@ -87,7 +87,7 @@ def update(self, attrs): Args: attrs (dict): Updated values for this record's _attributes """ - site = self.diffsync.nautobot_api.dcim.sites.get(name=self.name) + site = self.adapter.nautobot_api.dcim.sites.get(name=self.name) data = {} if "slug" in attrs: diff --git a/examples/06-ip-prefixes/adapter_ipam_a.py b/examples/06-ip-prefixes/adapter_ipam_a.py index 5463cd46..03508788 100644 --- a/examples/06-ip-prefixes/adapter_ipam_a.py +++ b/examples/06-ip-prefixes/adapter_ipam_a.py @@ -12,9 +12,9 @@ class IpamAPrefix(Prefix): """Implementation of Prefix create/update/delete methods for IPAM A.""" @classmethod - def create(cls, diffsync, ids, attrs): + def create(cls, adapter, ids, attrs): """Create a Prefix record in IPAM A.""" - diffsync.data.append( + adapter.data.append( { "cidr": ids["prefix"], "family": ipaddress.ip_address(ids["prefix"].split("/")[0]).version, @@ -24,11 +24,11 @@ def create(cls, diffsync, ids, attrs): } ) - return super().create(diffsync, ids=ids, attrs=attrs) + return super().create(adapter, ids=ids, attrs=attrs) def update(self, attrs): """Update a Prefix record in IPAM A.""" - for elem in self.diffsync.data: + for elem in self.adapter.data: if elem["cidr"] == self.prefix: if "vrf" in attrs: elem["vrf"] = attrs["vrf"] @@ -42,9 +42,9 @@ def update(self, attrs): def delete(self): """Delete a Prefix record in IPAM A.""" - for index, elem in enumerate(self.diffsync.data): + for index, elem in enumerate(self.adapter.data): if elem["cidr"] == self.prefix: - del self.diffsync.data[index] + del self.adapter.data[index] break return super().delete() diff --git a/examples/06-ip-prefixes/adapter_ipam_b.py b/examples/06-ip-prefixes/adapter_ipam_b.py index 7a6eadb3..4e66e19a 100644 --- a/examples/06-ip-prefixes/adapter_ipam_b.py +++ b/examples/06-ip-prefixes/adapter_ipam_b.py @@ -11,9 +11,9 @@ class IpamBPrefix(Prefix): """Implementation of Prefix create/update/delete methods for IPAM B.""" @classmethod - def create(cls, diffsync, ids, attrs): + def create(cls, adapter, ids, attrs): """Create a Prefix record in IPAM B.""" - diffsync.data.append( + adapter.data.append( { "network": ids["prefix"].split("/")[0], "prefix_length": int(ids["prefix"].split("/")[1]), @@ -23,14 +23,14 @@ def create(cls, diffsync, ids, attrs): } ) - return super().create(diffsync, ids=ids, attrs=attrs) + return super().create(adapter, ids=ids, attrs=attrs) def update(self, attrs): """Update a Prefix record in IPAM B.""" network = self.prefix.split("/")[0] prefix_length = int(self.prefix.split("/")[1]) - for elem in self.diffsync.data: + for elem in self.adapter.data: if elem["network"] == network and elem["prefix_length"] == prefix_length: if "vrf" in attrs: elem["vrf"] = attrs["vrf"] @@ -47,9 +47,9 @@ def delete(self): network = self.prefix.split("/")[0] prefix_length = int(self.prefix.split("/")[1]) - for index, elem in enumerate(self.diffsync.data): + for index, elem in enumerate(self.adapter.data): if elem["network"] == network and elem["prefix_length"] == prefix_length: - del self.diffsync.data[index] + del self.adapter.data[index] break return super().delete() diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index c72d2134..bfe096b6 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -35,14 +35,14 @@ class ErrorProneModelMixin: _counter: ClassVar[int] = 0 @classmethod - def create(cls, diffsync: Adapter, ids: Dict, attrs: Dict): + def create(cls, adapter: Adapter, ids: Dict, attrs: Dict): """As DiffSyncModel.create(), but periodically throw exceptions.""" cls._counter += 1 if not cls._counter % 5: raise ObjectNotCreated("Random creation error!") if not cls._counter % 4: return None # non-fatal error - return super().create(diffsync, ids, attrs) # type: ignore + return super().create(adapter, ids, attrs) # type: ignore def update(self, attrs: Dict): """As DiffSyncModel.update(), but periodically throw exceptions.""" @@ -69,7 +69,7 @@ class ExceptionModelMixin: """Test class that always throws exceptions when creating/updating/deleting instances.""" @classmethod - def create(cls, diffsync: Adapter, ids: Dict, attrs: Dict): + def create(cls, adapter: Adapter, ids: Dict, attrs: Dict): """As DiffSyncModel.create(), but always throw exceptions.""" raise NotImplementedError @@ -158,8 +158,8 @@ def interface(device_name="device1", name="eth0", **kwargs): @pytest.fixture -def generic_diffsync(): - """Provide a generic DiffSync instance.""" +def generic_adapter(): + """Provide a generic Adapter instance.""" return Adapter() @@ -173,7 +173,7 @@ class UnusedModel(DiffSyncModel): class GenericBackend(Adapter): - """An example semi-abstract subclass of DiffSync.""" + """An example semi-abstract subclass of Adapter.""" site = Site # to be overridden by subclasses device = Device diff --git a/tests/unit/test_diffsync.py b/tests/unit/test_diffsync.py index 6f7feaed..088bd4ba 100644 --- a/tests/unit/test_diffsync.py +++ b/tests/unit/test_diffsync.py @@ -1,4 +1,4 @@ -"""Unit tests for the DiffSync class.""" +"""Unit tests for the Adapter class.""" # pylint: disable=too-many-lines from unittest import mock @@ -12,339 +12,339 @@ from .conftest import Site, Device, Interface, TrackedDiff, BackendA, PersonA -def test_diffsync_default_name_type(generic_diffsync): - assert generic_diffsync.type == "Adapter" - assert generic_diffsync.name == "Adapter" +def test_diffsync_default_name_type(generic_adapter): + assert generic_adapter.type == "Adapter" + assert generic_adapter.name == "Adapter" -def test_diffsync_generic_load_is_noop(generic_diffsync): - generic_diffsync.load() - assert generic_diffsync.count() == 0 +def test_diffsync_generic_load_is_noop(generic_adapter): + generic_adapter.load() + assert generic_adapter.count() == 0 -def test_diffsync_dict_with_no_data(generic_diffsync): - assert generic_diffsync.dict() == {} +def test_diffsync_dict_with_no_data(generic_adapter): + assert generic_adapter.dict() == {} -def test_diffsync_str_with_no_data(generic_diffsync): - assert generic_diffsync.str() == "" +def test_diffsync_str_with_no_data(generic_adapter): + assert generic_adapter.str() == "" -def test_diffsync_len_with_no_data(generic_diffsync): - assert len(generic_diffsync) == 0 +def test_diffsync_len_with_no_data(generic_adapter): + assert len(generic_adapter) == 0 -def test_diffsync_diff_self_with_no_data_has_no_diffs(generic_diffsync): - assert generic_diffsync.diff_from(generic_diffsync).has_diffs() is False - assert generic_diffsync.diff_to(generic_diffsync).has_diffs() is False +def test_diffsync_diff_self_with_no_data_has_no_diffs(generic_adapter): + assert generic_adapter.diff_from(generic_adapter).has_diffs() is False + assert generic_adapter.diff_to(generic_adapter).has_diffs() is False -def test_diffsync_sync_self_with_no_data_is_noop(generic_diffsync): - generic_diffsync.sync_complete = mock.Mock() - generic_diffsync.sync_from(generic_diffsync) - diff = generic_diffsync.sync_to(generic_diffsync) +def test_diffsync_sync_self_with_no_data_is_noop(generic_adapter): + generic_adapter.sync_complete = mock.Mock() + generic_adapter.sync_from(generic_adapter) + diff = generic_adapter.sync_to(generic_adapter) # Check if the returning Diff object has diffs assert not diff.has_diffs() # sync_complete() should only be called if something actually changed - assert not generic_diffsync.sync_complete.called + assert not generic_adapter.sync_complete.called -def test_diffsync_get_with_no_data_fails(generic_diffsync): +def test_diffsync_get_with_no_data_fails(generic_adapter): with pytest.raises(ObjectNotFound): - generic_diffsync.get("anything", "myname") + generic_adapter.get("anything", "myname") with pytest.raises(ObjectNotFound): - generic_diffsync.get(DiffSyncModel, "") + generic_adapter.get(DiffSyncModel, "") -def test_diffsync_get_all_with_no_data_is_empty_list(generic_diffsync): - assert not list(generic_diffsync.get_all("anything")) - assert not list(generic_diffsync.get_all(DiffSyncModel)) +def test_diffsync_get_all_with_no_data_is_empty_list(generic_adapter): + assert not list(generic_adapter.get_all("anything")) + assert not list(generic_adapter.get_all(DiffSyncModel)) -def test_diffsync_get_by_uids_with_no_data(generic_diffsync): - assert not generic_diffsync.get_by_uids([], "anything") - assert not generic_diffsync.get_by_uids([], DiffSyncModel) +def test_diffsync_get_by_uids_with_no_data(generic_adapter): + assert not generic_adapter.get_by_uids([], "anything") + assert not generic_adapter.get_by_uids([], DiffSyncModel) with pytest.raises(ObjectNotFound): - generic_diffsync.get_by_uids(["any", "another"], "anything") + generic_adapter.get_by_uids(["any", "another"], "anything") with pytest.raises(ObjectNotFound): - generic_diffsync.get_by_uids(["any", "another"], DiffSyncModel) + generic_adapter.get_by_uids(["any", "another"], DiffSyncModel) -def test_diffsync_add_no_raises_existing_same_object(generic_diffsync): +def test_diffsync_add_no_raises_existing_same_object(generic_adapter): person = PersonA(name="Mikhail Yohman") modelname = person.get_type() uid = person.get_unique_id() # First attempt at adding object - generic_diffsync.add(person) - assert modelname in generic_diffsync.get_all_model_names() - assert any(uid == obj.get_unique_id() for obj in generic_diffsync.get_all(modelname)) + generic_adapter.add(person) + assert modelname in generic_adapter.get_all_model_names() + assert any(uid == obj.get_unique_id() for obj in generic_adapter.get_all(modelname)) - assert person == generic_diffsync.get(modelname, uid) + assert person == generic_adapter.get(modelname, uid) # Attempt to add again and make sure it doesn't raise an exception - generic_diffsync.add(person) - assert person is generic_diffsync.get(modelname, uid) - assert person is generic_diffsync.get(PersonA, "Mikhail Yohman") + generic_adapter.add(person) + assert person is generic_adapter.get(modelname, uid) + assert person is generic_adapter.get(PersonA, "Mikhail Yohman") -def test_diffsync_add_raises_already_exists_with_updated_object(generic_diffsync): +def test_diffsync_add_raises_already_exists_with_updated_object(generic_adapter): intf = Interface(device_name="device1", name="eth0") # A DiffSync can store arbitrary DiffSyncModel objects, even if it doesn't know about them at definition time. - generic_diffsync.add(intf) + generic_adapter.add(intf) # Create new interface with same identifiers so it's technically the same object, but set additional attribute new_intf = Interface(device_name="device1", name="eth0", interface_type="1000base-t") with pytest.raises(ObjectAlreadyExists) as error: - generic_diffsync.add(new_intf) + generic_adapter.add(new_intf) error_model = error.value.existing_object assert isinstance(error_model, DiffSyncModel) assert new_intf is error_model -def test_diffsync_get_or_instantiate_create_non_existent_object(generic_diffsync): - generic_diffsync.interface = Interface +def test_diffsync_get_or_instantiate_create_non_existent_object(generic_adapter): + generic_adapter.interface = Interface intf_identifiers = {"device_name": "device1", "name": "eth1"} # Assert that the object does not currently exist. with pytest.raises(ObjectNotFound): - generic_diffsync.get(Interface, intf_identifiers) + generic_adapter.get(Interface, intf_identifiers) - obj, created = generic_diffsync.get_or_instantiate(Interface, intf_identifiers) + obj, created = generic_adapter.get_or_instantiate(Interface, intf_identifiers) assert created - assert obj is generic_diffsync.get(Interface, intf_identifiers) - assert obj is generic_diffsync.get("interface", intf_identifiers) + assert obj is generic_adapter.get(Interface, intf_identifiers) + assert obj is generic_adapter.get("interface", intf_identifiers) -def test_diffsync_get_or_instantiate_retrieve_existing_object(generic_diffsync): +def test_diffsync_get_or_instantiate_retrieve_existing_object(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf = Interface(**intf_identifiers) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.get_or_instantiate(Interface, intf_identifiers) + obj, created = generic_adapter.get_or_instantiate(Interface, intf_identifiers) assert obj is intf assert not created -def test_diffsync_get_or_instantiate_retrieve_existing_object_w_attrs(generic_diffsync): +def test_diffsync_get_or_instantiate_retrieve_existing_object_w_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} intf = Interface(**intf_identifiers) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.get_or_instantiate(Interface, intf_identifiers, intf_attrs) + obj, created = generic_adapter.get_or_instantiate(Interface, intf_identifiers, intf_attrs) assert obj is intf assert not created assert obj.interface_type == "ethernet" assert obj.description is None -def test_diffsync_get_or_instantiate_retrieve_create_non_existent_w_attrs(generic_diffsync): - generic_diffsync.interface = Interface +def test_diffsync_get_or_instantiate_retrieve_create_non_existent_w_attrs(generic_adapter): + generic_adapter.interface = Interface intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} - obj, created = generic_diffsync.get_or_instantiate(Interface, intf_identifiers, intf_attrs) + obj, created = generic_adapter.get_or_instantiate(Interface, intf_identifiers, intf_attrs) assert created assert obj.interface_type == "1000base-t" assert obj.description == "Testing" - assert obj is generic_diffsync.get(Interface, intf_identifiers) - assert obj is generic_diffsync.get("interface", intf_identifiers) + assert obj is generic_adapter.get(Interface, intf_identifiers) + assert obj is generic_adapter.get("interface", intf_identifiers) -def test_diffsync_get_or_instantiate_retrieve_existing_object_wo_attrs(generic_diffsync): +def test_diffsync_get_or_instantiate_retrieve_existing_object_wo_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf = Interface(**intf_identifiers) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.get_or_instantiate(Interface, intf_identifiers) + obj, created = generic_adapter.get_or_instantiate(Interface, intf_identifiers) assert obj is intf assert not created assert obj.interface_type == "ethernet" assert obj.description is None -def test_diffsync_get_or_add_model_instance_create_non_existent_object(generic_diffsync): - generic_diffsync.interface = Interface +def test_diffsync_get_or_add_model_instance_create_non_existent_object(generic_adapter): + generic_adapter.interface = Interface intf_identifiers = {"device_name": "device1", "name": "eth1"} - intf = generic_diffsync.interface(**intf_identifiers) + intf = generic_adapter.interface(**intf_identifiers) # Assert that the object does not currently exist. with pytest.raises(ObjectNotFound): - generic_diffsync.get(Interface, intf_identifiers) + generic_adapter.get(Interface, intf_identifiers) - obj, created = generic_diffsync.get_or_add_model_instance(intf) + obj, created = generic_adapter.get_or_add_model_instance(intf) assert created - assert obj is generic_diffsync.get(Interface, intf_identifiers) - assert obj is generic_diffsync.get("interface", intf_identifiers) + assert obj is generic_adapter.get(Interface, intf_identifiers) + assert obj is generic_adapter.get("interface", intf_identifiers) -def test_diffsync_get_or_add_model_instance_retrieve_existing_object(generic_diffsync): +def test_diffsync_get_or_add_model_instance_retrieve_existing_object(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf = Interface(**intf_identifiers) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.get_or_add_model_instance(intf) + obj, created = generic_adapter.get_or_add_model_instance(intf) assert obj is intf assert not created -def test_diffsync_get_or_add_model_instance_retrieve_existing_object_w_attrs(generic_diffsync): +def test_diffsync_get_or_add_model_instance_retrieve_existing_object_w_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "ethernet"} intf_combine = {**intf_identifiers, **intf_attrs} intf = Interface(**intf_combine) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.get_or_add_model_instance(intf) + obj, created = generic_adapter.get_or_add_model_instance(intf) assert obj is intf assert not created assert obj.interface_type == "ethernet" assert obj.description is None -def test_diffsync_get_or_add_model_instance_retrieve_create_non_existent_w_attrs(generic_diffsync): - generic_diffsync.interface = Interface +def test_diffsync_get_or_add_model_instance_retrieve_create_non_existent_w_attrs(generic_adapter): + generic_adapter.interface = Interface intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} intf_combine = {**intf_identifiers, **intf_attrs} intf = Interface(**intf_combine) - obj, created = generic_diffsync.get_or_add_model_instance(intf) + obj, created = generic_adapter.get_or_add_model_instance(intf) assert created assert obj.interface_type == "1000base-t" assert obj.description == "Testing" - assert obj is generic_diffsync.get(Interface, intf_identifiers) - assert obj is generic_diffsync.get("interface", intf_identifiers) + assert obj is generic_adapter.get(Interface, intf_identifiers) + assert obj is generic_adapter.get("interface", intf_identifiers) -def test_diffsync_get_or_add_model_instance_retrieve_existing_object_wo_attrs(generic_diffsync): +def test_diffsync_get_or_add_model_instance_retrieve_existing_object_wo_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf = Interface(**intf_identifiers) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.get_or_add_model_instance(intf) + obj, created = generic_adapter.get_or_add_model_instance(intf) assert obj is intf assert not created assert obj.interface_type == "ethernet" assert obj.description is None -def test_diffsync_update_or_instantiate_retrieve_existing_object_w_updated_attrs(generic_diffsync): +def test_diffsync_update_or_instantiate_retrieve_existing_object_w_updated_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} intf = Interface(**intf_identifiers) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.update_or_instantiate(Interface, intf_identifiers, intf_attrs) + obj, created = generic_adapter.update_or_instantiate(Interface, intf_identifiers, intf_attrs) assert obj is intf assert not created assert obj.interface_type == "1000base-t" assert obj.description == "Testing" -def test_diffsync_update_or_instantiate_create_object(generic_diffsync): +def test_diffsync_update_or_instantiate_create_object(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} - obj, created = generic_diffsync.update_or_instantiate(Interface, intf_identifiers, {}) + obj, created = generic_adapter.update_or_instantiate(Interface, intf_identifiers, {}) assert created assert obj.interface_type == "ethernet" assert obj.description is None -def test_diffsync_update_or_instantiate_create_object_w_attrs(generic_diffsync): +def test_diffsync_update_or_instantiate_create_object_w_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} - obj, created = generic_diffsync.update_or_instantiate(Interface, intf_identifiers, intf_attrs) + obj, created = generic_adapter.update_or_instantiate(Interface, intf_identifiers, intf_attrs) assert created assert obj.interface_type == "1000base-t" assert obj.description == "Testing" -def test_diffsync_update_or_add_model_instance_retrieve_existing_object_w_updated_attrs(generic_diffsync): +def test_diffsync_update_or_add_model_instance_retrieve_existing_object_w_updated_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} intf_combine = {**intf_identifiers, **intf_attrs} intf = Interface(**intf_combine) - generic_diffsync.add(intf) + generic_adapter.add(intf) - obj, created = generic_diffsync.update_or_add_model_instance(intf) + obj, created = generic_adapter.update_or_add_model_instance(intf) assert obj is intf assert not created assert obj.interface_type == "1000base-t" assert obj.description == "Testing" -def test_diffsync_update_or_add_model_instance_create_object(generic_diffsync): +def test_diffsync_update_or_add_model_instance_create_object(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf = Interface(**intf_identifiers) - obj, created = generic_diffsync.update_or_add_model_instance(intf) + obj, created = generic_adapter.update_or_add_model_instance(intf) assert created assert obj.interface_type == "ethernet" assert obj.description is None -def test_diffsync_update_or_add_model_instance_create_object_w_attrs(generic_diffsync): +def test_diffsync_update_or_add_model_instance_create_object_w_attrs(generic_adapter): intf_identifiers = {"device_name": "device1", "name": "eth1"} intf_attrs = {"interface_type": "1000base-t", "description": "Testing"} intf_combine = {**intf_identifiers, **intf_attrs} intf = Interface(**intf_combine) - obj, created = generic_diffsync.update_or_add_model_instance(intf) + obj, created = generic_adapter.update_or_add_model_instance(intf) assert created assert obj.interface_type == "1000base-t" assert obj.description == "Testing" -def test_diffsync_get_with_generic_model(generic_diffsync, generic_diffsync_model): - generic_diffsync.add(generic_diffsync_model) +def test_diffsync_get_with_generic_model(generic_adapter, generic_diffsync_model): + generic_adapter.add(generic_diffsync_model) # The generic_diffsync_model has an empty identifier/unique-id - assert generic_diffsync.get(DiffSyncModel, "") == generic_diffsync_model - assert generic_diffsync.get(DiffSyncModel.get_type(), "") == generic_diffsync_model + assert generic_adapter.get(DiffSyncModel, "") == generic_diffsync_model + assert generic_adapter.get(DiffSyncModel.get_type(), "") == generic_diffsync_model # DiffSync doesn't know how to construct a uid str for a "diffsyncmodel" (it needs the class or instance, not a str) with pytest.raises(ValueError): - generic_diffsync.get(DiffSyncModel.get_type(), {}) + generic_adapter.get(DiffSyncModel.get_type(), {}) # Wrong object-type - no match with pytest.raises(ObjectNotFound): - generic_diffsync.get("", "") + generic_adapter.get("", "") # Wrong unique-id - no match with pytest.raises(ObjectNotFound): - generic_diffsync.get(DiffSyncModel, "myname") + generic_adapter.get(DiffSyncModel, "myname") -def test_diffsync_get_all_with_generic_model(generic_diffsync, generic_diffsync_model): - generic_diffsync.add(generic_diffsync_model) - assert list(generic_diffsync.get_all(DiffSyncModel)) == [generic_diffsync_model] - assert list(generic_diffsync.get_all(DiffSyncModel.get_type())) == [generic_diffsync_model] +def test_diffsync_get_all_with_generic_model(generic_adapter, generic_diffsync_model): + generic_adapter.add(generic_diffsync_model) + assert list(generic_adapter.get_all(DiffSyncModel)) == [generic_diffsync_model] + assert list(generic_adapter.get_all(DiffSyncModel.get_type())) == [generic_diffsync_model] # Wrong object-type - no match - assert not list(generic_diffsync.get_all("anything")) + assert not list(generic_adapter.get_all("anything")) -def test_diffsync_get_by_uids_with_generic_model(generic_diffsync, generic_diffsync_model): - generic_diffsync.add(generic_diffsync_model) - assert generic_diffsync.get_by_uids([""], DiffSyncModel) == [generic_diffsync_model] - assert generic_diffsync.get_by_uids([""], DiffSyncModel.get_type()) == [generic_diffsync_model] +def test_diffsync_get_by_uids_with_generic_model(generic_adapter, generic_diffsync_model): + generic_adapter.add(generic_diffsync_model) + assert generic_adapter.get_by_uids([""], DiffSyncModel) == [generic_diffsync_model] + assert generic_adapter.get_by_uids([""], DiffSyncModel.get_type()) == [generic_diffsync_model] # Wrong unique-id - no match with pytest.raises(ObjectNotFound): - generic_diffsync.get_by_uids(["myname"], DiffSyncModel) + generic_adapter.get_by_uids(["myname"], DiffSyncModel) # Valid unique-id mixed in with unknown ones with pytest.raises(ObjectNotFound): - generic_diffsync.get_by_uids(["aname", "", "anothername"], DiffSyncModel) + generic_adapter.get_by_uids(["aname", "", "anothername"], DiffSyncModel) -def test_diffsync_remove_with_generic_model(generic_diffsync, generic_diffsync_model): - generic_diffsync.add(generic_diffsync_model) - generic_diffsync.remove(generic_diffsync_model) +def test_diffsync_remove_with_generic_model(generic_adapter, generic_diffsync_model): + generic_adapter.add(generic_diffsync_model) + generic_adapter.remove(generic_diffsync_model) with pytest.raises(ObjectNotFound): - generic_diffsync.remove(generic_diffsync_model) + generic_adapter.remove(generic_diffsync_model) with pytest.raises(ObjectNotFound): - generic_diffsync.get(DiffSyncModel, "") - assert not list(generic_diffsync.get_all(DiffSyncModel)) + generic_adapter.get(DiffSyncModel, "") + assert not list(generic_adapter.get_all(DiffSyncModel)) with pytest.raises(ObjectNotFound): - generic_diffsync.get_by_uids([""], DiffSyncModel) + generic_adapter.get_by_uids([""], DiffSyncModel) def test_diffsync_subclass_validation_name_mismatch(): diff --git a/tests/unit/test_diffsync_model.py b/tests/unit/test_diffsync_model.py index e7d6a6b4..0adedb2f 100644 --- a/tests/unit/test_diffsync_model.py +++ b/tests/unit/test_diffsync_model.py @@ -159,41 +159,41 @@ def test_diffsync_model_subclass_add_remove(make_site, make_device, make_interfa device1.remove_child(device1_eth0) -def test_diffsync_model_dict_with_children(generic_diffsync, make_site, make_device, make_interface): - site1 = make_site(diffsync=generic_diffsync) - device1 = make_device(diffsync=generic_diffsync) - device1_eth0 = make_interface(diffsync=generic_diffsync) +def test_diffsync_model_dict_with_children(generic_adapter, make_site, make_device, make_interface): + site1 = make_site(diffsync=generic_adapter) + device1 = make_device(diffsync=generic_adapter) + device1_eth0 = make_interface(diffsync=generic_adapter) site1.add_child(device1) device1.add_child(device1_eth0) # test error handling - diffsync knows about site and device but not interface - generic_diffsync.add(site1) - generic_diffsync.add(device1) + generic_adapter.add(site1) + generic_adapter.add(device1) assert site1.dict() == {"devices": ["device1"], "model_flags": DiffSyncModelFlags.NONE, "name": "site1"} -def test_diffsync_model_json_with_children(generic_diffsync, make_site, make_device, make_interface): - site1 = make_site(diffsync=generic_diffsync) - device1 = make_device(diffsync=generic_diffsync) - device1_eth0 = make_interface(diffsync=generic_diffsync) +def test_diffsync_model_json_with_children(generic_adapter, make_site, make_device, make_interface): + site1 = make_site(diffsync=generic_adapter) + device1 = make_device(diffsync=generic_adapter) + device1_eth0 = make_interface(diffsync=generic_adapter) site1.add_child(device1) device1.add_child(device1_eth0) # test error handling - diffsync knows about site and device but not interface - generic_diffsync.add(site1) - generic_diffsync.add(device1) + generic_adapter.add(site1) + generic_adapter.add(device1) assert site1.json() == '{"name":"site1","devices":["device1"]}' -def test_diffsync_model_str_with_children(generic_diffsync, make_site, make_device, make_interface): - site1 = make_site(diffsync=generic_diffsync) - device1 = make_device(diffsync=generic_diffsync) - device1_eth0 = make_interface(diffsync=generic_diffsync) +def test_diffsync_model_str_with_children(generic_adapter, make_site, make_device, make_interface): + site1 = make_site(diffsync=generic_adapter) + device1 = make_device(diffsync=generic_adapter) + device1_eth0 = make_interface(diffsync=generic_adapter) site1.add_child(device1) device1.add_child(device1_eth0) # test error handling - diffsync knows about site and device but not interface - generic_diffsync.add(site1) - generic_diffsync.add(device1) + generic_adapter.add(site1) + generic_adapter.add(device1) assert ( site1.str() @@ -215,21 +215,21 @@ def test_diffsync_model_str_with_children(generic_diffsync, make_site, make_devi ) -def test_diffsync_model_subclass_crud(generic_diffsync): +def test_diffsync_model_subclass_crud(generic_adapter): """Test basic CRUD operations on generic DiffSyncModel subclasses.""" - device1 = Device.create(generic_diffsync, {"name": "device1"}, {"role": "spine"}) + device1 = Device.create(generic_adapter, {"name": "device1"}, {"role": "spine"}) assert isinstance(device1, Device) - assert device1.diffsync == generic_diffsync + assert device1.adapter == generic_adapter assert device1.name == "device1" assert device1.role == "spine" device1_eth0 = Interface.create( - generic_diffsync, + generic_adapter, {"name": "eth0", "device_name": "device1"}, {"description": "some description"}, ) assert isinstance(device1_eth0, Interface) - assert device1_eth0.diffsync == generic_diffsync + assert device1_eth0.adapter == generic_adapter assert device1_eth0.name == "eth0" assert device1_eth0.device_name == "device1" assert device1_eth0.description == "some description"