Ejemplo n.º 1
0
    def test_report(self):
        from pyclpa.cli import report

        with self.assertRaises(ParserError):
            report(Mock(args=[]))

        with self.assertRaises(ParserError):
            report(Mock(args=["format=csv"]))

        args = [self.data_path("KSL.tsv").as_posix()]

        with capture(report, Mock(args=args)) as out:
            self.assertIn("Convertible sounds", out)

        out = self.tmp_path("test.csv")
        report(Mock(args=args + ["format=csv", "outfile=" + out.as_posix()]))
        self.assertEqual(self._read_tsv(out), self._read_tsv(self.data_path("KSL_report.csv")))

        with capture(report, Mock(args=args + ["format=csv"])) as out:
            if hasattr(out, "decode"):
                out = out.decode("utf8")
            self.assertIn("existing", out)

        with capture(report, Mock(args=args + ["format=cldf"])) as out:
            if hasattr(out, "decode"):
                out = out.decode("utf8")
            self.assertIn("CLPA_TOKENS", out)

        out = self.tmp_path("test.md")
        report(Mock(args=args + ["outfile=" + out.as_posix()]))
        self.assertTrue(out.exists())
Ejemplo n.º 2
0
    def test_report(self):
        from pyclpa.cli import report

        with self.assertRaises(ParserError):
            report(Mock(args=[]))

        with self.assertRaises(ParserError):
            report(Mock(args=['format=csv']))

        args = [self.data_path('KSL.tsv').as_posix()]

        with capture(report, Mock(args=args)) as out:
            self.assertIn('Convertible sounds', out)

        out = self.tmp_path('test.csv')
        report(Mock(args=args + ['format=csv', 'outfile=' + out.as_posix()]))
        self.assertEqual(self._read_tsv(out),
                         self._read_tsv(self.data_path('KSL_report.csv')))

        with capture(report, Mock(args=args + ['format=csv'])) as out:
            if hasattr(out, 'decode'):
                out = out.decode('utf8')
            self.assertIn('existing', out)

        with capture(report, Mock(args=args + ['format=cldf'])) as out:
            if hasattr(out, 'decode'):
                out = out.decode('utf8')
            self.assertIn('CLPA_TOKENS', out)

        out = self.tmp_path('test.md')
        report(Mock(args=args + ['outfile=' + out.as_posix()]))
        self.assertTrue(out.exists())
Ejemplo n.º 3
0
    def test_ArgumentParser(self):
        from clldutils.clilib import ArgumentParser, ParserError

        def cmd(args):
            """
            docstring
            """
            if len(args.args) < 1:
                raise ParserError('not enough arguments')
            print(args.args[0])

        parser = ArgumentParser('pkg', cmd)

        with capture(parser.main, args=['help', 'cmd']) as out:
            self.assertIn('docstring', out)

        with capture(parser.main, args=['cmd', 'arg']) as out:
            self.assertIn('arg', out)

        self.assertEqual(parser.main(args=['cmd', 'arg']), 0)

        with capture(parser.main, args=['cmd']) as out:
            self.assertIn('not enough arguments', out)

        with capture_all(parser.main, args=['x']) as res:
            self.assertNotEqual(res[0], 0)
            self.assertTrue(res[1].startswith('invalid'))
Ejemplo n.º 4
0
 def test_lookup(self):
     from pyconcepticon.commands import lookup
     with capture(
             lookup, MagicMock(full_search=True, args=['sky'], language='en')) as out:
         self.assertIn('1732', out)
     with capture(lookup, MagicMock(args=['sky'], language='en')) as out:
         self.assertIn('1732', out)
Ejemplo n.º 5
0
 def test_bibfiles(self):
     c = self.api.bibfiles
     with capture(c.roundtrip_all) as out:
         self.assertIn('a.bib', out)
     with capture(c['b.bib'].show_characters) as out:
         self.assertIn('CJK UNIFIED IDEOGRAPH', out)
     abib = c[0]
     self.assertEqual(len(list(abib.iterentries())), 3)
     assert abib.size
     assert abib.mtime
