Beispiel #1
0
    def test_resources_modify(self):
        ress = Resources('<test>')
        foo = ress.create('foo')

        foo.add('one', 'two')

        foo.add('one', 'three')
        self.assertEqual(foo.get_all('one'), ['two', 'three'])

        foo.delete_all('one')
        foo.delete_all('nonexistent')
        self.assertEqual(foo.get_all('one'), [])
Beispiel #2
0
    def test_resources_create(self):
        ress = Resources('<test>')
        foo = ress.create('foo')
        ress.create('bar')
        ress.create('x1.x2.x3')
        self.assertRaises(ValueError, ress.create, '$')
        self.assertRaises(ValueError, ress.create, 'foo')

        self.assertEqual(foo, ress.get('foo'))
Beispiel #3
0
    def test_resources_dump(self):
        ress = Resources('<test>')

        val = self.dump_to_string(ress)
        self.assertEqual(val, '')
        val = self.dump_to_string(ress, 'I am a comment.')
        self.assertEqual(val, '# I am a comment.\n\n')
        val = self.dump_to_string(ress, ['one', 'two'])
        self.assertEqual(val, '# one\n# two\n\n')

        res = ress.create('foo')
        val = self.dump_to_string(ress)
        self.assertEqual(val, ':foo\n\n')

        res.add('one', 'two')
        val = self.dump_to_string(ress)
        self.assertEqual(val, ':foo\none: two\n\n')
Beispiel #4
0
    def test_resources(self):
        source = """# Resources test file

    # Another comment

:res1
# comment
one: 1
two: 2
two: II

:Foo.Bar
one: eleven
"""
        fl = io.StringIO(source)
        ress = Resources('<unit test>', fl)
        fl.close()

        self.assertTrue(ress.get('foo') is None)

        res = ress.get('res1')
        self.assertTrue(res in ress.resources())
        self.assertEqual(res.get_one('one'), '1')
        self.assertEqual(res.get_one('two'), '2')
        self.assertEqual(res.get_all('two'), ['2', 'II'])
        self.assertEqual(res.get_one('missing'), None)
        self.assertEqual(res.get_one('missing', 'def'), 'def')

        res = ress.get('Foo.Bar')
        self.assertTrue(res in ress.resources())
        self.assertEqual(res.get_one('one'), 'eleven')
        self.assertEqual(res.get_one('two'), None)

        keys = list(ress.keys())
        self.assertEqual(len(keys), 2)
        self.assertTrue('res1' in keys and 'Foo.Bar' in keys)
Beispiel #5
0
    def test_resources_build_tree(self):
        ress = Resources('<test>')
        dic = ress.build_tree()
        self.assertEqual(dic, {})

        ress.create('one')
        dic = ress.build_tree()
        self.assertEqual(dic, {'one': 'one'})

        ress.create('two')
        dic = ress.build_tree()
        self.assertEqual(dic, {'one': 'one', 'two': 'two'})

        ress.create('foo.three')
        dic = ress.build_tree()
        self.assertEqual(dic, {
            'one': 'one',
            'two': 'two',
            'foo': {
                'three': 'foo.three'
            }
        })

        ress.create('foo.four')
        dic = ress.build_tree()
        self.assertEqual(
            dic, {
                'one': 'one',
                'two': 'two',
                'foo': {
                    'three': 'foo.three',
                    'four': 'foo.four'
                }
            })

        ress.create('x.y.z')
        dic = ress.build_tree()
        self.assertEqual(
            dic,
            {
                'one': 'one',
                'two': 'two',
                'foo': {
                    'three': 'foo.three',
                    'four': 'foo.four'
                },
                'x': {
                    'y': {
                        'z': 'x.y.z'
                    }
                },
            },
        )

        ress.create('foo.four.bad')
        self.assertRaises(ValueError, ress.build_tree)
Beispiel #6
0
 def test_resources_empty(self):
     ress = Resources('<test>')
     self.assertEqual(len(ress), 0)
     self.assertTrue(ress.get('foo') is None)
     self.assertEqual(list(ress.keys()), [])
     self.assertEqual(ress.resources(), [])