Merge "Add if_exists and **kwargs columns to db_set"

This commit is contained in:
Zuul 2023-04-20 16:16:06 +00:00 committed by Gerrit Code Review
commit 770f77d86f
4 changed files with 61 additions and 9 deletions

View File

@ -152,7 +152,7 @@ class API(object, metaclass=abc.ABCMeta):
"""
@abc.abstractmethod
def db_set(self, table, record, *col_values):
def db_set(self, table, record, *col_values, if_exists=True, **columns):
"""Create a command to set fields in a record
:param table: The OVS table containing the record to be modified
@ -162,11 +162,14 @@ class API(object, metaclass=abc.ABCMeta):
:param col_values: The columns and their associated values
:type col_values: Tuples of (column, value). Values may be atomic
values or unnested sequences/mappings
:param if_exists: Do not fail if record does not exist
:type if_exists: bool
:param columns: The columns and their associated values
(mutually exclusive with col_values and col_values
is used if available)
:type columns: Dictionary of columns names and values
:returns: :class:`Command` with no result
"""
# TODO(twilson) Consider handling kwargs for arguments where order
# doesn't matter. Though that would break the assert_called_once_with
# unit tests
@abc.abstractmethod
def db_add(self, table, record, column, *values):

View File

@ -144,8 +144,9 @@ class Backend(object):
def db_destroy(self, table, record):
return cmd.DbDestroyCommand(self, table, record)
def db_set(self, table, record, *col_values):
return cmd.DbSetCommand(self, table, record, *col_values)
def db_set(self, table, record, *col_values, if_exists=True, **columns):
return cmd.DbSetCommand(self, table, record, *col_values,
if_exists=if_exists, **columns)
def db_add(self, table, record, column, *values):
return cmd.DbAddCommand(self, table, record, column, *values)

View File

@ -137,14 +137,22 @@ class DbDestroyCommand(BaseCommand):
class DbSetCommand(BaseCommand):
def __init__(self, api, table, record, *col_values):
def __init__(self, api, table, record, *col_values, if_exists=False,
**columns):
super().__init__(api)
self.table = table
self.record = record
self.col_values = col_values
self.col_values = col_values or columns.items()
self.if_exists = if_exists
def run_idl(self, txn):
record = self.api.lookup(self.table, self.record)
try:
record = self.api.lookup(self.table, self.record)
except idlutils.RowNotFound:
if self.if_exists:
return
raise
for col, val in self.col_values:
if isinstance(val, abc.Mapping):
# TODO(twilson) This is to make a unit/functional test that

View File

@ -113,3 +113,43 @@ class TestBackendDb(base.FunctionalTestCase):
'Queue', external_ids={'x': 'x'}).execute(check_error=True)
self.assertIsInstance(row, rowview.RowView)
self.api.db_destroy('Queue', row.uuid).execute(check_error=True)
def test_db_set_args(self):
brname = self.bridges[0]['name']
br = self.api.lookup('Bridge', brname)
ext_ids = {'test': 'value'}
self.api.db_set('Bridge', brname,
('external_ids', ext_ids)).execute(check_error=True)
self.assertEqual(ext_ids, br.external_ids)
def test_db_set_kwargs(self):
brname = self.bridges[0]['name']
br = self.api.lookup('Bridge', brname)
ext_ids = {'test': 'value'}
self.api.db_set('Bridge', brname,
external_ids=ext_ids).execute(check_error=True)
self.assertEqual(ext_ids, br.external_ids)
def test_db_set_if_exists(self):
brname = self.bridges[0]['name']
br = self.api.lookup('Bridge', brname)
ext_ids = {'test': 'value'}
self.api.db_set('Bridge', brname, if_exists=True,
external_ids=ext_ids).execute(check_error=True)
self.assertEqual(ext_ids, br.external_ids)
def test_db_set_if_exists_missing(self):
brname = "missing_bridge"
ext_ids = {'test': 'value'}
# Just ensure that this completes without throwing an exception
self.api.db_set('Bridge', brname, if_exists=True,
external_ids=ext_ids).execute(check_error=True)
def test_db_set_args_and_kwrags(self):
brname = self.bridges[0]['name']
br = self.api.lookup('Bridge', brname)
ext_ids = {'test': 'value'}
ext_ids2 = {'test2': 'value2'}
self.api.db_set('Bridge', brname, ('external_ids', ext_ids),
external_ids=ext_ids2).execute(check_error=True)
self.assertEqual(ext_ids, br.external_ids)