1import unittest
2
3from pgxnclient import Label, Term, Identifier
4
5
6class LabelTestCase(unittest.TestCase):
7    def test_ok(self):
8        for s in [
9            'd',
10            'a1234',
11            'abcd1234-5432XYZ',
12            'a12345678901234567890123456789012345678901234567890123456789012',
13        ]:
14            self.assertEqual(Label(s), s)
15            self.assertEqual(Label(s), Label(s))
16            self.assert_(Label(s) <= Label(s))
17            self.assert_(Label(s) >= Label(s))
18
19    def test_bad(self):
20        def ar(s):
21            try:
22                Label(s)
23            except ValueError:
24                pass
25            else:
26                self.fail("ValueError not raised: '%s'" % s)
27
28        for s in [
29            '',
30            ' a',
31            'a ',
32            '1a',
33            '-a',
34            'a-',
35            'a_b',
36            'a123456789012345678901234567890123456789012345678901234567890123',
37        ]:
38            ar(s)
39
40    def test_compare(self):
41        self.assertEqual(Label('a'), Label('A'))
42        self.assertNotEqual(str(Label('a')), str(Label('A')))  # preserving
43
44    def test_order(self):
45        self.assert_(Label('a') < Label('B') < Label('c'))
46        self.assert_(Label('A') < Label('b') < Label('C'))
47        self.assert_(Label('a') <= Label('B') <= Label('c'))
48        self.assert_(Label('A') <= Label('b') <= Label('C'))
49        self.assert_(Label('c') > Label('B') > Label('a'))
50        self.assert_(Label('C') > Label('b') > Label('A'))
51        self.assert_(Label('c') >= Label('B') >= Label('a'))
52        self.assert_(Label('C') >= Label('b') >= Label('A'))
53
54
55class TermTestCase(unittest.TestCase):
56    def test_ok(self):
57        for s in ['aa' 'adfkjh"()']:
58            self.assertEqual(Term(s), s)
59            self.assertEqual(Term(s), Term(s))
60            self.assert_(Term(s) <= Term(s))
61            self.assert_(Term(s) >= Term(s))
62
63    def test_bad(self):
64        def ar(s):
65            try:
66                Term(s)
67            except ValueError:
68                pass
69            else:
70                self.fail("ValueError not raised: '%s'" % s)
71
72        for s in ['a', 'aa ', 'a/a', 'a\\a', 'a\ta', 'aa\x01']:
73            ar(s)
74
75
76class TestIdentifier(unittest.TestCase):
77    def test_nonblank(self):
78        self.assertRaises(ValueError, Identifier, "")
79
80    def test_unquoted(self):
81        for s in ['x', 'xxxxx', 'abcxyz_0189', 'ABCXYZ_0189']:
82            self.assertEqual(Identifier(s), s)
83
84    def test_quoted(self):
85        for s, q in [('x-y', '"x-y"'), (' ', '" "'), ('x"y', '"x""y"')]:
86            self.assertEqual(Identifier(s), q)
87
88
89if __name__ == '__main__':
90    unittest.main()
91