<commit_before># This file is part of Pyrakoon, a distributed key-value store client.
#
# Copyright (C) 2010 Incubaid BVBA
#
# Licensees holding a valid Incubaid license may use this file in
# accordance with Incubaid's Arakoon commercial license agreement. For
# more information on how to enter into this agreement, please contact
# Incubaid (contact details can be found on www.arakoon.org/licensing).
#
# Alternatively, this file may be redistributed and/or modified under
# the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation. Under this license, this
# file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Affero General Public License for more details.
# You should have received a copy of the
# GNU Affero General Public License along with this program (file "COPYING").
# If not, see <http://www.gnu.org/licenses/>.

'''Tests for code in `pyrakoon.protocol`'''

import random
import inspect
import unittest

try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

from pyrakoon import errors, protocol, sequence

class TestTypeCheck(unittest.TestCase):
    '''Test `check` method implementations of `Type` classes'''

    def _run_test(self, type_, *values):
        for value, valid in values:
            if valid:
                type_.check(value)
            else:
                self.assertRaises((TypeError, ValueError), type_.check, value)

    def test_string(self):
        '''Test string checks'''

        self._run_test(protocol.STRING,
            ('', True),
            ('abc', True),

            (u'', False),
            (u'abc', False),
            (123, False),
            (None, False)
        )

    def test_unsigned_integer(self):
        '''Test unsigned integer checks'''

        self._run_test(protocol.UNSIGNED_INTEGER,
            (0, True),
            (1, True),
            ((2 ** 32) - 1, True),

            (-1, False),
            (2 ** 32, False),

            ('', False),
            (1.1, False),
            (None, False)
        )

    def test_signed_integer(self):
        '''Test signed integer checks'''

        self._run_test(protocol.SIGNED_INTEGER,
            (0, True),
            (1, True),
            (-1, True),
            (((2 ** 32) / 2) - 1, True),
            ((((2 ** 32) / 2) - 1) * (-1), True),

            (((2 ** 32) / 2), False),
            ((((2 ** 32) / 2)) * (-1), False),

            ('', False),
            (1.1, False),
            (None, False)
        )

    def test_bool(self):
        '''Test bool checks'''

        self._run_test(protocol.BOOL,
            (True, True),
            (False, True),

            (0, False),
            ('', False),
            (None, False)
        )

    def test_unit(self):
        '''Test unit checks'''

        self.assertRaises(NotImplementedError, protocol.UNIT.check, object())

    def test_step(self):
        '''Test step checks'''

        self._run_test(protocol.STEP,
            (sequence.Set('key', 'value'), True),
            (sequence.Delete('key'), True),
            (sequence.TestAndSet('key', 'oldvalue', 'newvalue'), True),

            (sequence.Sequence(), True),
            (sequence.Sequence(sequence.Set('key', 'value')), True),
            (sequence.Sequence(
                sequence.Set('key', 'value'), sequence.Delete('key')), True),
            (sequence.Sequence(
                sequence.Set('key', 'value'), sequence.Sequence(
                    sequence.Set('key', 'value'), sequence.Delete('key'))),
                True),

            (object(), False),
            (None, False),
            (sequence.Sequence(sequence.Delete('key'), object()), False)
        )

    def test_option(self):
        '''Test option checks'''

        type_ = protocol.Option(protocol.STRING)

        self._run_test(type_,
            (None, True),
            ('abc', True),
            ('', True),

            (1, False)
        )

    def test_list(self):
        '''Test list checks'''

        type_ = protocol.List(protocol.STRING)

        self._run_test(type_,
            (('abc',), True),
            (('abc', 'def',), True),
            ((), True),

            ('abc', False),
            ('', False),
            (u'abc', False),
            (u'', False),

            (('abc', None), False)
        )

    def test_product(self):
        '''Test product checks'''

        type_ = protocol.Product(protocol.UNSIGNED_INTEGER, protocol.STRING)

        self._run_test(type_,
            ((0, 'abc'), True),
            ((1, ''), True),

            ((), False),
            (None, False),
            ((-1, 'abc'), False),
            ((0, None), False)<commit_msg>Add string catch to product type tests<commit_after># This file is part of Pyrakoon, a distributed key-value store client.
#
# Copyright (C) 2010 Incubaid BVBA
#
# Licensees holding a valid Incubaid license may use this file in
# accordance with Incubaid's Arakoon commercial license agreement. For
# more information on how to enter into this agreement, please contact
# Incubaid (contact details can be found on www.arakoon.org/licensing).
#
# Alternatively, this file may be redistributed and/or modified under
# the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation. Under this license, this
# file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Affero General Public License for more details.
# You should have received a copy of the
# GNU Affero General Public License along with this program (file "COPYING").
# If not, see <http://www.gnu.org/licenses/>.

'''Tests for code in `pyrakoon.protocol`'''

import random
import inspect
import unittest

try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

from pyrakoon import errors, protocol, sequence

class TestTypeCheck(unittest.TestCase):
    '''Test `check` method implementations of `Type` classes'''

    def _run_test(self, type_, *values):
        for value, valid in values:
            if valid:
                type_.check(value)
            else:
                self.assertRaises((TypeError, ValueError), type_.check, value)

    def test_string(self):
        '''Test string checks'''

        self._run_test(protocol.STRING,
            ('', True),
            ('abc', True),

            (u'', False),
            (u'abc', False),
            (123, False),
            (None, False)
        )

    def test_unsigned_integer(self):
        '''Test unsigned integer checks'''

        self._run_test(protocol.UNSIGNED_INTEGER,
            (0, True),
            (1, True),
            ((2 ** 32) - 1, True),

            (-1, False),
            (2 ** 32, False),

            ('', False),
            (1.1, False),
            (None, False)
        )

    def test_signed_integer(self):
        '''Test signed integer checks'''

        self._run_test(protocol.SIGNED_INTEGER,
            (0, True),
            (1, True),
            (-1, True),
            (((2 ** 32) / 2) - 1, True),
            ((((2 ** 32) / 2) - 1) * (-1), True),

            (((2 ** 32) / 2), False),
            ((((2 ** 32) / 2)) * (-1), False),

            ('', False),
            (1.1, False),
            (None, False)
        )

    def test_bool(self):
        '''Test bool checks'''

        self._run_test(protocol.BOOL,
            (True, True),
            (False, True),

            (0, False),
            ('', False),
            (None, False)
        )

    def test_unit(self):
        '''Test unit checks'''

        self.assertRaises(NotImplementedError, protocol.UNIT.check, object())

    def test_step(self):
        '''Test step checks'''

        self._run_test(protocol.STEP,
            (sequence.Set('key', 'value'), True),
            (sequence.Delete('key'), True),
            (sequence.TestAndSet('key', 'oldvalue', 'newvalue'), True),

            (sequence.Sequence(), True),
            (sequence.Sequence(sequence.Set('key', 'value')), True),
            (sequence.Sequence(
                sequence.Set('key', 'value'), sequence.Delete('key')), True),
            (sequence.Sequence(
                sequence.Set('key', 'value'), sequence.Sequence(
                    sequence.Set('key', 'value'), sequence.Delete('key'))),
                True),

            (object(), False),
            (None, False),
            (sequence.Sequence(sequence.Delete('key'), object()), False)
        )

    def test_option(self):
        '''Test option checks'''

        type_ = protocol.Option(protocol.STRING)

        self._run_test(type_,
            (None, True),
            ('abc', True),
            ('', True),

            (1, False)
        )

    def test_list(self):
        '''Test list checks'''

        type_ = protocol.List(protocol.STRING)

        self._run_test(type_,
            (('abc',), True),
            (('abc', 'def',), True),
            ((), True),

            ('abc', False),
            ('', False),
            (u'abc', False),
            (u'', False),

            (('abc', None), False)
        )

    def test_product(self):
        '''Test product checks'''

        type_ = protocol.Product(protocol.UNSIGNED_INTEGER, protocol.STRING)

        self._run_test(type_,
            ((0, 'abc'), True),
            ((1, ''), True),

            ((), False),
            (None, False),
            ((-1, 'abc'), False),
            ((0, None), False),
            ('abc', False),
            (u'abc', False),
{'fraction_non_alphanumeric': 0.14244868783825182, 'fraction_numerical': 0.010415602981721638, 'mean_word_length': 1.912280701754386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7353105', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2639, 'n_words': 817}