コード例 #1
0
class MLDTests(unittest.TestCase):
    def setUp(self):
        self.mld = MultiLookupDict({
            '[foo]': {
                'alias': 'bar',
                'caption': 'baz',
                'value': 1
            },
            '[bar]': {
                'caption': 'foo',
                'value': 2
            },
            '[baz]': {
                'value': 3
            }
        })

    def test_multilookupdict_name_only(self):
        actual = self.mld['[baz]']
        self.assertEqual(3, actual['value'])

    def test_multilookupdict_alias_overrides_everything(self):
        actual = self.mld['bar']
        self.assertEqual(1, actual['value'])

    def test_mutlilookupdict_caption_overrides_id(self):
        actual = self.mld['foo']
        self.assertEqual(2, actual['value'])

    def test_mutlilookupdict_can_still_find_id_even_with_alias(self):
        actual = self.mld['[foo]']
        self.assertEqual(1, actual['value'])

    def test_mutlilookupdict_can_still_find_caption_even_with_alias(self):
        actual = self.mld['baz']
        self.assertEqual(1, actual['value'])

    def test_mutlilookupdict_can_still_find_id_even_with_caption(self):
        actual = self.mld['[bar]']
        self.assertEqual(2, actual['value'])

    def test_multilookupdict_gives_key_error_on_invalid_key(self):
        try:
            self.mld.get('foobar')
            self.fail('should have thrown key error')
        except KeyError as ex:
            self.assertEqual(str(ex), "'foobar'")

    def test_multilookupdict_get_returns_default_value(self):
        default_value = ('default', 'return', 'value')
        actual = self.mld.get('foobar', default_value)
        self.assertEqual(actual, default_value)

    def test_multilookupdict_get_returns_value(self):
        actual = self.mld.get('baz')
        self.assertEqual(1, actual['value'])
コード例 #2
0
 def setUp(self):
     self.mld = MultiLookupDict({
         '[foo]': {
             'alias': 'bar',
             'caption': 'baz',
             'value': 1
         },
         '[bar]': {
             'caption': 'foo',
             'value': 2
         },
         '[baz]': {
             'value': 3
         }
     })
コード例 #3
0
 def test_multilookupdict_can_be_empty(self):
     mld = MultiLookupDict()
     self.assertIsNotNone(mld)