]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_debversion.py
dak rm: warn when working on oldstable as on stable
[dak.git] / tests / dbtest_debversion.py
1 #!/usr/bin/env python
2
3 from db_test import DBDakTestCase
4
5 from daklib.dbconn import DBConn, DebVersion
6
7 from sqlalchemy import Table, Column, Integer, func
8 from sqlalchemy.orm import mapper
9 import unittest
10
11 class Version(object):
12     def __init__(self, version):
13         self.version = version
14
15     def __repr__(self):
16         return "<Version('%s')>" % self.version
17
18 class DebVersionTestCase(DBDakTestCase):
19     """
20     The DebVersionTestCase tests both comparison (<=, ==, >, !=), the in_()
21     method and aggregate functions (min, max) for the DebVersion type. To
22     simplify the test it creates a separate table 'version' which is not used
23     by dak itself.
24     """
25
26     def setUp(self):
27         super(DebVersionTestCase, self).setUp()
28         self.version_table = Table('version', self.metadata, \
29             Column('id', Integer, primary_key = True), \
30             Column('version', DebVersion), \
31             )
32         self.version_table.create(checkfirst = True)
33         mapper(Version, self.version_table)
34
35     def test_debversion(self):
36         v = Version('0.5~')
37         self.session.add(v)
38         v = Version('0.5')
39         self.session.add(v)
40         v = Version('1.0')
41         self.session.add(v)
42         q = self.session.query(Version)
43         self.assertEqual(3, q.count())
44         self.assertEqual(2, q.filter(Version.version <= '0.5').count())
45         self.assertEqual(1, q.filter(Version.version == '0.5').count())
46         self.assertEqual(2, q.filter(Version.version > '0.5~').count())
47         self.assertEqual(1, q.filter(Version.version > '0.5').count())
48         self.assertEqual(0, q.filter(Version.version > '1.0').count())
49         self.assertEqual(2, q.filter(Version.version != '1.0').count())
50         self.assertEqual(2, q.filter(Version.version.in_(['0.5~', '1.0'])).count())
51         q = self.session.query(func.min(Version.version))
52         self.assertEqual('0.5~', q.scalar())
53         q = self.session.query(func.max(Version.version))
54         self.assertEqual('1.0', q.scalar())
55
56     def tearDown(self):
57         self.session.rollback()
58         self.version_table.drop()
59         super(DebVersionTestCase, self).tearDown()
60
61 if __name__ == '__main__':
62     unittest.main()