Add FiWare parser test
This commit is contained in:
parent
56110e27bf
commit
c8da0fb6bb
|
@ -8,7 +8,6 @@ __pycache__/
|
|||
# Distribution / packaging
|
||||
.Python
|
||||
env/
|
||||
bin/
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
|
@ -51,4 +50,3 @@ coverage.xml
|
|||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
#!/bin/bash
|
||||
set -xe
|
||||
trap 'jobs -p | xargs --no-run-if-empty kill' INT TERM EXIT
|
||||
|
||||
export PATH=$PATH:/usr/local/bin
|
||||
export PIP_DOWNLOAD_CACHE=~/.pip_cache
|
||||
|
||||
WD=`pwd`
|
||||
POSTGRES_PORT=${POSTGRES_PORT:=5432}
|
||||
|
||||
|
||||
echo "Downloading CKAN..."
|
||||
git clone https://github.com/ckan/ckan
|
||||
|
||||
|
||||
echo "Checking Solr..."
|
||||
SOLR_ACTIVE=`nc -z localhost 8983; echo $?`
|
||||
|
||||
if [ $SOLR_ACTIVE -ne 0 ]
|
||||
then
|
||||
|
||||
echo "Downloading Solr..."
|
||||
CACHE_DIR=~/.cache
|
||||
FILE=solr-4.8.1.zip
|
||||
if [ ! -f "$CACHE_DIR/$FILE" ]
|
||||
then
|
||||
wget --quiet --directory-prefix=$CACHE_DIR http://apache.rediris.es/lucene/solr/4.8.1/$FILE
|
||||
fi
|
||||
|
||||
echo "Configuring and starting Solr..."
|
||||
unzip -q "$CACHE_DIR/$FILE"
|
||||
mv solr-4.8.1/example/solr/collection1/conf/schema.xml solr-4.8.1/example/solr/collection1/conf/schema.xml.bak
|
||||
ln -s $WD/ckan/ckan/config/solr/schema.xml solr-4.8.1/example/solr/collection1/conf/schema.xml
|
||||
cd solr-4.8.1/example
|
||||
java -jar start.jar 2>&1 > /dev/null &
|
||||
cd $WD
|
||||
|
||||
else
|
||||
echo "Solar is already installed..."
|
||||
fi
|
||||
|
||||
|
||||
echo "Setting up virtualenv..."
|
||||
virtualenv --no-site-packages virtualenv
|
||||
source virtualenv/bin/activate
|
||||
pip install --upgrade pip
|
||||
|
||||
|
||||
echo "Installing CKAN dependencies..."
|
||||
cd ckan
|
||||
python setup.py develop
|
||||
pip install -r requirements.txt --allow-all-external
|
||||
pip install -r dev-requirements.txt --allow-all-external
|
||||
cd ..
|
||||
|
||||
|
||||
echo "Removing databases from old executions..."
|
||||
sudo -u postgres psql -c "DROP DATABASE IF EXISTS datastore_test;"
|
||||
sudo -u postgres psql -c "DROP DATABASE IF EXISTS ckan_test;"
|
||||
sudo -u postgres psql -c "DROP USER IF EXISTS ckan_default;"
|
||||
|
||||
|
||||
echo "Creating the PostgreSQL user and database..."
|
||||
sudo -u postgres psql -c "CREATE USER ckan_default WITH PASSWORD 'pass';"
|
||||
sudo -u postgres psql -c 'CREATE DATABASE ckan_test WITH OWNER ckan_default;'
|
||||
sudo -u postgres psql -c 'CREATE DATABASE datastore_test WITH OWNER ckan_default;'
|
||||
|
||||
|
||||
echo "Modifying the configuration to setup properly the Postgres port..."
|
||||
mkdir -p data/storage
|
||||
echo "
|
||||
sqlalchemy.url = postgresql://ckan_default:pass@localhost:$POSTGRES_PORT/ckan_test
|
||||
ckan.datastore.write_url = postgresql://ckan_default:pass@localhost:$POSTGRES_PORT/datastore_test
|
||||
ckan.datastore.read_url = postgresql://datastore_default:pass@localhost:$POSTGRES_PORT/datastore_test
|
||||
|
||||
ckan.storage_path=data/storage" >> test.ini
|
||||
|
||||
|
||||
echo "Initializing the database..."
|
||||
sed -i "s/\(postgresql:\/\/.\+@localhost\)/\1:$POSTGRES_PORT/g" ckan/test-core.ini
|
||||
cd ckan
|
||||
paster db init -c test-core.ini
|
||||
cd ..
|
||||
|
||||
|
||||
echo "Installing ckanext-privatedatasets and its requirements..."
|
||||
python setup.py develop
|
||||
pip install -r dev-requirements.txt
|
||||
|
||||
|
||||
echo "Running tests..."
|
||||
nosetests --ckan --with-xunit --with-pylons=test.ini ckanext/privatedatasets/tests/ --with-coverage \
|
||||
--cover-package=ckanext.privatedatasets --cover-inclusive --cover-erase . --cover-xml
|
|
@ -0,0 +1,89 @@
|
|||
import unittest
|
||||
import ckanext.privatedatasets.parsers.fiware as fiware
|
||||
|
||||
from mock import MagicMock
|
||||
from nose_parameterized import parameterized
|
||||
|
||||
|
||||
TEST_CASES = {
|
||||
'one_ds': {
|
||||
'host': 'localhost',
|
||||
'json': '{"customer_name":"test", "resources": [{"url": "http://localhost/dataset/ds1"}]}',
|
||||
'result': {'errors': [], 'users_datasets': [{'user': 'test', 'datasets': ['ds1']}]}
|
||||
},
|
||||
'two_ds': {
|
||||
'host': 'localhost',
|
||||
'json': '{"customer_name":"test", "resources": [{"url": "http://localhost/dataset/ds1"},' +
|
||||
'{"url": "http://localhost/dataset/ds2"}]}',
|
||||
'result': {'errors': [], 'users_datasets': [{'user': 'test', 'datasets': ['ds1', 'ds2']}]}
|
||||
},
|
||||
'error': {
|
||||
'host': 'localhost',
|
||||
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"}]}',
|
||||
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta'],
|
||||
'users_datasets': [{'user': 'test', 'datasets': []}]}
|
||||
},
|
||||
'error_one_ds': {
|
||||
'host': 'localhost',
|
||||
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"},' +
|
||||
'{"url": "http://localhost/dataset/ds2"}]}',
|
||||
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta'],
|
||||
'users_datasets': [{'user': 'test', 'datasets': ['ds2']}]}
|
||||
},
|
||||
'two_errors': {
|
||||
'host': 'localhost',
|
||||
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"},' +
|
||||
'{"url": "http://localhostb/dataset/ds2"}]}',
|
||||
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta',
|
||||
'Dataset ds2 is associated with the CKAN instance located at localhostb'],
|
||||
'users_datasets': [{'user': 'test', 'datasets': []}]}
|
||||
},
|
||||
'two_errors_two_ds': {
|
||||
'host': 'example.com',
|
||||
'json': '{"customer_name":"test", "resources": [{"url": "http://localhosta/dataset/ds1"},' +
|
||||
'{"url": "http://example.es/dataset/ds2"}, {"url": "http://example.com/dataset/ds3"},' +
|
||||
'{"url": "http://example.com/dataset/ds4"}]}',
|
||||
'result': {'errors': ['Dataset ds1 is associated with the CKAN instance located at localhosta',
|
||||
'Dataset ds2 is associated with the CKAN instance located at example.es'],
|
||||
'users_datasets': [{'user': 'test', 'datasets': ['ds3', 'ds4']}]}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class FiWareParserTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
# Parser
|
||||
self.parser = fiware.FiWareNotificationParser()
|
||||
|
||||
# Mock functions
|
||||
self._request = fiware.request
|
||||
fiware.request = MagicMock()
|
||||
|
||||
#self._json_loads = fiware.helpers.json.loads
|
||||
#fiware.helpers.json.loads = MagicMock()
|
||||
|
||||
def tearDown(self):
|
||||
# Unmock functions
|
||||
#fiware.helpers.json.loads = self._json_loads
|
||||
fiware.request = self._request
|
||||
|
||||
@parameterized.expand([
|
||||
(TEST_CASES['one_ds']['host'], TEST_CASES['one_ds']['json'], TEST_CASES['one_ds']['result']),
|
||||
(TEST_CASES['two_ds']['host'], TEST_CASES['two_ds']['json'], TEST_CASES['two_ds']['result']),
|
||||
(TEST_CASES['error']['host'], TEST_CASES['error']['json'], TEST_CASES['error']['result']),
|
||||
(TEST_CASES['error_one_ds']['host'], TEST_CASES['error_one_ds']['json'], TEST_CASES['error_one_ds']['result']),
|
||||
(TEST_CASES['two_errors']['host'], TEST_CASES['two_errors']['json'], TEST_CASES['two_errors']['result']),
|
||||
(TEST_CASES['two_errors_two_ds']['host'], TEST_CASES['two_errors_two_ds']['json'], TEST_CASES['two_errors_two_ds']['result']),
|
||||
])
|
||||
def test_parse_notification(self, my_host, request_body, expected_result):
|
||||
|
||||
# Configure
|
||||
fiware.request.host = my_host
|
||||
fiware.request.body = request_body
|
||||
|
||||
# Call the function
|
||||
result = self.parser.parse_notification()
|
||||
|
||||
# Assert that the result is what we expected to be
|
||||
self.assertEquals(expected_result, result)
|
|
@ -0,0 +1 @@
|
|||
nose_parameterized==0.3.3
|
Loading…
Reference in New Issue