def test_content_equal(self): a = RawTextWidget.add_root(self.widgy_site, text='a') b = RawTextWidget.add_root(self.widgy_site, text='b') self.assertFalse(a.equal(b)) b.text = 'a' b.save() self.assertTrue(a.equal(b))
def test_get_history(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commits = reversed([tracker.commit() for i in range(6)]) self.assertSequenceEqual(list(tracker.get_history()), list(commits))
def versioned_model(self): node = RawTextWidget.add_root(widgy_site, text=self.string_to_look_for).node vt = VersionTracker.objects.create(working_copy=node) vt.commit() return VersionedPage.objects.create(version_tracker=vt)
def test_trees_unequal_depth(self): a = Bucket.add_root(self.widgy_site).add_child(self.widgy_site, RawTextWidget, text='a') b = RawTextWidget.add_root(self.widgy_site, text='a') self.assertFalse(a.node.trees_equal(b.node))
def test_prefetch_commits(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) user = User.objects.create() commits = reversed([tracker.commit(user=user) for i in range(6)]) with self.assertNumQueries(1): history = tracker.get_history_list() for commit in history: # root_node and author should be prefetched too commit.root_node.pk commit.author.pk self.assertEqual(list(commits), history) tracker = VersionTracker.objects.create( working_copy=RawTextWidget.add_root(self.widgy_site).node) self.assertEqual(tracker.get_history_list(), [])
def versioned_model(self): node = RawTextWidget.add_root(widgy_site, text=self.string_to_look_for).node vt = VersionTracker.objects.create(working_copy=node) vt.commit() return VersionedPage.objects.create( version_tracker=vt )
def test_versioned_override(self): owner = self.versioned_model() override_string = uuid.uuid4().hex override = RawTextWidget.add_root(widgy_site, text=override_string).node rendered = self.render({'owner': owner, 'field_name': 'version_tracker', 'root_node_override': override}) self.assertNotIn(self.string_to_look_for, rendered) self.assertIn(override_string, rendered)
def test_prefetch_commits(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) user = User.objects.create() commits = reversed([tracker.commit(user=user) for i in range(6)]) with self.assertNumQueries(1): history = tracker.get_history_list() for commit in history: # root_node and author should be prefetched too commit.root_node.pk commit.author.pk self.assertEqual(list(commits), history) tracker = VersionTracker.objects.create( working_copy=RawTextWidget.add_root(self.widgy_site).node ) self.assertEqual(tracker.get_history_list(), [])
def test_old_contents_cant_change(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit = tracker.commit() widget = commit.root_node.content widget.text = 'changed' with self.assertRaises(InvalidOperation): widget.save() self.assertEquals(Node.objects.get(pk=commit.root_node.pk).content.text, 'first') with self.assertRaises(InvalidOperation): widget.delete()
def test_old_contents_cant_change(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit = tracker.commit() widget = commit.root_node.content widget.text = 'changed' with self.assertRaises(InvalidOperation): widget.save() self.assertEquals( Node.objects.get(pk=commit.root_node.pk).content.text, 'first') with self.assertRaises(InvalidOperation): widget.delete()
def test_versioned_override(self): owner = self.versioned_model() override_string = uuid.uuid4().hex override = RawTextWidget.add_root(widgy_site, text=override_string).node rendered = self.render({ 'owner': owner, 'field_name': 'version_tracker', 'root_node_override': override }) self.assertNotIn(self.string_to_look_for, rendered) self.assertIn(override_string, rendered)
def test_commit(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit1 = tracker.commit() self.assertNotEqual(tracker.working_copy, tracker.head.root_node) textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() commit2 = tracker.commit() self.assertEqual(commit1.root_node.content.text, 'first') self.assertEqual(commit2.root_node.content.text, 'second') self.assertEqual(commit2.parent, commit1) self.assertEqual(tracker.head, commit2)
def test_frozen_node(self): c = RawTextWidget.add_root(self.widgy_site) node = c.node node.is_frozen = True node.save() c.text = 'asdf' with self.assertRaises(InvalidOperation): c.save() with self.assertRaises(InvalidOperation): c.delete() self.assertEqual(RawTextWidget.objects.get(pk=c.pk).text, '') with self.assertRaises(InvalidOperation): node.delete()
def test_revert(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit1 = tracker.commit() self.assertNotEqual(tracker.working_copy, tracker.head.root_node) textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() commit2 = tracker.commit() commit3 = tracker.revert_to(commit1) textwidget_content = tracker.working_copy.content textwidget_content.text = 'fourth' textwidget_content.save() commit4 = tracker.commit() self.assertEqual(['fourth', 'first', 'second', 'first'], [i.root_node.content.text for i in tracker.get_history()])
def test_frozen_db_is_canonical(self): # I'm not sure if this failure should be expected or not. Should a node # always recheck the database value? Or, should we use a database # trigger to prevent modifications at the db level? root_node = RawTextWidget.add_root(self.widgy_site, text='asdf') a = Node.objects.get(pk=root_node.pk).content b = Node.objects.get(pk=root_node.pk).content # Node has to be set before we set is_frozen, otherwise the call to # delete will refetch the node. a.node b.node a.node.is_frozen = True a.save() # Even though the b _instance_ isn't frozen, the entry in the database # is. It would be ok if this was a database error instead of # InvalidOperation, like if a BEFORE UPDATE trigger prevented an # update. with self.assertRaises(InvalidOperation): b.delete()
def test_reset(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit1 = tracker.commit() textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() tracker.reset() # the content should get deleted self.assertFalse(RawTextWidget.objects.filter(pk=textwidget_content.pk).exists()) textwidget_content = tracker.working_copy.content self.assertEqual(textwidget_content.text, 'first') # make sure working_copy is still editable textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() tracker.reset() textwidget_content = tracker.working_copy.content self.assertEqual(textwidget_content.text, 'first')
def test_revert(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit1 = tracker.commit() self.assertNotEqual(tracker.working_copy, tracker.head.root_node) textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() commit2 = tracker.commit() commit3 = tracker.revert_to(commit1) textwidget_content = tracker.working_copy.content textwidget_content.text = 'fourth' textwidget_content.save() commit4 = tracker.commit() self.assertEqual( ['fourth', 'first', 'second', 'first'], [i.root_node.content.text for i in tracker.get_history()])
def test_frozen_node_raw(self): # even the treebeard methods called directly on the node should be frozen node = RawTextWidget.add_root(self.widgy_site).node node.is_frozen = True node.save() # 0 queries ensures that the exception is raised before any # modification takes place with self.assertNumQueries(0): with self.assertRaises(InvalidOperation): node.delete() with self.assertRaises(InvalidOperation): node.add_child() with self.assertRaises(InvalidOperation): node.add_child() with self.assertRaises(InvalidOperation): node.add_sibling() with self.assertRaises(InvalidOperation): node.move(node)
def test_reset(self): root_node = RawTextWidget.add_root(self.widgy_site, text='first').node tracker = VersionTracker.objects.create(working_copy=root_node) commit1 = tracker.commit() textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() tracker.reset() # the content should get deleted self.assertFalse( RawTextWidget.objects.filter(pk=textwidget_content.pk).exists()) textwidget_content = tracker.working_copy.content self.assertEqual(textwidget_content.text, 'first') # make sure working_copy is still editable textwidget_content = tracker.working_copy.content textwidget_content.text = 'second' textwidget_content.save() tracker.reset() textwidget_content = tracker.working_copy.content self.assertEqual(textwidget_content.text, 'first')
def test_edit_content(self): content = RawTextWidget.add_root(self.widgy_site) content.text = 'hello' content.save() def doit(): url = content.to_json(self.widgy_site)['url'] return self.put(url, { 'attributes': {'text': 'goodbye'}, }) def fail(): resp = doit() self.assertEqual(resp.status_code, 403) self.assertEqual(RawTextWidget.objects.get().text, 'hello') def win(): resp = doit() self.assertEqual(resp.status_code, 200) self.assertEqual(RawTextWidget.objects.get().text, 'goodbye') # reset RawTextWidget.objects.update(text='hello') self.as_different_types_of_user(('change', RawTextWidget), fail, win)
def test_trees_unequal_content_type(self): a = CantGoAnywhereWidget.add_root(self.widgy_site) b = RawTextWidget.add_root(self.widgy_site, text='a') self.assertFalse(a.node.trees_equal(b.node))
def plain_model(self): node = RawTextWidget.add_root(widgy_site, text=self.string_to_look_for).node return HasAWidgy.objects.create( widgy=node, )
def test_trees_unequal_content(self): a = RawTextWidget.add_root(self.widgy_site, text='b') b = RawTextWidget.add_root(self.widgy_site, text='a') self.assertFalse(a.node.trees_equal(b.node))
def test_trees_equal_equal(self): a = RawTextWidget.add_root(self.widgy_site, text='a') b = RawTextWidget.add_root(self.widgy_site, text='a') self.assertTrue(a.node.trees_equal(b.node))
def make_tracker(site, vt_class=ReviewedVersionTracker): root_node = RawTextWidget.add_root(site, text='first').node tracker = vt_class.objects.create(working_copy=root_node) return tracker
def plain_model(self): node = RawTextWidget.add_root(widgy_site, text=self.string_to_look_for).node return HasAWidgy.objects.create(widgy=node, )