Skip to content

Commit

Permalink
First pass at serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
efagerberg committed Mar 20, 2018
0 parents commit ba88e84
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[run]
omit =
tests/conftest.py

[report]
precision = 1

[xml]
output = coverage.xml
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
venv
build
doc/build
dist
*.egg-info
*.swp
*.swn
*.swo
*.pyc
.idea
.python-version
.ropeproject
\#*\#
.eggs
.pytest_cache/
.coverage
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: python
sudo: required
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
env:
- NEO4J_VERSION="3.0.10"
- NEO4J_VERSION="3.1.7"
- NEO4J_VERSION="3.2.9"
- NEO4J_VERSION="3.3.3"
install:
- sudo apt-get update && sudo apt-get install oracle-java8-installer
- curl -L http://dist.neo4j.org/neo4j-community-$NEO4J_VERSION-unix.tar.gz | tar xz
before_script:
- JAVA_HOME=/usr/lib/jvm/java-8-oracle neo4j-community-$NEO4J_VERSION/bin/neo4j start
- sleep 10
script: "python setup.py test"
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Evan Fagerberg - https://github.com/efagerberg
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Version 0.0.1
* Starting version, includes base ability to serialize StructuredNode and StructuredRel
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Evan Fagerberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include README.rst
include AUTHORS.txt
include Changelog
include LICENSE
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
A serializer for neomodel models.


.. image:: https://secure.travis-ci.org/efagerberg/neomodel-serializer.png
:target: https://secure.travis-ci.org/efagerberg/neomodel-serializer/


.. image:: https://readthedocs.org/projects/neomodel-serializer/badge/?version=latest
:alt: Documentation Status
:scale: 100%
:target: https://neomodel-serializer.readthedocs.io/en/latest/?badge=latest

Requirements
============

- Python 2.7, 3.4+
- neo4j 3.0, 3.1, 3.2, 3.3
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3"

services:
tests:
image: "python:3.6-alpine"
volumes:
- .:/src
- pip-cache:/root/.cache/pip
working_dir: /src
command: sh -c "while ! nc neo4j 7687; do sleep 1; done; python -B setup.py test"
links:
- neo4j
environment:
NEO4J_BOLT_URL: bolt://neo4j:neo4j@neo4j:7687
neo4j:
image: "neo4j:3.1"
environment:
NEO4J_AUTH: none

volumes:
pip-cache:
driver: local
84 changes: 84 additions & 0 deletions serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from collections import OrderedDict
import json

from six import iteritems

from neomodel import Property
from neomodel.relationship_manager import RelationshipDefinition


class StructuredThingSerializer(object):
"""Initially made to only handle StructuredNodes
but found that it also worked for StructuredRels."""

def __init__(self, *args, **kwargs):
"""
Args:
instance (StructuredNode/StructuredRel): The thing to serialize.
fields (str[], optional): Fields to include.
Excludes subobject fields.
"""
self.instance = args[0]
self.fields = kwargs.get('fields', '__all__')
self.data = OrderedDict()

props = self.instance.defined_properties()

if self.should_include('id'):
self.data['id'] = self.instance.id

self.process_properties(props)
self.serialized_data = json.dumps(self.data)

def should_include(self, key):
return key in self.fields or self.fields == '__all__'

def process_properties(self, props):
for prop in iteritems(props):
self.process_property(prop)

def process_property(self, prop):
_, value = prop
if isinstance(value, Property):
self.process_neomodel_property(prop)
elif isinstance(value, RelationshipDefinition):
self.process_relationship_definition(prop)
else:
# If we get here, chances are neomodel has changed
msg = "Unexpected property received: {}".format(prop)
raise ValueError(msg)

def process_neomodel_property(self, prop):
key, _ = prop
if self.should_include(key):
self.data[key] = getattr(self.instance, key)

def process_relationship_definition(self, prop):
key, value = prop
if self.should_include(key):
rel_def = getattr(self.instance, key)
self.process_rel_def_nodes(rel_def, key)

def process_rel_def_nodes(self, rel_def, key):
self.data[key] = []
for node in rel_def:
self.process_rel_def_node(rel_def, node, key)

