1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 """Integration tests for bug hunter.""" 6 7 import csv 8 from optparse import Values 9 import os 10 import unittest 11 12 from bug_hunter import BugHunter 13 14 try: 15 import gdata.data 16 import gdata.projecthosting.client 17 except ImportError: 18 logging.error('gdata-client needs to be installed. Please install\n' 19 'and try again (http://code.google.com/p/gdata-python-client/)') 20 sys.exit(1) 21 22 23 class BugHunterTest(unittest.TestCase): 24 """Unit tests for the Bug Hunter class.""" 25 _TEST_FILENAME = 'test.csv' 26 27 def _CleanTestFile(self): 28 if os.path.exists(self._TEST_FILENAME): 29 os.remove(self._TEST_FILENAME) 30 31 def setUp(self): 32 self._CleanTestFile() 33 34 def tearDown(self): 35 self._CleanTestFile() 36 37 def _GetIssue(self): 38 return [{'issue_id': '0', 'title': 'title', 'author': 'author', 39 'status': 'status', 'state': 'state', 'content': 'content', 40 'comments': [], 'labels': [], 'urls': []}] 41 42 def _GetDefaultOption(self, set_10_days_ago, query='steps'): 43 ops = Values() 44 ops.query = query 45 if set_10_days_ago: 46 ops.interval_value = 10 47 ops.interval_unit = 'days' 48 else: 49 ops.interval_value = None 50 ops.email_entries = ['comments'] 51 ops.project_name = 'chromium' 52 ops.query_title = 'query title' 53 ops.max_comments = None 54 return ops 55 56 def testGetIssueReturnedIssue(self): 57 bh = BugHunter( 58 self._GetDefaultOption(False, 59 query=('audio opened-after:2010/10/10' 60 ' opened-before:2010/10/20'))) 61 self.assertEquals(len(bh.GetIssues()), 18) 62 63 def testGetIssueReturnedIssueWithStatus(self): 64 ops = self._GetDefaultOption(False) 65 ops.query = 'Feature:Media* Status:Unconfirmed' 66 issues = BugHunter(ops).GetIssues() 67 for issue in issues: 68 self.assertEquals(issue['status'], 'Unconfirmed') 69 70 def testGetIssueReturnNoIssue(self): 71 ops = self._GetDefaultOption(True) 72 ops.query = 'thisshouldnotmatchpleaseignorethis*' 73 self.assertFalse(BugHunter(ops).GetIssues()) 74 75 def testGetComments(self): 76 comments = BugHunter(self._GetDefaultOption(False)).GetComments(100000, 2) 77 self.assertEquals(len(comments), 2) 78 expected_comments = [(None, 'rby... (at] chromium.org', 79 '2011-10-31T19:54:40.000Z'), 80 (None, 'backer (at] chromium.org', 81 '2011-10-14T13:59:37.000Z')] 82 self.assertEquals(comments, expected_comments) 83 84 def testWriteIssuesToFileInCSV(self): 85 ops = self._GetDefaultOption(False) 86 bh = BugHunter(ops) 87 bh.WriteIssuesToFileInCSV(self._GetIssue(), self._TEST_FILENAME) 88 89 with open(self._TEST_FILENAME, 'r') as f: 90 reader = csv.reader(f) 91 self.assertEquals(reader.next(), ['status', 'content', 'state', 92 'issue_id', 'urls', 'title', 'labels', 93 'author', 'comments']) 94 self.assertEquals(reader.next(), ['status', 'content', 'state', '0', 95 '[]', 'title', '[]', 'author', '[]']) 96 self.assertRaises(StopIteration, reader.next) 97