Skip to content

Commit

Permalink
Switch to generators in gui and db_accessor.
Browse files Browse the repository at this point in the history
PEP8 fixes
  • Loading branch information
citizen-stig committed Jul 8, 2014
1 parent 9e95252 commit 7488935
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
13 changes: 3 additions & 10 deletions pyjtt/db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,12 @@ def get_issue(self, issue_key):
logger.debug('Issue is not found in local DB')

def get_worklog_for_issue(self, issue):
worklog = []
db_conn, cursor = self._connect_to_db()
cursor.execute('SELECT worklog_id, start_date, end_date, comment '
'FROM {worklog} WHERE jira_issue_id = ?'.format(worklog=self.WORKLOG_TABLE_NAME),
(issue.issue_id,))
for raw_worklog_entry in cursor.fetchall():
worklog_entry = self._parse_raw_worklog_entry(issue, raw_worklog_entry)
worklog.append(worklog_entry)
return worklog
yield self._parse_raw_worklog_entry(issue, raw_worklog_entry)

def add_worklog(self, worklog):

Expand Down Expand Up @@ -191,7 +188,6 @@ def update_worklog_entry(self, worklog_entry):
db_conn.close()

def get_day_worklog(self, day):
worklog = []
db_conn, cursor = self._connect_to_db()
cursor.execute('SELECT '
'{issues}.jira_issue_id,'
Expand All @@ -210,14 +206,11 @@ def get_day_worklog(self, day):
(day.strftime('%Y-%m-%d') + '%',))
for raw_entry in cursor.fetchall():
issue = self._parse_raw_issue(raw_entry[:3])
worklog.append(self._parse_raw_worklog_entry(issue, raw_entry[3:]))
return worklog
yield self._parse_raw_worklog_entry(issue, raw_entry[3:])

def get_all_issues(self):
issues = []
db_conn, cursor = self._connect_to_db()
cursor.execute('SELECT jira_issue_id, jira_issue_key, summary '
'FROM {issues}'.format(issues=self.ISSUES_TABLE_NAME))
for raw_issue_data in cursor.fetchall():
issues.append(self._parse_raw_issue(raw_issue_data))
return issues
yield self._parse_raw_issue(raw_issue_data)
19 changes: 14 additions & 5 deletions pyjtt/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ def change_worklog_entry(self):
parent=self)
edit_result = edit_window.exec_()
if edit_result == QtWidgets.QDialog.Accepted:
job = partial(self.app.update_worklog_entry, edit_window.worklog_entry)
job = partial(self.app.update_worklog_entry,
edit_window.worklog_entry)
self.tasks_queue.put(job)

def remove_worklog_entry(self):
Expand Down Expand Up @@ -505,18 +506,23 @@ def clear_status(self):
def filter_issues_table(self):
current_text = self.ui.lineIssueKey.text()
all_issues = self.app.get_all_issues()
issues = [x for x in all_issues if current_text in x.key or current_text in x.summary]
issues = (x for x in all_issues if current_text in x.key or
current_text in x.summary)
self.print_issues_table(issues)

def print_issues_table(self, issues):
logger.debug('Refreshing issues table')
self.ui.tableIssues.setRowCount(len(issues))
self.ui.tableIssues.setSortingEnabled(False)
row_count = 0
self.ui.tableIssues.setRowCount(row_count)
for row_num, issue in enumerate(issues):
row_count += 1
self.ui.tableIssues.setRowCount(row_count)
table_item = QtWidgets.QTableWidgetItem(issue.key)
table_item.issue = issue
self.ui.tableIssues.setItem(row_num, 0, table_item)
self.ui.tableIssues.setItem(row_num, 1, QtWidgets.QTableWidgetItem(issue.summary))
self.ui.tableIssues.setItem(row_num, 1,
QtWidgets.QTableWidgetItem(issue.summary))
self.ui.tableIssues.resizeColumnToContents(0)
self.ui.tableIssues.sortByColumn(0, 0)
self.ui.tableIssues.setSortingEnabled(True)
Expand All @@ -528,8 +534,11 @@ def print_worklog_table(self):
day_total = timedelta(seconds=0)

