dbtest_contents.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #! /usr/bin/env python3
  2. import unittest
  3. from db_test import DBDakTestCase
  4. from sqlalchemy.exc import IntegrityError
  5. from sqlalchemy.orm.exc import FlushError
  6. from daklib.contents import BinaryContentsWriter
  7. from daklib.dbconn import (
  8. BinContents,
  9. get_override,
  10. get_override_type,
  11. get_priorities,
  12. get_priority,
  13. get_section,
  14. get_sections,
  15. )
  16. class ContentsTestCase(DBDakTestCase):
  17. """
  18. This TestCase checks the behaviour of contents generation.
  19. """
  20. def test_duplicates1(self):
  21. """
  22. Test the BinContents class for duplication problems.
  23. """
  24. self.setup_binaries()
  25. contents1 = BinContents(
  26. file="usr/bin/hello", binary=self.binary["hello_2.2-1_i386"]
  27. )
  28. self.session.add(contents1)
  29. self.session.flush()
  30. # test duplicates
  31. contents2 = BinContents(
  32. file="usr/bin/hello", binary=self.binary["hello_2.2-1_i386"]
  33. )
  34. self.session.add(contents2)
  35. # SQLAlchemy 1.4 raises IntegrityError, previous versions
  36. # raised FlushError.
  37. # Reference: https://docs.sqlalchemy.org/en/14/changelog/changelog_14.html#change-f7b996813d7921f404e0cc8457951f9a
  38. self.assertRaises((FlushError, IntegrityError), self.session.flush)
  39. def test_duplicates2(self):
  40. """
  41. Test the BinContents class for more duplication problems.
  42. """
  43. self.setup_binaries()
  44. contents1 = BinContents(
  45. file="usr/bin/hello", binary=self.binary["hello_2.2-1_i386"]
  46. )
  47. self.session.add(contents1)
  48. contents2 = BinContents(
  49. file="usr/bin/gruezi", binary=self.binary["hello_2.2-1_i386"]
  50. )
  51. self.session.add(contents2)
  52. self.session.flush()
  53. # test duplicates
  54. contents2.file = "usr/bin/hello"
  55. self.assertRaises(IntegrityError, self.session.flush)
  56. def test_duplicates3(self):
  57. """
  58. Test the BinContents class even more.
  59. """
  60. self.setup_binaries()
  61. contents1 = BinContents(
  62. file="usr/bin/hello", binary=self.binary["hello_2.2-1_i386"]
  63. )
  64. self.session.add(contents1)
  65. # same file in different binary packages should be okay
  66. contents2 = BinContents(
  67. file="usr/bin/hello", binary=self.binary["gnome-hello_2.2-1_i386"]
  68. )
  69. self.session.add(contents2)
  70. self.session.flush()
  71. def test_overridetype(self):
  72. """
  73. Test the OverrideType class.
  74. """
  75. self.setup_overridetypes()
  76. self.assertEqual("deb", self.otype["deb"].overridetype)
  77. self.assertEqual(0, self.otype["deb"].overrides.count())
  78. self.assertEqual(self.otype["deb"], get_override_type("deb", self.session))
  79. def test_section(self):
  80. """
  81. Test Section class.
  82. """
  83. self.setup_sections()
  84. self.assertEqual("python", self.section["python"].section)
  85. self.assertEqual("python", self.section["python"])
  86. self.assertTrue(self.section["python"] != "java")
  87. self.assertEqual(self.section["python"], get_section("python", self.session))
  88. all_sections = get_sections(self.session)
  89. self.assertEqual(self.section["python"].section_id, all_sections["python"])
  90. self.assertEqual(0, self.section["python"].overrides.count())
  91. def test_priority(self):
  92. """
  93. Test Priority class.
  94. """
  95. self.setup_priorities()
  96. self.assertEqual("standard", self.prio["standard"].priority)
  97. self.assertEqual(3, self.prio["standard"].level)
  98. self.assertEqual("standard", self.prio["standard"])
  99. self.assertTrue(self.prio["standard"] != "extra")
  100. self.assertEqual(self.prio["standard"], get_priority("standard", self.session))
  101. all_priorities = get_priorities(self.session)
  102. self.assertEqual(self.prio["standard"].priority_id, all_priorities["standard"])
  103. self.assertEqual(0, self.prio["standard"].overrides.count())
  104. def test_override(self):
  105. """
  106. Test Override class.
  107. """
  108. self.setup_overrides()
  109. list = get_override("hello", session=self.session)
  110. self.assertEqual(3, len(list))
  111. self.assertTrue(self.override["hello_sid_main_udeb"] in list)
  112. self.assertTrue(self.override["hello_squeeze_main_deb"] in list)
  113. list = get_override("hello", suite="sid", session=self.session)
  114. self.assertEqual([self.override["hello_sid_main_udeb"]], list)
  115. list = get_override("hello", suite=["sid"], session=self.session)
  116. self.assertEqual([self.override["hello_sid_main_udeb"]], list)
  117. list = get_override("hello", component="contrib", session=self.session)
  118. self.assertEqual([self.override["hello_lenny_contrib_deb"]], list)
  119. list = get_override("hello", component=["contrib"], session=self.session)
  120. self.assertEqual([self.override["hello_lenny_contrib_deb"]], list)
  121. list = get_override("hello", overridetype="deb", session=self.session)
  122. self.assertEqual(2, len(list))
  123. self.assertTrue(self.override["hello_sid_main_udeb"] not in list)
  124. self.assertTrue(self.override["hello_squeeze_main_deb"] in list)
  125. list = get_override("hello", overridetype=["deb"], session=self.session)
  126. self.assertEqual(2, len(list))
  127. self.assertTrue(self.override["hello_sid_main_udeb"] not in list)
  128. self.assertTrue(self.override["hello_squeeze_main_deb"] in list)
  129. # test the backrefs
  130. self.assertEqual(
  131. self.override["hello_sid_main_udeb"], self.suite["sid"].overrides.one()
  132. )
  133. self.assertEqual(2, self.comp["main"].overrides.count())
  134. self.assertEqual(
  135. self.override["hello_sid_main_udeb"],
  136. self.comp["main"].overrides.filter_by(suite=self.suite["sid"]).one(),
  137. )
  138. self.assertEqual(
  139. self.override["hello_sid_main_udeb"], self.otype["udeb"].overrides.one()
  140. )
  141. def test_binarycontentswriter(self):
  142. """
  143. Test the BinaryContentsWriter class.
  144. """
  145. self.setup_binaries()
  146. self.setup_overrides()
  147. self.binary["hello_2.2-1_i386"].contents.append(
  148. BinContents(file="/usr/bin/hello")
  149. )
  150. self.session.flush()
  151. cw = BinaryContentsWriter(
  152. self.suite["squeeze"],
  153. self.arch["i386"],
  154. self.otype["deb"],
  155. self.comp["main"],
  156. )
  157. self.assertEqual(
  158. ["/usr/bin/hello python/hello\n"],
  159. cw.get_list(),
  160. )
  161. # test formatline and sort order
  162. self.assertEqual(
  163. "/usr/bin/hello python/hello\n",
  164. cw.formatline("/usr/bin/hello", "python/hello"),
  165. )
  166. # test unicode support
  167. self.binary["hello_2.2-1_i386"].contents.append(BinContents(file="\xc3\xb6"))
  168. self.session.flush()
  169. if __name__ == "__main__":
  170. unittest.main()