Skip to content

Commit

Permalink
chore: convert asserts to idiomatic python.
Browse files Browse the repository at this point in the history
  • Loading branch information
codito committed Jan 20, 2018
1 parent 1271836 commit a941452
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 184 deletions.
27 changes: 15 additions & 12 deletions pomito/task.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Pomito - Pomodoro timer on steroids
# Task concept and routines
"""Task concept and routines."""

import hashlib


class Task(object):
"""A Task is an activity that the user is working on during a pomodoro session."""

def __init__(self, uid, estimate, actual, tags, description):
"""Constructor for a Task
"""Create a Task.
Args:
description: Description of the task
Expand All @@ -23,34 +24,36 @@ def __init__(self, uid, estimate, actual, tags, description):
self.tags = tags
self.completed = 0 # If a task is completed, there should be no object created
except ValueError:
raise Exception("Attempt to parse invalid task. Task attributes: '{0}' '{1}' '{2}' '{3}' '{4}'"\
.format(uid, description, estimate, actual, tags))
raise Exception("Attempt to parse invalid task. Task attributes: '{0}' '{1}' '{2}' '{3}' '{4}'"
.format(uid, description, estimate, actual, tags))

# Every task will have a unique id, which is the md5sum of description.
if self.uid == None:
if self.uid is None:
m = hashlib.md5()
m.update(self.description.encode('utf-8'))
self.uid = m.hexdigest()

return

def __str__(self):
return "I:{0} | E:{1} | A:{2} | T:{3} | D:{4}".format(self.uid, self.estimate,
self.actual, self.tags,
self.description)
"""Get a string representation for the task."""
return "I:{0} | E:{1} | A:{2} | T:{3} | D:{4}"\
.format(self.uid, self.estimate, self.actual, self.tags,
self.description)

def update_actual(self):
"""Update actual pomodoros for a task."""
self.actual += 1
return

def update_estimate(self, new_estimate):
"""Update estimated pomodoros for a task."""
self.estimate = int(new_estimate)
return

def mark_complete(self):
"""Mark a task as complete."""
self.completed = 1
return


