def test_nested_element(self):
     element = AlphaElement(DictElement({'a': 'b',
                                         'c': MetadataElement('d')}))
     mock = MagicMock()
     element.accept(mock)
     mock.visit_alpha.assert_called()
     mock.visit_metadata.assert_called()
示例#2
0
    def test_visitation(self):

        element = AlphaElement({
            'foo':
            TaxonomyElement({
                'bar':
                PCOAElement({
                    'baz': MetadataElement('path to stuff!'),
                    'bart': PCOAElement({'a': 'b'})
                })
            })
        })

        class MockVisitor(ConfigElementVisitor):
            def visit_alpha(self, element):
                element.data = 725

            def visit_taxonomy(self, element):
                element.data = 'taxonomy!'

            def visit_pcoa(self, element):
                element.data = element.copy()
                element.data.update({'c': 'cookie'})

            def visit_metadata(self, element):
                element.data = ConfigElementVisitor

        element.accept(MockVisitor())

        self.assertEqual(element.gets().data, 725)
        self.assertEqual(element.gets('foo').data, 'taxonomy!')
        first_pcoa = element.gets('foo', 'bar')
        self.assertDictEqual(
            first_pcoa.data, {
                'baz': MetadataElement('path to stuff!'),
                'bart': PCOAElement({'a': 'b'}),
                'c': 'cookie',
            })
        self.assertEqual(
            element.gets('foo', 'bar', 'baz').data, ConfigElementVisitor)
        metadata = element.gets('foo', 'bar', 'baz').data
        self.assertEqual(metadata, ConfigElementVisitor)
        self.assertDictEqual(
            element.gets('foo', 'bar', 'bart').data, {
                'a': 'b',
                'c': 'cookie'
            })
示例#3
0
    def test_updates(self):
        element = DictElement({'a': DictElement({'b': None})})
        element.updates(MetadataElement('Arg'), 'a', 'b')
        self.assertEqual('Arg', element.gets('a', 'b'))

        element.updates('Arg2', 'a', 'b', 'c', 'd', 'e')
        self.assertEqual('Arg2', element.gets('a', 'b', 'c', 'd', 'e'))
        element.updates('Arg3', 'c', 'd')
        self.assertEqual('Arg3', element['c']['d'])

        element.updates(
            DictElement({'a': DictElement({'c': MetadataElement('Arg4')})}))
        self.assertEqual('Arg3', element['c']['d'])
        self.assertEqual('Arg4', element['a']['c'])

        exp = {'bar': {'qux': 'corge', 'hoge': 'piyo'}}
        foo = DictElement({'bar': DictElement({'qux': 'corge'})})
        foo.updates('piyo', 'bar', 'hoge')
        self.assertDictEqual(exp, foo)
        foo = DictElement({'bar': DictElement({'qux': 'corge'})})
        foo.updates({'bar': DictElement({'hoge': 'piyo'})})
        self.assertDictEqual(exp, foo)
        exp = {'bar': {'qux': 'corge', 'hoge': 'fuga'}}
        foo.updates('fuga', 'bar', 'hoge')
        self.assertDictEqual(exp, foo)
        foo.updates('waldo', 'bar', 'garp', 'grault')
        exp = {
            'bar': {
                'qux': 'corge',
                'hoge': 'fuga',
                'garp': {
                    'grault': 'waldo'
                }
            }
        }
        self.assertDictEqual(exp, foo)
        foo = DictElement()
        foo.updates({'datasets': {'garply': 'grault'}})
        self.assertDictEqual({'datasets': {'garply': 'grault'}}, foo)

        foo = DictElement()
        foo.updates({'datasets': AlphaElement({'garply': 'grault'})})
        self.assertDictEqual({'datasets': {'garply': 'grault'}}, foo)
        self.assertIsInstance(foo['datasets'], AlphaElement)
示例#4
0
 def setUp(self):
     super().setUp()
     faith_pd_values = [1, 2, 3, 4]
     faith_pd_index = ['s01', 's02', 's04', 's05']
     shannon_values = [7.24, 9.05, 8.25]
     shannon_index = ['s01', 's02', 'sOther']
     metadata = MockMetadataElement(
         pd.DataFrame(
             {
                 'age_cat': ['30s', '40s', '50s', '30s', '30s'],
                 'num_var': [3, 4, 5, 6, 7],
             },
             index=['s01', 's02', 's04', 's05', 'sOther']))
     self.resources = DictElement({
         'datasets':
         DictElement({
             'dataset1':
             DictElement({
                 '__metadata__':
                 metadata,
                 '__alpha__':
                 AlphaElement({
                     'faith_pd':
                     pd.Series(faith_pd_values, index=faith_pd_index),
                     'shannon':
                     pd.Series(shannon_values, index=shannon_index),
                 })
             }),
             'dataset2':
             DictElement({
                 '__metadata__': metadata,
             }),
         }),
     })
     self.resources.accept(TrivialVisitor())
     self.res_patcher = patch(
         'microsetta_public_api.api.plotting.get_resources')
     self.mock_resources = self.res_patcher.start()
     self.mock_resources.return_value = self.resources