Skip to content

Commit

Permalink
small helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mayabrandi committed Sep 30, 2020
1 parent b149ca5 commit 7d86ffb
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 194 deletions.
Empty file added tests/__init__.py
Empty file.
114 changes: 6 additions & 108 deletions tests/conftest.py
@@ -1,6 +1,8 @@
import pytest

from mongomock import MongoClient
from .small_helpers import SmallHelpers


DATABASE = "testdb"

Expand All @@ -27,114 +29,10 @@ def database(request, pymongo_client):
return database


def batch(
batch_id = "201860",
sequencing_date = "2022-03-10",
stdev_13= 0.0095,
stdev_18 = 0.0035,
stdev_21 = 0.0085,
):
"""A batch mock"""

return {
"_id": batch_id,
"SequencingDate": sequencing_date,
"Median_13": 0.994623404680424,
"Median_18": 1.01950547134618,
"Median_21": 1.00710552415657,
"Median_X": 0.8408228472301729,
"Median_Y": 0.00011224982090899999,
"Stdev_13": stdev_13,
"Stdev_18": stdev_18,
"Stdev_21": stdev_21,
"Stdev_X": 0.029800076293786,
"Stdev_Y": 0.0000653186791196846,
}


def sample(
batch_id: str = "201860",
sample_id: str = "2020-07452-02",
ratio_13: float = 0.97,
ratio_18: float = 0.87,
fetal_fraction: float = 11.5,
):
"""A sample mock"""

return {
"_id": sample_id,
"SampleID": sample_id,
"SampleType": "",
"Description": "",
"SampleProject": batch_id,
"Index1": "CTACGAAG",
"Index2": "CTCGACAG",
"Library_nM": "",
"QCFlag": "",
"Zscore_13": 10.1836097044367,
"Zscore_18": 14.2869756828577,
"Zscore_21": 2.71403500910501,
"Zscore_X": 31.293635764028803,
"Ratio_13": ratio_13,
"Ratio_18": ratio_18,
"Ratio_21": 1.0105402184799999,
"Ratio_X": 0.85445334198,
"Ratio_Y": 0.0000711090401026516,
"MappedReads": 29855557,
"GC_Dropout": 0.000172,
"AT_Dropout": 44.723144,
"Chr1_Ratio": 0.992293808478,
"Chr2_Ratio": 1.0065908236399999,
"Chr3_Ratio": 0.974151095888,
"Chr4_Ratio": 1.10624166283,
"Chr5_Ratio": 1.0035390122299999,
"Chr6_Ratio": 1.03661992778,
"Chr7_Ratio": 1.0604126148100002,
"Chr8_Ratio": 0.987816483295,
"Chr9_Ratio": 1.00004978895,
"Chr10_Ratio": 0.984502658354,
"Chr11_Ratio": 0.949426097179,
"Chr12_Ratio": 1.0228564842799999,
"Chr14_Ratio": 0.988492203388,
"Chr15_Ratio": 0.9931196645579999,
"Chr16_Ratio": 0.9728049597669999,
"Chr17_Ratio": 1.04339140264,
"Chr19_Ratio": 1.05685913186,
"Chr20_Ratio": 0.917316392889,
"Chr22_Ratio": 1.05769882843,
"Chr1": 1641640,
"Chr2": 1456893,
"Chr3": 1089600,
"Chr4": 873556,
"Chr5": 968727,
"Chr6": 949012,
"Chr7": 1005851,
"Chr8": 851501,
"Chr9": 797378,
"Chr10": 949427,
"Chr11": 853961,
"Chr12": 853961,
"Chr13": 478503,
"Chr14": 593269,
"Chr15": 614267,
"Chr16": 749565,
"Chr17": 837952,
"Chr18": 439127,
"Chr19": 714656,
"Chr20": 555672,
"Chr21": 250072,
"Chr22": 451194,
"ChrX": 758991,
"ChrY": 2123,
"FF_Formatted": fetal_fraction,
"FFY": 0,
"FFX": 13.14,
"DuplicationRate": 0.0853080382992,
"Bin2BinVariance": 0.054260164076747,
"UnfilteredCNVcalls": 44,
"CNVSegment": "Found",
"comment": "None",
}
@pytest.fixture(name="small_helpers")
def fixture_small_helpers():
"""Return a class with small helper functions"""
return SmallHelpers()


