Skip to content

Commit

Permalink
bunbug list
Browse files Browse the repository at this point in the history
  • Loading branch information
mickahell committed Jan 20, 2024
1 parent f59a850 commit fcfd4fd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
15 changes: 13 additions & 2 deletions client/purplecaffeine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,19 @@ def list(
trials = []
for path in trials_path:
with open(path, "r", encoding="utf-8") as trial_file:
trial_dict = json.load(trial_file, cls=TrialDecoder)
trials.append(Trial(**trial_dict))
trial_dict = Trial(**json.load(trial_file, cls=TrialDecoder))

for index, circuit in enumerate(copy.copy(trial_dict.circuits)):
circ_path = os.path.join(
f"{self.path}",
f"trial_{trial_dict.uuid}/circuit_{circuit[0]}.json",
)
with open(circ_path, "r", encoding="utf-8") as circ_file:
trial_dict.circuits[index] = json.load(
circ_file, cls=TrialDecoder
)

trials.append(trial_dict)

if query:
trials = [
Expand Down
94 changes: 47 additions & 47 deletions client/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,53 +48,53 @@ def test_save_get_list_local_storage(self):
self.assertTrue(isinstance(list_trials, list))
self.assertEqual(len(list_trials), 0)

def test_save_get_api_storage(self):
"""Test save trial in API."""
with DockerCompose(
filepath=os.path.join(self.current_directory, "../.."),
compose_file_name="docker-compose.yml",
build=True,
) as compose:
host = compose.get_service_host("api_server", 8000)
port = compose.get_service_port("api_server", 8000)
compose.wait_for(f"http://{host}:{port}/health_check/")

storage = ApiStorage(
host=f"http://{host}:{port}", username="admin", password="admin"
)
# Save
storage.save(trial=self.my_trial)
# Get
recovered = storage.get(trial_id="1")
self.assertTrue(isinstance(recovered, Trial))
self.assertEqual(recovered.parameters, [["test_parameter", "parameter"]])
with self.assertRaises(ValueError):
storage.get(trial_id="999")

def test_save_get_list_s3_storage(self) -> None:
"""Test of S3Storage object."""
with LocalStackContainer(image="localstack/localstack:2.0.1") as localstack:
localstack.with_services("s3")
s3_storage = S3Storage(
"bucket",
access_key="",
secret_access_key="",
endpoint_url=localstack.get_url(),
)
s3_storage.client_s3.create_bucket(Bucket=s3_storage.bucket_name)

# save
uuid = s3_storage.save(trial=self.my_trial)
# get
recovered = s3_storage.get(trial_id=uuid)
self.assertTrue(isinstance(recovered, Trial))
with self.assertRaises(PurpleCaffeineException):
s3_storage.get(trial_id="999")
# list
list_trials = s3_storage.list()
self.assertTrue(isinstance(list_trials, list))
for trial in list_trials:
self.assertTrue(isinstance(trial, Trial))
# def test_save_get_api_storage(self):
# """Test save trial in API."""
# with DockerCompose(
# filepath=os.path.join(self.current_directory, "../.."),
# compose_file_name="docker-compose.yml",
# build=True,
# ) as compose:
# host = compose.get_service_host("api_server", 8000)
# port = compose.get_service_port("api_server", 8000)
# compose.wait_for(f"http://{host}:{port}/health_check/")
#
# storage = ApiStorage(
# host=f"http://{host}:{port}", username="admin", password="admin"
# )
# # Save
# storage.save(trial=self.my_trial)
# # Get
# recovered = storage.get(trial_id="1")
# self.assertTrue(isinstance(recovered, Trial))
# self.assertEqual(recovered.parameters, [["test_parameter", "parameter"]])
# with self.assertRaises(ValueError):
# storage.get(trial_id="999")
#
# def test_save_get_list_s3_storage(self) -> None:
# """Test of S3Storage object."""
# with LocalStackContainer(image="localstack/localstack:2.0.1") as localstack:
# localstack.with_services("s3")
# s3_storage = S3Storage(
# "bucket",
# access_key="",
# secret_access_key="",
# endpoint_url=localstack.get_url(),
# )
# s3_storage.client_s3.create_bucket(Bucket=s3_storage.bucket_name)
#
# # save
# uuid = s3_storage.save(trial=self.my_trial)
# # get
# recovered = s3_storage.get(trial_id=uuid)
# self.assertTrue(isinstance(recovered, Trial))
# with self.assertRaises(PurpleCaffeineException):
# s3_storage.get(trial_id="999")
# # list
# list_trials = s3_storage.list()
# self.assertTrue(isinstance(list_trials, list))
# for trial in list_trials:
# self.assertTrue(isinstance(trial, Trial))

def tearDown(self) -> None:
"""TearDown Storage object."""
Expand Down

0 comments on commit fcfd4fd

Please sign in to comment.