Ejemplo n.º 6
0
    def test_show(self):
        from pyglottolog.commands import show

        with capture(show, self._args('**a:key**')) as out:
            self.assertIn('@misc'.encode('utf8') if PY2 else '@misc', out)

        with capture(show, self._args('a:key')) as out:
            self.assertIn('@misc'.encode('utf8') if PY2 else '@misc', out)

        with capture(show, self._args('abcd1236')) as out:
            self.assertIn(
                'Classification'.encode('utf8') if PY2 else 'Classificat', out)
Ejemplo n.º 7
0
    def test_union(self):
        from pyconcepticon.commands import union
        Args = namedtuple('Args', ['data', 'args'])

        with capture(
                union,
                Args(data='', args=['Swadesh-1955-100', 'Swadesh-1952-200'])) as out:
            self.assertEqual(208, len(out.split('\n')))

        with capture(
                union,
                Args(data='', args=['Swadesh-1952-200', 'Matisoff-1978-200'])) as out:
            self.assertEqual(301, len(out.split('\n')))
Ejemplo n.º 8
0
    def test_Collection(self):
        from pyglottolog.monsterlib._bibfiles_db import Database

        if not PY2:  # pragma: no cover
            return

        db = self.tmp_path('test.sqlite')
        with capture(self.api.bibfiles.to_sqlite, db) as out:
            self.assertIn('ENTRYTYPE', out)
        with capture(self.api.bibfiles.to_sqlite, db) as out:
            pass
        with capture(self.api.bibfiles.to_sqlite, db, rebuild=True) as out:
            pass

        db = Database(db, self.api.bibfiles)
        with capture(db.recompute, reload_priorities=self.api.bibfiles) as out:
            self.assertEqual(len(out.splitlines()), 34)
        with capture(db.is_uptodate, self.api.bibfiles[1:],
                     verbose=True) as out:
            self.assertEqual(len(out.splitlines()), 3)
        db.to_bibfile(self.tmp_path('out.bib'))
        db.to_csvfile(self.tmp_path('out.csv'))
        db.to_replacements(self.tmp_path('out.json'))
        self.assertEqual(db.to_hhmapping(), {'s:Karang:Tati-Harzani': 41999})
        with capture(db.trickle) as out:
            self.assertIn('2 changed 1 added in a', out)
        key, (entrytype, fields) = db[('b.bib', 'arakawa97')]
        self.assertEqual(entrytype, 'article')
        self.assertEqual(fields['volume'], '16')
        with capture(db.stats) as out:
            pass

        for attr in ['splits', 'merges', 'identified', 'combined']:
            with capture(getattr(db, 'show_' + attr)) as out:
                pass
Ejemplo n.º 9
0
    def test_map(self):
        if self.api.repos.exists():
            with capture(self.api.map,
                         self.fixture_path('conceptlist.tsv')) as out:
                self.assertIn('CONCEPTICON_ID', out)

            self.assertGreater(len(self.api.conceptsets['217'].concepts), 8)
Ejemplo n.º 10
0
    def test_main(self):
        from pyglottolog.monster import main

        with capture(main, repos=self.repos) as out:
            self.assertEqual(len(out.splitlines()), 70)
            self.assertIn('2 splitted', out)
            self.assertIn('2 merged', out)
Ejemplo n.º 11
0
    def test_fts(self):
        from pyglottolog.commands import refindex, refsearch, langindex, langsearch

        with self.assertRaises(ValueError):
            refsearch(self._args('Harzani year:1334'))

        refindex(self._args())
        with capture(refsearch, self._args('Harzani year:1334')) as out:
            self.assertIn("'Abd-al-'Ali Karang", out)

        langindex(self._args())
        with capture(langsearch, self._args('id:abcd*')) as out:
            self.assertIn("abcd1234", out)

        with capture(langsearch, self._args('classification')) as out:
            self.assertIn("abcd1234", out)
Ejemplo n.º 12
0
    def test_intersection(self):
        from pyconcepticon.commands import intersection
        Args = namedtuple('Args', ['data', 'args'])

        with capture(
                intersection,
                Args(data='', args=['Swadesh-1955-100', 'Swadesh-1952-200'])) as out:
            self.assertEqual(94, len(out.split('\n')))
