Skip to content

Commit

Permalink
test_storage: renamed 'p' to 'st'.
Browse files Browse the repository at this point in the history
'p' is reserved when you are in a pdb.
  • Loading branch information
mauritsvanrees committed Feb 13, 2019
1 parent c9360e9 commit 0c98367
Showing 1 changed file with 108 additions and 108 deletions.
216 changes: 108 additions & 108 deletions plone/app/redirector/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,179 +12,179 @@ class TestStorage(unittest.TestCase):

def test_storage_one_redirect(self):
# Add one redirect
p = RedirectionStorage()
self.assertFalse(p.has_path('/foo'))
p.add('/foo', '/bar')
self.assertTrue(p.has_path('/foo'))
self.assertEqual(p.get('/foo'), '/bar')
self.assertFalse(p.has_path('/bar'))
self.assertListEqual(p.redirects('/bar'), ['/foo'])
st = RedirectionStorage()
self.assertFalse(st.has_path('/foo'))
st.add('/foo', '/bar')
self.assertTrue(st.has_path('/foo'))
self.assertEqual(st.get('/foo'), '/bar')
self.assertFalse(st.has_path('/bar'))
self.assertListEqual(st.redirects('/bar'), ['/foo'])

def test_storage_no_slash(self):
# Standard Plone will created redirects with key
# /plone-site-id/some/path.
# But a slash at the beginning is not mandatory.
p = RedirectionStorage()
self.assertFalse(p.has_path('foo'))
p.add('foo', 'bar')
self.assertTrue(p.has_path('foo'))
self.assertEqual(p.get('foo'), 'bar')
self.assertFalse(p.has_path('bar'))
self.assertListEqual(p.redirects('bar'), ['foo'])
st = RedirectionStorage()
self.assertFalse(st.has_path('foo'))
st.add('foo', 'bar')
self.assertTrue(st.has_path('foo'))
self.assertEqual(st.get('foo'), 'bar')
self.assertFalse(st.has_path('bar'))
self.assertListEqual(st.redirects('bar'), ['foo'])

def test_storage_nested(self):
# Since Plone will created redirects with key
# /plone-site-id/some/path, testing with multiple slashes seems wise.
p = RedirectionStorage()
self.assertFalse(p.has_path('/plone/some/path'))
p.add('/plone/some/path', '/plone/a/different/path')
self.assertTrue(p.has_path('/plone/some/path'))
self.assertEqual(p.get('/plone/some/path'), '/plone/a/different/path')
self.assertFalse(p.has_path('/plone/a/different/path'))
self.assertListEqual(p.redirects('/plone/a/different/path'), ['/plone/some/path'])
st = RedirectionStorage()
self.assertFalse(st.has_path('/plone/some/path'))
st.add('/plone/some/path', '/plone/a/different/path')
self.assertTrue(st.has_path('/plone/some/path'))
self.assertEqual(st.get('/plone/some/path'), '/plone/a/different/path')
self.assertFalse(st.has_path('/plone/a/different/path'))
self.assertListEqual(st.redirects('/plone/a/different/path'), ['/plone/some/path'])

def test_storage_trailing_slash(self):
# trailing slashes are ignored
p = RedirectionStorage()
self.assertFalse(p.has_path('/foo/'))
p.add('/foo', '/bar')
self.assertTrue(p.has_path('/foo/'))
self.assertEqual(p.get('/foo/'), '/bar')
self.assertListEqual(p.redirects('/bar/'), ['/foo'])
st = RedirectionStorage()
self.assertFalse(st.has_path('/foo/'))
st.add('/foo', '/bar')
self.assertTrue(st.has_path('/foo/'))
self.assertEqual(st.get('/foo/'), '/bar')
self.assertListEqual(st.redirects('/bar/'), ['/foo'])

# This goes the other way around too
self.assertFalse(p.has_path('/quux'))
p.add('/quux/', '/baaz/')
self.assertTrue(p.has_path('/quux'))
self.assertEqual(p.get('/quux'), '/baaz')
self.assertListEqual(p.redirects('/baaz'), ['/quux'])
self.assertFalse(st.has_path('/quux'))
st.add('/quux/', '/baaz/')
self.assertTrue(st.has_path('/quux'))
self.assertEqual(st.get('/quux'), '/baaz')
self.assertListEqual(st.redirects('/baaz'), ['/quux'])

