14
14
15
15
from test .base import QiskitExperimentsTestCase
16
16
17
+ import uuid
17
18
import numpy as np
18
19
import pandas as pd
19
20
@@ -58,28 +59,33 @@ def test_raises_initializing_with_wrong_table(self):
58
59
# columns doesn't match with default_columns
59
60
TestBaseTable .TestTable (wrong_table )
60
61
62
+ def test_get_entry (self ):
63
+ """Test getting an entry from the table."""
64
+ table = TestBaseTable .TestTable ({"x" : [1.0 , 2.0 , 3.0 ]})
65
+ self .assertListEqual (table .get_entry ("x" ).to_list (), [1.0 , 2.0 , 3.0 ])
66
+
61
67
def test_add_entry (self ):
62
68
"""Test adding data with default keys to table."""
63
69
table = TestBaseTable .TestTable ()
64
70
table .add_entry (index = "x" , value1 = 0.0 , value2 = 1.0 , value3 = 2.0 )
65
71
66
- self .assertListEqual (table .loc [ "x" ] .to_list (), [0.0 , 1.0 , 2.0 ])
72
+ self .assertListEqual (table .get_entry ( "x" ) .to_list (), [0.0 , 1.0 , 2.0 ])
67
73
68
74
def test_add_entry_with_missing_key (self ):
69
75
"""Test adding entry with partly specified keys."""
70
76
table = TestBaseTable .TestTable ()
71
77
table .add_entry (index = "x" , value1 = 0.0 , value3 = 2.0 )
72
78
73
79
# NaN value cannot be compared with assert
74
- np .testing .assert_equal (table .loc [ "x" ] .to_list (), [0.0 , float ("nan" ), 2.0 ])
80
+ np .testing .assert_equal (table .get_entry ( "x" ) .to_list (), [0.0 , float ("nan" ), 2.0 ])
75
81
76
82
def test_add_entry_with_new_key (self ):
77
83
"""Test adding data with new keys to table."""
78
84
table = TestBaseTable .TestTable ()
79
85
table .add_entry (index = "x" , value1 = 0.0 , value2 = 1.0 , value3 = 2.0 , extra = 3.0 )
80
86
81
87
self .assertListEqual (table .get_columns (), ["value1" , "value2" , "value3" , "extra" ])
82
- self .assertListEqual (table .loc [ "x" ] .to_list (), [0.0 , 1.0 , 2.0 , 3.0 ])
88
+ self .assertListEqual (table .get_entry ( "x" ) .to_list (), [0.0 , 1.0 , 2.0 , 3.0 ])
83
89
84
90
def test_add_entry_with_new_key_with_existing_entry (self ):
85
91
"""Test adding new key will expand existing entry."""
@@ -88,10 +94,24 @@ def test_add_entry_with_new_key_with_existing_entry(self):
88
94
table .add_entry (index = "y" , value1 = 0.0 , value2 = 1.0 , value3 = 2.0 , extra = 3.0 )
89
95
90
96
self .assertListEqual (table .get_columns (), ["value1" , "value2" , "value3" , "extra" ])
91
- self .assertListEqual (table .loc [ "y" ] .to_list (), [0.0 , 1.0 , 2.0 , 3.0 ])
97
+ self .assertListEqual (table .get_entry ( "y" ) .to_list (), [0.0 , 1.0 , 2.0 , 3.0 ])
92
98
93
99
# NaN value cannot be compared with assert
94
- np .testing .assert_equal (table .loc ["x" ].to_list (), [0.0 , 1.0 , 2.0 , float ("nan" )])
100
+ np .testing .assert_equal (table .get_entry ("x" ).to_list (), [0.0 , 1.0 , 2.0 , float ("nan" )])
101
+
102
+ def test_drop_entry (self ):
103
+ """Test drop entry from the table."""
104
+ table = TestBaseTable .TestTable ()
105
+ table .add_entry (index = "x" , value1 = 0.0 , value2 = 1.0 , value3 = 2.0 )
106
+ table .drop_entry ("x" )
107
+
108
+ self .assertEqual (len (table ), 0 )
109
+
110
+ def test_drop_non_existing_entry (self ):
111
+ """Test dropping non-existing entry raises ValueError."""
112
+ table = TestBaseTable .TestTable ()
113
+ with self .assertRaises (ValueError ):
114
+ table .drop_entry ("x" )
95
115
96
116
def test_return_only_default_columns (self ):
97
117
"""Test extra entry is correctly recognized."""
@@ -131,7 +151,7 @@ def test_container_is_immutable(self):
131
151
self .assertListEqual (dataframe .loc ["x" ].to_list (), [100 , 0.2 , 0.3 ])
132
152
133
153
# Original object in the experiment payload is preserved
134
- self .assertListEqual (table .loc [ "x" ] .to_list (), [0.1 , 0.2 , 0.3 ])
154
+ self .assertListEqual (table .get_entry ( "x" ) .to_list (), [0.1 , 0.2 , 0.3 ])
135
155
136
156
def test_round_trip (self ):
137
157
"""Test JSON roundtrip serialization with the experiment encoder."""
@@ -149,7 +169,7 @@ def test_add_entry_with_result_id(self):
149
169
"""Test adding entry with result_id. Index is created by truncating long string."""
150
170
table = AnalysisResultTable ()
151
171
table .add_entry (result_id = "9a0bdec8c0104ef7bb7db84939717a6b" , value = 0.123 )
152
- self .assertEqual (table .loc [ "9a0bdec8" ] .value , 0.123 )
172
+ self .assertEqual (table .get_entry ( "9a0bdec8" ) .value , 0.123 )
153
173
154
174
def test_extra_column_name_is_always_returned (self ):
155
175
"""Test extra column names are always returned in filtered column names."""
@@ -165,6 +185,16 @@ def test_extra_column_name_is_always_returned(self):
165
185
all_columns = table .filter_columns ("all" )
166
186
self .assertTrue ("extra" in all_columns )
167
187
188
+ def test_listing_result_id (self ):
189
+ """Test returning result IDs of all stored entries."""
190
+ table = AnalysisResultTable ()
191
+
192
+ ref_ids = [uuid .uuid4 ().hex for _ in range (10 )]
193
+ for ref_id in ref_ids :
194
+ table .add_entry (result_id = ref_id , value = 0 )
195
+
196
+ self .assertListEqual (table .result_ids (), ref_ids )
197
+
168
198
def test_no_overlap_result_id (self ):
169
199
"""Test automatically prepare unique result IDs for sufficient number of entries."""
170
200
table = AnalysisResultTable ()
0 commit comments