|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2002-2020 "Neo4j," |
| 5 | +# Neo4j Sweden AB [http://neo4j.com] |
| 6 | +# |
| 7 | +# This file is part of Neo4j. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from neo4j.data import DataHydrator |
| 25 | +from neo4j.packstream import Structure |
| 26 | + |
| 27 | +# python -m pytest -s -v tests/unit/test_data.py |
| 28 | + |
| 29 | + |
| 30 | +def test_can_hydrate_node_structure(): |
| 31 | + hydrant = DataHydrator() |
| 32 | + |
| 33 | + struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) |
| 34 | + alice, = hydrant.hydrate([struct]) |
| 35 | + |
| 36 | + assert alice.id == 123 |
| 37 | + assert alice.labels == {"Person"} |
| 38 | + assert set(alice.keys()) == {"name"} |
| 39 | + assert alice.get("name") == "Alice" |
| 40 | + |
| 41 | + |
| 42 | +def test_hydrating_unknown_structure_returns_same(): |
| 43 | + hydrant = DataHydrator() |
| 44 | + |
| 45 | + struct = Structure(b'?', "foo") |
| 46 | + mystery, = hydrant.hydrate([struct]) |
| 47 | + |
| 48 | + assert mystery == struct |
| 49 | + |
| 50 | + |
| 51 | +def test_can_hydrate_in_list(): |
| 52 | + hydrant = DataHydrator() |
| 53 | + |
| 54 | + struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) |
| 55 | + alice_in_list, = hydrant.hydrate([[struct]]) |
| 56 | + |
| 57 | + assert isinstance(alice_in_list, list) |
| 58 | + |
| 59 | + alice, = alice_in_list |
| 60 | + |
| 61 | + assert alice.id == 123 |
| 62 | + assert alice.labels == {"Person"} |
| 63 | + assert set(alice.keys()) == {"name"} |
| 64 | + assert alice.get("name") == "Alice" |
| 65 | + |
| 66 | + |
| 67 | +def test_can_hydrate_in_dict(): |
| 68 | + hydrant = DataHydrator() |
| 69 | + |
| 70 | + struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) |
| 71 | + alice_in_dict, = hydrant.hydrate([{"foo": struct}]) |
| 72 | + |
| 73 | + assert isinstance(alice_in_dict, dict) |
| 74 | + |
| 75 | + alice = alice_in_dict["foo"] |
| 76 | + |
| 77 | + assert alice.id == 123 |
| 78 | + assert alice.labels == {"Person"} |
| 79 | + assert set(alice.keys()) == {"name"} |
| 80 | + assert alice.get("name") == "Alice" |
0 commit comments