1# This code is part of the Biopython distribution and governed by its
2# license.  Please see the LICENSE file that should have been included
3# as part of this package.
4
5"""Tests for psw module."""
6
7import doctest
8import unittest
9import random
10import sys
11
12from Bio.Wise import psw
13
14if "requires_wise" in sys.modules:
15    del sys.modules["requires_wise"]
16import requires_wise  # noqa: E402
17
18
19class TestPSW(unittest.TestCase):
20    def test_Alignment_normal(self):
21        a = psw.Alignment()
22
23        a.append(psw.ColumnUnit(0, 98, "SEQUENCE"))
24        a.append(psw.ColumnUnit(1, 200, "SEQUENCE"))
25        a.append(psw.ColumnUnit(0, 98, "INSERT"))
26        a.append(psw.ColumnUnit(1, 201, "SEQUENCE"))
27        a.append(psw.ColumnUnit(0, 98, "END"))
28        a.append(psw.ColumnUnit(1, 201, "END"))
29
30        self.assertEqual(str(a), "[SEQUENCE(98, 200), INSERT(98, 201), END(98, 201)]")
31
32    def test_Alignment_assertions(self):
33        a = psw.Alignment()
34
35        self.assertRaises(AssertionError, a.append, psw.ColumnUnit(1, 200, "SEQUENCE"))
36        a.append(psw.ColumnUnit(0, 98, "SEQUENCE"))
37        self.assertRaises(AssertionError, a.append, psw.ColumnUnit(0, 200, "SEQUENCE"))
38        a.append(psw.ColumnUnit(1, 200, "SEQUENCE"))
39        self.assertRaises(AssertionError, a.append, psw.ColumnUnit(1, 200, "SEQUENCE"))
40
41    def test_AlignmentColumn_kinds(self):
42        ac = psw.AlignmentColumn(psw.ColumnUnit(0, random.randint(0, 9999), "SEQUENCE"))
43        ac.append(psw.ColumnUnit(1, random.randint(0, 9999), "INSERT"))
44        self.assertEqual(ac.kind, "INSERT")
45
46        ac = psw.AlignmentColumn(psw.ColumnUnit(0, random.randint(0, 9999), "INSERT"))
47        ac.append(psw.ColumnUnit(1, random.randint(0, 9999), "SEQUENCE"))
48        self.assertEqual(ac.kind, "INSERT")
49
50        ac = psw.AlignmentColumn(psw.ColumnUnit(0, random.randint(0, 9999), "SEQUENCE"))
51        ac.append(psw.ColumnUnit(1, random.randint(0, 9999), "SEQUENCE"))
52        self.assertEqual(ac.kind, "SEQUENCE")
53
54        ac = psw.AlignmentColumn(psw.ColumnUnit(0, random.randint(0, 9999), "SEQUENCE"))
55        ac.append(psw.ColumnUnit(1, random.randint(0, 9999), "END"))
56        self.assertEqual(ac.kind, "END")
57
58    def test_AlignmentColumn_repr(self):
59        ac = psw.AlignmentColumn(psw.ColumnUnit(0, 34, "SEQUENCE"))
60        ac.append(psw.ColumnUnit(1, 55, "END"))
61        self.assertEqual(repr(ac), "END(34, 55)")
62
63    def test_AlignmentColumn_full(self):
64        ac = psw.AlignmentColumn(psw.ColumnUnit(0, random.randint(0, 9999), "SEQUENCE"))
65        ac.append(psw.ColumnUnit(1, random.randint(0, 9999), "END"))
66        self.assertRaises(
67            psw.AlignmentColumnFullException,
68            ac.append,
69            psw.ColumnUnit(1, random.randint(0, 9999), "END"),
70        )
71
72    def test_AlignmentColumn_assertions(self):
73        self.assertRaises(
74            AssertionError,
75            psw.AlignmentColumn,
76            psw.ColumnUnit(1, random.randint(0, 9999), "SEQUENCE"),
77        )
78
79        ac = psw.AlignmentColumn(psw.ColumnUnit(0, random.randint(0, 9999), "SEQUENCE"))
80        self.assertRaises(
81            AssertionError,
82            ac.append,
83            psw.ColumnUnit(0, random.randint(0, 9999), "SEQUENCE"),
84        )
85
86    def test_ColumnUnit(self):
87        self.assertEqual(
88            repr(psw.ColumnUnit(0, 33, "SEQUENCE")),
89            "ColumnUnit(unit=0, column=33, kind='SEQUENCE')",
90        )
91
92        self.assertEqual(
93            repr(psw.ColumnUnit(1, 33, "INSERT")),
94            "ColumnUnit(unit=1, column=33, kind='INSERT')",
95        )
96
97        self.assertEqual(
98            repr(psw.ColumnUnit(1, 33, "END")),
99            "ColumnUnit(unit=1, column=33, kind='END')",
100        )
101
102    PARSED = (
103        "[SEQUENCE(39, 22), SEQUENCE(40, 23), SEQUENCE(41, 24), "
104        "SEQUENCE(42, 25), SEQUENCE(43, 26), SEQUENCE(44, 27), END(0, 27)]"
105    )
106
107
108def run_tests(argv):
109    test_suite = testing_suite()
110    runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
111    runner.run(test_suite)
112
113
114def testing_suite():
115    """Generate the suite of tests."""
116    unittest_suite = unittest.TestSuite()
117
118    test_loader = unittest.TestLoader()
119    test_loader.testMethodPrefix = "test_"
120    tests = [TestPSW]
121
122    for test in tests:
123        cur_suite = test_loader.loadTestsFromTestCase(test)
124        unittest_suite.addTest(cur_suite)
125
126    doctest_suite = doctest.DocTestSuite(psw)
127
128    big_suite = unittest.TestSuite((unittest_suite, doctest_suite))
129
130    return big_suite
131
132
133if __name__ == "__main__":
134    unittest_suite = unittest.TestLoader().loadTestsFromName("test_psw")
135    doctest_suite = doctest.DocTestSuite(psw)
136    suite = unittest.TestSuite((unittest_suite, doctest_suite))
137    runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
138    runner.run(suite)
139