Ejemplo n.º 13
0
    def test_check(self):
        from pyclpa.cli import check

        with self.assertRaises(ParserError):
            check(Mock(args=[]))

        with capture(check, Mock(args=["abcd"])) as out:
            self.assertIn("?", out)
Ejemplo n.º 14
0
    def test_monster(self):
        from pyglottolog.cli import monster

        if not PY2:  # pragma: no cover
            return

        with capture(monster, Mock(repos=self.repos)) as out:
            assert out
Ejemplo n.º 15
0
    def test_tree(self):
        from pyglottolog.cli import tree

        with capture(tree, Mock(repos=self.repos,
                                args=['abcd1234', 'language'])) as out:
            out = out.decode('utf8')
            self.assertIn('<l>', out)
            self.assertNotIn('<d>', out)
Ejemplo n.º 16
0
    def test_monster(self):
        from pyglottolog.commands import bib

        if not PY2:  # pragma: no cover
            return

        with capture(bib, self._args()) as out:
            assert out
Ejemplo n.º 17
0
    def test_check(self):
        from pyclpa.cli import check

        with self.assertRaises(ParserError):
            check(Mock(args=[]))

        with capture(check, Mock(args=['abcd'])) as out:
            self.assertIn('?', out)
Ejemplo n.º 18
0
    def test_tree(self):
        from pyglottolog.cli import tree

        with capture(tree, Mock(repos=self.repos, args=['abcd1234', 'language'])) as out:
            if not isinstance(out, text_type):
                out = out.decode('utf8')
            self.assertIn('<l>', out)
            self.assertNotIn('<d>', out)
Ejemplo n.º 19
0
    def test_create(self):
        from pyglottolog.commands import create

        with capture(create, self._args('abcd1234', 'new name',
                                        'language')) as out:
            self.assertIn('Info written', out)
            self.assertIn(
                'new name',
                [c.name for c in self.api.languoid('abcd1234').children])
Ejemplo n.º 20
0
    def test_main(self):
        from pyglottolog.monster import main

        if not PY2:  # pragma: no cover
            return

        with capture(main, repos=self.repos) as out:
            self.assertEqual(len(out.splitlines()), 70)
            self.assertIn('2 splitted', out)
            self.assertIn('2 merged', out)
Ejemplo n.º 21
0
    def test_main(self):
        from pyglottolog.monster import compile

        if not PY2:  # pragma: no cover
            return

        with capture(compile, self.api) as out:
            self.assertEqual(len(out.splitlines()), 43)
            self.assertIn('2 splitted', out)
            self.assertIn('2 merged', out)
Ejemplo n.º 22
0
 def test_operation(self):
     wl = write('csv',
                make_wordlist(),
                _outdir=self.tmp_path(),
                delimiter=',')
     args = '-o {0} operation distances csv:IWordlist:{1}:delimiter=,'.format(
         self.tmp_path(), wl)
     with capture(cli.main, *args.split()) as out:
         self.assertIn('written', out)
     self.assertTrue(bool(list(self.tmp_path().glob('*.json'))))
Ejemplo n.º 23
0
    def test_check(self):
        from pyglottolog.commands import check

        with capture(check, self._args('refs')):
            pass

        with capture(check, self._args()) as out:
            self.assertIn('family', out)
        for call in self.log.error.call_args_list:
            self.assertIn('unregistered glottocode', call[0][0])
        self.assertEqual(self.log.error.call_count, 4)

        copytree(self.api.tree.joinpath('abcd1234', 'abcd1235'),
                 self.api.tree.joinpath('abcd1235'))

        with capture(check, self._args()):
            self.assertIn(
                'duplicate glottocode',
                ''.join(c[0][0] for c in self.log.error.call_args_list))
            self.assertEqual(self.log.error.call_count, 6)
Ejemplo n.º 24
0
    def test_datasets(self):
        from pycldf.cli import datasets

        with self.assertRaises(ParserError):
            datasets(MagicMock(args=MagicMock()))

        with self.assertRaises(ParserError):
            datasets(MagicMock(args=[self.tmp_path('new').as_posix()]))

        with capture(datasets, MagicMock(args=[FIXTURES.as_posix(), "dc:title"])) as out:
            self.assertIn('a cldf dataset', out)
