Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mayabrandi committed Sep 30, 2020
1 parent 77c2291 commit b149ca5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 28 deletions.
2 changes: 0 additions & 2 deletions NIPTool/server/templates/statistics.html
Expand Up @@ -40,7 +40,6 @@ <h1 class="page-header"><strong>Quality Control/QC </strong></h1>

////////////////// Box Plots //////////////////
{% for plot_name in box_plots %}
var XY_plot = document.getElementById('')
var data = []
var layout = {
title: '{{plot_name}} - ({{nr_batches}} most recent batches)',
Expand Down Expand Up @@ -88,7 +87,6 @@ <h1 class="page-header"><strong>Quality Control/QC </strong></h1>
<script>
////////////////// Scatter plots //////////////////
{% for plot_name in scatter_plots %}
var XY_plot = document.getElementById('')
var data = []

var layout = {
Expand Down
1 change: 1 addition & 0 deletions NIPTool/server/utils.py
Expand Up @@ -348,6 +348,7 @@ def get_statistics_for_box_plot(adapter, batches: list, fields: list):
pipe = [match, lookup, unwind, group]
#maybe add a fina sort to the pipe
box_plot_data = list(adapter.sample_aggregate(pipe))

return box_plot_data


Expand Down
17 changes: 6 additions & 11 deletions tests/conftest.py
Expand Up @@ -27,7 +27,6 @@ def database(request, pymongo_client):
return database


@pytest.fixture(scope="function")
def batch(
batch_id = "201860",
sequencing_date = "2022-03-10",
Expand All @@ -37,7 +36,7 @@ def batch(
):
"""A batch mock"""

batch= {
return {
"_id": batch_id,
"SequencingDate": sequencing_date,
"Median_13": 0.994623404680424,
Expand All @@ -51,33 +50,31 @@ def batch(
"Stdev_X": 0.029800076293786,
"Stdev_Y": 0.0000653186791196846,
}
return batch


@pytest.fixture(scope="function")
def sample(
batch_id: str = "201860",
_id: str = "2020-07452-02",
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": _id,
"SampleID": _id,
"_id": sample_id,
"SampleID": sample_id,
"SampleType": "",
"Description": "",
"SampleProject": batch_id,
"Index1": "CTACGAAG",
"Index2": "CTCGACAG",
"Library_nM": "",
"QCFlag": "",
"Zscore_13": -10.1836097044367,
"Zscore_13": 10.1836097044367,
"Zscore_18": 14.2869756828577,
"Zscore_21": 2.71403500910501,
"Zscore_X": -31.293635764028803,
"Zscore_X": 31.293635764028803,
"Ratio_13": ratio_13,
"Ratio_18": ratio_18,
"Ratio_21": 1.0105402184799999,
Expand Down Expand Up @@ -136,9 +133,7 @@ def sample(
"Bin2BinVariance": 0.054260164076747,
"UnfilteredCNVcalls": 44,
"CNVSegment": "Found",
"added": {"$date": "2020-04-29T15:33:20.516Z"},
"comment": "None",
"updated": {"$date": "2020-04-30T10:39:16.464Z"},
}


Expand Down
122 changes: 107 additions & 15 deletions tests/server/test_utils.py
@@ -1,6 +1,11 @@
from NIPTool.server import create_app
from NIPTool.server.utils import get_last_batches
from NIPTool.server.utils import (
get_last_batches,
get_statistics_for_box_plot,
get_statistics_for_scatter_plot,
)
from NIPTool.adapter.plugin import NiptAdapter
from conftest import batch, sample
from datetime import datetime


Expand Down Expand Up @@ -47,25 +52,70 @@ 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, sample, batch):
def test_get_statistics_for_box_plot(database):
app.db = database
app.adapter = NiptAdapter(database.client, db_name=database.name)
# GIVEN a database with

# 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(batch_id="201860", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201860", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201860", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201861", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201861", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201861", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201862", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201862", ratio_13=0.97, fetal_fraction=11.5),
sample(batch_id="201862", ratio_13=0.97, fetal_fraction=11.5),
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,
),
]
)

Expand All @@ -76,5 +126,47 @@ def test_get_statistics_for_box_plot(database, sample, batch):
fields=["Ratio_13", "FF_Formatted"],
)

# THEN the results
assert results == []
# 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


def test_get_statistics_for_scatter_plot(database):
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),
]

# WHEN running get_statistics_for_scatter_plot
results = get_statistics_for_scatter_plot(
batches=batches, fields=["Stdev_13", "Stdev_21"]
)

# THEN the results should look like this:
expected_result = {
"201860": {"date": "2022-03-10", "Stdev_13": 0.02, "Stdev_21": 0.01},
"201861": {"date": "2022-03-10", "Stdev_13": 0.01, "Stdev_21": 0.09},
"201862": {"date": "2022-03-10", "Stdev_13": 0.05, "Stdev_21": 0.08},
}
assert results == expected_result

0 comments on commit b149ca5

Please sign in to comment.