Exemplo n.º 1
0
def test_import_empty_taxonomy_throws():
    backend = make_backend(Mock())

    # We don't know how to locate the taxonomy with no root, so there's not
    # much option other than to throw.  Remember there could be multiple
    # taxonomies in the database.
    with raises(occubrow.errors.EmptyTaxonomyError):
        backend.import_taxonomy(INPUT_TAXONOMY)
def test_precedes_relationship(neo4j_driver):
    repository = RealNeo4jRepository(neo4j_driver)
    backend = make_backend(repository)

    for phrase in phrases:
        repository.add_precedes_links(phrase)

    assert backend.graph_matches(EXPECTED_DATA)
def test_can_retrieve_entire_graph(neo4j_driver):
    backend = make_backend(RealNeo4jRepository(neo4j_driver))

    with neo4j_driver.session() as session:
        session.run("MATCH (a) DETACH DELETE a")
        session.run(
            "CREATE (a:Person {name:'Alice'})-[:KNOWS]->(b:Person {name:'Bob'})"
        )
        assert backend.graph_matches(EXPECTED_DATA)
Exemplo n.º 4
0
def test_single_root_taxonomy_imports():
    input_data = {'id': 'A_Single_Root'}

    mock_repository = Mock()
    runmock = mock_repository.run_statement
    backend = make_backend(mock_repository)
    backend.import_taxonomy(input_data)

    runmock.assert_called_once_with('CREATE (t:Taxon {content: $content})',
                                    content='A_Single_Root')
Exemplo n.º 5
0
def test_import_worked(neo4j_driver):
    input_data = {
        'id':
        'Music',
        'children': [{
            'id': 'Rock',
            'children': []
        }, {
            'id': 'Classical',
            'children': []
        }]
    }

    expected_graph = {
        'directed':
        True,
        'graph': {},
        'links': [{
            'source': 0,
            'target': 1,
            'type': 'SUPERCATEGORY_OF'
        }, {
            'source': 0,
            'target': 2,
            'type': 'SUPERCATEGORY_OF'
        }],
        'multigraph':
        False,
        'nodes': [{
            'content': 'Music',
            'id': 0,
            'label': 'Taxon'
        }, {
            'content': 'Rock',
            'id': 1,
            'label': 'Taxon'
        }, {
            'content': 'Classical',
            'id': 2,
            'label': 'Taxon'
        }]
    }

    backend = make_backend(RealNeo4jRepository(neo4j_driver))
    backend.import_taxonomy(input_data)
    assert backend.graph_matches(expected_graph)
def test_can_retrieve_entire_graph_mocked():
    mock_neo4j_repository = unittest.mock.Mock()

    mock_neo4j_repository.pull_graph.return_value = {
        'nodes': [
            Node(1, 'Person', {'name': 'Alice'}),
            Node(2, 'Person', {'name': 'Bob'})
        ],
        'rels': [Relationship(1, 2, {}, 'KNOWS')]
    }

    backend = make_backend(mock_neo4j_repository)

    # This is just going to no-op
    mock_neo4j_repository.execute("MATCH (a) DETACH DELETE a")
    mock_neo4j_repository.execute(
        "CREATE (a:Person {name:'Alice'})-[:KNOWS]->(b:Person {name:'Bob'})")

    assert backend.graph_matches(EXPECTED_DATA)
Exemplo n.º 7
0
def test_small_taxonomy_imports():
    input_data = {
        'id':
        'Music',
        'children': [{
            'id': 'Rock',
            'children': []
        }, {
            'id': 'Classical',
            'children': []
        }]
    }

    # For the isolated test, we check that the backend submits the correct
    # Cypher query to the repository.  We can't really test on raw strings
    # because we use NPD's feature of parameterized queries.  So we have to
    # shim the driver's "run" interface and test against that instead.
    #
    # The use of $properties here is a bit unfortunate because it's shared
    # knowledge between the tests & code.

    mock_repository = Mock()
    runmock = mock_repository.run_statement
    backend = make_backend(mock_repository)
    backend.import_taxonomy(input_data)

    calls = [
        call('CREATE (t:Taxon {content: $content})', content='Music'),
        call('CREATE (t:Taxon {content: $content})', content='Rock'),
        call('CREATE (t:Taxon {content: $content})', content='Classical'),
        call(
            'MATCH (t1:Taxon {content: $start_node}), (t2:Taxon {content: $end_node})\n                CREATE (t1)-[:SUPERCATEGORY_OF]->(t2)',
            end_node='Rock',
            start_node='Music'),
        call(
            'MATCH (t1:Taxon {content: $start_node}), (t2:Taxon {content: $end_node})\n                CREATE (t1)-[:SUPERCATEGORY_OF]->(t2)',
            end_node='Classical',
            start_node='Music')
    ]

    runmock.assert_has_calls(calls, any_order=True)
Exemplo n.º 8
0
def test_can_export_taxonomy_tree(neo4j_driver):
    backend = make_backend(RealNeo4jRepository(neo4j_driver))
    backend.import_taxonomy(input_data)
    root = 'Music'
    tree_data = backend.export_taxonomy_tree(root)
    assert tree_matches(tree_data, EXPECTED_EXPORT_DATA)
Exemplo n.º 9
0
def test_taxonomy_export():
    repository = get_mocked_repository()
    b = make_backend(repository)
    root = 'Music'
    tree_data = b.export_taxonomy_tree(root)
    assert tree_data == EXPECTED_EXPORT_DATA
Exemplo n.º 10
0
def test_get_tree():
    repository = Mock()
    backend = make_backend(repository)
    repository.pull_graph.return_value = PRELOADED_SENTENCES

    assert backend.get_token_tree('keep', 4, 0) == WANTED_DATA