Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe committed Sep 26, 2023
1 parent a3f84a2 commit df26f6b
Show file tree
Hide file tree
Showing 25 changed files with 16,947 additions and 26,466 deletions.
4 changes: 4 additions & 0 deletions cases_files/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@freeze_time("2012-01-14 12:00:01")
class IndividualExternalFileTest(TestCase):
def setUp(self):
super().setUp()
self.maxDiff = None # show full diff

def test_create(self):
Expand All @@ -37,6 +38,7 @@ def test_update(self):
@freeze_time("2012-01-14 12:00:01")
class PedigreeExternalFileTest(TestCase):
def setUp(self):
super().setUp()
self.maxDiff = None # show full diff

def test_create(self):
Expand All @@ -56,6 +58,7 @@ def test_update(self):
@freeze_time("2012-01-14 12:00:01")
class IndividualInternalFileTest(TestCase):
def setUp(self):
super().setUp()
self.maxDiff = None # show full diff

def test_create(self):
Expand All @@ -79,6 +82,7 @@ def test_update(self):
@freeze_time("2012-01-14 12:00:01")
class PedigreeInternalFileTest(TestCase):
def setUp(self):
super().setUp()
self.maxDiff = None # show full diff

def test_create(self):
Expand Down
2 changes: 2 additions & 0 deletions cases_import/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@freeze_time("2012-01-14 12:00:01")
class CaseImportActionTest(TestCase):
def setUp(self):
super().setUp()
self.maxDiff = None # show full diff

def test_create(self):
Expand All @@ -36,6 +37,7 @@ def test_update(self):

class CaseImportBackgroundJobTest(TestCase):
def setUp(self):
super().setUp()
self.maxDiff = None # show full diff
self.user = self.make_user("test_user")

Expand Down
6 changes: 5 additions & 1 deletion cases_import/tests/test_models_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

