~ddormer/renamer/1125067-add-prepare-method

« back to all changes in this revision

Viewing changes to renamer/test/test_history.py

  • Committer: Jonathan Jacobs
  • Date: 2010-10-24 17:41:36 UTC
  • mfrom: (83.2.23 undo-command)
  • Revision ID: korpse@slipgate.za.net-20101024174136-geqsnh247drgj5hu
Merge lp:~renamer-developers/renamer/undo-command.

Implement persistent history of Renamer actions and undoing them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from axiom.store import Store
 
2
 
 
3
from twisted.python.filepath import FilePath
 
4
from twisted.trial.unittest import TestCase
 
5
 
 
6
from renamer import errors, history, irenamer
 
7
from renamer.plugins.actions import SymlinkAction
 
8
 
 
9
 
 
10
 
 
11
def FakeOptions():
 
12
    return {}
 
13
 
 
14
 
 
15
 
 
16
class FakeAction(object):
 
17
    def do(self, options):
 
18
        pass
 
19
 
 
20
 
 
21
    def undo(self, options):
 
22
        pass
 
23
 
 
24
 
 
25
 
 
26
class HistoryTests(TestCase):
 
27
    """
 
28
    Tests for L{renamer.history.History}.
 
29
    """
 
30
    def setUp(self):
 
31
        self.store = Store()
 
32
        self.history = history.History(store=self.store)
 
33
 
 
34
 
 
35
    def test_newChangeset(self):
 
36
        """
 
37
        L{renamer.history.History.newChangeset} creates a new changeset
 
38
        instance and does not track it immediately.
 
39
        """
 
40
        cs = self.history.newChangeset()
 
41
        self.assertIdentical(type(cs), history.Changeset)
 
42
        self.assertEquals(list(self.history.getChangesets()), [])
 
43
 
 
44
 
 
45
    def test_pruneChangesets(self):
 
46
        """
 
47
        L{renamer.history.History.pruneChangesets} removes empty changesets
 
48
        (changesets without any actions) from the database.
 
49
        """
 
50
        cs = self.history.newChangeset()
 
51
        self.assertEquals(list(self.history.getChangesets()), [])
 
52
 
 
53
        action = cs.newAction(
 
54
            u'fake', FilePath(u'src'), FilePath(u'dst'), verify=False)
 
55
 
 
56
        # Unused action.
 
57
        cs.newAction(
 
58
            u'fake', FilePath(u'src'), FilePath(u'dst'), verify=False)
 
59
 
 
60
        self.assertEquals(list(cs.getActions()), [])
 
61
        self.assertEquals(cs.numActions, 0)
 
62
 
 
63
        def _adapter(action):
 
64
            return FakeAction()
 
65
 
 
66
        cs.do(action, FakeOptions(), _adapter=_adapter)
 
67
 
 
68
        self.assertEquals(
 
69
            list(cs.getActions()), [action])
 
70
        self.assertEquals(cs.numActions, 1)
 
71
        prunedChangesets, prunedActions = self.history.pruneChangesets()
 
72
        self.assertEquals(prunedChangesets, 0)
 
73
        self.assertEquals(prunedActions, 1)
 
74
        self.assertEquals(list(self.history.getChangesets()), [cs])
 
75
 
 
76
        cs.undo(action, FakeOptions(), _adapter=_adapter)
 
77
        self.assertEquals(list(cs.getActions()), [])
 
78
        self.assertEquals(cs.numActions, 0)
 
79
        prunedChangesets, prunedActions = self.history.pruneChangesets()
 
80
        self.assertEquals(prunedChangesets, 1)
 
81
        self.assertEquals(prunedActions, 0)
 
82
        self.assertEquals(list(self.history.getChangesets()), [])
 
83
 
 
84
 
 
85
 
 
86
class ChangesetTests(TestCase):
 
87
    """
 
88
    Tests for L{renamer.history.Changeset}.
 
89
    """
 
90
    def setUp(self):
 
91
        self.store = Store()
 