def get_null_task():
"""Returns a null task."""
"""Get a null task."""
return Task(0, 0, 0, None, "No task selected.")
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ attr=!perf
max-line-length=120
exclude=.venv
per-file-ignores =
**/*.py: F405
tests/*.py: D101,D102
tests/*.py: D101,D102,D103

[pydocstyle]
match_dir=pomito
Expand Down
34 changes: 13 additions & 21 deletions tests/hooks/test_activity.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# -*- coding: utf-8 -*-
# Tests for the Activity hook
"""Tests for the Activity hook."""

import unittest
from peewee import Using

from pomito.hooks.activity import ActivityHook, ActivityModel
from pomito.test import PomitoTestFactory

from pyfakefs import fake_filesystem
import sure


class ActivityHookTests(unittest.TestCase):
Expand All @@ -33,19 +31,16 @@ def tearDown(self):
def test_initialize_sets_activity_db(self):
count = ActivityModel.select().count()

count.should.be.equal(0)
assert count == 0

def test_initialize_creates_signal_handlers(self):
activityhook = ActivityHook(self.pomodoro_service)

activityhook.initialize()

self._count_receivers(self.pomodoro_service.signal_session_stopped)\
.should.be.equal(2)
self._count_receivers(self.pomodoro_service.signal_break_stopped)\
.should.be.equal(2)
self._count_receivers(self.pomodoro_service.signal_interruption_stopped)\
.should.be.equal(2)
assert self._receivers(self.pomodoro_service.signal_session_stopped, 2)
assert self._receivers(self.pomodoro_service.signal_break_stopped, 2)
assert self._receivers(self.pomodoro_service.signal_interruption_stopped, 2)
activityhook.close()

def test_close_disconnects_signal_handlers(self):
Expand All @@ -55,35 +50,32 @@ def test_close_disconnects_signal_handlers(self):
activityhook.close()

# Count is one because there is already a listener for self.activityhook
self._count_receivers(self.pomodoro_service.signal_session_stopped)\
.should.be.equal(1)
self._count_receivers(self.pomodoro_service.signal_break_stopped)\
.should.be.equal(1)
self._count_receivers(self.pomodoro_service.signal_interruption_stopped)\
.should.be.equal(1)
assert self._receivers(self.pomodoro_service.signal_session_stopped, 1)
assert self._receivers(self.pomodoro_service.signal_break_stopped, 1)
assert self._receivers(self.pomodoro_service.signal_interruption_stopped, 1)

def test_log_handles_session_stop_event(self):
test_task = self._create_task(100, 'session_start_task')
self.pomodoro_service.start_session(test_task)
self.pomodoro_service.stop_session()

activities = ActivityModel.get(ActivityModel.category == 'session')
activities.should.not_be.none
assert activities is not None

def test_log_handles_break_stop_event(self):
self.pomodoro_service.start_break()
self.pomodoro_service.stop_break()

self._dump_activity_model()
activities = ActivityModel.get(ActivityModel.category == 'break')
activities.should.not_be.none
assert activities is not None

def test_log_handles_interruption_stop_event(self):
self.pomodoro_service.start_interruption(None, False, False)
self.pomodoro_service.stop_interruption()

act = ActivityModel.get(ActivityModel.category == 'interruption')
act.shouldnt.be.none
assert act is not None

def _create_task(self, uid, description):
from pomito.task import Task
Expand All @@ -96,7 +88,7 @@ def _dump_activity_model(self):
print("{0};{1};{2}".format(activity.timestamp, activity.category,
activity.data))

def _count_receivers(self, signal):
def _receivers(self, signal, count):
from blinker.base import ANY

return sum(1 for r in signal.receivers_for(ANY))
return sum(1 for r in signal.receivers_for(ANY)) == count
41 changes: 19 additions & 22 deletions tests/plugins/task/test_asana.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""Tests for asana task plugin."""

import unittest
from unittest.mock import Mock, MagicMock, patch
from unittest.mock import Mock, MagicMock

from asana.asana import AsanaAPI
from pyfakefs import fake_filesystem
from sure import expect

from pomito.plugins.task.asana import AsanaTask
from pomito.task import Task
from pomito.test import FakeMessageDispatcher, PomitoTestFactory
from pomito.test import PomitoTestFactory


# pylint: skip-file
class AsanaTests(unittest.TestCase):
dummy_workspace_list = [{'id': 1, 'name': "ws1"}, {'id': 2, 'name': "ws2"}]
dummy_task_list = [{'id': 1, 'name': "task1"}, {'id': 2, 'name': "task2"}]
Expand Down Expand Up @@ -45,10 +43,10 @@ def tearDown(self):
self.pomodoro_service._pomito_instance.exit()

def test_asana_can_create_asana_plugin(self):
expect(self.asana).to_not.be.none
assert self.asana is not None

def test_asana_raises_valueerror_for_no_pomodoro_service(self):
expect(AsanaTask).when.called_with(None).to.throw(ValueError)
self.assertRaises(ValueError, AsanaTask, None)

def test_initialize_reads_api_key_from_config_file(self):
# Create a mock factory to create AsanaAPI object
Expand All @@ -63,18 +61,16 @@ def test_initialize_does_not_throw_for_no_config(self):
# Set _get_asana_api as None to ensure initialize throws
a = AsanaTask(self.pomodoro_service, None)

expect(a.initialize).when.called.to_not.throw()
a.initialize()

def test_initialize_does_not_throw_for_default_get_asana_api(self):
a = AsanaTask(self.pomodoro_service)

expect(a.initialize).when.called.to_not.throw()
a.initialize()

def test_get_tasks_does_not_throw_without_initialize(self):
# Set asana_api as None so that AttributeError is thrown
x = lambda: list(self.asana.get_tasks())

expect(x).when.called.to_not.throw()
assert list(self.asana.get_tasks()) is not None

def test_get_tasks_returns_list_of_task_objects(self):
self.asana_api.list_workspaces\
Expand All @@ -84,12 +80,12 @@ def test_get_tasks_returns_list_of_task_objects(self):

tasks = list(self.asana.get_tasks())

expect(len(tasks)).to.equal(1)
expect(tasks[0].uid).to.equal(1)
expect(tasks[0].description).to.equal("t")
expect(tasks[0].estimate).to.equal(0)
expect(tasks[0].actual).to.equal(0)
expect(tasks[0].tags).to.be.none
assert len(tasks) == 1
assert tasks[0].uid == 1
assert tasks[0].description == "t"
assert tasks[0].estimate == 0
assert tasks[0].actual == 0
assert tasks[0].tags is None

def test_get_tasks_gets_tasks_from_all_workspaces(self):
self.asana_api.list_workspaces.return_value = self.dummy_workspace_list
Expand All @@ -99,15 +95,16 @@ def test_get_tasks_gets_tasks_from_all_workspaces(self):
tasks = list(self.asana.get_tasks())

self.asana_api.list_workspaces.assert_called_once_with()
expect(len(tasks)).to.equal(4)
assert len(tasks) == 4

def test_get_tasks_only_gets_tasks_assigned_to_me(self):
self.asana_api.list_workspaces.return_value = self.dummy_workspace_list
self.asana.initialize()

tasks = list(self.asana.get_tasks())

expect(self.asana_api.list_tasks.call_count).to.equal(2)
assert tasks is not None
assert self.asana_api.list_tasks.call_count == 2
self.asana_api.list_tasks.assert_any_call(1, "me")
self.asana_api.list_tasks.assert_any_call(2, "me")

Expand All @@ -117,12 +114,12 @@ def test_get_tasks_returns_empty_list_for_no_workspace(self):

tasks = list(self.asana.get_tasks())

expect(tasks).to.equal([])
assert tasks == []

def test_get_tasks_returns_empty_list_for_no_tasks(self):
self.asana_api.list_workspaces.return_value = self.dummy_workspace_list
self.asana.initialize()

tasks = list(self.asana.get_tasks())

expect(tasks).to.equal([])
assert tasks == []
13 changes: 5 additions & 8 deletions tests/plugins/task/test_nulltask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@
"""Tests for NullTask plugin."""

import unittest
from sure import expect

from pomito.plugins.task import nulltask, TaskPlugin


# flake8: noqa
class NullTaskTests(unittest.TestCase):

"""Tests for NullTask."""

def setUp(self):
self.task = nulltask.NullTask(None)

def test_nulltask_is_a_task_plugin(self):
expect(issubclass(nulltask.NullTask, TaskPlugin)).true
assert issubclass(nulltask.NullTask, TaskPlugin)

def test_nulltask_initialize_should_not_throw(self):
expect(self.task.initialize).when.called.to_not.throw()
self.task.initialize()

def test_nulltask_get_tasks_returns_empty_list(self):
expect(self.task.get_tasks()).to.be.empty
assert len(self.task.get_tasks()) == 0

def test_nulltask_get_tasks_by_filter_returns_empty_list(self):
expect(self.task.get_tasks_by_filter("")).to.be.empty
assert len(self.task.get_tasks_by_filter("")) == 0

def test_nulltask_get_task_by_id_returns_none(self):
expect(self.task.get_task_by_id(1)).to.be.none
assert self.task.get_task_by_id(1) is None
26 changes: 12 additions & 14 deletions tests/plugins/task/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""Tests for the TaskPlugin"""
# -*- coding: utf-8 -*-
"""Tests for the TaskPlugin."""

import unittest
from unittest.mock import MagicMock

from sure import expect

from pomito import task
from pomito.test import FakeTaskPlugin

# pylint: disable=missing-docstring, invalid-name

class TaskPluginTests(unittest.TestCase):
def setUp(self):
self.taskPlugin = FakeTaskPlugin()
Expand All @@ -25,35 +24,34 @@ def test_get_tasks_by_filter_returns_generator_type(self):

tasks = self.taskPlugin.get_tasks_by_filter(None)

expect(tasks).to.be.a(types.GeneratorType)
assert isinstance(tasks, types.GeneratorType)

def test_get_tasks_by_filter_returns_all_tasks_for_no_filter(self):
tasks = list(self.taskPlugin.get_tasks_by_filter(None))

expect(tasks).to.have.length_of(12)
expect(tasks).to.equal(self.task_list)
assert len(tasks) == 12
assert tasks == self.task_list

def test_get_tasks_by_filter_returns_all_tasks_for_star_filter(self):
tasks = list(self.taskPlugin.get_tasks_by_filter("*"))

expect(tasks).to.have.length_of(12)
expect(tasks).to.equal(self.task_list)
assert len(tasks) == 12
assert tasks == self.task_list

def test_get_task_by_id_returns_none_if_no_matching_task(self):
t = self.taskPlugin.get_task_by_id(9)

expect(t).to.be.none
assert t is None

def test_get_task_by_id_returns_task_with_full_id(self):
t = self.taskPlugin.get_task_by_id(100982)

expect(t).to.equal(self.task_list[0])
assert t == self.task_list[0]

def test_get_task_by_id_returns_task_with_matching_idish(self):
t = self.taskPlugin.get_task_by_id(10099)

expect(t).to.equal(self.task_list[1])
assert t == self.task_list[1]

def test_get_task_by_id_throws_if_multiple_tasks_match_idish(self):
expect(self.taskPlugin.get_task_by_id)\
.when.called_with(1009).to.throw(ValueError)
self.assertRaises(ValueError, self.taskPlugin.get_task_by_id, 1009)
Loading

0 comments on commit a941452

Please sign in to comment.