summaryrefslogtreecommitdiff
path: root/test/logging_tests.py
blob: ed7bf24b5166a7a2a80b8989c8e35863b4f3c67d (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import testsupport
import StringIO, unittest
import sqlite

class LogFileTemplate:
    def write(self, s):
        pass

class LogFile:
    def __init__(self):
        pass

def init_LogFile():
    LogFile.write = LogFileTemplate.write

class CommandLoggingTests(unittest.TestCase, testsupport.TestSupport):
    def tearDown(self):
        try:
            self.cnx.close()
            self.removefile()
        except AttributeError:
            pass
        except sqlite.InterfaceError:
            pass

    def CheckNoWrite(self):
        init_LogFile()
        del LogFile.write
        logger = LogFile()
        try:
            self.cnx = sqlite.connect(self.getfilename(),
                command_logfile=logger)

            self.fail("ValueError not raised")
        except ValueError:
            pass

    def CheckWriteNotCallable(self):
        logger = LogFile()
        logger.write = 5
        try:
            self.cnx = sqlite.connect(self.getfilename(),
                command_logfile=logger)

            self.fail("ValueError not raised")
        except ValueError:
            pass

    def CheckLoggingWorks(self):
        logger = StringIO.StringIO()

        expected_output = "\n".join([
            "BEGIN", "CREATE TABLE TEST(FOO INTEGER)",
            "INSERT INTO TEST(FOO) VALUES (5)",
            "ROLLBACK"]) + "\n"

        self.cnx = sqlite.connect(self.getfilename(),
            command_logfile=logger)
        cu = self.cnx.cursor()
        cu.execute("CREATE TABLE TEST(FOO INTEGER)")
        cu.execute("INSERT INTO TEST(FOO) VALUES (%i)", (5,))
        self.cnx.rollback()

        logger.seek(0)
        real_output = logger.read()
        
        if expected_output != real_output:
            self.fail("Logging didn't produce expected output.")

def suite():
    command_logging_suite = unittest.makeSuite(CommandLoggingTests, "Check")
    return command_logging_suite

def main():
    runner = unittest.TextTestRunner()
    runner.run(suite())

if __name__ == "__main__":
    main()