Skip to content

Commit 650e723

Browse files
committed
fix UT
1 parent b20dfb9 commit 650e723

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

config/validated_config_db_connector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def add_patch_entry():
8787
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
8888
return gcu_patch
8989

90-
def apply_patch(self, gcu_patch):
90+
def apply_patch(self, gcu_patch, table):
9191
format = ConfigFormat.CONFIGDB.name
9292
config_format = ConfigFormat[format.upper()]
9393

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

115115
gcu_patch = self.create_gcu_patch(op, table, key, value, mod_entry=True)
116-
self.apply_patch(gcu_patch)
116+
self.apply_patch(gcu_patch, table)
117117

118118
def validated_set_entry(self, table, key, value):
119119
if value is not None:

tests/validated_config_db_connector_test.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
SAMPLE_TABLE = 'VLAN'
1919
SAMPLE_KEY = 'Vlan1000'
2020
SAMPLE_VALUE_EMPTY = None
21+
SAMPLE_VALUE = 'test'
22+
SAMPLE_VALUE_DICT = {'sample_field_key': 'sample_field_value'}
23+
SAMPLE_PATCH = [{"op": "add", "path": "/VLAN", "value": "sample value"}]
2124

2225

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

39+
def test_validated_mod_entry(self):
40+
mock_generic_updater = mock.Mock()
41+
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
42+
successful_application = validated_config_db_connector.ValidatedConfigDBConnector.validated_mod_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_DICT)
43+
assert successful_application
44+
3645
def test_validated_delete_table_invalid_delete(self):
3746
mock_generic_updater = mock.Mock()
3847
mock_generic_updater.apply_patch = mock.Mock(side_effect=ValueError)
@@ -41,8 +50,16 @@ def test_validated_delete_table_invalid_delete(self):
4150
assert not delete_table_success
4251

4352
def test_create_gcu_patch(self):
44-
expected_gcu_patch = jsonpatch.JsonPatch([{"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": "test"}])
45-
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_table', return_value=True):
46-
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_entry', return_value=True):
47-
created_gcu_patch = validated_config_db_connector.ValidatedConfigDBConnector.create_gcu_patch(ValidatedConfigDBConnector(ConfigDBConnector()), "add", "PORTCHANNEL", "PortChannel01", "test")
53+
expected_gcu_patch = jsonpatch.JsonPatch([{"op": "add", "path": "/PORTCHANNEL", "value": {}}, {"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": {}}, {"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": "test"}])
54+
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_table', return_value=False):
55+
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_entry', return_value=False):
56+
created_gcu_patch = validated_config_db_connector.ValidatedConfigDBConnector.create_gcu_patch(ValidatedConfigDBConnector(ConfigDBConnector()), "add", "PORTCHANNEL", "PortChannel01", SAMPLE_VALUE)
4857
assert expected_gcu_patch == created_gcu_patch
58+
59+
def test_apply_patch(self):
60+
mock_generic_updater = mock.Mock()
61+
mock_generic_updater.apply_patch = mock.Mock(side_effect=EmptyTableError)
62+
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
63+
with mock.patch('validated_config_db_connector.ValidatedConfigDBConnector.validated_delete_table', return_value=True):
64+
apply_patch_success = validated_config_db_connector.ValidatedConfigDBConnector.apply_patch(mock.Mock(), SAMPLE_PATCH, SAMPLE_TABLE)
65+
assert not apply_patch_success

0 commit comments

Comments
 (0)