def test_storage_two_redirects(self):
# Add multiple redirects.
p = RedirectionStorage()
p.add('/foo', '/bar')
p.add('/baz', '/bar')
self.assertTrue(p.has_path('/baz'))
self.assertEqual(p.get('/baz'), '/bar')
self.assertListEqual(sorted(p.redirects('/bar')), ['/baz', '/foo'])
st = RedirectionStorage()
st.add('/foo', '/bar')
st.add('/baz', '/bar')
self.assertTrue(st.has_path('/baz'))
self.assertEqual(st.get('/baz'), '/bar')
self.assertListEqual(sorted(st.redirects('/bar')), ['/baz', '/foo'])

def test_storage_update_redirect(self):
# Update a redirect
p = RedirectionStorage()
p.add('/foo', '/bar')
p.add('/baz', '/bar')
p.add('/foo', '/quux')
self.assertTrue(p.has_path('/foo'))
self.assertEqual(p.get('/foo'), '/quux')
self.assertListEqual(p.redirects('/bar'), ['/baz'])
self.assertListEqual(p.redirects('/quux'), ['/foo'])
st = RedirectionStorage()
st.add('/foo', '/bar')
st.add('/baz', '/bar')
st.add('/foo', '/quux')
self.assertTrue(st.has_path('/foo'))
self.assertEqual(st.get('/foo'), '/quux')
self.assertListEqual(st.redirects('/bar'), ['/baz'])
self.assertListEqual(st.redirects('/quux'), ['/foo'])

def test_storage_remove_redirect(self):
# Remove a redirect
p = RedirectionStorage()
p.add('/foo', '/bar')
p.remove('/foo')
self.assertFalse(p.has_path('/foo'))
self.assertEqual(p.get('/foo', default='_notfound_'), '_notfound_')
self.assertListEqual(p.redirects('/bar'), [])
st = RedirectionStorage()
st.add('/foo', '/bar')
st.remove('/foo')
self.assertFalse(st.has_path('/foo'))
self.assertEqual(st.get('/foo', default='_notfound_'), '_notfound_')
self.assertListEqual(st.redirects('/bar'), [])

def test_storage_chain(self):
# Update a redirect in a chain
p = RedirectionStorage()
p.add('/fred', '/foo')
self.assertEqual(p.get('/fred'), '/foo')
self.assertListEqual(sorted(p.redirects('/foo')), ['/fred'])

p.add('/fred', '/barney')
self.assertEqual(p.get('/fred'), '/barney')
self.assertListEqual(sorted(p.redirects('/foo')), [])
self.assertListEqual(sorted(p.redirects('/barney')), ['/fred'])

p.add('/barney', '/wilma')
self.assertEqual(p.get('/fred'), '/wilma')
self.assertEqual(p.get('/barney'), '/wilma')
st = RedirectionStorage()
st.add('/fred', '/foo')
self.assertEqual(st.get('/fred'), '/foo')
self.assertListEqual(sorted(st.redirects('/foo')), ['/fred'])

st.add('/fred', '/barney')
self.assertEqual(st.get('/fred'), '/barney')
self.assertListEqual(sorted(st.redirects('/foo')), [])
self.assertListEqual(sorted(st.redirects('/barney')), ['/fred'])

st.add('/barney', '/wilma')
self.assertEqual(st.get('/fred'), '/wilma')
self.assertEqual(st.get('/barney'), '/wilma')
self.assertListEqual(
sorted(p.redirects('/wilma')), ['/barney', '/fred']
sorted(st.redirects('/wilma')), ['/barney', '/fred']
)
self.assertListEqual(sorted(p.redirects('/barney')), [])
self.assertListEqual(sorted(st.redirects('/barney')), [])

