#!/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)

[((30, 18, 30, 77), 'neo4j.v1.Record', 'Record', ({(30, 25, 30, 43): "['name', 'empire']", (30, 45, 30, 76): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((31, 18, 31, 77), 'neo4j.v1.Record', 'Record', ({(31, 25, 31, 43): "['name', 'empire']", (31, 45, 31, 76): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((32, 18, 32, 75), 'neo4j.v1.Record', 'Record', ({(32, 25, 32, 43): "['name', 'empire']", (32, 45, 32, 74): "['Stefan', 'Das Deutschland']"}, {}), "(['name', 'empire'], ['Stefan', 'Das Deutschland'])", False, 'from neo4j.v1 import Record\n'), ((38, 18, 38, 77), 'neo4j.v1.Record', 'Record', ({(38, 25, 38, 43): "['name', 'empire']", (38, 45, 38, 76): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((39, 18, 39, 77), 'neo4j.v1.Record', 'Record', ({(39, 25, 39, 43): "['name', 'empire']", (39, 45, 39, 76): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((40, 18, 40, 75), 'neo4j.v1.Record', 'Record', ({(40, 25, 40, 43): "['name', 'empire']", (40, 45, 40, 74): "['Stefan', 'Das Deutschland']"}, {}), "(['name', 'empire'], ['Stefan', 'Das Deutschland'])", False, 'from neo4j.v1 import Record\n'), ((46, 19, 46, 78), 'neo4j.v1.Record', 'Record', ({(46, 26, 46, 44): "['name', 'empire']", (46, 46, 46, 77): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((50, 19, 50, 78), 'neo4j.v1.Record', 'Record', ({(50, 26, 50, 44): "['name', 'empire']", (50, 46, 50, 77): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((57, 19, 57, 78), 'neo4j.v1.Record', 'Record', ({(57, 26, 57, 44): "['name', 'empire']", (57, 46, 57, 77): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((61, 19, 61, 78), 'neo4j.v1.Record', 'Record', ({(61, 26, 61, 44): "['name', 'empire']", (61, 46, 61, 77): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((65, 19, 65, 78), 'neo4j.v1.Record', 'Record', ({(65, 26, 65, 44): "['name', 'empire']", (65, 46, 65, 77): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((69, 19, 69, 78), 'neo4j.v1.Record', 'Record', ({(69, 26, 69, 44): "['name', 'empire']", (69, 46, 69, 77): "['Nigel', 'The British Empire']"}, {}), "(['name', 'empire'], ['Nigel', 'The British Empire'])", False, 'from neo4j.v1 import Record\n'), ((73, 12, 73, 67), 'neo4j.v1.Record', 'Record', ({(73, 19, 73, 45): "['name', 'age', 'married']", (73, 47, 73, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n'), ((85, 12, 85, 67), 'neo4j.v1.Record', 'Record', ({(85, 19, 85, 45): "['name', 'age', 'married']", (85, 47, 85, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n'), ((89, 12, 89, 67), 'neo4j.v1.Record', 'Record', ({(89, 19, 89, 45): "['name', 'age', 'married']", (89, 47, 89, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n'), ((101, 12, 101, 67), 'neo4j.v1.Record', 'Record', ({(101, 19, 101, 45): "['name', 'age', 'married']", (101, 47, 101, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n'), ((113, 12, 113, 67), 'neo4j.v1.Record', 'Record', ({(113, 19, 113, 45): "['name', 'age', 'married']", (113, 47, 113, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n'), ((128, 12, 128, 67), 'neo4j.v1.Record', 'Record', ({(128, 19, 128, 45): "['name', 'age', 'married']", (128, 47, 128, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n'), ((144, 12, 144, 67), 'neo4j.v1.Record', 'Record', ({(144, 19, 144, 45): "['name', 'age', 'married']", (144, 47, 144, 66): "['Alice', 33, True]"}, {}), "(['name', 'age', 'married'], ['Alice', 33, True])", False, 'from neo4j.v1 import Record\n')]