Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/test_db_story.py
blob: 66f46fcd2227c243570e1b21fa06c3c71823fcf4 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# python import - http://docs.python.org/library/unittest.html
import os, sys, unittest

# add lib path to current python path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

# atoidejouer import
from atoidejouer.db import story
from atoidejouer.tools import storage


class TestDBStory(unittest.TestCase):

    def setUp(self):
        story.DB()

    def tearDown(self):
        story.DB()._del(all=True)

    def test_all(self):
        all = [r for r in story.DB()._all()]
        self.assertEqual(len(all), 0,
                "should not have row! found: %s" % len(all))

    def test_add(self):
        # second row
        key = story.Key(None, 'helo.png', 'image/png', '12345')
        story.DB().add(key)
        all = [r for r in story.DB()._all()]
        self.assertEqual(len(all), 1,
                "should have 1 row! found: %s" % len(all))
        self.assertEqual(all[0], key, "not the same row: %s" % all[0])
        # second row
        key = story.Key(None, 'hola.png', 'image/png', '67890')
        story.DB().add(key)
        all = [r for r in story.DB()._all()]
        self.assertEqual(len(all), 2,
                "should have 1 row! found: %s" % len(all))
        self.assertEqual(all[1], key, "not the same row: %s" % all[1])

    def test_get(self):
        key = story.Key(None, 'helo.png', 'image/png', '12345')
        story.DB().add(key)
        all = [r for r in story.DB().get(story.Key(name='helo'))]
        self.assertEqual(len(all), 1,
                "should have 1 row! found: %s" % len(all))
        self.assertEqual(all[0], key, "not the same row: %s" % all[0])

    def test_update(self):
        key = story.Key(None, 'helo.png', 'image/png', '12345', layer=1)
        story.DB().add(key)
        all = [r for r in story.DB()._all()]
        key = story.Key(id=all[0].id, title='hola.png', timestamp='12345', layer=2)
        story.DB().update(key)
        all = [r for r in story.DB()._all()]
        self.assertEqual(len(all), 1,
                "should have 1 row! found: %s" % len(all))
        # NOTE: title, mime_type and timestamp can not be changed
        exp_key = story.Key(None, 'helo.png', 'image/png', '12345', layer=2)
        self.assertEqual(all[0], exp_key,
                "not the same row\n`%s`\ninstead of\n`%s`" % (all[0], exp_key))

    def test_duration(self):
        story.DB().add(story.Key(title='helo', mime_type='img', timestamp='12345', time=0))
        story.DB().add(story.Key(title='helo', mime_type='img', timestamp='12345', time=4))
        story.DB().add(story.Key(title='helo', mime_type='img', timestamp='12345', time=5))
        story.DB().add(story.Key(title='helo', mime_type='img', timestamp='12345', time=6))
        _all = [r for r in story.DB()._all()]
        # duration update +2
        # print '+2 ...'
        _all[0].update_duration(2)
        _all = [r for r in story.DB()._all()]
        self.assertEqual(len(_all), 6,
                "should have 6 rows! found: %s" % _all)
        self.assertEqual(_all[4].time, 1)
        self.assertEqual(_all[5].time, 2)
        self.assertEqual(_all[4].duration, 1)
        self.assertEqual(_all[5].duration, 0)
        self.assertEqual(_all[1].duration, 2, _all[1])
        self.assertEqual(_all[2].duration, 1, _all[2])
        self.assertEqual(_all[3].duration, 0, _all[3])
        # duration update +3
        # print '+3 ...'
        _all[0].update_duration(3)
        _all = [r for r in story.DB()._all()]
        self.assertEqual(len(_all), 7,
                "should have 7 rows! found: %s" % len(_all))
        self.assertEqual(_all[4].time, 1)
        self.assertEqual(_all[5].time, 2)
        self.assertEqual(_all[6].time, 3)
        self.assertEqual(_all[4].duration, 2)
        self.assertEqual(_all[5].duration, 1)
        self.assertEqual(_all[6].duration, 0)
        self.assertEqual(_all[1].duration, 2)
        self.assertEqual(_all[2].duration, 1)
        self.assertEqual(_all[3].duration, 0)
        # duration update +5
        # print '+5 ...'
        _all[0].update_duration(5)
        _all = [r for r in story.DB()._all()]
        self.assertEqual(len(_all), 7,
                "should have 7 rows! found: %s" % len(_all))
        self.assertEqual(_all[4].time, 1)
        self.assertEqual(_all[5].time, 2)
        self.assertEqual(_all[6].time, 3)
        self.assertEqual(_all[1].time, 4)
        self.assertEqual(_all[2].time, 5)
        self.assertEqual(_all[3].time, 6)
        self.assertEqual(_all[4].duration, 4)
        self.assertEqual(_all[5].duration, 3)
        self.assertEqual(_all[6].duration, 2)
        self.assertEqual(_all[1].duration, 1)
        self.assertEqual(_all[2].duration, 0)
        self.assertEqual(_all[3].duration, 0)
        # duration update +3 again
        # print '+3 again ...'
        _all[0].update_duration(3)
        _all = [r for r in story.DB()._all()]
        self.assertEqual(len(_all), 5,
                "should have 5 rows! found: %s" % _all)
        self.assertEqual(_all[2].time, 1)
        self.assertEqual(_all[3].time, 2)
        self.assertEqual(_all[4].time, 3)
        self.assertEqual(_all[1].time, 6)
        self.assertEqual(_all[2].duration, 2)
        self.assertEqual(_all[3].duration, 1)
        self.assertEqual(_all[4].duration, 0)
        self.assertEqual(_all[1].duration, 0)
        # duration update +0 back
        # print '+0 back ...'
        _all[0].update_duration(0)
        _all = [r for r in story.DB()._all()]
        self.assertEqual(len(_all), 2,
                "should have 4 rows! found: %s" % len(_all))
        self.assertEqual(_all[0].time, 0)
        self.assertEqual(_all[1].time, 6)
        self.assertEqual(_all[0].duration, 0)
        self.assertEqual(_all[1].duration, 0)


def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestDBStory('test_all'))
    suite.addTest(TestDBStory('test_add'))
    suite.addTest(TestDBStory('test_get'))
    suite.addTest(TestDBStory('test_update'))
    suite.addTest(TestDBStory('test_duration'))
    return suite


if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=1).run(suite())