~pedronis/u1db/sync_integration_tests

« back to all changes in this revision

Viewing changes to u1todo/test_u1todo.py

  • Committer: Eric Casteleijn
  • Date: 2012-04-18 18:21:02 UTC
  • mto: This revision was merged to the branch mainline in revision 249.
  • Revision ID: eric.casteleijn@canonical.com-20120418182102-3kvy5oob1sesfysa
first stab at the u1todo backend.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from testtools import TestCase
 
2
from u1todo import Task, TodoStore, INDEXES, DONE
 
3
from u1db.backends import inmemory
 
4
 
 
5
 
 
6
class TodoStoreTestCase(TestCase):
 
7
 
 
8
    def setUp(self):
 
9
        super(TodoStoreTestCase, self).setUp()
 
10
        self.db = inmemory.InMemoryDatabase("u1todo")
 
11
 
 
12
    def test_initialize_db(self):
 
13
        """Creates indexes."""
 
14
        TodoStore(self.db)
 
15
        self.assertEqual(INDEXES, dict(self.db.list_indexes()))
 
16
 
 
17
    def test_indexes_are_added(self):
 
18
        """New indexes are added when a new store is created."""
 
19
        TodoStore(self.db)
 
20
        INDEXES['foo'] = ['bar']
 
21
        self.assertNotIn('foo', dict(self.db.list_indexes()))
 
22
        TodoStore(self.db)
 
23
        self.assertIn('foo', dict(self.db.list_indexes()))
 
24
 
 
25
    def test_indexes_are_updated(self):
 
26
        """Indexes are updated when a new store is created."""
 
27
        TodoStore(self.db)
 
28
        new_expression = ['newtags']
 
29
        INDEXES['tags'] = new_expression
 
30
        self.assertNotEqual(
 
31
            new_expression, dict(self.db.list_indexes())['tags'])
 
32
        TodoStore(self.db)
 
33
        self.assertEqual(new_expression, dict(self.db.list_indexes())['tags'])
 
34
 
 
35
    def test_tag_task(self):
 
36
        store = TodoStore(self.db)
 
37
        task = Task(self.db)
 
38
        tag = "you're it"
 
39
        store.tag_task(task, [tag])
 
40
        self.assertEqual([tag], task.tags)
 
41
 
 
42
 
 
43
class TaskTestCase(TestCase):
 
44
    """Tests for Task."""
 
45
 
 
46
    def setUp(self):
 
47
        super(TaskTestCase, self).setUp()
 
48
        self.db = inmemory.InMemoryDatabase("u1todo")
 
49
 
 
50
    def test_task(self):
 
51
        """Initializing a task generates a doc_id."""
 
52
        task = Task(self.db)
 
53
        self.assertIsNotNone(task.doc_id)
 
54
 
 
55
    def test_set_title(self):
 
56
        """Changing the title is persistent."""
 
57
        task = Task(self.db)
 
58
        title = "new task"
 
59
        task.title = title
 
60
        new_task = Task(self.db, doc_id=task.doc_id)
 
61
        self.assertEqual(title, new_task.title)
 
62
 
 
63
    def test_set_done(self):
 
64
        task = Task(self.db)
 
65
        self.assertFalse(task.done)
 
66
        task.done = DONE
 
67
        self.assertTrue(task.done)
 
68
 
 
69
    def test_set_done_persists(self):
 
70
        task = Task(self.db)
 
71
        self.assertFalse(task.done)
 
72
        task.done = DONE
 
73
        new_task = Task(self.db, doc_id=task.doc_id)
 
74
        self.assertTrue(new_task.done)
 
75
 
 
76
    def test_tags(self):
 
77
        """Tags property returns a tuple."""
 
78
        task = Task(self.db)
 
79
        self.assertEqual([], task.tags)
 
80
 
 
81
    def set_tags(self):
 
82
        task = Task(self.db)
 
83
        task.tags = ["foo", "bar"]
 
84
        self.assertEqual(["foo", "bar"], task.tags)
 
85
 
 
86
    def set_tags_persists(self):
 
87
        """Tags are saved to the database."""
 
88
        task = Task(self.db)
 
89
        task.tags = ("foo", "bar")
 
90
        new_task = Task(self.db, doc_id=task.doc_id)
 
91
        self.assertEqual(["foo", "bar"], new_task.tags)
 
92
 
 
93
    def test_add_tag(self):
 
94
        """Tag is added to task's tags."""
 
95
        task = Task(self.db)
 
96
        task.add_tag("foo")
 
97
        self.assertEqual(["foo"], task.tags)
 
98
 
 
99
    def test_add_tag_persists(self):
 
100
        """Tag is saved to the database."""
 
101
        task = Task(self.db)
 
102
        task.add_tag("foo")
 
103
        new_task = Task(self.db, doc_id=task.doc_id)
 
104
        self.assertEqual(["foo"], new_task.tags)
 
105
 
 
106
    def test_remove_tag(self):
 
107
        """Tag is removed from task's tags."""
 
108
        task = Task(self.db)
 
109
        task.add_tag("foo")
 
110
        task.add_tag("bar")
 
111
        self.assertEqual(["foo", "bar"], task.tags)
 
112
        task.remove_tag("foo")
 
113
        self.assertEqual(["bar"], task.tags)