PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Python | 109 lines | 80 code | 5 blank | 24 comment | 2 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 
 22
 23class TestDBHandler(object):
 24    """Test the DBHandler"""
 25
 26    def setup_class(cls):
 27        """Setup this class"""
 28        cls.sequences = []
 29
 30    def teardown_class(cls):
 31        """Remove our sequences"""
 32        for s in cls.sequences:
 33            try:
 34                s.delete()
 35            except:
 36                pass
 37
 38    def test_sequence_generator_no_rollover(self):
 39        """Test the sequence generator without rollover"""
 40        from boto.sdb.db.sequence import SequenceGenerator
 41        gen = SequenceGenerator("ABC")
 42        assert(gen("") == "A")
 43        assert(gen("A") == "B")
 44        assert(gen("B") == "C")
 45        assert(gen("C") == "AA")
 46        assert(gen("AC") == "BA")
 47
 48    def test_sequence_generator_with_rollover(self):
 49        """Test the sequence generator with rollover"""
 50        from boto.sdb.db.sequence import SequenceGenerator
 51        gen = SequenceGenerator("ABC", rollover=True)
 52        assert(gen("") == "A")
 53        assert(gen("A") == "B")
 54        assert(gen("B") == "C")
 55        assert(gen("C") == "A")
 56
 57    def test_sequence_simple_int(self):
 58        """Test a simple counter sequence"""
 59        from boto.sdb.db.sequence import Sequence
 60        s = Sequence()
 61        self.sequences.append(s)
 62        assert(s.val == 0)
 63        assert(s.next() == 1)
 64        assert(s.next() == 2)
 65        s2 = Sequence(s.id)
 66        assert(s2.val == 2)
 67        assert(s.next() == 3)
 68        assert(s.val == 3)
 69        assert(s2.val == 3)
 70
 71    def test_sequence_simple_string(self):
 72        from boto.sdb.db.sequence import Sequence, increment_string
 73        s = Sequence(fnc=increment_string)
 74        self.sequences.append(s)
 75        assert(s.val == "A")
 76        assert(s.next() == "B")
 77
 78    def test_fib(self):
 79        """Test the fibonacci sequence generator"""
 80        from boto.sdb.db.sequence import fib
 81        # Just check the first few numbers in the sequence
 82        lv = 0
 83        for v in [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]:
 84            assert(fib(v, lv) == lv+v)
 85            lv = fib(v, lv)
 86
 87    def test_sequence_fib(self):
 88        """Test the fibonacci sequence"""
 89        from boto.sdb.db.sequence import Sequence, fib
 90        s = Sequence(fnc=fib)
 91        s2 = Sequence(s.id)
 92        self.sequences.append(s)
 93        assert(s.val == 1)
 94        # Just check the first few numbers in the sequence
 95        for v in [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]:
 96            assert(s.next() == v)
 97            assert(s.val == v)
 98            assert(s2.val == v) # it shouldn't matter which reference we use since it's garunteed to be consistent
 99
100    def test_sequence_string(self):
101        """Test the String incrementation sequence"""
102        from boto.sdb.db.sequence import Sequence, increment_string
103        s = Sequence(fnc=increment_string)
104        self.sequences.append(s)
105        assert(s.val == "A")
106        assert(s.next() == "B")
107        s.val = "Z"
108        assert(s.val == "Z")
109        assert(s.next() == "AA")