<commit_before>#/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2011-2014, <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging

import pytest

from py2neo import node
from py2neo.core import authenticate, _get_headers, Graph, Node, Relationship


logging.basicConfig(
    format="%(asctime)s %(levelname)s %(name)s %(message)s",
    level=logging.DEBUG,
)


def test_can_use_graph_if_no_trailing_slash_supplied(graph):
    alice, = graph.create(node(name="Alice"))
    assert isinstance(alice, Node)
    assert alice["name"] == "Alice"


def test_authentication_adds_the_correct_header():
    from py2neo.core import _headers
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    authenticate("localhost:7474", "arthur", "excalibur")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='


def test_can_add_same_header_twice():
    from py2neo.core import _headers
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    authenticate("localhost:7474", "arthur", "excalibur")
    authenticate("localhost:7474", "arthur", "excalibur")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='


def test_implicit_authentication_through_resource_constructor():
    from py2neo.core import _headers, Resource
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    resource = Resource("http://arthur:excalibur@localhost:7474/")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='
    assert resource.headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='


class TestGraph(object):

    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph

    def test_can_get_same_instance(self):
        graph_1 = Graph()
        graph_2 = Graph()
        assert graph_1 is graph_2

    def test_neo4j_version_format(self):
        version = self.graph.neo4j_version
        print(version)
        assert isinstance(version, tuple)
        assert len(version) == 4
        assert isinstance(version[0], int)
        assert isinstance(version[1], int)
        assert isinstance(version[2], int)

    def test_create_single_empty_node(self):
        a, = self.graph.create({})

    def test_get_node_by_id(self):
        a1, = self.graph.create({"foo": "bar"})
        a2 = self.graph.node(a1._id)
        assert a1 == a2

    def test_create_node_with_property_dict(self):
        node, = self.graph.create({"foo": "bar"})
        assert node["foo"] == "bar"

    def test_create_node_with_mixed_property_types(self):
        node, = self.graph.create(
            {"number": 13, "foo": "bar", "true": False, "fish": "chips"}
        )
        assert len(node.properties) == 4
        assert node["fish"] == "chips"
        assert node["foo"] == "bar"
        assert node["number"] == 13
        assert not node["true"]<commit_msg>Fix unit test for version_tuple.<commit_after>#/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2011-2014, <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging

import pytest

from py2neo import node
from py2neo.core import authenticate, _get_headers, Graph, Node, Relationship


logging.basicConfig(
    format="%(asctime)s %(levelname)s %(name)s %(message)s",
    level=logging.DEBUG,
)


def test_can_use_graph_if_no_trailing_slash_supplied(graph):
    alice, = graph.create(node(name="Alice"))
    assert isinstance(alice, Node)
    assert alice["name"] == "Alice"


def test_authentication_adds_the_correct_header():
    from py2neo.core import _headers
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    authenticate("localhost:7474", "arthur", "excalibur")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='


def test_can_add_same_header_twice():
    from py2neo.core import _headers
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    authenticate("localhost:7474", "arthur", "excalibur")
    authenticate("localhost:7474", "arthur", "excalibur")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='


def test_implicit_authentication_through_resource_constructor():
    from py2neo.core import _headers, Resource
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    resource = Resource("http://arthur:excalibur@localhost:7474/")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='
    assert resource.headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='


class TestGraph(object):

    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph

    def test_can_get_same_instance(self):
        graph_1 = Graph()
        graph_2 = Graph()
        assert graph_1 is graph_2

    def test_neo4j_version_format(self):
        version = self.graph.neo4j_version
        print(version)
        assert isinstance(version, tuple)
        assert 3 <= len(version) <= 4
        assert isinstance(version[0], int)
        assert isinstance(version[1], int)
        assert isinstance(version[2], int)

    def test_create_single_empty_node(self):
        a, = self.graph.create({})

    def test_get_node_by_id(self):
        a1, = self.graph.create({"foo": "bar"})
        a2 = self.graph.node(a1._id)
        assert a1 == a2

    def test_create_node_with_property_dict(self):
        node, = self.graph.create({"foo": "bar"})
        assert node["foo"] == "bar"

    def test_create_node_with_mixed_property_types(self):
        node, = self.graph.create(
            {"number": 13, "foo": "bar", "true": False, "fish": "chips"}
        )
        assert len(node.properties) == 4
        assert node["fish"] == "chips"
        assert node["foo"] == "bar"
        assert node["number"] == 13
        assert not node["true"]
{'fraction_non_alphanumeric': 0.12888198757763975, 'fraction_numerical': 0.023291925465838508, 'mean_word_length': 3.813179347826087, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7204970', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2262, 'n_words': 540}