##########################################
Expand Down
110 changes: 24 additions & 86 deletions tests/server/test_utils.py
Expand Up @@ -5,7 +5,6 @@
get_statistics_for_scatter_plot,
)
from NIPTool.adapter.plugin import NiptAdapter
from conftest import batch, sample
from datetime import datetime


Expand Down Expand Up @@ -52,110 +51,49 @@ def test_get_last_batches_to_fiew_batches_in_database(database):
assert results == [batch1, batch2, batch3, batch4]


def test_get_statistics_for_box_plot(database):
def test_get_statistics_for_box_plot(database, small_helpers):
app.db = database
app.adapter = NiptAdapter(database.client, db_name=database.name)

# GIVEN a database with thre batches and nine samples like below
database.batch.insert_many(
[batch(batch_id="201860"), batch(batch_id="201861"), batch(batch_id="201862")]
)
database.sample.insert_many(
[
sample(
sample_id="07452-00",
batch_id="201860",
ratio_13=0.91,
fetal_fraction=10.5,
),
sample(
sample_id="07452-01",
batch_id="201860",
ratio_13=0.92,
fetal_fraction=11.5,
),
sample(
sample_id="07452-02",
batch_id="201860",
ratio_13=0.93,
fetal_fraction=12.5,
),
sample(
sample_id="07452-03",
batch_id="201861",
ratio_13=0.94,
fetal_fraction=13.5,
),
sample(
sample_id="07452-04",
batch_id="201861",
ratio_13=0.95,
fetal_fraction=14.5,
),
sample(
sample_id="07452-05",
batch_id="201861",
ratio_13=0.96,
fetal_fraction=15.5,
),
sample(
sample_id="07452-06",
batch_id="201862",
ratio_13=0.97,
fetal_fraction=16.5,
),
sample(
sample_id="07452-07",
batch_id="201862",
ratio_13=0.98,
fetal_fraction=17.5,
),
sample(
sample_id="07452-08",
batch_id="201862",
ratio_13=0.99,
fetal_fraction=18.5,
),
]
)
batches = ["201860", "201861", "201862"]
for batch_id in batches:
batch = small_helpers.batch(batch_id = batch_id)
database.batch.insert_one(batch)
for i in [1,2,3]:
sample = small_helpers.sample(
sample_id = batch_id+str(i),
batch_id = batch_id,
ratio_13=i,
fetal_fraction=i)
database.sample.insert_one(sample)


# WHEN running get_statistics_for_box_plot
results = get_statistics_for_box_plot(
adapter=app.adapter,
batches=["201860", "201861", "201862"],
batches=batches,
fields=["Ratio_13", "FF_Formatted"],
)

# THEN the results shoule be:
expected_result = [
{
"FF_Formatted": [10.5, 11.5, 12.5],
"Ratio_13": [0.91, 0.92, 0.93],
"_id": {"batch": "201860", "date": "2022-03-10"},
},
{
"FF_Formatted": [13.5, 14.5, 15.5],
"Ratio_13": [0.94, 0.95, 0.96],
"_id": {"batch": "201861", "date": "2022-03-10"},
},
{
"FF_Formatted": [16.5, 17.5, 18.5],
"Ratio_13": [0.97, 0.98, 0.99],
"_id": {"batch": "201862", "date": "2022-03-10"},
},
]
assert results == expected_result
for index, result in enumerate(results):
assert result == {
"FF_Formatted": [1, 2, 3],
"Ratio_13": [1, 2, 3],
"_id": {"batch": batches[index], "date": "2022-03-10"},
}


def test_get_statistics_for_scatter_plot(database):
def test_get_statistics_for_scatter_plot(database, small_helpers):
app.db = database
app.adapter = NiptAdapter(database.client, db_name=database.name)

# GIVEN a list with three batch documents:
batches = [
batch(batch_id="201860", stdev_13=0.02, stdev_21=0.01),
batch(batch_id="201861", stdev_13=0.01, stdev_21=0.09),
batch(batch_id="201862", stdev_13=0.05, stdev_21=0.08),
small_helpers.batch(batch_id="201860", stdev_13=0.02, stdev_21=0.01),
small_helpers.batch(batch_id="201861", stdev_13=0.01, stdev_21=0.09),
small_helpers.batch(batch_id="201862", stdev_13=0.05, stdev_21=0.08),
]

