aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/db/test_db.py
blob: 5e146fd4780a3957c251e2781fa0d026bcfa96b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Tests for db/phenotypes.py"""
from unittest import TestCase
from unittest import mock

import pytest

from gn3.db import update
from gn3.db import diff_from_dict
from gn3.db.phenotypes import Phenotype


class TestCrudMethods(TestCase):
    """Test cases for CRUD methods"""

    @pytest.mark.unit_test
    def test_update_phenotype_with_no_data(self):
        """Test that a phenotype is updated correctly if an empty Phenotype dataclass
        is provided

        """
        db_mock = mock.MagicMock()
        self.assertEqual(update(
            conn=db_mock, table="Phenotype",
            data=Phenotype(), where=Phenotype()), None)

    @pytest.mark.unit_test
    def test_update_phenotype_with_data(self):
        """
        Test that a phenotype is updated correctly if some
        data is provided
        """
        db_mock = mock.MagicMock()
        with db_mock.cursor() as cursor:
            type(cursor).rowcount = 1
            self.assertEqual(update(
                conn=db_mock, table="Phenotype",
                data=Phenotype(
                    pre_pub_description="Test Pre Pub",
                    submitter="Rob",
                    post_pub_description="Test Post Pub"),
                where=Phenotype(id_=1, owner="Rob")), 1)
            cursor.execute.assert_called_once_with(
                "UPDATE Phenotype SET "
                "Pre_publication_description = %s, "
                "Post_publication_description = %s, "
                "Submitter = %s WHERE id = %s AND Owner = %s",
                ('Test Pre Pub', 'Test Post Pub', 'Rob', 1, 'Rob'))

    @pytest.mark.unit_test
    def test_diff_from_dict(self):
        """Test that a correct diff is generated"""
        self.assertEqual(diff_from_dict({"id": 1, "data": "a"},
                                        {"id": 2, "data": "b"}),
                         {"id": {"old": 1, "new": 2},
                          "data": {"old": "a", "new": "b"}})