Skip to content

Commit

Permalink
Adjust battDB tests and bakery for unique constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianDAlessandro committed Nov 29, 2022
1 parent a02dd9a commit a73bf7c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
18 changes: 14 additions & 4 deletions tests/battDB/baker_recipes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from model_bakery.recipe import Recipe, foreign_key
from model_bakery.recipe import Recipe, foreign_key, seq

from battDB.models import (
Batch,
Expand All @@ -22,7 +22,11 @@
from tests.dfndb.baker_recipes import component, parameter
from tests.management.baker_recipes import user

device_specification = Recipe(DeviceSpecification, user_owner=foreign_key(user))
device_specification = Recipe(
DeviceSpecification,
user_owner=foreign_key(user),
name=seq("Device Specification"),
)

device_parameter = Recipe(
DeviceParameter,
Expand Down Expand Up @@ -57,11 +61,17 @@
parser = Recipe(Parser, user_owner=foreign_key(user))

equipment = Recipe(
Equipment, institution=foreign_key(org), user_owner=foreign_key(user)
Equipment,
institution=foreign_key(org),
user_owner=foreign_key(user),
name=seq("Equipment"),
)

experiment = Recipe(
Experiment, config=foreign_key(device_config), user_owner=foreign_key(user)
Experiment,
config=foreign_key(device_config),
user_owner=foreign_key(user),
name=seq("Experiment"),
)

edf = Recipe(ExperimentDataFile, user_owner=foreign_key(user))
Expand Down
10 changes: 10 additions & 0 deletions tests/battDB/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def test_get_absolute_url(self):
url = self.model.get_absolute_url()
self.assertIn("battDB/exps", url)

def test_unique_name(self):
with self.assertRaises(ValidationError):
new = baker.make_recipe(
"tests.battDB.experiment",
name=self.model.name,
user_owner=self.model.user_owner,
)
new.clean()
baker.make_recipe("tests.battDB.experiment", name=self.model.name)


class TestExperimentDataFile(TestCase):
def setUp(self):
Expand Down
17 changes: 12 additions & 5 deletions tests/dfndb/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ def test_str(self):
f"{self.expected['name']} ({self.expected['formula']})",
)

def test_unique_together(self):
baker.make_recipe("tests.dfndb.compound", name="Carbon Dioxide", formula="CO3")
def test_unique(self):
from django.db import transaction

with self.assertRaises(IntegrityError):
baker.make_recipe(
"tests.dfndb.compound", name="Carbon Dioxide", formula="CO2"
)
with transaction.atomic():
baker.make_recipe(
"tests.dfndb.compound", name="Carbon Dioxide", formula="CO3"
)
with self.assertRaises(IntegrityError):
with transaction.atomic():
baker.make_recipe(
"tests.dfndb.compound", name="Carbon Dioxide 2", formula="CO2"
)

def test_invalid_formula(self):
with self.assertRaises(ValidationError):
Expand Down

0 comments on commit a73bf7c

Please sign in to comment.