92
        self.history = history.History(store=self.store)
 
93
 
 
94
 
 
95
    def test_newInvalidAction(self):
 
96
        """
 
97
        L{renamer.history.Changeset.newAction} raises
 
98
        L{renamer.errors.NoSuchAction} if the action name specified does not
 
99
        refer to a valid action.
 
100
        """
 
101
        cs = self.history.newChangeset()
 
102
        self.assertRaises(errors.NoSuchAction,
 
103
            cs.newAction, 'THIS_IS_NOT_REAL', FilePath(u'a'), FilePath(u'b'))
 
104
 
 
105
 
 
106
    def test_representations(self):
 
107
        """
 
108
        L{renamer.history.Changeset.asHumanly} returns a human-readable and
 
109
        accurate representation of a changeset.
 
110
 
 
111
        L{renamer.history.Changeset.__repr__} returns a useful and accurate
 
112
        representation of a changeset.
 
113
        """
 
114
        cs = self.history.newChangeset()
 
115
 
 
116
        self.assertTrue(
 
117
            cs.asHumanly().startswith(
 
118
                u'Changeset with 0 action(s) ('))
 
119
 
 
120
        self.assertEquals(
 
121
            repr(cs),
 
122
            '<Changeset 0 action(s) created=%r modified=%r>' % (
 
123
                cs.created, cs.modified))
 
124
 
 
125
        action = cs.newAction(
 
126
            u'fake', FilePath(u'src'), FilePath(u'dst'), verify=False)
 
127
 
 
128
        def _adapter(action):
 
129
            return FakeAction()
 
130
 
 
131
        cs.do(action, FakeOptions(), _adapter=_adapter)
 
132
 
 
133
        self.assertTrue(
 
134
            cs.asHumanly().startswith(
 
135
                u'Changeset with 1 action(s) ('))
 
136
 
 
137
        self.assertEquals(
 
138
            repr(cs),
 
139
            '<Changeset 1 action(s) created=%r modified=%r>' % (
 
140
                cs.created, cs.modified))
 
141
 
 
142
 
 
143
 
 
144
class ActionTests(TestCase):
 
145
    """
 
146
    Tests for L{renamer.history.Action}.
 
147
    """
 
148
    def setUp(self):
 
149
        self.store = Store()
 
150
        self.history = history.History(store=self.store)
 
151
 
 
152
 
 
153
    def test_adaption(self):
 
154
        """
 
155
        Adapting a L{renamer.history.Action} object to
 
156
        L{renamer.irenamer.IRenamingAction} results in an object implementing
 
157
        C{IRenamingAction} that can perform forward and reverse actions.
 
158
        """
 
159
        cs = self.history.newChangeset()
 
160
        action = cs.newAction(u'symlink', FilePath(u'src'), FilePath(u'dst'))
 
161
        a = irenamer.IRenamingAction(action)
 
162
        self.assertIdentical(type(a), SymlinkAction)
 
163
        self.assertTrue(irenamer.IRenamingAction.providedBy(type(a)))
 
164
 
 
165
 
 
166
    def test_representations(self):
 
167
        """
 
168
        L{renamer.history.Action.asHumanly} returns a human-readable and
 
169
        accurate representation of an action.
 
170
 
 
171
        L{renamer.history.Action.__repr__} returns a useful and accurate
 
172
        representation of an action.
 
173
        """
 
174
        cs = self.history.newChangeset()
 
175
        src = FilePath(u'src')
 
176
        dst = FilePath(u'dst')
 
177
        action = cs.newAction(u'fake', src, dst, verify=False)
 
178
 
 
179
        self.assertTrue(
 
180
            action.asHumanly().startswith(
 
181
                u'Fake: %s => %s (' % (src.path, dst.path)))
 
182
 
 
183
        self.assertEquals(
 
184
            repr(action),
 
185
            '<Action name=%r src=%r dst=%r created=%r>' % (
 
186
                action.name, action.src, action.dst, action.created))