Skip to content

Commit

Permalink
fix UT
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelmsft committed Oct 28, 2022
1 parent b20dfb9 commit 650e723
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions config/validated_config_db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def add_patch_entry():
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
return gcu_patch

def apply_patch(self, gcu_patch):
def apply_patch(self, gcu_patch, table):
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]

Expand All @@ -113,7 +113,7 @@ def validated_mod_entry(self, table, key, value):
op = "remove"

gcu_patch = self.create_gcu_patch(op, table, key, value, mod_entry=True)
self.apply_patch(gcu_patch)
self.apply_patch(gcu_patch, table)

def validated_set_entry(self, table, key, value):
if value is not None:
Expand Down
25 changes: 21 additions & 4 deletions tests/validated_config_db_connector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
SAMPLE_TABLE = 'VLAN'
SAMPLE_KEY = 'Vlan1000'
SAMPLE_VALUE_EMPTY = None
SAMPLE_VALUE = 'test'
SAMPLE_VALUE_DICT = {'sample_field_key': 'sample_field_value'}
SAMPLE_PATCH = [{"op": "add", "path": "/VLAN", "value": "sample value"}]


class TestValidatedConfigDBConnector(TestCase):
Expand All @@ -33,6 +36,12 @@ def test_validated_set_entry_empty_table(self):
remove_entry_success = validated_config_db_connector.ValidatedConfigDBConnector.validated_set_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_EMPTY)
assert not remove_entry_success

def test_validated_mod_entry(self):
mock_generic_updater = mock.Mock()
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
successful_application = validated_config_db_connector.ValidatedConfigDBConnector.validated_mod_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_DICT)
assert successful_application

def test_validated_delete_table_invalid_delete(self):
mock_generic_updater = mock.Mock()
mock_generic_updater.apply_patch = mock.Mock(side_effect=ValueError)
Expand All @@ -41,8 +50,16 @@ def test_validated_delete_table_invalid_delete(self):
assert not delete_table_success

def test_create_gcu_patch(self):
expected_gcu_patch = jsonpatch.JsonPatch([{"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": "test"}])
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_table', return_value=True):
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_entry', return_value=True):
created_gcu_patch = validated_config_db_connector.ValidatedConfigDBConnector.create_gcu_patch(ValidatedConfigDBConnector(ConfigDBConnector()), "add", "PORTCHANNEL", "PortChannel01", "test")
expected_gcu_patch = jsonpatch.JsonPatch([{"op": "add", "path": "/PORTCHANNEL", "value": {}}, {"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": {}}, {"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": "test"}])
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_table', return_value=False):
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_entry', return_value=False):
created_gcu_patch = validated_config_db_connector.ValidatedConfigDBConnector.create_gcu_patch(ValidatedConfigDBConnector(ConfigDBConnector()), "add", "PORTCHANNEL", "PortChannel01", SAMPLE_VALUE)
assert expected_gcu_patch == created_gcu_patch

def test_apply_patch(self):
mock_generic_updater = mock.Mock()
mock_generic_updater.apply_patch = mock.Mock(side_effect=EmptyTableError)
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
with mock.patch('validated_config_db_connector.ValidatedConfigDBConnector.validated_delete_table', return_value=True):
apply_patch_success = validated_config_db_connector.ValidatedConfigDBConnector.apply_patch(mock.Mock(), SAMPLE_PATCH, SAMPLE_TABLE)
assert not apply_patch_success

0 comments on commit 650e723

Please sign in to comment.