aboutsummaryrefslogtreecommitdiff
path: root/wqflask
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask')
-rw-r--r--wqflask/tests/unit/base/test_species.py151
1 files changed, 59 insertions, 92 deletions
diff --git a/wqflask/tests/unit/base/test_species.py b/wqflask/tests/unit/base/test_species.py
index 87f89607..d7ba30a3 100644
--- a/wqflask/tests/unit/base/test_species.py
+++ b/wqflask/tests/unit/base/test_species.py
@@ -1,12 +1,9 @@
"""Tests wqflask/base/species.py"""
-
-import unittest
-from unittest import mock
+import pytest
from base.species import TheSpecies
from base.species import IndChromosome
from base.species import Chromosomes
from collections import OrderedDict
-from wqflask import app
from dataclasses import dataclass
@@ -27,91 +24,61 @@ class MockDataset:
group: MockGroup
-class TestTheSpecies(unittest.TestCase):
- """Tests for TheSpecies class"""
- @mock.patch('base.species.Chromosomes')
- def test_create_species_with_null_species_name(self, mock_chromosome):
- """Test that TheSpecies is instantiated correctly when the
-species_name is provided."""
- mock_chromosome.return_value = 1
- test_species = TheSpecies(dataset="random_dataset", species_name="a")
- self.assertEqual(test_species.name, "a")
- self.assertEqual(test_species.chromosomes, 1)
-
- @mock.patch('base.species.Chromosomes')
- def test_create_species_with_species_name(self, mock_chromosome):
- """Test that TheSpecies is instantiated correctly when the
-species_name is not provided."""
- mock_chromosome.return_value = 1
- test_species = TheSpecies(dataset="random_dataset")
- self.assertEqual(test_species.dataset, "random_dataset")
- self.assertEqual(test_species.chromosomes, 1)
- mock_chromosome.assert_called_once_with(dataset="random_dataset")
-
-
-class TestIndChromosome(unittest.TestCase):
- """Tests for IndChromosome class"""
-
- def test_create_ind_chromosome(self):
- """Test that IndChromosome is instantiated correctly"""
- test_ind_chromosome = IndChromosome(name="Test", length=10000000)
- self.assertEqual(test_ind_chromosome.name, "Test")
- self.assertEqual(test_ind_chromosome.length, 10000000)
- self.assertEqual(test_ind_chromosome.mb_length, 10)
-
-
-@unittest.skip("Too complicated")
-class TestChromosomes(unittest.TestCase):
- """Tests for Chromosomes class"""
- maxDiff = None
-
- def setUp(self):
- self.app_context = app.app_context()
- self.app_context.push()
-
- def tearDown(self):
- self.app_context.pop()
-
- @mock.patch("base.species.g")
- def test_create_chromosomes_with_no_species(self, mock_db):
- """Test instantiating a chromosome without a species"""
- mock_db.db.execute.return_value.fetchall.return_value = [
- MockChromosome(1, "X", 100),
- MockChromosome(2, "Y", 1000),
- MockChromosome(3, "Z", 10000),
- ]
- mock_dataset = MockDataset(MockGroup("Random"))
- test_chromosomes = Chromosomes(dataset=mock_dataset)
- self.assertEqual(
- list(test_chromosomes.chromosomes.keys()),
- [1, 2, 3]
- )
- self.assertEqual(test_chromosomes.dataset, mock_dataset)
- mock_db.db.execute.assert_called_with(
- "SELECT Chr_Length.Name, Chr_Length.OrderId, Length "
- "FROM Chr_Length, InbredSet WHERE "
- "Chr_Length.SpeciesId = InbredSet.SpeciesId AND "
- "InbredSet.Name = 'Random' ORDER BY OrderId"
- )
-
- @mock.patch("base.species.g")
- def test_create_chromosomes_with_species(self, mock_db):
- """Test instantiating a chromosome with a species"""
- mock_db.db.execute.return_value.fetchall.return_value = [
- MockChromosome(1, "X", 100),
- MockChromosome(2, "Y", 1000),
- MockChromosome(3, "Z", 10000),
- ]
- mock_dataset = MockDataset(MockGroup("Random"))
- test_chromosomes = Chromosomes(dataset=mock_dataset,
- species="testSpecies")
- self.assertEqual(
- list(test_chromosomes.chromosomes.keys()),
- [1, 2, 3]
- )
- mock_db.db.execute.assert_called_with(
- "SELECT Chr_Length.Name, Chr_Length.OrderId, Length "
- "FROM Chr_Length, Species WHERE "
- "Chr_Length.SpeciesId = Species.SpeciesId AND "
- "Species.Name = 'Testspecies' ORDER BY OrderId"
- )
+@pytest.mark.parametrize(
+ ("species_name", "dataset", "expected_name", "chromosome_param"),
+ (("BXD", None, "BXD", 1),
+ (None, "Random Dataset", None, 1)))
+def test_species(mocker, species_name, dataset,
+ expected_name, chromosome_param):
+ mock_conn = mocker.patch("base.species.database_connection")
+ mock_conn.return_value.__enter__.return_value = mocker.MagicMock()
+ _c = mocker.patch("base.species.Chromosomes",
+ return_value=chromosome_param)
+ with mock_conn() as conn:
+ test_species = TheSpecies(dataset=dataset,
+ species_name=species_name)
+ _c.assert_called_with(conn=conn, species=species_name,
+ dataset=dataset)
+ assert test_species.name == expected_name
+ assert test_species.chromosomes == chromosome_param
+
+
+@pytest.mark.parametrize(
+ ("name", "length", "mb_length"),
+ (("Test A", 10000000, 10),
+ ("Test B", 100, 0.0001)))
+def test_create_ind_chromosome(name, length, mb_length):
+ _ind = IndChromosome(name=name, length=length)
+ assert _ind.name == name
+ assert _ind.length == length
+ assert _ind.mb_length == mb_length
+
+
+@pytest.mark.parametrize(
+ ("species", "dataset", "expected_call"),
+ (("bxd", MockDataset(MockGroup("Random")),
+ ("SELECT Chr_Length.Name, Chr_Length.OrderId, Length "
+ "FROM Chr_Length, Species WHERE "
+ "Chr_Length.SpeciesId = Species.SpeciesId AND "
+ "Species.Name = %s "
+ "ORDER BY OrderId", ("Bxd",))),
+ (None, MockDataset(MockGroup("Random")),
+ ("SELECT Chr_Length.Name, Chr_Length.OrderId, "
+ "Length FROM Chr_Length, InbredSet WHERE "
+ "Chr_Length.SpeciesId = InbredSet.SpeciesId AND "
+ "InbredSet.Name = "
+ "%s ORDER BY OrderId", ("Random",)))))
+def test_create_chromosomes(mocker, species, dataset, expected_call):
+ mock_conn = mocker.MagicMock()
+ with mock_conn.cursor() as cursor:
+ cursor.fetchall.return_value = (("1", 2, 10,),
+ ("2", 3, 11,),
+ ("4", 5, 15,),)
+ _c = Chromosomes(conn=mock_conn,
+ dataset=dataset, species=species)
+ assert _c.chromosomes == OrderedDict([
+ ("1", IndChromosome("1", 10)),
+ ("2", IndChromosome("2", 11)),
+ ("4", IndChromosome("4", 15)),
+ ])
+ cursor.execute.assert_called_with(*expected_call)