dbtest_debversion.py 2.2 KB

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