Ejemplo n.º 1
0
class BaseConfigTest(TestCase):
    '''
    Tests for configuration stored in database and accessed via Config module.
    Checks setting and getting keys and values in config.
    '''

    fixtures = ['active_user.json']
    
    def setUp(self):
        typenames = ['int', 'bool', 'list_groupnames', 'unicode']
        for typename in typenames:
            ConfigurationValueType(name=typename).save()
        t_int = ConfigurationValueType.objects.get(name='int')
        t_bool = ConfigurationValueType.objects.get(name='bool')
        t_unicode = ConfigurationValueType.objects.get(name='unicode')
#        t_list_groupnames = ConfigurationValueType.objects.get(name='list_groupnames')

        user = User.objects.get(pk=1)
        self.c = Config(user)
        
        data = [
            ('mickey_mouse_creator', 'Disney', t_unicode),
            ('random_text', 'p2835ybnas12da', t_unicode),
            ('most_funny_number', '8', t_int),
            ('some_true_value', 'true', t_bool),
            ('some_false_value', 'false', t_bool),
            ('some_bool_value', 'false', t_bool),
#            ('simple_int_list', '[1, 2, 7]'),
#            ('empty_list', '[]'),
            ]
        for k, v, t in data:
            ConfigModel(key=k, value=v, type=t).save()

    def test_containing(self):
        ''' Checks if config contains values set up in setUp method. '''
        self.assertTrue('most_funny_number' in self.c)
        self.assertTrue('some_true_value' in self.c)
        self.assertTrue('some_false_value' in self.c)
        self.assertTrue('simple_int_list' in self.c)
        self.assertTrue('empty_list' in self.c)
        self.assertFalse('this_string_is_not_in_config' in self.c)


    ''' string tests '''
    def test_get_string(self):
        self.assertEqual('Disney', self.c['mickey_mouse_creator'])

    def test_updating_string_with_brackets(self):
        self.c['mickey_mouse_creator'] = 'Walt Disney'
        self.assertEqual(unicode, type(self.c.get_str('mickey_mouse_creator')))
        self.assertEqual('Walt Disney', self.c.get_str('mickey_mouse_creator'))

    def test_updating_string_with_setter(self):
        m_key, m_value = u'mickey_mouse_creator', u'Walt Disney'
        self.c.set_str(m_key, m_value)
        self.assertEqual(unicode, type(self.c.get_str(m_key)))
        self.assertEqual(m_value, self.c.get_str(m_key))

        r_key, r_value = u'random_text', u'sadjboqi73t2fhj'
        self.c.set_str(r_key, r_value)
        self.assertEqual(unicode, type(self.c[r_key]))
        self.assertEqual(r_value, self.c[r_key])

    def test_creating_string_with_brackets(self):
        self.c['almost_random_prefix'] = 'tralala'
        self.assertEqual('tralala', self.c['almost_random_prefix'])

    def test_creating_string_with_setter(self):
        self.c.set_str('almost_random_prefix', 'tralala')
        self.assertEqual('tralala', self.c['almost_random_prefix'])


    ''' int tests '''
    def test_get_int(self):
        self.assertEqual(8, self.c.get_int('most_funny_number'))

    def test_updating_int_with_brackets(self):
        self.c['most_funny_number'] = 42
        self.assertEqual(type(42), type(self.c.get_int('most_funny_number')))
        self.assertEqual(42, self.c.get_int('most_funny_number'))

    def test_updating_int_with_setter(self):
        self.c.set_int('most_funny_number', 42)
        self.assertEqual(type(42), type(self.c.get_int('most_funny_number')))
        self.assertEqual(42, self.c.get_int('most_funny_number'))

    def test_creating_int_with_brackets(self):
        self.c['some_int_name'] = 123654
        self.assertEqual(123654, self.c.get_int('some_int_name'))

    def test_creating_int_with_setter(self):
        self.c.set_int('some_int_name', 123654)
        self.assertEqual(123654, self.c.get_int('some_int_name'))


    ''' bool tests '''
    def test_get_bool(self):
        self.assertEqual(True, self.c.get_bool('some_true_value'))
        self.assertEqual(False, self.c.get_bool('some_false_value'))

    def test_updating_bool_with_brackets(self):
        self.c['some_bool_value'] = True
        self.assertEqual(type(True), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(True, self.c.get_bool('some_bool_value'))
        #
        self.c['some_bool_value'] = False
        self.assertEqual(type(False), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(False, self.c.get_bool('some_bool_value'))

    def test_updating_bool_with_setter(self):
        self.c.set_bool('some_bool_value', True)
        self.assertEqual(type(True), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(True, self.c.get_bool('some_bool_value'))
        #
        self.c.set_bool('some_bool_value', False)
        self.assertEqual(type(False), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(False, self.c.get_bool('some_bool_value'))

    def test_creating_bool_with_brackets(self):
        self.c['green_apples_exists'] = True
        self.assertEqual(Config._true_value, self.c['green_apples_exists'])           # when using brackets Config doesn't know value's type
        self.assertEqual(type(True), type(self.c.get_bool('green_apples_exists')))    # so you should use get_bool() while _true_value is private
        self.assertEqual(True, self.c.get_bool('green_apples_exists'))
        self.assertEqual(False, not self.c.get_bool('green_apples_exists'))

    def test_creating_bool_with_setter(self):
        self.c.set_bool('green_apples_exists', True)
        self.assertEqual(Config._true_value, self.c['green_apples_exists'])           # when using brackets Config doesn't know value's type
        self.assertEqual(type(True), type(self.c.get_bool('green_apples_exists')))    # so you should use get_bool() while _true_value is private
        self.assertEqual(True, self.c.get_bool('green_apples_exists'))
        self.assertEqual(False, not self.c.get_bool('green_apples_exists'))


    ''' list tests '''
    def test_get_list(self):
        self.assertEqual([1,2,7], self.c.get_list('simple_int_list'))
        self.assertEqual([], self.c.get_list('empty_list'))

    def test_updating_list_with_brackets(self):
        self.c['simple_int_list'] = [5, 25, 256, 20]
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([5, 25, 256, 20], self.c.get_list('simple_int_list'))
        #
        self.c['simple_int_list'] = []
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([], self.c.get_list('simple_int_list'))

    def test_updating_list_with_setter(self):
        self.c.set_list('simple_int_list', [5, 25, 256, 20])
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([5, 25, 256, 20], self.c.get_list('simple_int_list'))
        #
        self.c.set_list('simple_int_list', [])
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([], self.c.get_list('simple_int_list'))

    def test_creating_list_with_brackets(self):
        self.c['mixed_list'] = [1, 3.1416,'hello', "world"]
        self.assertEqual(type([]), type(self.c.get_list('mixed_list')))
        self.assertEqual([1,3.1416, 'hello', "world"], self.c.get_list('mixed_list'))

    def test_creating_list_with_setter(self):
        self.c.set_list('mixed_list', [1, 3.1416,'hello', "world"])
        self.assertEqual(type([]), type(self.c.get_list('mixed_list')))
        self.assertEqual([1,3.1416, 'hello', "world"], self.c.get_list('mixed_list'))
Ejemplo n.º 2
0
class BaseConfigTest(TestCase):
    '''
    Tests for configuration stored in database and accessed via Config module.
    Checks setting and getting keys and values in config.
    '''

    fixtures = ['active_user.json']

    def setUp(self):
        typenames = ['int', 'bool', 'list_groupnames', 'unicode']
        for typename in typenames:
            ConfigurationValueType(name=typename).save()
        t_int = ConfigurationValueType.objects.get(name='int')
        t_bool = ConfigurationValueType.objects.get(name='bool')
        t_unicode = ConfigurationValueType.objects.get(name='unicode')
        #        t_list_groupnames = ConfigurationValueType.objects.get(name='list_groupnames')

        user = User.objects.get(pk=1)
        self.c = Config(user)

        data = [
            ('mickey_mouse_creator', 'Disney', t_unicode),
            ('random_text', 'p2835ybnas12da', t_unicode),
            ('most_funny_number', '8', t_int),
            ('some_true_value', 'true', t_bool),
            ('some_false_value', 'false', t_bool),
            ('some_bool_value', 'false', t_bool),
            #            ('simple_int_list', '[1, 2, 7]'),
            #            ('empty_list', '[]'),
        ]
        for k, v, t in data:
            ConfigModel(key=k, value=v, type=t).save()

    def test_containing(self):
        ''' Checks if config contains values set up in setUp method. '''
        self.assertTrue('most_funny_number' in self.c)
        self.assertTrue('some_true_value' in self.c)
        self.assertTrue('some_false_value' in self.c)
        self.assertTrue('simple_int_list' in self.c)
        self.assertTrue('empty_list' in self.c)
        self.assertFalse('this_string_is_not_in_config' in self.c)

    ''' string tests '''

    def test_get_string(self):
        self.assertEqual('Disney', self.c['mickey_mouse_creator'])

    def test_updating_string_with_brackets(self):
        self.c['mickey_mouse_creator'] = 'Walt Disney'
        self.assertEqual(unicode, type(self.c.get_str('mickey_mouse_creator')))
        self.assertEqual('Walt Disney', self.c.get_str('mickey_mouse_creator'))

    def test_updating_string_with_setter(self):
        m_key, m_value = u'mickey_mouse_creator', u'Walt Disney'
        self.c.set_str(m_key, m_value)
        self.assertEqual(unicode, type(self.c.get_str(m_key)))
        self.assertEqual(m_value, self.c.get_str(m_key))

        r_key, r_value = u'random_text', u'sadjboqi73t2fhj'
        self.c.set_str(r_key, r_value)
        self.assertEqual(unicode, type(self.c[r_key]))
        self.assertEqual(r_value, self.c[r_key])

    def test_creating_string_with_brackets(self):
        self.c['almost_random_prefix'] = 'tralala'
        self.assertEqual('tralala', self.c['almost_random_prefix'])

    def test_creating_string_with_setter(self):
        self.c.set_str('almost_random_prefix', 'tralala')
        self.assertEqual('tralala', self.c['almost_random_prefix'])

    ''' int tests '''

    def test_get_int(self):
        self.assertEqual(8, self.c.get_int('most_funny_number'))

    def test_updating_int_with_brackets(self):
        self.c['most_funny_number'] = 42
        self.assertEqual(type(42), type(self.c.get_int('most_funny_number')))
        self.assertEqual(42, self.c.get_int('most_funny_number'))

    def test_updating_int_with_setter(self):
        self.c.set_int('most_funny_number', 42)
        self.assertEqual(type(42), type(self.c.get_int('most_funny_number')))
        self.assertEqual(42, self.c.get_int('most_funny_number'))

    def test_creating_int_with_brackets(self):
        self.c['some_int_name'] = 123654
        self.assertEqual(123654, self.c.get_int('some_int_name'))

    def test_creating_int_with_setter(self):
        self.c.set_int('some_int_name', 123654)
        self.assertEqual(123654, self.c.get_int('some_int_name'))

    ''' bool tests '''

    def test_get_bool(self):
        self.assertEqual(True, self.c.get_bool('some_true_value'))
        self.assertEqual(False, self.c.get_bool('some_false_value'))

    def test_updating_bool_with_brackets(self):
        self.c['some_bool_value'] = True
        self.assertEqual(type(True), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(True, self.c.get_bool('some_bool_value'))
        #
        self.c['some_bool_value'] = False
        self.assertEqual(type(False), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(False, self.c.get_bool('some_bool_value'))

    def test_updating_bool_with_setter(self):
        self.c.set_bool('some_bool_value', True)
        self.assertEqual(type(True), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(True, self.c.get_bool('some_bool_value'))
        #
        self.c.set_bool('some_bool_value', False)
        self.assertEqual(type(False), type(self.c.get_bool('some_bool_value')))
        self.assertEqual(False, self.c.get_bool('some_bool_value'))

    def test_creating_bool_with_brackets(self):
        self.c['green_apples_exists'] = True
        self.assertEqual(
            Config._true_value, self.c['green_apples_exists']
        )  # when using brackets Config doesn't know value's type
        self.assertEqual(
            type(True), type(self.c.get_bool('green_apples_exists'))
        )  # so you should use get_bool() while _true_value is private
        self.assertEqual(True, self.c.get_bool('green_apples_exists'))
        self.assertEqual(False, not self.c.get_bool('green_apples_exists'))

    def test_creating_bool_with_setter(self):
        self.c.set_bool('green_apples_exists', True)
        self.assertEqual(
            Config._true_value, self.c['green_apples_exists']
        )  # when using brackets Config doesn't know value's type
        self.assertEqual(
            type(True), type(self.c.get_bool('green_apples_exists'))
        )  # so you should use get_bool() while _true_value is private
        self.assertEqual(True, self.c.get_bool('green_apples_exists'))
        self.assertEqual(False, not self.c.get_bool('green_apples_exists'))

    ''' list tests '''

    def test_get_list(self):
        self.assertEqual([1, 2, 7], self.c.get_list('simple_int_list'))
        self.assertEqual([], self.c.get_list('empty_list'))

    def test_updating_list_with_brackets(self):
        self.c['simple_int_list'] = [5, 25, 256, 20]
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([5, 25, 256, 20], self.c.get_list('simple_int_list'))
        #
        self.c['simple_int_list'] = []
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([], self.c.get_list('simple_int_list'))

    def test_updating_list_with_setter(self):
        self.c.set_list('simple_int_list', [5, 25, 256, 20])
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([5, 25, 256, 20], self.c.get_list('simple_int_list'))
        #
        self.c.set_list('simple_int_list', [])
        self.assertEqual(type([]), type(self.c.get_list('simple_int_list')))
        self.assertEqual([], self.c.get_list('simple_int_list'))

    def test_creating_list_with_brackets(self):
        self.c['mixed_list'] = [1, 3.1416, 'hello', "world"]
        self.assertEqual(type([]), type(self.c.get_list('mixed_list')))
        self.assertEqual([1, 3.1416, 'hello', "world"],
                         self.c.get_list('mixed_list'))

    def test_creating_list_with_setter(self):
        self.c.set_list('mixed_list', [1, 3.1416, 'hello', "world"])
        self.assertEqual(type([]), type(self.c.get_list('mixed_list')))
        self.assertEqual([1, 3.1416, 'hello', "world"],
                         self.c.get_list('mixed_list'))