Skip to content

Commit

Permalink
pydantic str to ObjectId rather than ObjectId directly
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardusrendy committed Aug 8, 2024
1 parent 9f50700 commit e2c641a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions alab_management/experiment_view/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def if_provided_must_be_valid_objectid(cls, v):
return # management will autogenerate a valid objectid

try:
return ObjectId(v)
return str(ObjectId(v))
except Exception as exc:
raise ValueError(
"An experiment received over the API contained a sample with an invalid sample_id. The sample_id was "
"set to {v}, which is not a valid ObjectId."
f"set to {v}, which is not a valid ObjectId."
) from exc

@field_validator("metadata")
Expand Down Expand Up @@ -55,7 +55,7 @@ def if_provided_must_be_valid_objectid(cls, v):
return # management will autogenerate a valid objectid

try:
return ObjectId(v)
return str(ObjectId(v))
except Exception as exc:
raise ValueError(
"An experiment received over the API contained a task with an invalid task_id. The task_id was set to "
Expand Down
20 changes: 13 additions & 7 deletions alab_management/experiment_view/experiment_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,25 @@ def create_experiment(self, experiment: InputExperiment) -> ObjectId:

# confirm that no task/sample id's already exist in the database. This is possible when users manually set
# these id's
for sample in experiment.samples:
if sample.sample_id is None:
experiment=experiment.model_dump(mode="python")

for sample in experiment["samples"]:
if sample["sample_id"] is None:
continue # ALabOS will assign a sample id, this is always safe
if self.sample_view.exists(sample_id=sample.sample_id):
else:
sample["sample_id"] = ObjectId(sample["sample_id"])
if self.sample_view.exists(sample_id=sample["sample_id"]):
raise ValueError(
f"Sample id {sample.sample_id} already exists in the database! Please use another id. This "
f"experiment was not submitted."
)

for task in experiment.tasks:
if task.task_id is None:
for task in experiment["tasks"]:
if task["task_id"] is None:
continue
if self.task_view.exists(task_id=task.task_id):
else:
task["task_id"] = ObjectId(task["task_id"])
if self.task_view.exists(task_id=task["task_id"]):
raise ValueError(
f"Task id {task.task_id} already exists in the database! Please use another id. This experiment "
f"was not submitted."
Expand All @@ -74,7 +80,7 @@ def create_experiment(self, experiment: InputExperiment) -> ObjectId:
# all good, lets submit the experiment into ALabOS!
result = self._experiment_collection.insert_one(
{
**experiment.dict(),
**experiment,
"submitted_at": datetime.now(),
"status": ExperimentStatus.PENDING.name,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_experiment_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_create_experiment(self):
exp_id = self.experiment_view.create_experiment(exp_template)
exp = self.experiment_view.get_experiment(exp_id)

exp_dict = exp_template.dict()
exp_dict = exp_template.model_dump(mode="python")
exp_dict["_id"] = exp_id
exp_dict["status"] = "PENDING"

Expand Down

0 comments on commit e2c641a

Please sign in to comment.