Example #1
0
def test_dump():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)
    tmp = tempfile.mktemp()
    with open(tmp, 'w', encoding='utf8') as fp:
        biocjson.dump(collection, fp)
    with open(tmp, encoding='utf8') as fp:
        collection = biocjson.load(fp)
    assert_everything(collection)
Example #2
0
def test_dump(tmp_path):
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    filepath = tmp_path / 'foo.json'
    with open(filepath, 'w', encoding='utf8') as fp:
        biocjson.dump(collection, fp)
    with open(filepath, encoding='utf8') as fp:
        collection = biocjson.load(fp)
    assert_everything(collection)
Example #3
0
def test_iterreader_sentence():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    s = io.StringIO()
    with biocjson.iterwriter(s) as writer:
        for sen in collection.documents[1].passages[0].sentences:
            writer.write(sen)

    del collection.documents[1].passages[0].sentences[:]
    with pytest.raises(IndexError):
        assert_everything(collection)

    with biocjson.iterreader(io.StringIO(s.getvalue())) as reader:
        for obj in reader:
            collection.documents[1].passages[0].add_sentence(obj)

    assert_everything(collection)


#
#
# def test_level():
#     with pytest.raises(ValueError):
#         BioCJsonIterReader(io.StringIO(), level=-1)
Example #4
0
def test_toJSON():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)
    obj = biocjson.toJSON(collection)
    assert obj['documents'][0]['id'] == '1'

    with pytest.raises(TypeError):
        biocjson.toJSON({})
Example #5
0
def test_jsoniterwriter(tmp_path):
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    filepath = tmp_path / 'foo.json'
    with biocjson.iterwriter(filepath) as writer:
        for doc in collection.documents:
            writer.write(doc)

    del collection.documents[:]
    with biocjson.iterreader(filepath) as reader:
        for doc in reader:
            collection.add_document(doc)
    assert_everything(collection)
Example #6
0
    def __load_collection_json(bioc_json: str, is_file: bool = True):
        """load a json bioc collection .
        It will return a bioc collection object.

        :param bioc_json: a str path to a bioc file or a bioc input json string
        :param is_file: if True bioc_input is a path else it is a string
        :returns:  a bioc collection object
        """
        if is_file:
            with open(bioc_json, 'r') as fp:
                collection = biocjson.load(fp)
            return (collection)
        else:
            collection = biocjson.loads(bioc_json)
            return (collection)
Example #7
0
def test_iterreader_passage():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    s = io.StringIO()
    with biocjson.iterwriter(s) as writer:
        for p in collection.documents[0].passages:
            writer.write(p)

    del collection.documents[0].passages[:]
    with pytest.raises(IndexError):
        assert_everything(collection)

    with biocjson.iterreader(io.StringIO(s.getvalue())) as reader:
        for obj in reader:
            collection.documents[0].add_passage(obj)

    assert_everything(collection)
Example #8
0
def test_BioCJsonIterReader_sentence():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    s = io.StringIO()
    writer = BioCJsonIterWriter(s, level=bioc.SENTENCE)
    for sen in collection.documents[1].passages[0].sentences:
        writer.write(sen)

    del collection.documents[1].passages[0].sentences[:]
    with pytest.raises(IndexError):
        assert_everything(collection)

    reader = BioCJsonIterReader(io.StringIO(s.getvalue()), level=bioc.SENTENCE)
    for obj in reader:
        collection.documents[1].passages[0].add_sentence(obj)

    assert_everything(collection)
Example #9
0
def test_BioCJsonIterReader_document():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    s = io.StringIO()
    writer = BioCJsonIterWriter(s, level=bioc.DOCUMENT)
    for doc in collection.documents:
        writer.write(doc)

    del collection.documents[:]
    with pytest.raises(IndexError):
        assert_everything(collection)

    reader = BioCJsonIterReader(io.StringIO(s.getvalue()), level=bioc.DOCUMENT)
    for obj in reader:
        collection.add_document(obj)

    assert_everything(collection)
Example #10
0
def test_level():
    with pytest.raises(ValueError):
        BioCJsonIterWriter(io.StringIO(), level=-1)

    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)

    with pytest.raises(ValueError):
        writer = BioCJsonIterWriter(io.StringIO(), level=bioc.SENTENCE)
        writer.write(collection.documents[0])

    with pytest.raises(ValueError):
        writer = BioCJsonIterWriter(io.StringIO(), level=bioc.PASSAGE)
        writer.write(collection.documents[0])

    with pytest.raises(ValueError):
        writer = BioCJsonIterWriter(io.StringIO(), level=bioc.DOCUMENT)
        writer.write(collection.documents[0].passages[0])
Example #11
0
def test_load():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)
    assert_everything(collection)
Example #12
0
def test_dumps():
    with open(file, encoding='utf8') as fp:
        collection = biocjson.load(fp)
    s = biocjson.dumps(collection)
    collection = biocjson.loads(s)
    assert_everything(collection)