def test_storage_destroy_target(self):
# Destroy the target of a redirect
p = RedirectionStorage()
p.add('/fred', '/barney')
p.add('/barney', '/wilma')
p.destroy('/wilma')
self.assertFalse(p.has_path('/barney'))
self.assertFalse(p.has_path('/fred'))
self.assertListEqual(p.redirects('/wilma'), [])
st = RedirectionStorage()
st.add('/fred', '/barney')
st.add('/barney', '/wilma')
st.destroy('/wilma')
self.assertFalse(st.has_path('/barney'))
self.assertFalse(st.has_path('/fred'))
self.assertListEqual(st.redirects('/wilma'), [])

def test_storage_iterator(self):
# We can get an iterator over all existing paths
p = RedirectionStorage()
self.assertListEqual(sorted(iter(p)), [])
st = RedirectionStorage()
self.assertListEqual(sorted(iter(st)), [])

# Add one
p.add('/baz', '/bar')
self.assertListEqual(sorted(iter(p)), ['/baz'])
st.add('/baz', '/bar')
self.assertListEqual(sorted(iter(st)), ['/baz'])

# Now add some more
p.add('/foo', '/bar')
p.add('/barney', '/wilma')
self.assertListEqual(sorted(p), ['/barney', '/baz', '/foo'])
st.add('/foo', '/bar')
st.add('/barney', '/wilma')
self.assertListEqual(sorted(st), ['/barney', '/baz', '/foo'])

def test_storage_no_circular(self):
# Circular references are ignored
p = RedirectionStorage()
p.add('/circle', '/circle')
self.assertFalse(p.has_path('/circle'))
self.assertEqual(p.get('/circle', '_marker_'), '_marker_')
self.assertListEqual(p.redirects('/circle'), [])
st = RedirectionStorage()
st.add('/circle', '/circle')
self.assertFalse(st.has_path('/circle'))
self.assertEqual(st.get('/circle', '_marker_'), '_marker_')
self.assertListEqual(st.redirects('/circle'), [])

def test_storage_three_step_circular_rename(self):
# What about three step circular rename ?
p = RedirectionStorage()
st = RedirectionStorage()

# Add first redirect.
p.add('first', 'second')
st.add('first', 'second')

# There is only one redirect.

self.assertEqual(p.get('first'), 'second')
self.assertIsNone(p.get('second'))
self.assertIsNone(p.get('third'))
self.assertEqual(st.get('first'), 'second')
self.assertIsNone(st.get('second'))
self.assertIsNone(st.get('third'))

# There is one back reference.

self.assertListEqual(p.redirects('first'), [])
self.assertListEqual(p.redirects('second'), ['first'])
self.assertListEqual(p.redirects('third'), [])
self.assertListEqual(st.redirects('first'), [])
self.assertListEqual(st.redirects('second'), ['first'])
self.assertListEqual(st.redirects('third'), [])

# Add second redirect.
p.add('second', 'third')
st.add('second', 'third')

# There are now two.

self.assertEqual(p.get('first'), 'third')
self.assertEqual(p.get('second'), 'third')
self.assertIsNone(p.get('third'))
self.assertEqual(st.get('first'), 'third')
self.assertEqual(st.get('second'), 'third')
self.assertIsNone(st.get('third'))

# There are two back references as well.
self.assertListEqual(p.redirects('first'), [])
self.assertListEqual(p.redirects('second'), [])
self.assertListEqual(p.redirects('third'), ['first', 'second'])
self.assertListEqual(st.redirects('first'), [])
self.assertListEqual(st.redirects('second'), [])
self.assertListEqual(st.redirects('third'), ['first', 'second'])

# Add third redirect, CIRCULAR.
p.add('third', 'first')
st.add('third', 'first')

# There are still only two redirects.
self.assertIsNone(p.get('first'))
self.assertEqual(p.get('second'), 'first')
self.assertEqual(p.get('third'), 'first')
self.assertIsNone(st.get('first'))
self.assertEqual(st.get('second'), 'first')
self.assertEqual(st.get('third'), 'first')

# And same for the back references.
self.assertListEqual(p.redirects('first'), ['second', 'third'])
self.assertListEqual(p.redirects('second'), [])
self.assertListEqual(p.redirects('third'), [])
self.assertListEqual(st.redirects('first'), ['second', 'third'])
self.assertListEqual(st.redirects('second'), [])
self.assertListEqual(st.redirects('third'), [])


def test_suite():
Expand Down

0 comments on commit 0c98367

Please sign in to comment.