#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2018 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# 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.


from unittest import TestCase

from neo4j.v1 import Record


class RecordTestCase(TestCase):

    def test_record_equality(self):
        record1 = Record(["name", "empire"], ["Nigel", "The British Empire"])
        record2 = Record(["name", "empire"], ["Nigel", "The British Empire"])
        record3 = Record(["name", "empire"], ["Stefan", "Das Deutschland"])
        assert record1 == record2
        assert record1 != record3
        assert record2 != record3

    def test_record_hashing(self):
        record1 = Record(["name", "empire"], ["Nigel", "The British Empire"])
        record2 = Record(["name", "empire"], ["Nigel", "The British Empire"])
        record3 = Record(["name", "empire"], ["Stefan", "Das Deutschland"])
        assert hash(record1) == hash(record2)
        assert hash(record1) != hash(record3)
        assert hash(record2) != hash(record3)

    def test_record_iter(self):
        a_record = Record(["name", "empire"], ["Nigel", "The British Empire"])
        assert list(a_record.__iter__()) == ["name", "empire"]

    def test_record_copy(self):
        original = Record(["name", "empire"], ["Nigel", "The British Empire"])
        duplicate = original.copy()
        assert dict(original) == dict(duplicate)
        assert original.keys() == duplicate.keys()
        assert original is not duplicate

    def test_record_as_dict(self):
        a_record = Record(["name", "empire"], ["Nigel", "The British Empire"])
        assert dict(a_record) == {"name": "Nigel", "empire": "The British Empire"}

    def test_record_as_list(self):
        a_record = Record(["name", "empire"], ["Nigel", "The British Empire"])
        assert list(a_record) == ["name", "empire"]

    def test_record_len(self):
        a_record = Record(["name", "empire"], ["Nigel", "The British Empire"])
        assert len(a_record) == 2

    def test_record_repr(self):
        a_record = Record(["name", "empire"], ["Nigel", "The British Empire"])
        assert repr(a_record) == "<Record name='Nigel' empire='The British Empire'>"

    def test_record_data(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertEqual(r.data(), {"name": "Alice", "age": 33, "married": True})
        self.assertEqual(r.data("name"), {"name": "Alice"})
        self.assertEqual(r.data("age", "name"), {"age": 33, "name": "Alice"})
        self.assertEqual(r.data("age", "name", "shoe size"), {"age": 33, "name": "Alice", "shoe size": None})
        self.assertEqual(r.data(0, "name"), {"name": "Alice"})
        self.assertEqual(r.data(0), {"name": "Alice"})
        self.assertEqual(r.data(1, 0), {"age": 33, "name": "Alice"})
        with self.assertRaises(IndexError):
            _ = r.data(1, 0, 999)

    def test_record_keys(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertEqual(r.keys(), ("name", "age", "married"))

    def test_record_values(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertEqual(r.values(), ("Alice", 33, True))
        self.assertEqual(r.values("name"), ("Alice",))
        self.assertEqual(r.values("age", "name"), (33, "Alice"))
        self.assertEqual(r.values("age", "name", "shoe size"), (33, "Alice", None))
        self.assertEqual(r.values(0, "name"), ("Alice", "Alice"))
        self.assertEqual(r.values(0), ("Alice",))
        self.assertEqual(r.values(1, 0), (33, "Alice"))
        with self.assertRaises(IndexError):
            _ = r.values(1, 0, 999)

    def test_record_items(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertEqual(r.items(), [("name", "Alice"), ("age", 33), ("married", True)])
        self.assertEqual(r.items("name"), [("name", "Alice")])
        self.assertEqual(r.items("age", "name"), [("age", 33), ("name", "Alice")])
        self.assertEqual(r.items("age", "name", "shoe size"), [("age", 33), ("name", "Alice"), ("shoe size", None)])
        self.assertEqual(r.items(0, "name"), [("name", "Alice"), ("name", "Alice")])
        self.assertEqual(r.items(0), [("name", "Alice")])
        self.assertEqual(r.items(1, 0), [("age", 33), ("name", "Alice")])
        with self.assertRaises(IndexError):
            _ = r.items(1, 0, 999)

    def test_record_index(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertEqual(r.index("name"), 0)
        self.assertEqual(r.index("age"), 1)
        self.assertEqual(r.index("married"), 2)
        with self.assertRaises(KeyError):
            _ = r.index("shoe size")
        self.assertEqual(r.index(0), 0)
        self.assertEqual(r.index(1), 1)
        self.assertEqual(r.index(2), 2)
        with self.assertRaises(IndexError):
            _ = r.index(3)
        with self.assertRaises(TypeError):
            _ = r.index(None)

    def test_record_value(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertEqual(r.value(), "Alice")
        self.assertEqual(r.value("name"), "Alice")
        self.assertEqual(r.value("age"), 33)
        self.assertEqual(r.value("married"), True)
        self.assertEqual(r.value("shoe size"), None)
        self.assertEqual(r.value("shoe size", 6), 6)
        self.assertEqual(r.value(0), "Alice")
        self.assertEqual(r.value(1), 33)
        self.assertEqual(r.value(2), True)
        self.assertEqual(r.value(3), None)
        self.assertEqual(r.value(3, 6), 6)
        with self.assertRaises(TypeError):
            _ = r.value(None)

    def test_record_contains(self):
        r = Record(["name", "age", "married"], ["Alice", 33, True])
        self.assertTrue("name" in r)
        self.assertTrue("age" in r)
        self.assertTrue("married" in r)
        self.assertFalse("shoe size" in r)
        self.assertTrue(0 in r)
        self.assertTrue(1 in r)
        self.assertTrue(2 in r)
        self.assertFalse(3 in r)
        with self.assertRaises(TypeError):
            _ = r.index(None)

[((888, 947), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (894, 947), False, 'from neo4j.v1 import Record\n'), ((966, 1025), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (972, 1025), False, 'from neo4j.v1 import Record\n'), ((1044, 1101), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Stefan', 'Das Deutschland']"], {}), "(['name', 'empire'], ['Stefan', 'Das Deutschland'])\n", (1050, 1101), False, 'from neo4j.v1 import Record\n'), ((1258, 1317), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (1264, 1317), False, 'from neo4j.v1 import Record\n'), ((1336, 1395), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (1342, 1395), False, 'from neo4j.v1 import Record\n'), ((1414, 1471), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Stefan', 'Das Deutschland']"], {}), "(['name', 'empire'], ['Stefan', 'Das Deutschland'])\n", (1420, 1471), False, 'from neo4j.v1 import Record\n'), ((1662, 1721), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (1668, 1721), False, 'from neo4j.v1 import Record\n'), ((1837, 1896), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (1843, 1896), False, 'from neo4j.v1 import Record\n'), ((2129, 2188), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (2135, 2188), False, 'from neo4j.v1 import Record\n'), ((2327, 2386), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (2333, 2386), False, 'from neo4j.v1 import Record\n'), ((2490, 2549), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (2496, 2549), False, 'from neo4j.v1 import Record\n'), ((2636, 2695), 'neo4j.v1.Record', 'Record', (["['name', 'empire']", "['Nigel', 'The British Empire']"], {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])\n", (2642, 2695), False, 'from neo4j.v1 import Record\n'), ((2826, 2881), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (2832, 2881), False, 'from neo4j.v1 import Record\n'), ((3522, 3577), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (3528, 3577), False, 'from neo4j.v1 import Record\n'), ((3688, 3743), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (3694, 3743), False, 'from neo4j.v1 import Record\n'), ((4304, 4359), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (4310, 4359), False, 'from neo4j.v1 import Record\n'), ((5054, 5109), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (5060, 5109), False, 'from neo4j.v1 import Record\n'), ((5636, 5691), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (5642, 5691), False, 'from neo4j.v1 import Record\n'), ((6328, 6383), 'neo4j.v1.Record', 'Record', (["['name', 'age', 'married']", "['Alice', 33, True]"], {}), "(['name', 'age', 'married'], ['Alice', 33, True])\n", (6334, 6383), False, 'from neo4j.v1 import Record\n')]