From 7b8ffda6de6be67736074d707015ed22a55d1185 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 May 2021 22:17:56 +0300 Subject: tests: test_species: Add test cases for TestTheSpecies class --- wqflask/tests/unit/base/test_species.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 wqflask/tests/unit/base/test_species.py (limited to 'wqflask/tests') diff --git a/wqflask/tests/unit/base/test_species.py b/wqflask/tests/unit/base/test_species.py new file mode 100644 index 00000000..950909a1 --- /dev/null +++ b/wqflask/tests/unit/base/test_species.py @@ -0,0 +1,27 @@ +"""Tests wqflask/base/species.py""" + +import unittest +from unittest import mock +from base.species import TheSpecies + + +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") -- cgit v1.2.3 From 31d447d5d70cf18a41c6e139a152fe4cfb864bb4 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 May 2021 22:24:02 +0300 Subject: tests: test_species: Test that IndChromosome instantiates correctly --- wqflask/tests/unit/base/test_species.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'wqflask/tests') diff --git a/wqflask/tests/unit/base/test_species.py b/wqflask/tests/unit/base/test_species.py index 950909a1..42aa9092 100644 --- a/wqflask/tests/unit/base/test_species.py +++ b/wqflask/tests/unit/base/test_species.py @@ -3,6 +3,7 @@ import unittest from unittest import mock from base.species import TheSpecies +from base.species import IndChromosome class TestTheSpecies(unittest.TestCase): @@ -25,3 +26,14 @@ species_name is not provided.""" 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) -- cgit v1.2.3 From b2b81942c6dda717ec8e7beeac61477f0e67a387 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 May 2021 23:27:00 +0300 Subject: templates: test_species: Add tests for instantiating Chromosomes --- wqflask/tests/unit/base/test_species.py | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'wqflask/tests') diff --git a/wqflask/tests/unit/base/test_species.py b/wqflask/tests/unit/base/test_species.py index 42aa9092..487bbd7f 100644 --- a/wqflask/tests/unit/base/test_species.py +++ b/wqflask/tests/unit/base/test_species.py @@ -4,6 +4,27 @@ import unittest from unittest import mock 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 + + +@dataclass +class MockChromosome: + OrderId: int + Name: str + Length: int + + +@dataclass +class MockGroup: + name: str + + +@dataclass +class MockDataset: + group: MockGroup class TestTheSpecies(unittest.TestCase): @@ -37,3 +58,59 @@ class TestIndChromosome(unittest.TestCase): self.assertEqual(test_ind_chromosome.name, "Test") self.assertEqual(test_ind_chromosome.length, 10000000) self.assertEqual(test_ind_chromosome.mb_length, 10) + + +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) + 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" + ) + self.assertEqual( + list(test_chromosomes.chromosomes.keys()), + [1, 2, 3] + ) + self.assertEqual(test_chromosomes.dataset, mock_dataset) + + @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") + 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" + ) + self.assertEqual( + list(test_chromosomes.chromosomes.keys()), + [1, 2, 3] + ) -- cgit v1.2.3 From 3f0d45848280b77a35a569682e93291204769cc8 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 May 2021 23:56:32 +0300 Subject: tests: test_species: Update failing tests --- wqflask/tests/unit/base/test_species.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'wqflask/tests') diff --git a/wqflask/tests/unit/base/test_species.py b/wqflask/tests/unit/base/test_species.py index 487bbd7f..9b5c023c 100644 --- a/wqflask/tests/unit/base/test_species.py +++ b/wqflask/tests/unit/base/test_species.py @@ -81,17 +81,17 @@ class TestChromosomes(unittest.TestCase): ] 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" ) - self.assertEqual( - list(test_chromosomes.chromosomes.keys()), - [1, 2, 3] - ) - self.assertEqual(test_chromosomes.dataset, mock_dataset) @mock.patch("base.species.g") def test_create_chromosomes_with_species(self, mock_db): @@ -104,13 +104,13 @@ class TestChromosomes(unittest.TestCase): 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" ) - self.assertEqual( - list(test_chromosomes.chromosomes.keys()), - [1, 2, 3] - ) -- cgit v1.2.3