/socorro/unittest/external/postgresql/test_crash_adu_by_build_signature.py

https://github.com/lauraxt/socorro · Python · 243 lines · 203 code · 30 blank · 10 comment · 0 complexity · bc5f3251ba6ed9d54ee1b4e4454f0c11 MD5 · raw file

  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import datetime
  5. from nose.plugins.attrib import attr
  6. from nose.tools import eq_, ok_
  7. from socorro.lib import datetimeutil
  8. from .unittestbase import PostgreSQLTestCase
  9. #==============================================================================
  10. @attr(integration='postgres') # for nosetests
  11. class IntegrationTestCrashAduByBuildSignature(PostgreSQLTestCase):
  12. """Test of Crash ADU By Build Signature stored procedures"""
  13. #--------------------------------------------------------------------------
  14. def setUp(self):
  15. """ Populate product_info table with fake data """
  16. super(IntegrationTestCrashAduByBuildSignature, self).setUp()
  17. cursor = self.connection.cursor()
  18. # Insert data
  19. self.now = datetimeutil.utc_now()
  20. self.tomorrow = self.now + datetime.timedelta(days=1)
  21. tomorrow = self.tomorrow.date()
  22. now = self.now.date()
  23. cursor.execute("""
  24. INSERT INTO products
  25. (product_name, sort, rapid_release_version, release_name)
  26. VALUES
  27. (
  28. 'Firefox',
  29. 1,
  30. '8.0',
  31. 'firefox'
  32. );
  33. """)
  34. cursor.execute("""
  35. INSERT INTO release_channels
  36. (release_channel, sort)
  37. VALUES
  38. (
  39. 'Release', 1
  40. ),
  41. (
  42. 'Beta', 2
  43. );
  44. """)
  45. cursor.execute("""
  46. INSERT INTO product_versions
  47. (product_version_id,
  48. product_name, major_version, release_version, version_string,
  49. build_date, sunset_date, featured_version, build_type,
  50. version_sort, has_builds, is_rapid_beta, build_type_enum)
  51. VALUES
  52. (
  53. 1,
  54. 'Firefox',
  55. '8.0',
  56. '8.0',
  57. '8.0',
  58. '%(now)s',
  59. '%(now)s',
  60. False,
  61. 'release',
  62. '0008000',
  63. True,
  64. False,
  65. 'release'
  66. );
  67. """ % {'now': now})
  68. cursor.execute("""
  69. INSERT INTO signatures
  70. (first_build, first_report, signature)
  71. VALUES
  72. ('20130701120000', '%(now)s', 'Fake Signature #1')
  73. """ % {'now': now})
  74. cursor.execute("""
  75. SELECT signature_id FROM signatures
  76. WHERE signature = 'Fake Signature #1'
  77. """)
  78. signature_id = cursor.fetchone()[0]
  79. cursor.execute("""
  80. SELECT product_version_id
  81. FROM product_versions
  82. WHERE product_name = 'Firefox' and version_string = '8.0'
  83. """)
  84. product_version_id = cursor.fetchone()[0]
  85. cursor.execute("""
  86. INSERT INTO reports_clean
  87. (address_id,
  88. build,
  89. date_processed,
  90. domain_id,
  91. flash_version_id,
  92. os_name,
  93. os_version_id,
  94. process_type,
  95. reason_id,
  96. release_channel,
  97. signature_id,
  98. uuid,
  99. build_type,
  100. product_version_id)
  101. VALUES
  102. (1,
  103. '%(build)s',
  104. '%(now)s',
  105. 1,
  106. 1,
  107. 'windows',
  108. '9',
  109. 'browser',
  110. 1,
  111. 'release',
  112. '%(signature_id)s',
  113. 'a1',
  114. 'release',
  115. '%(product_version_id)s')""" %
  116. {'now': now,
  117. 'build': now.strftime('%Y%m%d'),
  118. 'signature_id': signature_id,
  119. 'product_version_id': product_version_id})
  120. cursor.execute("""
  121. INSERT INTO build_adu
  122. (product_version_id,
  123. build_date,
  124. adu_date,
  125. os_name,
  126. adu_count)
  127. VALUES
  128. (%(product_version_id)s,
  129. '%(now)s',
  130. '%(now)s',
  131. 'windows',
  132. 123),
  133. (%(product_version_id)s,
  134. '%(tomorrow)s',
  135. '%(tomorrow)s',
  136. 'windows',
  137. 321) """ % {
  138. 'product_version_id': product_version_id,
  139. 'now': now,
  140. 'tomorrow': tomorrow
  141. })
  142. def tearDown(self):
  143. """ Cleanup the database, delete tables and functions """
  144. cursor = self.connection.cursor()
  145. cursor.execute("""
  146. TRUNCATE products,
  147. product_versions,
  148. release_channels,
  149. signatures,
  150. reports_clean,
  151. build_adu,
  152. crash_adu_by_build_signature
  153. CASCADE
  154. """)
  155. self.connection.commit()
  156. super(IntegrationTestCrashAduByBuildSignature, self).tearDown()
  157. def test_stored_procedure(self):
  158. cursor = self.connection.cursor()
  159. now = self.now.date()
  160. tomorrow = self.tomorrow.date()
  161. cursor.execute("""
  162. SELECT update_crash_adu_by_build_signature('%(now)s')
  163. """ % {'now': now})
  164. ok_(cursor.fetchone()[0])
  165. cursor.execute("""
  166. SELECT
  167. signature,
  168. adu_date,
  169. build_date,
  170. buildid::text,
  171. crash_count,
  172. adu_count,
  173. os_name,
  174. channel
  175. FROM
  176. crash_adu_by_build_signature
  177. WHERE build_date = '%(now)s'""" % {'now': now})
  178. expected = ('Fake Signature #1',
  179. now,
  180. now,
  181. now.strftime('%Y%m%d'),
  182. 1,
  183. 123,
  184. 'windows',
  185. 'release')
  186. eq_(cursor.fetchall()[0], expected)
  187. # ensure that we show builds with no crashes
  188. expected = ('',
  189. tomorrow,
  190. tomorrow,
  191. '0',
  192. 0,
  193. 321,
  194. 'windows',
  195. 'release')
  196. cursor.execute("""
  197. SELECT update_crash_adu_by_build_signature('%(tomorrow)s')
  198. """ % {'tomorrow': tomorrow})
  199. ok_(cursor.fetchone()[0])
  200. cursor.execute("""
  201. SELECT
  202. signature,
  203. adu_date,
  204. build_date,
  205. buildid::text,
  206. crash_count,
  207. adu_count,
  208. os_name,
  209. channel
  210. FROM
  211. crash_adu_by_build_signature
  212. WHERE build_date = '%(tomorrow)s'""" % {'tomorrow': tomorrow})
  213. eq_(cursor.fetchall()[1], expected)