Ejemplo n.º 25
0
    def test_tree(self):
        from pyglottolog.commands import tree, ParserError

        with self.assertRaises(ParserError):
            tree(self._args())

        with self.assertRaises(ParserError):
            tree(self._args('xyz'))

        with capture(tree, self._args('abc', 'language')) as out:
            if not isinstance(out, text_type):
                out = out.decode('utf8')
            self.assertIn('language', out)
            self.assertNotIn('dialect', out)
Ejemplo n.º 26
0
    def test_stats(self):
        from pycldf.cli import stats

        with self.assertRaises(ParserError):
            stats(MagicMock(args=MagicMock()))

        with self.assertRaises(ParserError):
            stats(MagicMock(args=[self.tmp_path('new').as_posix()]))

        ds = make_dataset(rows=[[1, 1, 1, 1]])
        ds.write(self.tmp_path(), archive=True)
        with capture(stats,
                     MagicMock(args=[self.tmp_path('test.zip').as_posix()])) as out:
            self.assertIn('languages: 1', out)

        with capture(stats,
                     MagicMock(args=[
                         FIXTURES.joinpath('ds1.csv-metadata.json').as_posix()])) as out:
            self.assertIn('languages: 1', out)

        with capture(stats,
                     MagicMock(args=[FIXTURES.joinpath('ds1.csv').as_posix()])) as out:
            self.assertIn('languages: 1', out)
Ejemplo n.º 27
0
    def test_lff(self):
        from pyglottolog.commands import tree2lff, lff2tree

        tree2lff(self._args())
        with self.api.build_path('dff.txt').open(encoding='utf8') as fp:
            dfftxt = fp.read().replace('dialect', 'Dialect Name')
        with self.api.build_path('dff.txt').open('w', encoding='utf8') as fp:
            fp.write(dfftxt)
        with capture(lff2tree, self._args()) as out:
            self.assertIn('git status', out)
        self.assertEqual(self.api.languoid('abcd1236').name, 'Dialect Name')
        tree2lff(self._args())
        with self.api.build_path('dff.txt').open(encoding='utf8') as fp:
            self.assertEqual(dfftxt, fp.read())
Ejemplo n.º 28
0
    def test_capture_all(self):
        from clldutils.testing import capture_all, capture

        def func():
            print('hello')
            print('world', file=sys.stderr)
            return 5

        with capture_all(func) as res:
            ret, out, err = res
            self.assertEqual(ret, 5)
            self.assertEqual(out.strip(), 'hello')
            self.assertEqual(err.strip(), 'world')

        with capture(func) as out:
            self.assertEqual(out.strip(), 'hello')
Ejemplo n.º 29
0
def test_keyid():
    from pyglottolog.monsterlib._libmonster import keyid

    for fields, res in [
        ({}, '__missingcontrib__'),
        (dict(author='An Author'), 'author_no-titlend'),
        (dict(editor='An Author'), 'author_no-titlend'),
        (dict(author='An Author', title='A rather long title'), 'author_rather-longnd'),
        (dict(author='An Author', title='Title', year='2014'), 'author_title2014'),
        (dict(author='An Author', volume='IV'), 'author_no-titleivnd'),
        (dict(author='An Author', extra_hash='a'), 'author_no-titlenda'),
    ]:
        assert_equal(keyid(fields, {}), res)

    with capture(keyid, dict(author='An Author and '), {}) as out:
        assert 'Unparsed' in out
Ejemplo n.º 30
0
def test_keyid():
    from pyglottolog.monsterlib._libmonster import keyid

    for fields, res in [
        ({}, '__missingcontrib__'),
        (dict(author='An Author'), 'author_no-titlend'),
        (dict(editor='An Author'), 'author_no-titlend'),
        (dict(author='An Author',
              title='A rather long title'), 'author_rather-longnd'),
        (dict(author='An Author', title='Title',
              year='2014'), 'author_title2014'),
        (dict(author='An Author', volume='IV'), 'author_no-titleivnd'),
        (dict(author='An Author', extra_hash='a'), 'author_no-titlenda'),
    ]:
        assert_equal(keyid(fields, {}), res)

    with capture(keyid, dict(author='An Author and '), {}) as out:
        assert 'Unparsed' in out
