Back to index

plone3  3.1.7
testChangeSet.py
Go to the documentation of this file.
00001 #
00002 # CMFDiffTool tests
00003 #
00004 
00005 import BaseTestCase
00006 from Products.CMFDiffTool.ChangeSet import ChangeSet
00007 from Products.CMFDiffTool.ChangeSet import BaseChangeSet
00008 from Acquisition import aq_base
00009 
00010 class TestChangeSet(BaseTestCase.BaseTestCase):
00011     """Tests for ChangeSet objects"""
00012 
00013     def afterSetUp(self):
00014         self.p_diff = self.portal.portal_diff
00015         cs = BaseChangeSet('my_changeset')
00016         # ChangeSet needs an acquisition wrapper
00017         self.cs = cs.__of__(self.portal)
00018 
00019     def testInterface(self):
00020         """Ensure that tool instances implement the portal_diff interface"""
00021         from Products.CMFDiffTool.interfaces.IChangeSet import IChangeSet
00022         self.failUnless(IChangeSet.isImplementedByInstancesOf(ChangeSet))
00023         self.failUnless(IChangeSet.isImplementedByInstancesOf(BaseChangeSet))
00024 
00025     def setupTestObjects(self):
00026         self.folder.invokeFactory('Document','doc1', title='My Title')
00027         self.folder.manage_pasteObjects(
00028                                      self.folder.manage_copyObjects(['doc1']))
00029         self.portal.portal_diff.setDiffField('Document', 'Title', 'Field Diff')
00030 
00031     def setupTestFolders(self):
00032         self.folder.invokeFactory('Folder','folder1', title='My Folder Title')
00033         self.folder.folder1.invokeFactory('Document','doc1', title='My Title1')
00034         self.folder.folder1.invokeFactory('Document','doc2', title='My Title2')
00035         self.folder.folder1.invokeFactory('Document','doc3', title='My Title3')
00036         self.folder.manage_pasteObjects(
00037                                   self.folder.manage_copyObjects(['folder1']))
00038         self.portal.portal_diff.setDiffField('Document', 'Title', 'Field Diff')
00039         self.portal.portal_diff.setDiffField('Folder', 'title', 'Field Diff')
00040 
00041     def testChangeSetUnchanged(self):
00042         self.setupTestObjects()
00043         self.cs.computeDiff(self.folder.doc1, self.folder.copy_of_doc1)
00044         diffs = self.cs.getDiffs()
00045         self.assertEqual(len(diffs), 1)
00046         self.failUnless(diffs[0].same)
00047 
00048     def testChangeSetChanged(self):
00049         self.setupTestObjects()
00050         self.folder.copy_of_doc1.setTitle('My New Title')
00051         self.cs.computeDiff(self.folder.doc1, self.folder.copy_of_doc1)
00052         diffs = self.cs.getDiffs()
00053         self.assertEqual(len(diffs), 1)
00054         self.failIf(diffs[0].same)
00055         self.assertEqual(diffs[0].ndiff(), '- My Title\n+ My New Title')
00056 
00057     def testChangeSetFolderUnchanged(self):
00058         self.setupTestFolders()
00059         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00060         diffs = self.cs.getDiffs()
00061         self.assertEqual(len(diffs), 1)
00062         self.failUnless(diffs[0].same)
00063         sub_cs = self.cs.getSubDiffs()
00064         self.assertEqual(len(sub_cs), 3)
00065         for i in range(len(sub_cs)):
00066             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00067             sub_diffs = sub_cs[i].getDiffs()
00068             self.assertEqual(len(sub_diffs), 1)
00069             self.failUnless(sub_cs[0].same)
00070 
00071     def testChangeSetFolderChanged(self):
00072         self.setupTestFolders()
00073         self.folder.copy_of_folder1.setTitle('My New Title')
00074         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00075         diffs = self.cs.getDiffs()
00076         self.assertEqual(len(diffs), 1)
00077         self.failIf(diffs[0].same)
00078         self.assertEqual(diffs[0].ndiff(), '- My Folder Title\n+ My New Title')
00079         self.failIf(self.cs._added)
00080         self.failIf(self.cs._removed)
00081         sub_cs = self.cs.getSubDiffs()
00082         self.assertEqual(len(sub_cs), 3)
00083         # The sub diffs should show no changes
00084         for i in range(len(sub_cs)):
00085             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00086             sub_diffs = sub_cs[i].getDiffs()
00087             self.assertEqual(len(sub_diffs), 1)
00088             self.failUnless(sub_diffs[0].same)
00089 
00090     def testChangeSetFolderDocChanged(self):
00091         self.setupTestFolders()
00092         self.folder.copy_of_folder1.doc1.setTitle('My New Title')
00093         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00094         diffs = self.cs.getDiffs()
00095         self.assertEqual(len(diffs), 1)
00096         self.failUnless(diffs[0].same)
00097         self.failIf(self.cs._added)
00098         self.failIf(self.cs._removed)
00099         sub_cs = self.cs.getSubDiffs()
00100         self.assertEqual(len(sub_cs), 3)
00101         for i in range(len(sub_cs)):
00102             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00103             sub_diffs = sub_cs[i].getDiffs()
00104             self.assertEqual(len(sub_diffs), 1)
00105             # doc1 has changed
00106             if sub_cs[i].getId() == 'doc1':
00107                 self.failIf(sub_diffs[0].same)
00108                 self.assertEqual(sub_diffs[0].ndiff(),
00109                                                 '- My Title1\n+ My New Title')
00110             else:
00111                 self.failUnless(sub_diffs[0].same)
00112 
00113     def testChangeSetFolderDocRemoved(self):
00114         self.setupTestFolders()
00115         self.folder.copy_of_folder1.manage_delObjects('doc1')
00116         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00117         diffs = self.cs.getDiffs()
00118         self.assertEqual(len(diffs), 1)
00119         self.failUnless(diffs[0].same)
00120         sub_cs = self.cs.getSubDiffs()
00121         # We only have two potentially changed objects
00122         self.assertEqual(len(sub_cs), 2)
00123         # The sub diffs should show no changes
00124         for i in range(len(sub_cs)):
00125             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00126             sub_diffs = sub_cs[i].getDiffs()
00127             self.assertEqual(len(sub_diffs), 1)
00128             self.failUnless(sub_diffs[0].same)
00129         self.failIf(self.cs._added)
00130         self.assertEqual(list(self.cs._removed), ['doc1'])
00131 
00132     def testChangeSetFolderDocAdded(self):
00133         self.setupTestFolders()
00134         self.folder.copy_of_folder1.invokeFactory('Document','doc4',
00135                                                          title='My Doc Title')
00136         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00137         diffs = self.cs.getDiffs()
00138         self.assertEqual(len(diffs), 1)
00139         self.failUnless(diffs[0].same)
00140         sub_cs = self.cs.getSubDiffs()
00141         self.assertEqual(len(sub_cs), 3)
00142         # The sub diffs should show no changes
00143         for i in range(len(sub_cs)):
00144             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00145             sub_diffs = sub_cs[i].getDiffs()
00146             self.assertEqual(len(sub_diffs), 1)
00147             self.failUnless(sub_diffs[0].same)
00148         self.failIf(self.cs._removed)
00149         self.assertEqual(list(self.cs._added), ['doc4'])
00150 
00151     def testChangeSetFolderReordered(self):
00152         self.setupTestFolders()
00153         if hasattr(aq_base(self.folder.copy_of_folder1),'moveObjectsToTop'):
00154             self.folder.copy_of_folder1.moveObjectsToTop(['doc3'])
00155         elif hasattr(aq_base(self.folder.copy_of_folder1),
00156                                                         'moveObjectsByDelta'):
00157             self.folder.copy_of_folder1.moveObjectsByDelta(['doc3'], -3)
00158         else:
00159             # We don't have an orderable folder give up
00160             return
00161         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00162         diffs = self.cs.getDiffs()
00163         self.assertEqual(len(diffs), 1)
00164         self.failUnless(diffs[0].same)
00165         self.failIf(self.cs._added)
00166         self.failIf(self.cs._removed)
00167         sub_cs = self.cs.getSubDiffs()
00168         self.assertEqual(len(sub_cs), 3)
00169         # The sub diffs should show no changes
00170         for i in range(len(sub_cs)):
00171             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00172             sub_diffs = sub_cs[i].getDiffs()
00173             self.assertEqual(len(sub_diffs), 1)
00174             self.failUnless(sub_diffs[0].same)
00175         # XXX we need an explicit way of noting reorders
00176 
00177     def testChangeSetFolderComplex(self):
00178         self.setupTestFolders()\
00179         # Add a new sub object
00180         self.folder.copy_of_folder1.invokeFactory('Document','doc4',
00181                                                          title='My Doc Title')
00182         # Delete a sub object
00183         self.folder.copy_of_folder1.manage_delObjects('doc2')
00184         # Change one object
00185         self.folder.copy_of_folder1.doc3.setTitle('My New Title')
00186         # Change the folder itself
00187         self.folder.copy_of_folder1.setTitle('My New Title')
00188         # Move the changed object
00189         if hasattr(aq_base(self.folder.copy_of_folder1),'moveObjectsToTop'):
00190             self.folder.copy_of_folder1.moveObjectsToTop(['doc3'])
00191         elif hasattr(aq_base(self.folder.copy_of_folder1),
00192                                                         'moveObjectsByDelta'):
00193             self.folder.copy_of_folder1.moveObjectsByDelta(['doc3'], -3)
00194         else:
00195             # We don't have an orderable folder give up
00196             return
00197 
00198         self.cs.computeDiff(self.folder.folder1, self.folder.copy_of_folder1)
00199         diffs = self.cs.getDiffs()
00200         self.assertEqual(len(diffs), 1)
00201         self.failIf(diffs[0].same)
00202         self.assertEqual(diffs[0].ndiff(), '- My Folder Title\n+ My New Title')
00203         self.assertEqual(list(self.cs._added), ['doc4'])
00204         self.assertEqual(list(self.cs._removed), ['doc2'])
00205         sub_cs = self.cs.getSubDiffs()
00206         # We only have two potentially changed objects
00207         self.assertEqual(len(sub_cs), 2)
00208         # The sub diffs should show no changes
00209         for i in range(len(sub_cs)):
00210             self.failUnless(isinstance(sub_cs[i], BaseChangeSet))
00211             sub_diffs = sub_cs[i].getDiffs()
00212             self.assertEqual(len(sub_diffs), 1)
00213             if sub_cs[i].getId() == 'doc3':
00214                 self.failIf(sub_diffs[0].same)
00215                 self.assertEqual(sub_diffs[0].ndiff(),
00216                                                 '- My Title3\n+ My New Title')
00217             else:
00218                 self.failUnless(sub_diffs[0].same)
00219         # XXX we need an explicit way of noting reorders
00220 
00221 
00222 def test_suite():
00223     import unittest
00224     suite = unittest.TestSuite()
00225     suite.addTest(unittest.makeSuite(TestChangeSet))
00226     return suite
00227