Ejemplo n.º 1
0
    def test_summary(self):

        d = Dictionary(items=self.valid_content, description='A test dictionary.')
        s = d.summary()

        # a DictionarySummary must be returned
        self.assertIsInstance(s, DictionarySummary)

        # has description correctly propagated
        self.assertEqual(s.description, 'A test dictionary.')
Ejemplo n.º 2
0
    def test_get(self):

        d = Dictionary(items=self.valid_content)

        # valid keys
        for key, value in self.valid_content_promoted.items():
            self.assertEqual(d.get(key), value)

        # invalid key
        r = d.get('abc')
        self.assertEqual(r, None)

        # invalid key, alternate default
        r = d.get('abc', 'spoon')
        self.assertEqual(r, 'spoon')
Ejemplo n.º 3
0
    def test_contains(self):

        d = Dictionary(self.valid_content)

        # valid keys
        for key in self.valid_content.keys():
            self.assertTrue(key in d)

        # invalid keys
        self.assertFalse('non_existent' in d)
Ejemplo n.º 4
0
    def test_getitem(self):

        d = Dictionary(items=self.valid_content)

        # valid keys
        for key, value in self.valid_content_promoted.items():
            self.assertEqual(d[key], value)

        # invalid key
        with self.assertRaises(KeyError):
            v = d['abc']
Ejemplo n.º 5
0
    def test_from_dict(self):

        # valid data
        d = Dictionary.from_dict(self.valid_content_serialised)

        # invalid data
        with self.assertRaises(ValueError):
            d = DictionarySummary.from_dict({})

        with self.assertRaises(ValueError):
            d = DictionarySummary.from_dict(
                {
                    '_group': 'invalid',
                    '_class': 'wrong',
                    '_type': 'object',
                    '_version': 1,
                    'description': 'A dictionary.',
                    'items': {}
                }
            )
Ejemplo n.º 6
0
    def test_setitem(self):

        d = Dictionary()

        # valid values
        d['alpha'] = 'apple'
        d['beta'] = np.int8(1)
        d['abcdefghijklmnopqrstuvwxyz0123456789_.-'] = np.float64(1.0)

        self.assertEqual(d._items['alpha'], 'apple')
        self.assertEqual(d._items['beta'], np.int8(1))
        self.assertEqual(d._items['abcdefghijklmnopqrstuvwxyz0123456789_.-'], np.float64(1.0))

        # invalid key
        with self.assertRaises(ValueError):
            d['ABC'] = 'blah'

        # invalid value
        with self.assertRaises(TypeError):
            d['abc'] = []
Ejemplo n.º 7
0
    def test_init_description(self):

        d = Dictionary(description='A test dictionary.')
        self.assertEqual(d.description, 'A test dictionary.')
        self.assertEqual(d._items, {})
Ejemplo n.º 8
0
    def test_init_items(self):

        d = Dictionary(items=self.valid_content)
        self.assertEqual(d.description, 'A dictionary.')
        self.assertEqual(d._items, self.valid_content_promoted)
Ejemplo n.º 9
0
    def test_init_default(self):

        d = Dictionary()
        self.assertEqual(d.description, 'A dictionary.')
        self.assertEqual(d._items, {})
Ejemplo n.º 10
0
    def test_dataclass_properties(self):

        d = Dictionary()
        self.assertEqual(d.CLASS, 'dictionary')
        self.assertEqual(d.GROUP, 'core')
        self.assertEqual(d.VERSION, 1)
Ejemplo n.º 11
0
    def test_iter(self):

        d = Dictionary(self.valid_content)
        for key in iter(d):
            self.assertTrue(key in d._items)
Ejemplo n.º 12
0
    def test_to_dict(self):

        d = Dictionary(self.valid_content)
        self.assertEqual(d.to_dict(), self.valid_content_serialised)
Ejemplo n.º 13
0
    def test_delitem(self):

        d = Dictionary(items=self.valid_content)
        self.assertTrue('int8_value' in d._items)
        del d['int8_value']
        self.assertFalse('int8_value' in d._items)
Ejemplo n.º 14
0
    def test_update(self):

        d = Dictionary()

        # valid content
        d.update(self.valid_content, apple=90, banana=7.8)
        for key, value in self.valid_content_promoted.items():
            self.assertEqual(d._items[key], value)
        self.assertEqual(d._items['apple'], np.int64(90))
        self.assertEqual(d._items['banana'], np.float64(7.8))

        # invalid content via dictionary
        with self.assertRaises(ValueError):
            d.update({'ABC': 5})

        with self.assertRaises(TypeError):
            d.update({'abc': []})

        # invalid argument via kwargs
        with self.assertRaises(ValueError):
            d.update(ABC=5)

        with self.assertRaises(TypeError):
            d.update(abc=[])
Ejemplo n.º 15
0
    def test_clear(self):

        d = Dictionary(self.valid_content)
        d.clear()
        self.assertEqual(d._items, {})
Ejemplo n.º 16
0
    def test_items(self):

        d = Dictionary(self.valid_content)
        r = dict(d.items())
        self.assertEqual(r, self.valid_content_promoted)
Ejemplo n.º 17
0
    def test_values(self):

        d = Dictionary(self.valid_content)
        values_original = set(self.valid_content_promoted.values())
        values_parsed = set(d.values())
        self.assertFalse(values_original - values_parsed)
Ejemplo n.º 18
0
    def test_keys(self):

        d = Dictionary(self.valid_content)
        keys_original = set(self.valid_content.keys())
        keys_parsed = set(d.keys())
        self.assertFalse(keys_original - keys_parsed)
Ejemplo n.º 19
0
    def test_len(self):

        d = Dictionary(self.valid_content)
        self.assertEqual(len(d), 15)