test_utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import pickle
  2. import unittest2 as unittest
  3. from celery import utils
  4. from celery.utils import promise, mpromise, maybe_promise
  5. def double(x):
  6. return x * 2
  7. class test_chunks(unittest.TestCase):
  8. def test_chunks(self):
  9. # n == 2
  10. x = utils.chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 2)
  11. self.assertListEqual(list(x),
  12. [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]])
  13. # n == 3
  14. x = utils.chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 3)
  15. self.assertListEqual(list(x),
  16. [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]])
  17. # n == 2 (exact)
  18. x = utils.chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 2)
  19. self.assertListEqual(list(x),
  20. [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
  21. class test_utils(unittest.TestCase):
  22. def test_get_full_cls_name(self):
  23. Class = type("Fox", (object, ), {"__module__": "quick.brown"})
  24. self.assertEqual(utils.get_full_cls_name(Class), "quick.brown.Fox")
  25. def test_is_iterable(self):
  26. for a in "f", ["f"], ("f", ), {"f": "f"}:
  27. self.assertTrue(utils.is_iterable(a))
  28. for b in object(), 1:
  29. self.assertFalse(utils.is_iterable(b))
  30. def test_padlist(self):
  31. self.assertListEqual(utils.padlist(["George", "Costanza", "NYC"], 3),
  32. ["George", "Costanza", "NYC"])
  33. self.assertListEqual(utils.padlist(["George", "Costanza"], 3),
  34. ["George", "Costanza", None])
  35. self.assertListEqual(utils.padlist(["George", "Costanza", "NYC"], 4,
  36. default="Earth"),
  37. ["George", "Costanza", "NYC", "Earth"])
  38. def test_firstmethod_AttributeError(self):
  39. self.assertIsNone(utils.firstmethod("foo")([object()]))
  40. def test_firstmethod_promises(self):
  41. class A(object):
  42. def __init__(self, value=None):
  43. self.value = value
  44. def m(self):
  45. return self.value
  46. self.assertEqual("four", utils.firstmethod("m")([
  47. A(), A(), A(), A("four"), A("five")]))
  48. self.assertEqual("four", utils.firstmethod("m")([
  49. A(), A(), A(), promise(lambda: A("four")), A("five")]))
  50. def test_first(self):
  51. iterations = [0]
  52. def predicate(value):
  53. iterations[0] += 1
  54. if value == 5:
  55. return True
  56. return False
  57. self.assertEqual(5, utils.first(predicate, xrange(10)))
  58. self.assertEqual(iterations[0], 6)
  59. iterations[0] = 0
  60. self.assertIsNone(utils.first(predicate, xrange(10, 20)))
  61. self.assertEqual(iterations[0], 10)
  62. def test_get_cls_by_name__instance_returns_instance(self):
  63. instance = object()
  64. self.assertIs(utils.get_cls_by_name(instance), instance)
  65. class test_promise(unittest.TestCase):
  66. def test__str__(self):
  67. self.assertEqual(str(promise(lambda: "the quick brown fox")),
  68. "the quick brown fox")
  69. def test__repr__(self):
  70. self.assertEqual(repr(promise(lambda: "fi fa fo")),
  71. "'fi fa fo'")
  72. def test_evaluate(self):
  73. self.assertEqual(promise(lambda: 2 + 2)(), 4)
  74. self.assertEqual(promise(lambda x: x * 4, 2), 8)
  75. self.assertEqual(promise(lambda x: x * 8, 2)(), 16)
  76. def test_cmp(self):
  77. self.assertEqual(promise(lambda: 10), promise(lambda: 10))
  78. self.assertNotEqual(promise(lambda: 10), promise(lambda: 20))
  79. def test__reduce__(self):
  80. x = promise(double, 4)
  81. y = pickle.loads(pickle.dumps(x))
  82. self.assertEqual(x(), y())
  83. def test__deepcopy__(self):
  84. from copy import deepcopy
  85. x = promise(double, 4)
  86. y = deepcopy(x)
  87. self.assertEqual(x._fun, y._fun)
  88. self.assertEqual(x._args, y._args)
  89. self.assertEqual(x(), y())
  90. class test_mpromise(unittest.TestCase):
  91. def test_is_memoized(self):
  92. it = iter(xrange(20, 30))
  93. p = mpromise(it.next)
  94. self.assertEqual(p(), 20)
  95. self.assertTrue(p.evaluated)
  96. self.assertEqual(p(), 20)
  97. self.assertEqual(repr(p), "20")
  98. class test_maybe_promise(unittest.TestCase):
  99. def test_evaluates(self):
  100. self.assertEqual(maybe_promise(promise(lambda: 10)), 10)
  101. self.assertEqual(maybe_promise(20), 20)