Ejemplo n.º 31
0
    def test_link(self):
        from pyconcepticon.commands import link

        with self.assertRaises(ParserError):
            link(Mock(args=['.'], data=None))

        def nattr(p, attr):
            return len(nfilter([getattr(i, attr, None) for i in read_all(p)]))

        test = self.tmp_path('test.tsv')
        copy(self.fixture_path('conceptlist.tsv'), test)
        self.assertEqual(nattr(test, 'CONCEPTICON_GLOSS'), 0)
        link(Mock(args=[test], data=None))
        self.assertEqual(nattr(test, 'CONCEPTICON_GLOSS'), 1)

        copy(self.fixture_path('conceptlist2.tsv'), test)
        with capture(link, Mock(args=[test], data=None)) as out:
            self.assertIn('unknown CONCEPTICON_GLOSS', out)
            self.assertIn('mismatch', out)
Ejemplo n.º 32
0
    def test_Collection(self):
        from pyglottolog.monsterlib._bibfiles import Collection
        from pyglottolog.monsterlib._bibfiles_db import Database

        if not PY2:  # pragma: no cover
            return

        c = Collection(self.references.joinpath('bibtex'))
        with capture(c.check_all) as out:
            self.assertNotIn('invalid', out)
        with capture(c.roundtrip_all) as out:
            self.assertIn('a.bib', out)
        with capture(c['b.bib'].show_characters) as out:
            self.assertIn('CJK UNIFIED IDEOGRAPH', out)
        abib = c[0]
        self.assertEqual(len(list(abib.iterentries())), 2)
        assert abib.size
        assert abib.mtime

        db = self.tmp_path('test.sqlite').as_posix()
        with capture(c.to_sqlite, db) as out:
            self.assertIn('entries total', out)
        with capture(c.to_sqlite, db) as out:
            pass
        with capture(c.to_sqlite, db, rebuild=True) as out:
            pass

        db = Database(db)
        with capture(db.recompute, reload_priorities=c) as out:
            self.assertEqual(len(out.splitlines()), 32)
        with capture(db.is_uptodate, c[1:], verbose=True) as out:
            self.assertEqual(len(out.splitlines()), 3)
        db.to_bibfile(self.tmp_path('out.bib'))
        db.to_csvfile(self.tmp_path('out.csv'))
        db.to_replacements(self.tmp_path('out.json'))
        self.assertEqual(db.to_hhmapping(), {'s:Karang:Tati-Harzani': 41999})
        with capture(db.trickle, c) as out:
            self.assertIn('2 changed 0 added in a.bib', out)
        key, (entrytype, fields) = db[('b.bib', 'arakawa97')]
        self.assertEqual(entrytype, 'article')
        self.assertEqual(fields['volume'], '16')
        with capture(db.stats) as out:
            pass

        for attr in ['splits', 'merges', 'identified', 'combined']:
            with capture(getattr(db, 'show_' + attr)) as out:
                pass
Ejemplo n.º 33
0
    def test_attributes(self):
        from pyconcepticon.commands import attributes

        with capture(attributes, MagicMock(data=None)) as out:
            self.assertIn('Occurrences', out)
Ejemplo n.º 34
0
    def test_metadata(self):
        from pyglottolog.commands import metadata

        with capture(metadata, self._args()) as out:
            self.assertIn("longitude", out)
    def test_attributes(self):
        from pyconcepticon.commands import attributes

        with capture(attributes, MagicMock()) as out:
            self.assertIn('Columns', out)
Ejemplo n.º 36
0
 def test_help(self):
     with capture(cli.main, 'help', 'operation') as out:
         self.assertIn('positional arguments', out)
Ejemplo n.º 37
0
    def test_monster(self):
        from pyglottolog.cli import monster

        with capture(monster, Mock(repos=self.repos)) as out:
            assert out
Ejemplo n.º 38
0
    def test_newick(self):
        from pyglottolog.commands import newick

        with capture(newick, self._args('abcd1235')) as out:
            self.assertIn('language', out)