def test_apply_bugsummary_changes(self):
        # apply_bugsummary_changes takes a target and a tuple of changes
        # from calculate_bugsummary_changes and flushes the changes to
        # the DB.
        product = self.factory.makeProduct()
        self.assertContentEqual([], get_bugsummary_rows(product))
        NEW = BugTaskStatus.NEW
        TRIAGED = BugTaskStatus.TRIAGED
        LOW = BugTaskImportance.LOW
        HIGH = BugTaskImportance.HIGH

        # Add a couple of rows to start.
        with dbuser('bugsummaryrebuild'):
            apply_bugsummary_changes(
                product,
                {(NEW, None, HIGH, False, None, None, None): 2,
                (TRIAGED, None, LOW, False, None, None, None): 4},
                {}, [])
        self.assertContentEqual(
            [(NEW, None, HIGH, False, None, None, None, 2),
             (TRIAGED, None, LOW, False, None, None, None, 4)],
            get_bugsummary_rows(product))

        # Delete one, mutate the other.
        with dbuser('bugsummaryrebuild'):
            apply_bugsummary_changes(
                product,
                {}, {(NEW, None, HIGH, False, None, None, None): 3},
                [(TRIAGED, None, LOW, False, None, None, None)])
        self.assertContentEqual(
            [(NEW, None, HIGH, False, None, None, None, 3)],
            get_bugsummary_rows(product))
 def test_get_bugsummary_rows(self):
     product = self.factory.makeProduct()
     rollup_journal()
     orig_rows = set(get_bugsummary_rows(product))
     task = self.factory.makeBug(target=product).default_bugtask
     rollup_journal()
     new_rows = set(get_bugsummary_rows(product))
     self.assertContentEqual(
         [(task.status, None, task.importance, False, None, None, None, 1)],
         new_rows - orig_rows)
    def test_script(self):
        product = self.factory.makeProduct()
        self.factory.makeBug(target=product)
        self.assertEqual(0, get_bugsummary_rows(product).count())
        self.assertEqual(1, get_bugsummaryjournal_rows(product).count())
        transaction.commit()

        exit_code, out, err = run_script('scripts/bugsummary-rebuild.py')
        self.addDetail("stdout", text_content(out))
        self.addDetail("stderr", text_content(err))
        self.assertEqual(0, exit_code)

        transaction.commit()
        self.assertEqual(1, get_bugsummary_rows(product).count())
        self.assertEqual(0, get_bugsummaryjournal_rows(product).count())
 def test_rebuild_bugsummary_for_target(self):
     # rebuild_bugsummary_for_target rebuilds BugSummary for a
     # specific target from BugTaskFlat. Since it ignores the
     # journal, it also removes any relevant journal entries.
     product = self.factory.makeProduct()
     self.factory.makeBug(target=product)
     self.assertEqual(0, get_bugsummary_rows(product).count())
     self.assertEqual(1, get_bugsummaryjournal_rows(product).count())
     log = BufferLogger()
     with dbuser('bugsummaryrebuild'):
         rebuild_bugsummary_for_target(product, log)
     self.assertEqual(1, get_bugsummary_rows(product).count())
     self.assertEqual(0, get_bugsummaryjournal_rows(product).count())
     self.assertThat(
         log.getLogBufferAndClear(),
         MatchesRegex(
             'DEBUG Rebuilding %s\nDEBUG Added {.*: 1L}' % product.name))