dbtest_fingerprint.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. from db_test import DBDakTestCase
  3. from daklib.dbconn import Fingerprint, Uid
  4. from daklib.dak_exceptions import DBUpdateError
  5. from sqlalchemy.exc import IntegrityError
  6. import unittest
  7. class FingerprintTestCase(DBDakTestCase):
  8. """
  9. The FingerprintTestCase tests the relation between Fingerprint and Uid
  10. objects.
  11. 1. It creates a fresh Fingerprint object.
  12. 2. It assigns a fresh Uid object to the Fingerprint object.
  13. 3. It fetches the Uid object from the database.
  14. 4. It checks that the original fingerprint is assigned to the freshly
  15. fetched Uid object.
  16. Furthermore it checks various constraints like not null and unique.
  17. TODO: the not null constraints should be enforced by the constructor in
  18. dbconn.py. Should we check the exact format of the fingerprint?
  19. """
  20. def test_relation(self):
  21. fingerprint = Fingerprint(fingerprint = 'deadbeefdeadbeef')
  22. self.session.add(fingerprint)
  23. query = self.session.query(Fingerprint)
  24. self.assertEqual(1, query.count())
  25. self.assertEqual('deadbeefdeadbeef', query.one().fingerprint)
  26. fingerprint.uid = Uid(uid = 'ftp-master@debian.org', name = 'ftpteam')
  27. uid = self.session.query(Uid).one()
  28. self.assertEqual('ftp-master@debian.org', uid.uid)
  29. self.assertEqual('ftpteam', uid.name)
  30. self.assertEqual(1, len(uid.fingerprint))
  31. self.assertEqual('deadbeefdeadbeef', uid.fingerprint[0].fingerprint)
  32. def fingerprint_no_fingerprint(self):
  33. self.session.add(Fingerprint())
  34. self.session.flush()
  35. def fingerprint_duplicate_fingerprint(self):
  36. self.session.add(Fingerprint(fingerprint = 'affe0815'))
  37. self.session.add(Fingerprint(fingerprint = 'affe0815'))
  38. self.session.flush()
  39. def uid_no_uid(self):
  40. self.session.add(Uid(name = 'foobar'))
  41. self.session.flush()
  42. def uid_duplicate_uid(self):
  43. self.session.add(Uid(uid = 'duplicate'))
  44. self.session.add(Uid(uid = 'duplicate'))
  45. self.session.flush()
  46. def test_exceptions(self):
  47. self.assertRaises(DBUpdateError, self.fingerprint_no_fingerprint)
  48. self.session.rollback()
  49. self.assertRaises(IntegrityError, self.fingerprint_duplicate_fingerprint)
  50. self.session.rollback()
  51. self.assertRaises(DBUpdateError, self.uid_no_uid)
  52. self.session.rollback()
  53. self.assertRaises(IntegrityError, self.uid_duplicate_uid)
  54. self.session.rollback()
  55. if __name__ == '__main__':
  56. unittest.main()