Skip to content

Add add_or_update to DiffSync class. #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b2dc459
Add add_or_update to DiffSync class.
FragmentedPacket Oct 11, 2021
8161a2d
Merge branch 'main' of git://github.com/networktocode/diffsync into 5…
FragmentedPacket Oct 26, 2021
b172cf0
Update diffsync.add to only raise ObjectAlreadyExists if objects diff…
FragmentedPacket Oct 26, 2021
182542b
Pylint to pass for tests for diffsync.add
FragmentedPacket Oct 26, 2021
7ed20cb
Add get_or_create along with tests.
FragmentedPacket Oct 27, 2021
f4f63e4
Addressed some of the feedback from Glenn.
FragmentedPacket Oct 29, 2021
c4c32cf
Rename get_or_create/get_or_instantiate. Update tests.
FragmentedPacket Oct 29, 2021
e61b560
Add testing for update_or_create. Return object in ObjectAlreadyExist…
FragmentedPacket Oct 30, 2021
e917055
Update doc strings
FragmentedPacket Oct 31, 2021
8f3b328
Add example of new methods.
FragmentedPacket Oct 31, 2021
c43f789
Updates to add device to diffsync. Update tests. Update test names an…
FragmentedPacket Nov 6, 2021
95cb2f0
Few doc updates for licensing year. Remove var assignment for error t…
FragmentedPacket Nov 6, 2021
37b3bca
Update diffsync/__init__.py
FragmentedPacket Nov 8, 2021
93a0a74
Update tests/unit/test_diffsync.py
FragmentedPacket Nov 8, 2021
025a401
Require attrs for update_or, Set existing_object as attribute in Obje…
FragmentedPacket Nov 8, 2021
6c747f1
Add README.md to example-04. Add Example 04 to examples/index.rst
FragmentedPacket Nov 12, 2021
4f9bf4b
Use python 3.9.7 for black.
FragmentedPacket Nov 12, 2021
c970516
Update to support 3.9 and black for compatibility.
FragmentedPacket Nov 12, 2021
647d971
Believe I fixed the dependencies
FragmentedPacket Nov 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update doc strings
  • Loading branch information
FragmentedPacket committed Oct 31, 2021
commit e917055f5bc36c2b9ad103cc31c76394679fccbd
8 changes: 5 additions & 3 deletions diffsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def remove(self, obj: DiffSyncModel, remove_children: bool = False):
def get_or_instantiate(
self, model: Type[DiffSyncModel], ids: Dict, attrs: Dict = None
) -> Tuple[DiffSyncModel, bool]:
"""Attempt to get the object with provided identifiers or create it with provided identifiers and attrs.
"""Attempt to get the object with provided identifiers or instantiate it with provided identifiers and attrs.

Args:
model (DiffSyncModel): The DiffSyncModel to get or create.
Expand All @@ -720,8 +720,10 @@ def get_or_instantiate(

return obj, created

def update_or_create(self, model: Type[DiffSyncModel], ids: Dict, attrs: Dict = None) -> Tuple[DiffSyncModel, bool]:
"""Attempt to update an existing object with provided ids/attrs or create it with provided identifiers and attrs.
def update_or_instantiate(
self, model: Type[DiffSyncModel], ids: Dict, attrs: Dict = None
) -> Tuple[DiffSyncModel, bool]:
"""Attempt to update an existing object with provided ids/attrs or instantiate it with provided identifiers and attrs.

Args:
model (DiffSyncModel): The DiffSyncModel to get or create.
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_diffsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_diffsync_get_or_update_retrieve_existing_object(generic_diffsync):
intf = Interface(**intf_indentifiers)
generic_diffsync.add(intf)

obj, created = generic_diffsync.update_or_create(Interface, intf_indentifiers)
obj, created = generic_diffsync.update_or_instantiate(Interface, intf_indentifiers)
assert obj is intf
assert not created
assert obj.interface_type == "ethernet"
Expand All @@ -172,7 +172,7 @@ def test_diffsync_get_or_update_retrieve_existing_object_w_updated_attrs(generic
intf = Interface(**intf_indentifiers)
generic_diffsync.add(intf)

obj, created = generic_diffsync.update_or_create(Interface, intf_indentifiers, intf_attrs)
obj, created = generic_diffsync.update_or_instantiate(Interface, intf_indentifiers, intf_attrs)
assert obj is intf
assert not created
assert obj.interface_type == "1000base-t"
Expand All @@ -182,7 +182,7 @@ def test_diffsync_get_or_update_retrieve_existing_object_w_updated_attrs(generic
def test_diffsync_get_or_update_create_object(generic_diffsync):
intf_indentifiers = {"device_name": "device1", "name": "eth1"}

obj, created = generic_diffsync.update_or_create(Interface, intf_indentifiers)
obj, created = generic_diffsync.update_or_instantiate(Interface, intf_indentifiers)
assert created
assert obj.interface_type == "ethernet"
assert obj.description is None
Expand All @@ -192,7 +192,7 @@ def test_diffsync_get_or_update_create_object_w_attrs(generic_diffsync):
intf_indentifiers = {"device_name": "device1", "name": "eth1"}
intf_attrs = {"interface_type": "1000base-t", "description": "Testing"}

obj, created = generic_diffsync.update_or_create(Interface, intf_indentifiers, intf_attrs)
obj, created = generic_diffsync.update_or_instantiate(Interface, intf_indentifiers, intf_attrs)
assert created
assert obj.interface_type == "1000base-t"
assert obj.description == "Testing"
Expand Down