Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more migration tests #141

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/test-migrations.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

python manage.py test --pattern="test_*mig.py" --noinput
python manage.py test --pattern="test_*mig.py" --noinput --keepdb
87 changes: 87 additions & 0 deletions database/tests/test_import_rmg_models_mig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django_test_migrations.contrib.unittest_case import MigratorTestCase
from database.templatetags.utils import fields
from database.scripts.import_rmg_models import (
get_reaction_hash,
get_species_hash,
Expand Down Expand Up @@ -28,3 +29,89 @@ def test_consistent_reaction_hash(self):
get_reaction_hash(stoich_species) == reaction.hash,
f"Reaction {reaction} has inconsistent hash with its stoich-species pairs",
)

def _find_kinetics_data(self, kinetics):
for model_name in [
"Arrhenius",
"ArrheniusEP",
"Chebyshev",
"KineticsData",
"Lindemann",
"MultiArrhenius",
"PDepArrhenius",
"MultiPDepArrhenius",
"Troe",
"ThirdBody",
]:
model = self.model(model_name)
try:
return model.objects.get(kinetics=kinetics)
except model.DoesNotExist:
continue

def _get_kinetics_hash(self, kinetics):
source_id = kinetics.source.id if kinetics.source else ""
bd = kinetics.base_data
data = "".join(x[1] for x in fields(self._find_kinetics_data(kinetics))) if bd else ""
base_data_signature = (
"".join(
str(x)
for x in [
bd.order,
bd.min_temp,
bd.max_temp,
bd.min_pressure,
bd.max_pressure,
*bd.collider_efficiencies.values_list("id", flat=True),
]
)
if bd
else ""
)
signature = "".join(
str(x)
for x in [
kinetics.prime_id,
kinetics.reaction.id,
source_id,
kinetics.reverse,
base_data_signature,
data,
]
)

return hash(signature)

def test_unique_kinetics(self):
Kinetics = self.model("Kinetics")
hashes = [self._get_kinetics_hash(k) for k in Kinetics.objects.all()]

self.assertEqual(len(hashes), len(set(hashes)))

def _get_thermo_hash(self, thermo):
signature = "".join(
str(x)
for x in [
thermo.coeffs_poly1,
thermo.coeffs_poly2,
thermo.enthalpy_formation,
thermo.preferred_key,
thermo.prime_id,
thermo.reference_pressure,
thermo.reference_temp,
thermo.source,
thermo.species,
thermo.temp_max_1,
thermo.temp_max_2,
thermo.temp_min_1,
thermo.temp_min_2,
]
)

return hash(signature)

def test_unique_thermo(self):
Thermo = self.model("Thermo")
hashes = [self._get_thermo_hash(t) for t in Thermo.objects.all()]

self.assertEqual(len(hashes), len(set(hashes)))