self.ui.tableDayWorklog.setSortingEnabled(False)
self.ui.tableDayWorklog.setRowCount(len(day_worklog))
row_count = 0
self.ui.tableDayWorklog.setRowCount(row_count)
for row_num, worklog_entry in enumerate(day_worklog):
row_count += 1
self.ui.tableDayWorklog.setRowCount(row_count)
issue_key_item = QtWidgets.QTableWidgetItem(worklog_entry.issue.key)
issue_key_item.worklog_entry = worklog_entry
self.ui.tableDayWorklog.setItem(row_num,
Expand Down
24 changes: 12 additions & 12 deletions tests/db_accessor_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_reopen_db(self):
del self.accessor
new_accessor = db_accessor.DBAccessor(self.filename)
new_from_db = new_accessor.get_all_issues()
self.assertEqual(len(new_from_db), 1)
self.assertEqual(len(list(new_from_db)), 1)

def test_get_nonexisted_issue(self):
none = self.accessor.get_issue('TST-969')
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_remove_issue(self):
self.accessor.remove_issue(issue2)

rest_issues = self.accessor.get_all_issues()
self.assertEqual(len(rest_issues), 1)
self.assertEqual(len(list(rest_issues)), 1)

def test_add_and_get_worklog_one(self):
issue = base_classes.JiraIssue('12345', 'TST-123', 'Юникод')
Expand All @@ -83,7 +83,7 @@ def test_add_and_get_worklog_one(self):
self.accessor.add_issue(issue)
self.accessor.add_worklog(worklog)

worklog_from_db = self.accessor.get_worklog_for_issue(issue)
worklog_from_db = list(self.accessor.get_worklog_for_issue(issue))

self.assertEqual(len(worklog), len(worklog_from_db))
self.assertEqual(worklog_from_db[0].started, worklog[0].started)
Expand All @@ -109,11 +109,11 @@ def test_add_and_get_worklog_many(self):
self.accessor.add_issue(issue)
self.accessor.add_worklog(worklog)

worklog_from_db = self.accessor.get_worklog_for_issue(issue)
worklog_from_db = list(self.accessor.get_worklog_for_issue(issue))

self.assertEqual(len(worklog), len(worklog_from_db))
self.assertTrue(worklog_from_db[0].comment in ('Комментарий',
'Комментарий два'))
'Комментарий два'))

def test_add_and_get_worklog_entry(self):
issue = base_classes.JiraIssue('12345', 'TST-123', 'Юникод')
Expand All @@ -125,7 +125,7 @@ def test_add_and_get_worklog_entry(self):
self.accessor.add_issue(issue)
self.accessor.add_worklog_entry(worklog_entry)

worklog_entry_from_db = self.accessor.get_worklog_for_issue(issue)[0]
worklog_entry_from_db = next(self.accessor.get_worklog_for_issue(issue))
self.assertEqual(worklog_entry_from_db.started, worklog_entry.started)
self.assertEqual(worklog_entry_from_db.ended, worklog_entry.ended)
self.assertEqual(worklog_entry_from_db.comment, worklog_entry.comment)
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_remove_worklog(self):

worklog_from_db = self.accessor.get_worklog_for_issue(issue)

self.assertEqual(len(worklog) - 1, len(worklog_from_db))
self.assertEqual(len(list(worklog)) - 1, len(list(worklog_from_db)))

def test_update_worklog_entry(self):
issue = base_classes.JiraIssue('12345', 'TST-123', 'Юникод')
Expand All @@ -167,7 +167,7 @@ def test_update_worklog_entry(self):
self.accessor.add_worklog_entry(worklog_entry)
worklog_entry.started = datetime(2013, 12, 12, 12)
self.accessor.update_worklog_entry(worklog_entry)
worklog_entry_from_db = self.accessor.get_worklog_for_issue(issue)[0]
worklog_entry_from_db = next(self.accessor.get_worklog_for_issue(issue))
self.assertEqual(worklog_entry_from_db.started.year, 2013)

def test_update_worklog_many(self):
Expand All @@ -187,7 +187,7 @@ def test_update_worklog_many(self):
self.accessor.add_issue(issue)
self.accessor.add_worklog(worklog)

worklog_from_db = self.accessor.get_worklog_for_issue(issue)
worklog_from_db = list(self.accessor.get_worklog_for_issue(issue))

self.assertEqual(len(worklog), len(worklog_from_db))
self.assertTrue(worklog_from_db[0].comment in ('Комментарий',
Expand Down Expand Up @@ -254,17 +254,17 @@ def test_get_day_worklog(self):
self.accessor.add_issue(issue2)
self.accessor.add_worklog(worklog)
day1_worklog = self.accessor.get_day_worklog(datetime(2014, 1, 2))
self.assertEqual(len(day1_worklog), 5)
self.assertEqual(len(list(day1_worklog)), 5)
day2_worklog = self.accessor.get_day_worklog(datetime(2014, 1, 3))
self.assertEqual(len(day2_worklog), 2)
self.assertEqual(len(list(day2_worklog)), 2)

def test_get_all_issues(self):
n = 5
for i in range(n):
issue = base_classes.JiraIssue('1234%s' % i, 'TST-%s' % i, 'Юникод')
self.accessor.add_issue(issue)
issues_from_db = self.accessor.get_all_issues()
self.assertEqual(len(issues_from_db), n)
self.assertEqual(len(list(issues_from_db)), n)


if __name__ == '__main__':
Expand Down

0 comments on commit 7488935

Please sign in to comment.