|
| 1 | +#-*- coding: ISO-8859-1 -*- |
| 2 | +# pysqlite2/test/hooks.py: tests for various SQLite-specific hooks |
| 3 | +# |
| 4 | +# Copyright (C) 2006 Gerhard Häring <gh@ghaering.de> |
| 5 | +# |
| 6 | +# This file is part of pysqlite. |
| 7 | +# |
| 8 | +# This software is provided 'as-is', without any express or implied |
| 9 | +# warranty. In no event will the authors be held liable for any damages |
| 10 | +# arising from the use of this software. |
| 11 | +# |
| 12 | +# Permission is granted to anyone to use this software for any purpose, |
| 13 | +# including commercial applications, and to alter it and redistribute it |
| 14 | +# freely, subject to the following restrictions: |
| 15 | +# |
| 16 | +# 1. The origin of this software must not be misrepresented; you must not |
| 17 | +# claim that you wrote the original software. If you use this software |
| 18 | +# in a product, an acknowledgment in the product documentation would be |
| 19 | +# appreciated but is not required. |
| 20 | +# 2. Altered source versions must be plainly marked as such, and must not be |
| 21 | +# misrepresented as being the original software. |
| 22 | +# 3. This notice may not be removed or altered from any source distribution. |
| 23 | + |
| 24 | +import os, unittest |
| 25 | +import pysqlite2.dbapi2 as sqlite |
| 26 | + |
| 27 | +class CollationTests(unittest.TestCase): |
| 28 | + def setUp(self): |
| 29 | + pass |
| 30 | + |
| 31 | + def tearDown(self): |
| 32 | + pass |
| 33 | + |
| 34 | + def CheckCreateCollationNotCallable(self): |
| 35 | + con = sqlite.connect(":memory:") |
| 36 | + try: |
| 37 | + con.create_collation("X", 42) |
| 38 | + self.fail("should have raised a TypeError") |
| 39 | + except TypeError, e: |
| 40 | + self.failUnlessEqual(e.args[0], "parameter must be callable") |
| 41 | + |
| 42 | + def CheckCreateCollationNotAscii(self): |
| 43 | + con = sqlite.connect(":memory:") |
| 44 | + try: |
| 45 | + con.create_collation("collä", cmp) |
| 46 | + self.fail("should have raised a ProgrammingError") |
| 47 | + except sqlite.ProgrammingError, e: |
| 48 | + pass |
| 49 | + |
| 50 | + def CheckCollationIsUsed(self): |
| 51 | + def mycoll(x, y): |
| 52 | + # reverse order |
| 53 | + return -cmp(x, y) |
| 54 | + |
| 55 | + con = sqlite.connect(":memory:") |
| 56 | + con.create_collation("mycoll", mycoll) |
| 57 | + sql = """ |
| 58 | + select x from ( |
| 59 | + select 'a' as x |
| 60 | + union |
| 61 | + select 'b' as x |
| 62 | + union |
| 63 | + select 'c' as x |
| 64 | + ) order by x collate mycoll |
| 65 | + """ |
| 66 | + result = con.execute(sql).fetchall() |
| 67 | + if result[0][0] != "c" or result[1][0] != "b" or result[2][0] != "a": |
| 68 | + self.fail("the expected order was not returned") |
| 69 | + |
| 70 | + con.create_collation("mycoll", None) |
| 71 | + try: |
| 72 | + result = con.execute(sql).fetchall() |
| 73 | + self.fail("should have raised an OperationalError") |
| 74 | + except sqlite.OperationalError, e: |
| 75 | + self.failUnlessEqual(e.args[0], "no such collation sequence: mycoll") |
| 76 | + |
| 77 | + def CheckCollationRegisterTwice(self): |
| 78 | + """ |
| 79 | + Register two different collation functions under the same name. |
| 80 | + Verify that the last one is actually used. |
| 81 | + """ |
| 82 | + con = sqlite.connect(":memory:") |
| 83 | + con.create_collation("mycoll", cmp) |
| 84 | + con.create_collation("mycoll", lambda x, y: -cmp(x, y)) |
| 85 | + result = con.execute(""" |
| 86 | + select x from (select 'a' as x union select 'b' as x) order by x collate mycoll |
| 87 | + """).fetchall() |
| 88 | + if result[0][0] != 'b' or result[1][0] != 'a': |
| 89 | + self.fail("wrong collation function is used") |
| 90 | + |
| 91 | + def CheckDeregisterCollation(self): |
| 92 | + """ |
| 93 | + Register a collation, then deregister it. Make sure an error is raised if we try |
| 94 | + to use it. |
| 95 | + """ |
| 96 | + con = sqlite.connect(":memory:") |
| 97 | + con.create_collation("mycoll", cmp) |
| 98 | + con.create_collation("mycoll", None) |
| 99 | + try: |
| 100 | + con.execute("select 'a' as x union select 'b' as x order by x collate mycoll") |
| 101 | + self.fail("should have raised an OperationalError") |
| 102 | + except sqlite.OperationalError, e: |
| 103 | + if not e.args[0].startswith("no such collation sequence"): |
| 104 | + self.fail("wrong OperationalError raised") |
| 105 | + |
| 106 | +def suite(): |
| 107 | + collation_suite = unittest.makeSuite(CollationTests, "Check") |
| 108 | + return unittest.TestSuite((collation_suite,)) |
| 109 | + |
| 110 | +def test(): |
| 111 | + runner = unittest.TextTestRunner() |
| 112 | + runner.run(suite()) |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + test() |
0 commit comments