test_ordered_set.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from django.test import TestCase
  2. import pickle
  3. from jet.ordered_set import OrderedSet
  4. class OrderedSetTestCase(TestCase):
  5. def test_pickle(self):
  6. set1 = OrderedSet('abracadabra')
  7. roundtrip = pickle.loads(pickle.dumps(set1))
  8. self.assertEqual(roundtrip, set1)
  9. def test_empty_pickle(self):
  10. empty_oset = OrderedSet()
  11. empty_roundtrip = pickle.loads(pickle.dumps(empty_oset))
  12. self.assertEqual(empty_roundtrip, empty_oset)
  13. def test_order(self):
  14. set1 = OrderedSet('abracadabra')
  15. self.assertEqual(len(set1), 5)
  16. self.assertEqual(set1, OrderedSet(['a', 'b', 'r', 'c', 'd']))
  17. self.assertEqual(list(reversed(set1)), ['d', 'c', 'r', 'b', 'a'])
  18. def test_binary_operations(self):
  19. set1 = OrderedSet('abracadabra')
  20. set2 = OrderedSet('simsalabim')
  21. self.assertNotEqual(set1, set2)
  22. self.assertEqual(set1 & set2, OrderedSet(['a', 'b']))
  23. self.assertEqual(set1 | set2, OrderedSet(['a', 'b', 'r', 'c', 'd', 's', 'i', 'm', 'l']))
  24. self.assertEqual(set1 - set2, OrderedSet(['r', 'c', 'd']))
  25. def test_remove(self):
  26. set1 = OrderedSet('abracadabra')
  27. set1.remove('a')
  28. set1.remove('b')
  29. self.assertEqual(set1, OrderedSet('rcd'))
  30. self.assertNotIn('a', set1)
  31. self.assertNotIn('b', set1)
  32. self.assertIn('r', set1)
  33. set1.discard('a')
  34. set1.discard('a')
  35. def test_remove_error(self):
  36. set1 = OrderedSet('abracadabra')
  37. try:
  38. set1.remove('z')
  39. except KeyError:
  40. pass
  41. def test_clear(self):
  42. set1 = OrderedSet('abracadabra')
  43. set1.clear()
  44. self.assertEqual(len(set1), 0)
  45. self.assertEqual(set1, OrderedSet())
  46. def test_pop(self):
  47. set1 = OrderedSet('ab')
  48. elem = set1.pop()
  49. self.assertEqual(elem, 'b')
  50. elem = set1.pop()
  51. self.assertEqual(elem, 'a')
  52. self.assertRaises(KeyError, set1.pop)