class ExecutorTestMixin:
def _setUpExecutor(self, action, fac_kwargs=None):
super().setUp()
self.user = self.make_user()
self.caseimportaction = CaseImportActionFactory(
action=action,
Expand Down Expand Up @@ -56,6 +55,7 @@ class ImportCreateTest(ExecutorTestMixin, TestCaseSnapshot, TestCase):
"""

def setUp(self):
super().setUp()
self._setUpExecutor(CaseImportAction.ACTION_CREATE)

def test_run(self):
Expand All @@ -71,6 +71,7 @@ class ImportCreateWithDragenQcTest(ExecutorTestMixin, TestCaseSnapshot, TestCase
"""

def setUp(self):
super().setUp()
self.maxDiff = None
self._setUpExecutor(
CaseImportAction.ACTION_CREATE,
Expand Down Expand Up @@ -354,6 +355,7 @@ class ImportCreateWithSamtoolsQcTest(ExecutorTestMixin, TestCaseSnapshot, TestCa
"""

def setUp(self):
super().setUp()
self.maxDiff = None
self._setUpExecutor(
CaseImportAction.ACTION_CREATE,
Expand Down Expand Up @@ -421,6 +423,7 @@ class ImportCreateWithCraminoQcTest(ExecutorTestMixin, TestCaseSnapshot, TestCas
"""

def setUp(self):
super().setUp()
self.maxDiff = None
self._setUpExecutor(
CaseImportAction.ACTION_CREATE,
Expand Down Expand Up @@ -463,6 +466,7 @@ class ImportCreateWithNgsbitsQcTest(ExecutorTestMixin, TestCaseSnapshot, TestCas
"""

def setUp(self):
super().setUp()
self.maxDiff = None
self._setUpExecutor(
CaseImportAction.ACTION_CREATE,
Expand Down
102 changes: 51 additions & 51 deletions cases_qc/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,83 +294,83 @@ class Meta:
model = BcftoolsStatsSnRecord

key = factory.Faker("word")
value = factory.Sequence(lambda n: n)
value = factory.Faker("pyint")


class SamtoolsStatsFqRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsFqRecord

cycle = factory.Sequence(lambda n: n)
counts = factory.Sequence(lambda n: [n])
cycle = factory.Faker("pyint")
counts = factory.Faker("pylist", value_types=[int])


class SamtoolsStatsGcRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsGcRecord

gc_content = factory.Sequence(lambda n: 0.1 * n)
count = factory.Sequence(lambda n: n)
gc_content = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
count = factory.Faker("pyint")


class SamtoolsStatsBasePercentagesRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsBasePercentagesRecord

cycle = factory.Sequence(lambda n: n)
percentages = factory.Sequence(lambda n: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
cycle = factory.Faker("pyint")
percentages = factory.Faker("pylist", value_types=[float])


class SamtoolsStatsIsRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsIsRecord

insert_size = factory.Sequence(lambda n: n)
pairs_total = factory.Sequence(lambda n: n)
pairs_inward = factory.Sequence(lambda n: n)
pairs_outward = factory.Sequence(lambda n: n)
pairs_other = factory.Sequence(lambda n: n)
insert_size = factory.Faker("pyint")
pairs_total = factory.Faker("pyint")
pairs_inward = factory.Faker("pyint")
pairs_outward = factory.Faker("pyint")
pairs_other = factory.Faker("pyint")


class SamtoolsStatsHistoRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsHistoRecord

value = factory.Sequence(lambda n: n)
count = factory.Sequence(lambda n: n)
value = factory.Faker("pyint")
count = factory.Faker("pyint")


class SamtoolsStatsIdRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsIdRecord

length = factory.Sequence(lambda n: n)
ins = factory.Sequence(lambda n: n)
dels = factory.Sequence(lambda n: n)
length = factory.Faker("pyint")
ins = factory.Faker("pyint")
dels = factory.Faker("pyint")


class SamtoolsStatsIcRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsIcRecord

cycle = factory.Sequence(lambda n: n)
ins_fwd = factory.Sequence(lambda n: n)
dels_fwd = factory.Sequence(lambda n: n)
ins_rev = factory.Sequence(lambda n: n)
dels_rev = factory.Sequence(lambda n: n)
cycle = factory.Faker("pyint")
ins_fwd = factory.Faker("pyint")
dels_fwd = factory.Faker("pyint")
ins_rev = factory.Faker("pyint")
dels_rev = factory.Faker("pyint")


class SamtoolsStatsGcdRecordFactory(factory.Factory):
class Meta:
model = SamtoolsStatsGcdRecord

gc_content = factory.Sequence(lambda n: 0.1 * n)
unique_seq_percentiles = factory.Sequence(lambda n: 0.1 * n)
dp_percentile_10 = factory.Sequence(lambda n: 0.1 * n)
dp_percentile_25 = factory.Sequence(lambda n: 0.1 * n)
dp_percentile_50 = factory.Sequence(lambda n: 0.1 * n)
dp_percentile_75 = factory.Sequence(lambda n: 0.1 * n)
dp_percentile_90 = factory.Sequence(lambda n: 0.1 * n)
gc_content = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
unique_seq_percentiles = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
dp_percentile_10 = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
dp_percentile_25 = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
dp_percentile_50 = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
dp_percentile_75 = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)
dp_percentile_90 = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)


class SamtoolsStatsMainMetricsFactory(factory.django.DjangoModelFactory):
Expand Down Expand Up @@ -428,22 +428,22 @@ class SamtoolsFlagstatRecordFactory(factory.Factory):
class Meta:
model = SamtoolsFlagstatRecord

total = factory.Sequence(lambda n: n)
primary = factory.Sequence(lambda n: n)
secondary = factory.Sequence(lambda n: n)
supplementary = factory.Sequence(lambda n: n)
duplicates = factory.Sequence(lambda n: n)
duplicates_primary = factory.Sequence(lambda n: n)
mapped = factory.Sequence(lambda n: n)
mapped_primary = factory.Sequence(lambda n: n)
paired = factory.Sequence(lambda n: n)
fragment_first = factory.Sequence(lambda n: n)
fragment_last = factory.Sequence(lambda n: n)
properly_paired = factory.Sequence(lambda n: n)
with_itself_and_mate_mapped = factory.Sequence(lambda n: n)
singletons = factory.Sequence(lambda n: n)
with_mate_mapped_to_different_chr = factory.Sequence(lambda n: n)
with_mate_mapped_to_different_chr_mapq5 = factory.Sequence(lambda n: n)
total = factory.Faker("pyint")
primary = factory.Faker("pyint")
secondary = factory.Faker("pyint")
supplementary = factory.Faker("pyint")
duplicates = factory.Faker("pyint")
duplicates_primary = factory.Faker("pyint")
mapped = factory.Faker("pyint")
mapped_primary = factory.Faker("pyint")
paired = factory.Faker("pyint")
fragment_first = factory.Faker("pyint")
fragment_last = factory.Faker("pyint")
properly_paired = factory.Faker("pyint")
with_itself_and_mate_mapped = factory.Faker("pyint")
singletons = factory.Faker("pyint")
with_mate_mapped_to_different_chr = factory.Faker("pyint")
with_mate_mapped_to_different_chr_mapq5 = factory.Faker("pyint")


class SamtoolsFlagstatMetricsFactory(factory.django.DjangoModelFactory):
Expand All @@ -469,9 +469,9 @@ class Meta:
model = SamtoolsIdxstatsRecord

contig_name = factory.Faker("word")
contig_len = factory.Sequence(lambda n: n)
mapped = factory.Sequence(lambda n: n)
unmapped = factory.Sequence(lambda n: n)
contig_len = factory.Faker("pyint")
mapped = factory.Faker("pyint")
unmapped = factory.Faker("pyint")


class SamtoolsIdxstatsMetricsFactory(factory.django.DjangoModelFactory):
Expand All @@ -496,15 +496,15 @@ class Meta:
model = CraminoSummaryRecord

key = factory.Faker("word")
value = factory.Sequence(lambda n: n)
value = factory.Faker("pyint")


class CraminoChromNormalizedCountsRecordFactory(factory.Factory):
class Meta:
model = CraminoChromNormalizedCountsRecord

chrom_name = factory.Faker("word")
normalized_counts = factory.Sequence(lambda n: 0.1 * n)
normalized_counts = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)


class CraminoMetricsFactory(factory.django.DjangoModelFactory):
Expand All @@ -529,7 +529,7 @@ class Meta:
model = NgsbitsMappingqcRecord

key = factory.Faker("word")
value = factory.Sequence(lambda n: 0.1 * n)
value = factory.Faker("pyfloat", min_value=0.0, max_value=1.0)


class NgsbitsMappingqcMetricsFactory(factory.django.DjangoModelFactory):
Expand Down
41 changes: 41 additions & 0 deletions cases_qc/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import datetime
import json
import random
import typing
from unittest import mock
import uuid

import factory


def extract_from_dict(vals: typing.Any, keys: typing.Iterable[str]) -> dict[str, typing.Any]:
"""Helper to extract certain values from the dictionary."""
Expand Down Expand Up @@ -38,3 +41,41 @@ def default(self, obj):
def flatten_via_json(data: dict) -> dict:
"""Flatten a dictionary by converting it to JSON and back."""
return json.loads(json.dumps(data, cls=FlattenEncoder))


def fake_urandom(size: int, /) -> bytes:
"""Fake version of urandom that uses random."""
return bytes(random.randint(0, 255) for _ in range(size))


class FixRandomSeedMixin:
def setUp(self):
"""Set fixed seeds."""
super().setUp()
# determine the seed
seed = getattr(self, "__fixrandomseed_fixed_seed", 42)
# seed the random module and keep state for recovery
self._FixRandomSeedMixin__random_state = random.getstate()
random.seed(seed)
# patch all factory.Faker random states
self._FixRandomSeedMixin__faker_generators = []
default_faker = factory.Faker._get_faker()
for the_factory in default_faker.factories:
self._FixRandomSeedMixin__faker_generators.append(the_factory._Generator__random)
the_factory._Generator__random = random.Random(seed)
# patch the os.urandom function
self._FixRandomSeedMixin_patcher = mock.patch("os.urandom", new=fake_urandom)
random.seed(seed)

def tearDown(self):
"""Reset the random state"""
super().tearDown()
# recover all factory.Faker random states
default_faker = factory.Faker._get_faker()
for the_factory, generator in zip(default_faker.factories, self._FixRandomSeedMixin__faker_generators):
the_factory._Generator__random = generator
# recover the random module's state
random.setstate(self._FixRandomSeedMixin__random_state)
# recover the os.urandom function
self._FixRandomSeedMixin_patcher.stop()
random.setstate(self._FixRandomSeedMixin__random_state)
Loading

0 comments on commit df26f6b

Please sign in to comment.