Exemple #1
0
def test_circular_warning(recwarn):
    warnings.simplefilter("always")
    inner_doc = Node()
    main_doc = Node(data=inner_doc)
    inner_doc.set_data(main_doc)
    assert len(recwarn) == 1
    assert recwarn.pop(CircularReferenceWarning)
    def node_exist(node, variable=None):
        variable = str(variable) if variable is not None else "n"

        cypher_command = "{match} RETURN {variable}".format(
            match=NodeCommands(node).match_command(variable=variable,
                                                   node_type=node.node_type),
            variable=variable)

        results = CypherOperations._execute(
            [Operation(OperationType.RETRIEVE, command=cypher_command)])

        results_data = [
            row for sub_results in [result.data() for result in results]
            for row in sub_results
        ]

        if len(results_data) == 0:
            return None

        result_value = results_data[0][variable]

        if type(result_value) != Neo4jNode:
            return None  # maybe raise exception here

        node_properties = {
            Node.get_properties_mapping()[k]: v
            for k, v in expand(result_value).items()
        }
        node_properties["node_type"] = node.node_type

        return Node(**node_properties) == node
    def query_node(node_type=None, variable=None, **properties):
        results = []
        variable = str(variable) if variable is not None else "n"
        cypher_command = "{match} RETURN {variable}".format(
            match=NodeCommands.match_command_for_type(variable=variable,
                                                      node_type=node_type,
                                                      **properties),
            variable=variable)

        cypher_results = CypherOperations._execute(
            [Operation(OperationType.RETRIEVE, command=cypher_command)])

        results_data = [
            row
            for sub_results in [result.data() for result in cypher_results]
            for row in sub_results
        ]

        if len(results_data) == 0:
            return results

        for data in results_data:
            result_value = data[variable]

            if type(result_value) != Neo4jNode:
                continue  # maybe raise exception here

            node_properties = {
                Node.get_properties_mapping()[k]: v
                for k, v in expand(result_value).items()
            }

            results.append(Node(**node_properties))

        return results
Exemple #4
0
def test_ttl_is_numeric():
    with pytest.raises(Exception) as e_info:
        Node(ttl="NOPE")
        assert type(e_info) == CoreDocumentException
    Node(ttl=0)
    Node(ttl="0")
    Node(ttl="0.123")
    Node(ttl=.123)
Exemple #5
0
def test_hash():
    d = Node(TEST_DATA)
    dd = d.clone(new=False)
    assert dd == d
    assert hash(d) == hash(dd)
    assert hash(d) != hash(Node(TEST_DATA))
    dd.set_data(None)
    assert hash(d) != hash(dd)
Exemple #6
0
def test_init_stream_with_exc(tmpdir):
    str_value = dumps(TEST_VAL)
    val = tmpdir.mkdir("sub").join("value.json")
    val.write(str_value[0:len(str_value) - 2])
    with pytest.raises(Exception) as e_info:
        Node(data=val)
        assert type(e_info) == CoreDocumentException
Exemple #7
0
def test_encoding_warning(recwarn):
    encoding = "utf-16"
    bytes_val = dumps(TEST_VAL).encode(encoding)
    warnings.simplefilter("always")
    doc = Node(data=bytes_val)
    assert len(recwarn) == 1
    assert recwarn.pop(EncodingWarning)
    assert doc.encoding.lower() == encoding
Exemple #8
0
def test_children():
    parent = Node(Node(TEST_DATA), Node(TEST_DATA))
    assert len(parent.children()) == 2

    parent = Node(Node(TEST_DATA), Node(TEST_DATA))
    assert len(parent.children()) == 2

    parent = Node(Node(Node(TEST_DATA)))
    assert len(parent.children()) == 2

    parent = Node(dict(docs=Node(Node(TEST_DATA))))
    assert len(parent.children()) == 2

    parent = Node([dict(docs=Node(Node(TEST_DATA))), Node(TEST_DATA)])
    assert len(parent.children()) == 3

    parent = Node(dict(docs=[Node(Node(TEST_DATA)), Node(TEST_DATA)]))
    assert len(parent.children()) == 3

    parent = Node("STRING")
    assert (len(parent.children())) == 0

    parent = Node("STRING", Node("STRING"))
    assert (len(parent.children())) == 1
Exemple #9
0
def test_dict_from_str():
    dico = Node.from_str(dumps(dict(test=Node().toDict())))
    assert type(dico) == dict
    assert type(dico["test"]) == NodeBaseClass
Exemple #10
0
def test_init_stream(tmpdir):
    str_value = dumps(TEST_VAL)
    val = tmpdir.mkdir("sub").join("value.json")
    val.write(str_value)
    doc = Node(data=val)
    assert doc.get_data() == TEST_VAL
Exemple #11
0
def test_init_with_serialize_exc():
    val = dumps({"test": "value"})
    with pytest.raises(Exception) as e_info:
        Node(data=val[0:len(val) - 2])
        assert type(e_info) == CoreDocumentException
        assert "Unexpected character" in str(e_info)
Exemple #12
0
def test_list_from_str():
    lst = Node.from_str(dumps([Node().toDict(), Node()]))
    assert type(lst) == list
    for d in lst:
        assert type(d) == NodeBaseClass
Exemple #13
0
def test_new_from_str():
    d = Node(TEST_DATA)
    dd = Node.from_str(str(d), new=True)
    assert str(dd) != str(d)
Exemple #14
0
def test_init():
    doc = Node()
    assert doc.get_data() is None
    assert doc.id is not None
    assert type(doc.id) == str
    assert doc.key is None
    assert doc.encoding == "utf-8"

    id = str(uuid4())
    key = "this is a key"
    doc = Node(id=id, key=key, data=TEST_VAL)
    assert doc.id == id
    assert doc.key == key
    assert doc.get_data() == TEST_VAL

    doc = Node(data=TEST_DATA)
    assert doc.get_data() == TEST_DATA

    encoding = DEFAULT_ENCODING
    bytes_val = TEST_DATA.encode(encoding)
    doc = Node(data=TEST_DATA)
    assert doc.get_data() == bytes_val.decode(encoding)

    encoding = "utf-16"
    bytes_val = TEST_DATA.encode(encoding)
    doc = Node(data=bytes_val, encoding=encoding)
    assert doc.get_data() == bytes_val.decode(encoding)
Exemple #15
0
def test_name_is_string():
    with pytest.raises(Exception) as e_info:
        Node(name=123)
        assert type(e_info) == CoreDocumentException
    Node(name="name")
    assert True
Exemple #16
0
def test_from_str():
    d = Node(TEST_DATA)
    dd = Node.from_str(str(d))
    assert str(dd) == str(d)
Exemple #17
0
def test_child():
    child = Node(TEST_DATA)
    parent = Node(child)
    children = parent.children()
    assert child in children
    assert len(children) == 1
Exemple #18
0
def test_other_from_str():
    o = Node.from_str(dumps(dict(test="test")))
    assert type(o) == dict
    assert type(o["test"]) == str
Exemple #19
0
def test_equal():
    d = Node(TEST_DATA)
    dd = d.clone(new=False)
    assert d == dd
Exemple #20
0
def test_repr():
    d = Node()
    r = repr(d)
    assert r == "<type '{c}' - value {v}>".format(c=type(d).__name__,
                                                  v=dumps(d))