Skip to content

Commit

Permalink
Add test for key order preservation and remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
nkanazawa1989 committed Aug 7, 2023
1 parent b00945b commit 2cda8dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 2 additions & 4 deletions qiskit_experiments/database_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,8 @@ def add_entry(
if index in self._container.index:
raise ValueError(f"Table index {index} already exists in the table.")

columns = self.get_columns()
missing = kwargs.keys() - set(columns)
if missing:
self.add_columns(*sorted(missing))
if kwargs.keys() - set(self.get_columns()):
self.add_columns(*kwargs.keys())

template = dict.fromkeys(self.get_columns())
template.update(kwargs)
Expand Down
10 changes: 10 additions & 0 deletions test/framework/test_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def test_add_entry_with_new_key(self):
self.assertListEqual(table.get_columns(), ["value1", "value2", "value3", "extra"])
self.assertListEqual(table.get_entry("x").to_list(), [0.0, 1.0, 2.0, 3.0])

def test_add_entry_with_multiple_new_keys(self):
"""Test new keys are added to column and the key order is preserved."""
table = TestBaseTable.TestTable()
table.add_entry(index="x", phi=0.1, lamb=0.2, theta=0.3)

self.assertListEqual(
table.get_columns(),
["value1", "value2", "value3", "phi", "lamb", "theta"],
)

def test_add_entry_with_new_key_with_existing_entry(self):
"""Test adding new key will expand existing entry."""
table = TestBaseTable.TestTable()
Expand Down

0 comments on commit 2cda8dc

Please sign in to comment.