Example #1
0
    def test_datapackage_declare(self):
        from tempfile import NamedTemporaryFile
        import datapackage
        from os import unlink

        doc = MetatabDoc(test_data('datapackage_ex2.csv'))

        d = doc.as_dict()

        f = open('/tmp/package.json', 'w')  # NamedTemporaryFile(delete=False)
        f.write(json.dumps(d, indent=4))
        f.close()

        try:
            dp = datapackage.DataPackage(f.name)
            dp.validate()
        except:
            with open(f.name) as f2:
                print(f2.read())
            raise

        print(f.name)
        # unlink(f.name)

        doc = MetatabDoc(test_data('example1.csv'))

        from metatab.datapackage import convert_to_datapackage

        print(json.dumps(convert_to_datapackage(doc), indent=4))
Example #2
0
    def test_serializer(self):

        return

        doc = MetatabDoc(test_data('schema.csv'))
        d = doc.as_dict()

        s = Serializer()
        s.load_declarations(d)

        sections = defaultdict(list)

        for e in s.semiflatten(d):
            print(e)

        return

        for e in sorted(s.serialize(d)):
            has_int = any(isinstance(ki, int) for ki in e[0])
            key_no_int = tuple(ki for ki in e[0] if not isinstance(ki, int))
            print(key_no_int)
            pr = '.'.join(key_no_int[-2:])
            t = Term(pr, e[1], row=0, col=0, file_name=None)
            section = s.decl['terms'].get(t.join(), {}).get('section', 'Root')

            sections[section].append(t)

        return

        for k, v in sections.items():
            print("=====", k)
            for t in v:
                print(t)
Example #3
0
    def test_children(self):

        doc = MetatabDoc(test_data('children.csv'))

        for t in doc.terms:
            print(t)

        import json
        print(json.dumps(doc.as_dict(), indent=4))

        for t in doc.as_dict()['parent']:
            self.assertEquals(
                {
                    'prop1': 'prop1',
                    'prop2': 'prop2',
                    '@value': 'parent'
                }, t)
Example #4
0
    def test_sections(self):

        doc = MetatabDoc()

        s = doc.new_section("SectionOne", "A B C".split())

        print(doc.sections)

        print(doc.as_dict())
Example #5
0
    def test_includes(self):

        doc = MetatabDoc(test_data('include1.csv'))
        d = doc.as_dict()

        for t in doc['root'].terms:
            print(t)

        print(d)

        self.assertEquals(
            ['Include File 1', 'Include File 2', 'Include File 3'], d['note'])

        self.assertTrue(any('include2.csv' in e for e in d['include']))
        self.assertTrue(any('include3.csv' in e for e in d['include']))
Example #6
0
    def test_parse_everything(self):

        all = [
            'example1.csv', 'example2.csv', 'example1-web.csv', 'include1.csv',
            'include2.csv', 'include3.csv', 'children.csv', 'children2.csv',
            'datapackage_ex1.csv', 'datapackage_ex1_web.csv',
            'datapackage_ex2.csv', 'issue1.csv'
        ]

        all = ['example1.csv']

        for fn in all:

            print('Testing ', fn)

            path = test_data(fn)

            json_path = test_data('json', fn.replace('.csv', '.json'))

            with open(path) as f:

                doc = MetatabDoc(path)
                d = doc.as_dict()

                if not exists(json_path):
                    with open(json_path, 'w') as f:
                        print("Writing", json_path)
                        json.dump(d, f, indent=4)

                with open(json_path) as f:
                    d2 = json.load(f)

                #import json
                #print(json.dumps(d, indent=4))

                self.compare_dict(d, d2)