def process_rel_def_node(self, rel_def, node, key):
rel_def_data = StructuredThingSerializer(node).data
rels = rel_def.all_relationships(node)

self.process_relationships(rels, rel_def_data, key)

def process_relationships(self, rels, rel_def_data, key):
for rel in rels:
self.process_relationship(rel, rel_def_data, key)

self.data[key].append(rel_def_data)

def process_relationship(self, rel, rel_def_data, key):
rel_data = StructuredThingSerializer(rel).data
rel_data.pop('id')

if rel_data:
rel_def_data.update(rel_data)
14 changes: 14 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool:pytest]
addopts = tests
--strict
--cov tests
--cov serializers
--color yes
--cov-branch
--cov-fail-under 100
--cov-report term-missing
--cov-config .coveragerc
--durations 10

[aliases]
test = pytest
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
from setuptools import setup, find_packages

setup(
name='neomodel-serializer',
version='0.0.1.dev1',
description='Serialize neomodels',
long_description=open('README.rst').read(),
author='Evan Fagerberg',
author_email='adioevan@gmail.com',
zip_safe=True,
url='http://github.com/efagerberg/neomodel-serializer',
license='MIT',
packages=find_packages(exclude=('tests',)),
keywords='neo4j neomodel',
install_requires=['neomodel>=3.2.5', 'six'],
setup_requires=['pytest-runner']
if any(x in ('pytest', 'test') for x in sys.argv) else [],
tests_require=['mock', 'pytest', 'pytest-cov', 'pytest-xdist'],
classifiers=[
"Development Status :: 2 - Pre-Alpha",
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Topic :: Database",
])
62 changes: 62 additions & 0 deletions test-with-docker-compose.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash

# This script allows to run the test suite against all supported Python
# interpreters and neo4j versions on a host that has Docker Compose and
# Docker client installed.
# The script aborts on the first failure of any combination.
# It is not intended


write_compose_file () {
cat > docker-compose.yml <<EOL
version: "3"
services:
tests:
image: "python:${PYTHON_VERSION}-alpine"
volumes:
- .:/src
- pip-cache:/root/.cache/pip
working_dir: /src
command: sh -c "while ! nc neo4j 7687; do sleep 1; done; python -B setup.py test"
links:
- neo4j
environment:
NEO4J_BOLT_URL: bolt://neo4j:neo4j@neo4j:7687
neo4j:
image: "neo4j:${NEO4J_VERSION}"
environment:
NEO4J_AUTH: none
volumes:
pip-cache:
driver: local
EOL
}


clean () {
docker-compose rm --stop --force -v neo4j
rm -f docker-compose.myl
}


rm -f ${dir}/**/*.pyc
find ${dir} -name __pycache__ -exec rm -Rf {} \;

for NEO4J_VERSION in 3.0 3.1 3.2 3.3; do
for PYTHON_VERSION in 2.7 3.4 3.5 3.6; do
write_compose_file
docker-compose up -d neo4j
docker-compose up tests
RESULT=$?
if [ ${RESULT} != "0" ]; then
echo "Tests with Python ${PYTHON_VERSION} against neo4j ${NEO4J_VERSION} failed. Stopping."
clean
exit ${RESULT}
fi
done
clean
done

exit 0
Empty file added tests/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import print_function
import warnings
import os

from neomodel import config, db, clear_neo4j_database, change_neo4j_password
from neo4j.v1 import CypherError

warnings.simplefilter('default')

config.DATABASE_URL = os.environ.get(
'NEO4J_BOLT_URL', 'bolt://neo4j:neo4j@localhost:7687')
config.AUTO_INSTALL_LABELS = True

try:
clear_neo4j_database(db)
except CypherError as ce:
# handle instance without password being changed
if 'The credentials you provided were valid, but must be changed before you can use this instance' in str(ce):
change_neo4j_password(db, 'test')
db.set_connection('bolt://neo4j:test@localhost:7687')

print("New database with no password set, setting password to 'test'")
print("Please 'export NEO4J_BOLT_URL=bolt://neo4j:test@localhost:7687' for subsequent test runs")
else:
raise ce

0 comments on commit ba88e84

Please sign in to comment.