# WHEN running get_statistics_for_scatter_plot
Expand Down
114 changes: 114 additions & 0 deletions tests/small_helpers.py
@@ -0,0 +1,114 @@
"""Class with small helper functions"""


class SmallHelpers:
"""Hold small methods that might be helpful for the tests"""
@staticmethod
def batch(
batch_id="201860",
sequencing_date="2022-03-10",
stdev_13=0.0095,
stdev_18=0.0035,
stdev_21=0.0085,
):
"""A batch mock"""

return {
"_id": batch_id,
"SequencingDate": sequencing_date,
"Median_13": 0.994623404680424,
"Median_18": 1.01950547134618,
"Median_21": 1.00710552415657,
"Median_X": 0.8408228472301729,
"Median_Y": 0.00011224982090899999,
"Stdev_13": stdev_13,
"Stdev_18": stdev_18,
"Stdev_21": stdev_21,
"Stdev_X": 0.029800076293786,
"Stdev_Y": 0.0000653186791196846,
}

@staticmethod
def sample(
batch_id: str = "201860",
sample_id: str = "2020-07452-02",
ratio_13: float = 0.97,
ratio_18: float = 0.87,
fetal_fraction: float = 11.5,
):
"""A sample mock"""

return {
"_id": sample_id,
"SampleID": sample_id,
"SampleType": "",
"Description": "",
"SampleProject": batch_id,
"Index1": "CTACGAAG",
"Index2": "CTCGACAG",
"Library_nM": "",
"QCFlag": "",
"Zscore_13": 10.1836097044367,
"Zscore_18": 14.2869756828577,
"Zscore_21": 2.71403500910501,
"Zscore_X": 31.293635764028803,
"Ratio_13": ratio_13,
"Ratio_18": ratio_18,
"Ratio_21": 1.0105402184799999,
"Ratio_X": 0.85445334198,
"Ratio_Y": 0.0000711090401026516,
"MappedReads": 29855557,
"GC_Dropout": 0.000172,
"AT_Dropout": 44.723144,
"Chr1_Ratio": 0.992293808478,
"Chr2_Ratio": 1.0065908236399999,
"Chr3_Ratio": 0.974151095888,
"Chr4_Ratio": 1.10624166283,
"Chr5_Ratio": 1.0035390122299999,
"Chr6_Ratio": 1.03661992778,
"Chr7_Ratio": 1.0604126148100002,
"Chr8_Ratio": 0.987816483295,
"Chr9_Ratio": 1.00004978895,
"Chr10_Ratio": 0.984502658354,
"Chr11_Ratio": 0.949426097179,
"Chr12_Ratio": 1.0228564842799999,
"Chr14_Ratio": 0.988492203388,
"Chr15_Ratio": 0.9931196645579999,
"Chr16_Ratio": 0.9728049597669999,
"Chr17_Ratio": 1.04339140264,
"Chr19_Ratio": 1.05685913186,
"Chr20_Ratio": 0.917316392889,
"Chr22_Ratio": 1.05769882843,
"Chr1": 1641640,
"Chr2": 1456893,
"Chr3": 1089600,
"Chr4": 873556,
"Chr5": 968727,
"Chr6": 949012,
"Chr7": 1005851,
"Chr8": 851501,
"Chr9": 797378,
"Chr10": 949427,
"Chr11": 853961,
"Chr12": 853961,
"Chr13": 478503,
"Chr14": 593269,
"Chr15": 614267,
"Chr16": 749565,
"Chr17": 837952,
"Chr18": 439127,
"Chr19": 714656,
"Chr20": 555672,
"Chr21": 250072,
"Chr22": 451194,
"ChrX": 758991,
"ChrY": 2123,
"FF_Formatted": fetal_fraction,
"FFY": 0,
"FFX": 13.14,
"DuplicationRate": 0.0853080382992,
"Bin2BinVariance": 0.054260164076747,
"UnfilteredCNVcalls": 44,
"CNVSegment": "Found",
"comment": "None",
}

0 comments on commit 7d86ffb

Please sign in to comment.