PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/tests/db/test_sequence.py

#
Python | 109 lines | 87 code | 2 blank | 20 comment | 0 complexity | 9b11824d586ff0ef6a1b9d4643f9d28b MD5 | raw file
  1. # Copyright (c) 2010 Chris Moyer http://coredumped.org/
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. class TestDBHandler(object):
  22. """Test the DBHandler"""
  23. def setup_class(cls):
  24. """Setup this class"""
  25. cls.sequences = []
  26. def teardown_class(cls):
  27. """Remove our sequences"""
  28. for s in cls.sequences:
  29. try:
  30. s.delete()
  31. except:
  32. pass
  33. def test_sequence_generator_no_rollover(self):
  34. """Test the sequence generator without rollover"""
  35. from boto.sdb.db.sequence import SequenceGenerator
  36. gen = SequenceGenerator("ABC")
  37. assert(gen("") == "A")
  38. assert(gen("A") == "B")
  39. assert(gen("B") == "C")
  40. assert(gen("C") == "AA")
  41. assert(gen("AC") == "BA")
  42. def test_sequence_generator_with_rollover(self):
  43. """Test the sequence generator with rollover"""
  44. from boto.sdb.db.sequence import SequenceGenerator
  45. gen = SequenceGenerator("ABC", rollover=True)
  46. assert(gen("") == "A")
  47. assert(gen("A") == "B")
  48. assert(gen("B") == "C")
  49. assert(gen("C") == "A")
  50. def test_sequence_simple_int(self):
  51. """Test a simple counter sequence"""
  52. from boto.sdb.db.sequence import Sequence
  53. s = Sequence()
  54. self.sequences.append(s)
  55. assert(s.val == 0)
  56. assert(s.next() == 1)
  57. assert(s.next() == 2)
  58. s2 = Sequence(s.id)
  59. assert(s2.val == 2)
  60. assert(s.next() == 3)
  61. assert(s.val == 3)
  62. assert(s2.val == 3)
  63. def test_sequence_simple_string(self):
  64. from boto.sdb.db.sequence import Sequence, increment_string
  65. s = Sequence(fnc=increment_string)
  66. self.sequences.append(s)
  67. assert(s.val == "A")
  68. assert(s.next() == "B")
  69. def test_fib(self):
  70. """Test the fibonacci sequence generator"""
  71. from boto.sdb.db.sequence import fib
  72. # Just check the first few numbers in the sequence
  73. lv = 0
  74. for v in [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]:
  75. assert(fib(v, lv) == lv+v)
  76. lv = fib(v, lv)
  77. def test_sequence_fib(self):
  78. """Test the fibonacci sequence"""
  79. from boto.sdb.db.sequence import Sequence, fib
  80. s = Sequence(fnc=fib)
  81. s2 = Sequence(s.id)
  82. self.sequences.append(s)
  83. assert(s.val == 1)
  84. # Just check the first few numbers in the sequence
  85. for v in [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]:
  86. assert(s.next() == v)
  87. assert(s.val == v)
  88. assert(s2.val == v) # it shouldn't matter which reference we use since it's garunteed to be consistent
  89. def test_sequence_string(self):
  90. """Test the String incrementation sequence"""
  91. from boto.sdb.db.sequence import Sequence, increment_string
  92. s = Sequence(fnc=increment_string)
  93. self.sequences.append(s)
  94. assert(s.val == "A")
  95. assert(s.next() == "B")
  96. s.val = "Z"
  97. assert(s.val == "Z")
  98. assert(s.next() == "AA")