dbtest_fingerprint.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #! /usr/bin/env python3
  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(IntegrityError, self.fingerprint_no_fingerprint)
  48. self.session.rollback()
  49. self.assertRaises(
  50. IntegrityError, self.fingerprint_duplicate_fingerprint)
  51. self.session.rollback()
  52. self.assertRaises(IntegrityError, self.uid_no_uid)
  53. self.session.rollback()
  54. self.assertRaises(IntegrityError, self.uid_duplicate_uid)
  55. self.session.rollback()
  56. if __name__ == '__main__':
  57. unittest.main()