Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Adding test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
sim51 committed Dec 9, 2018
1 parent 8d4cfb8 commit 675823c
Show file tree
Hide file tree
Showing 17 changed files with 874 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,15 @@ SELECT year, id FROM cypher('MATCH (y:Year)<-[r]-(m) RETURN y.value AS year, m.
2015 | 10017211
(10 lignes)
----


== Run test

=== Construct docker images

[source,shell]
----
docker-compose build
docker-compose up --detach
----
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3'
services:
# Neo4j service
neo4j:
build:
context: .
dockerfile: ./docker/neo4j/Dockerfile
ports:
- "7474:7474"
- "7687:7687"
volumes:
- .:/source
container_name: "fdw-neo4j"
# Custom postgres from dokerfile
postgres:
build:
context: .
dockerfile: ./docker/postgres/Dockerfile
ports:
- 5432:5432
volumes:
- .:/source
container_name: "fdw-pg"
6 changes: 6 additions & 0 deletions docker/neo4j/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM neo4j:3.5-enterprise AS fdwNeo4j

ENV NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
ENV EXTENSION_SCRIPT=/source/docker/neo4j/init.sh

RUN apk add --no-cache curl
33 changes: 33 additions & 0 deletions docker/neo4j/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
echo "~~~"
echo "~ Changing password"
echo "~~~"
su-exec neo4j bin/neo4j-admin set-initial-password admin

echo "~~~"
echo "~ Starting Neo4j"
echo "~~~"
su-exec neo4j bin/neo4j start

# Wait until the database is up and ready
echo "~~~"
echo "~ Waiting Neo4j to be ready"
echo "~~~"
until $(curl --output /dev/null --silent --head --fail http://localhost:7474); do
printf '.'
sleep 1
done
echo

# Load the data with the cypher shell
echo "~~~"
echo "~ Init database"
echo "~~~"
su-exec neo4j bin/cypher-shell -u neo4j -p admin < /source/docker/neo4j/initdb.gql

tail -f logs/*

echo "~~~"
echo "~ Stopping Neo4j"
echo "~~~"
su-exec neo4j bin/neo4j stop
511 changes: 511 additions & 0 deletions docker/neo4j/initdb.gql

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM postgres:10 AS fdwPostgres

ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
ENV POSTGRES_DB test

RUN mkdir -p /neo4j-fdw
COPY . /neo4j-fdw/source

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
postgresql-server-dev-10 \
python-dev \
python-setuptools \
python-dev \
python-pip \
postgresql-plpython-10 \
git \
&& rm -rf /var/lib/apt/lists/*

RUN ["chmod", "+x", "/neo4j-fdw/source/docker/postgres/init.sh"]
RUN /neo4j-fdw/source/docker/postgres/init.sh

ADD ./docker/postgres/initdb.sql /docker-entrypoint-initdb.d
13 changes: 13 additions & 0 deletions docker/postgres/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Install Multicorn
echo "~~~~~~~~~~~~~ Installing multicorn"
cd /neo4j-fdw/
git clone git://github.com/Kozea/Multicorn.git
cd Multicorn
make && make install

# Install neo4j fdw
echo "~~~~~~~~~~~~~ Installing Neo4j FDW"
cd /neo4j-fdw/source/
python setup.py install
35 changes: 35 additions & 0 deletions docker/postgres/initdb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

CREATE EXTENSION multicorn;

CREATE SERVER multicorn_neo4j FOREIGN DATA WRAPPER multicorn
OPTIONS (
wrapper 'neo4jPg.neo4jfdw.Neo4jForeignDataWrapper',
url 'bolt://neo4j:7687',
user 'neo4j',
password 'admin'
);

CREATE FOREIGN TABLE movie (
id bigint NOT NULL,
title varchar NOT NULL,
released smallint,
tagline varchar
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (n:Movie) RETURN id(n) AS id, n.title AS title, n.released AS released, n.tagline AS tagline'
);

CREATE FOREIGN TABLE person (
id bigint NOT NULL,
name varchar NOT NULL,
born smallint
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (n:Person) RETURN id(n) AS id, n.name AS name, n.born AS born'
);

CREATE FOREIGN TABLE actedIn (
id bigint NOT NULL,
movie_id bigint NOT NULL,
person_id bigint NOT NULL
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) RETURN id(r) AS id, id(p) AS person_id, id(m) AS movie_id'
);
2 changes: 1 addition & 1 deletion neo4jPg/neo4jfdw.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from multicorn import ForeignDataWrapper
from multicorn.utils import log_to_postgres, ERROR, WARNING, DEBUG
from neo4j.v1 import GraphDatabase, basic_auth
from neo4j import GraphDatabase, basic_auth
import re


Expand Down
24 changes: 24 additions & 0 deletions pg_regress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

rm -f /tmp/regression.diffs

TEST_DIRECTORY=$1

TEST_SUITE=""
for file in $TEST_DIRECTORY/sql/*.sql
do
NAME=`basename $file .sql`
TEST_SUITE="$TEST_SUITE $NAME"
done

echo "$TEST_SUITE"
su postgres <<EOF
export PATH=$PATH:/usr/lib/postgresql/10/lib/pgxs/src/test/regress/
cd $TEST_DIRECTORY
echo "pg_regress $TEST_SUITE"
pg_regress --outputdir=/tmp $TEST_SUITE
EOF

if [ -f '/tmp/regression.diffs' ]; then
exit 1
fi
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from setuptools import setup, find_packages

import neo4jPg

setup(
Expand Down
30 changes: 30 additions & 0 deletions test/expected/000_create_foreign_database.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE EXTENSION multicorn;
CREATE SERVER multicorn_neo4j FOREIGN DATA WRAPPER multicorn
OPTIONS (
wrapper 'neo4jPg.neo4jfdw.Neo4jForeignDataWrapper',
url 'bolt://neo4j:7687',
user 'neo4j',
password 'admin'
);
CREATE FOREIGN TABLE movie (
id bigint NOT NULL,
title varchar NOT NULL,
released smallint,
tagline varchar
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (n:Movie) RETURN id(n) AS id, n.title AS title, n.released AS released, n.tagline AS tagline'
);
CREATE FOREIGN TABLE person (
id bigint NOT NULL,
name varchar NOT NULL,
born smallint
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (n:Person) RETURN id(n) AS id, n.name AS name, n.born AS born'
);
CREATE FOREIGN TABLE actedIn (
id bigint NOT NULL,
movie_id bigint NOT NULL,
person_id bigint NOT NULL
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) RETURN id(r) AS id, id(p) AS person_id, id(m) AS movie_id'
);
52 changes: 52 additions & 0 deletions test/expected/010_movie_queries.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
SELECT * FROM movie LIMIT 10;
id | title | released | tagline
----+------------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | The Matrix | 1999 | Welcome to the Real World
9 | The Matrix Reloaded | 2003 | Free your mind
10 | The Matrix Revolutions | 2003 | Everything that has a beginning has an end
11 | The Devil's Advocate | 1997 | Evil has its winning ways
15 | A Few Good Men | 1992 | In the heart of the nation's capital, in a courthouse of the U.S. government, one man will stop at nothing to keep his honor, and one will stop at nothing to find the truth.
29 | Top Gun | 1986 | I feel the need, the need for speed.
37 | Jerry Maguire | 2000 | The rest of his life begins now.
46 | Stand By Me | 1986 | For some, it's the last real taste of innocence, and the first real taste of life. But for everyone, it's the time that memories are made of.
52 | As Good as It Gets | 1997 | A comedy from the heart that goes for the throat.
56 | What Dreams May Come | 1998 | After life there is more. The end is just the beginning.
(10 rows)

SELECT * FROM movie m WHERE m.released > 2000 ORDER BY m.released DESC LIMIT 10;
id | title | released | tagline
-----+------------------------+----------+-------------------------------------------------------------------------------------------------------
105 | Cloud Atlas | 2012 | Everything is connected
128 | Ninja Assassin | 2009 | Prepare to enter a secret world of assassins
137 | Frost/Nixon | 2008 | 400 million people were waiting for the truth.
121 | Speed Racer | 2008 | Speed has no limits
159 | Charlie Wilson's War | 2007 | A stiff drink. A little mascara. A lot of nerve. Who said they couldn't bring down the Soviet empire.
111 | The Da Vinci Code | 2006 | Break The Codes
116 | V for Vendetta | 2006 | Freedom! Forever!
92 | RescueDawn | 2006 | Based on the extraordinary true story of one man's fight for freedom
161 | The Polar Express | 2004 | This Holiday Season… Believe
154 | Something's Gotta Give | 2003 |
(10 rows)

SELECT * FROM movie m WHERE m.title = 'The Matrix';
id | title | released | tagline
----+------------+----------+---------------------------
0 | The Matrix | 1999 | Welcome to the Real World
(1 row)

SELECT * FROM movie m WHERE m.title LIKE '%Matrix%';
id | title | released | tagline
----+------------------------+----------+--------------------------------------------
0 | The Matrix | 1999 | Welcome to the Real World
9 | The Matrix Reloaded | 2003 | Free your mind
10 | The Matrix Revolutions | 2003 | Everything that has a beginning has an end
(3 rows)

SELECT * FROM movie m WHERE m.title ILIKE '%matrix%';
id | title | released | tagline
----+------------------------+----------+--------------------------------------------
0 | The Matrix | 1999 | Welcome to the Real World
9 | The Matrix Reloaded | 2003 | Free your mind
10 | The Matrix Revolutions | 2003 | Everything that has a beginning has an end
(3 rows)

35 changes: 35 additions & 0 deletions test/sql/000_create_foreign_database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

CREATE EXTENSION multicorn;

CREATE SERVER multicorn_neo4j FOREIGN DATA WRAPPER multicorn
OPTIONS (
wrapper 'neo4jPg.neo4jfdw.Neo4jForeignDataWrapper',
url 'bolt://neo4j:7687',
user 'neo4j',
password 'admin'
);

CREATE FOREIGN TABLE movie (
id bigint NOT NULL,
title varchar NOT NULL,
released smallint,
tagline varchar
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (n:Movie) RETURN id(n) AS id, n.title AS title, n.released AS released, n.tagline AS tagline'
);

CREATE FOREIGN TABLE person (
id bigint NOT NULL,
name varchar NOT NULL,
born smallint
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (n:Person) RETURN id(n) AS id, n.name AS name, n.born AS born'
);

CREATE FOREIGN TABLE actedIn (
id bigint NOT NULL,
movie_id bigint NOT NULL,
person_id bigint NOT NULL
) SERVER multicorn_neo4j OPTIONS (
cypher 'MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) RETURN id(r) AS id, id(p) AS person_id, id(m) AS movie_id'
);
5 changes: 5 additions & 0 deletions test/sql/010_movie_queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT * FROM movie LIMIT 10;
SELECT * FROM movie m WHERE m.released > 2000 ORDER BY m.released DESC LIMIT 10;
SELECT * FROM movie m WHERE m.title = 'The Matrix';
SELECT * FROM movie m WHERE m.title LIKE '%Matrix%';
SELECT * FROM movie m WHERE m.title ILIKE '%matrix%';
10 changes: 10 additions & 0 deletions test/test_query_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import unittest
from neo4jPg import neo4jfdw

class SimplisticTest(unittest.TestCase):

def test(self):
self.assertTrue(True)

if __name__ == '__main__':
unittest.main()
59 changes: 59 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash


####################################################################
# Before all tests
####################################################################
echo "~~~"
echo "~ Before all tests"
echo "~~~"
echo

echo
echo "~ Run docker compose"
docker-compose rm -f
docker-compose pull
docker-compose up --build --detach
sleep 10

####################################################################
# Run python unit test
####################################################################
echo "~~~"
echo "~ Running python tests"
echo "~~~"
echo

py.test

RESULT=$?
if [ $RESULT -gt 0 ]; then
echo "Some python tests failed"
exit 1
fi

####################################################################
# Running pg_regress tests
####################################################################
echo "~~~"
echo "~ Running pg_regress tests"
echo "~~~"
echo

echo "~ Execute pg_regress tests"
docker exec -it fdw-pg /source/pg_regress.sh /source/test
if [ $? -gt 0 ]; then
echo "Some regress test failed"
exit 1
fi

####################################################################
# After all tests
####################################################################
echo "~~~"
echo "~ After all tests"
echo "~~~"
echo

echo "~ Stop docker containers"
docker-compose down

0 comments on commit 675823c

Please sign in to comment.