def test_get_figure(self): """Test getting figure.""" exp_data = DbExperimentData(experiment_type="qiskit_test") figure_template = "hello world {}" name_template = "figure_{}.svg" for idx in range(3): exp_data.add_figures( str.encode(figure_template.format(idx)), figure_names=name_template.format(idx) ) idx = randrange(3) expected_figure = str.encode(figure_template.format(idx)) self.assertEqual(expected_figure, exp_data.figure(name_template.format(idx))) self.assertEqual(expected_figure, exp_data.figure(idx)) file_name = uuid.uuid4().hex self.addCleanup(os.remove, file_name) exp_data.figure(idx, file_name) with open(file_name, "rb") as file: self.assertEqual(expected_figure, file.read())
def test_add_figure_overwrite(self): """Test updating an existing figure.""" hello_bytes = str.encode("hello world") friend_bytes = str.encode("hello friend!") exp_data = DbExperimentData(backend=self.backend, experiment_type="qiskit_test") fn = exp_data.add_figures(hello_bytes) with self.assertRaises(DbExperimentEntryExists): exp_data.add_figures(friend_bytes, fn) exp_data.add_figures(friend_bytes, fn, overwrite=True) self.assertEqual(friend_bytes, exp_data.figure(fn))
def test_add_figure_plot(self): """Test adding a matplotlib figure.""" figure, ax = plt.subplots() ax.plot([1, 2, 3]) service = self._set_mock_service() exp_data = DbExperimentData(backend=self.backend, experiment_type="qiskit_test") exp_data.add_figures(figure, save_figure=True) self.assertEqual(figure, exp_data.figure(0)) service.create_figure.assert_called_once() _, kwargs = service.create_figure.call_args self.assertIsInstance(kwargs["figure"], bytes)
def test_add_figure(self): """Test adding a new figure.""" hello_bytes = str.encode("hello world") file_name = uuid.uuid4().hex self.addCleanup(os.remove, file_name) with open(file_name, "wb") as file: file.write(hello_bytes) sub_tests = [ ("file name", file_name, None), ("file bytes", hello_bytes, None), ("new name", hello_bytes, "hello_again.svg"), ] for name, figure, figure_name in sub_tests: with self.subTest(name=name): exp_data = DbExperimentData(backend=self.backend, experiment_type="qiskit_test") fn = exp_data.add_figures(figure, figure_name) self.assertEqual(hello_bytes, exp_data.figure(fn))
def test_add_figures(self): """Test adding multiple new figures.""" hello_bytes = [str.encode("hello world"), str.encode("hello friend")] file_names = [uuid.uuid4().hex, uuid.uuid4().hex] for idx, fn in enumerate(file_names): self.addCleanup(os.remove, fn) with open(fn, "wb") as file: file.write(hello_bytes[idx]) sub_tests = [ ("file names", file_names, None), ("file bytes", hello_bytes, None), ("new names", hello_bytes, ["hello1.svg", "hello2.svg"]), ] for name, figures, figure_names in sub_tests: with self.subTest(name=name): exp_data = DbExperimentData(backend=self.backend, experiment_type="qiskit_test") added_names = exp_data.add_figures(figures, figure_names) for idx, added_fn in enumerate(added_names): self.assertEqual(hello_bytes[idx], exp_data.figure(added_fn))