Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #138 from postatum/101462172_test_scaffold
Browse files Browse the repository at this point in the history
Test nefertari_starter scaffold
  • Loading branch information
jstoiko committed May 1, 2016
2 parents effb2f2 + 718a61d commit 1e0e5ff
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ env:
- TOXENV=py27
- TOXENV=py33
- TOXENV=py34
- TOXENV=py35
python: 3.5
install:
- pip install tox
script: tox
services:
- mongodb
before_install:
- travis_retry curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.7.5.deb && sudo dpkg -i --force-confnew --force-confdef elasticsearch-1.7.5.deb
- sudo service elasticsearch start
before_script:
- sleep 10
- travis_retry curl -XDELETE 'http://localhost:9200/nefertari_starter/'
- mongo nefertari_starter --eval 'db.dropDatabase();'
1 change: 0 additions & 1 deletion nefertari/scaffolds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import binascii
import os
import subprocess

Expand Down
Empty file.
123 changes: 123 additions & 0 deletions nefertari/scaffolds/nefertari_starter/+package+/tests/api.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#%RAML 0.8
---
title: Items API
documentation:
- title: Items REST API
content: |
Welcome to the Items API.
baseUri: http://{host}:{port}/{version}
version: api
mediaType: application/json
protocols: [HTTP]

/items:
displayName: Collection of items
head:
description: Head request
responses:
200:
description: Return headers
options:
description: Options request
responses:
200:
description: Return available HTTP methods
get:
description: Get all item
responses:
200:
description: Returns a list of items
post:
description: Create a new item
body:
application/json:
schema: !include items.json
example: |
{
"id": "507f191e810c19729de860ea",
"name": "Banana",
"description": "Tasty"
}
responses:
201:
description: Created item
body:
application/json:
schema: !include items.json
delete:
description: Delete multiple items
responses:
200:
description: Deleted multiple items
patch:
description: Update multiple items
body:
application/json:
example: { "name": "Car" }
responses:
200:
description: Updated multiple items
put:
description: Update multiple items
body:
application/json:
example: { "name": "Plane" }
responses:
200:
description: Updated multiple items

/{id}:
uriParameters:
id:
displayName: Item id
type: string
example: 507f191e810c19729de860ea
displayName: Collection-item
head:
description: Head request
responses:
200:
description: Return headers
options:
description: Options request
responses:
200:
description: Return available HTTP methods
get:
description: Get a particular item
responses:
200:
body:
application/json:
schema: !include items.json

delete:
description: Delete a particular item
responses:
200:
description: Deleted item
patch:
description: Update a particular item
body:
application/json:
example: { "name": "Tree" }
responses:
200:
body:
application/json:
schema: !include items.json
put:
description: Replace a particular item
body:
application/json:
example: |
{
"id": "507f191e810c19729de860ea",
"name": "Horse",
"description": "Not tasty"
}
responses:
200:
body:
application/json:
schema: !include items.json
29 changes: 29 additions & 0 deletions nefertari/scaffolds/nefertari_starter/+package+/tests/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "object",
"title": "Item schema",
"$schema": "http://json-schema.org/draft-04/schema",
"required": ["name"],
"properties": {
"id": {
"type": ["string", "null"],
"_db_settings": {
"type": "id_field",
"required": true,
"primary_key": true
}
},
"name": {
"type": "string",
"_db_settings": {
"type": "string",
"required": true
}
},
"description": {
"type": ["string", "null"],
"_db_settings": {
"type": "text"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-e git+https://github.com/ramses-tech/ra.git@develop#egg=ra
-e git+https://github.com/ramses-tech/nefertari-mongodb.git@develop#egg=nefertari-mongodb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import ra
import webtest
import pytest

appdir = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', '..'))
ramlfile = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'api.raml'))
testapp = webtest.TestApp('config:local.ini', relative_to=appdir)


@pytest.fixture(autouse=True)
def setup(req, examples):
""" Setup database state for tests.

NOTE: For objects to be created, transaction needs to be commited
as follows:
import transaction
transaction.commit()
"""
from nefertari import engine
Item = engine.get_document_cls('Item')

if req.match(exclude='POST /items'):
if Item.get_collection(_count=True) == 0:
example = examples.build('item')
Item(**example).save()


# ra entry point: instantiate the API test suite
api = ra.api(ramlfile, testapp)
api.autotest()
84 changes: 84 additions & 0 deletions nefertari/scripts/scaffold_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
""" Reworked version of Pyramid scaffold "tests" module.
https://github.com/Pylons/pyramid/blob/master/pyramid/scaffolds/tests.py
"""
import sys
import os
import shutil
import tempfile
from subprocess import check_call, Popen, PIPE
from argparse import ArgumentParser


class ScaffoldTestCommand(object):
# Engine codes when creating an app from scaffold
SQLA_ENGINE_CODE = '1'
MONGO_ENGINE_CODE = '2'
ENGINE = MONGO_ENGINE_CODE
file = __file__

def make_venv(self, directory): # pragma: no cover
import virtualenv
from virtualenv import Logger
logger = Logger([(Logger.level_for_integer(2), sys.stdout)])
virtualenv.logger = logger
virtualenv.create_environment(directory,
site_packages=False,
clear=False,
unzip_setuptools=True)

def run_tests(self, scaff_name): # pragma: no cover
proj_name = scaff_name.title()
orig_path = os.environ.get('PATH', '')
try:
self.old_cwd = os.getcwd()
self.directory = tempfile.mkdtemp()
self.make_venv(self.directory)
os.environ['PATH'] = os.path.join(
self.directory, 'bin') + ':' + orig_path

# Install library in created env
here = os.path.abspath(os.path.dirname(self.file))
os.chdir(os.path.dirname(os.path.dirname(here)))
check_call(['python', 'setup.py', 'develop'])
os.chdir(self.directory)

# Create app from scaffold and install it
popen = Popen(
['bin/pcreate', '-s', scaff_name, proj_name],
stdin=PIPE, stdout=PIPE,
universal_newlines=True)
popen.communicate(self.ENGINE)
os.chdir(proj_name)
check_call(['python', 'setup.py', 'install'])

# Install test requirements
test_reqs = os.path.join(scaff_name, 'tests', 'requirements.txt')
if os.path.exists(test_reqs):
check_call(['pip', 'install', '-r', test_reqs])

# Run actual scaffold tests
check_call(['py.test', scaff_name])
finally:
os.environ['PATH'] = orig_path
shutil.rmtree(self.directory)
os.chdir(self.old_cwd)

def parse_args(self):
parser = ArgumentParser()
parser.add_argument(
'-s', '--scaffold', help='Scaffold name',
required=True)
self.args = parser.parse_args()

def run(self):
self.parse_args()
self.run_tests(self.args.scaffold)


def main(*args, **kwargs):
ScaffoldTestCommand().run()


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ releases
sphinx
sphinxcontrib-fulltoc
webtest
virtualenv

-e .
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[tox]
envlist =
py27,
py33,py34
py33,py34,py35,

[testenv]
setenv =
PYTHONHASHSEED=0
deps = -rrequirements.dev
commands = py.test {posargs:--cov nefertari tests}
commands =
py.test {posargs:--cov nefertari tests}
python nefertari/scripts/scaffold_test.py -s nefertari_starter

[testenv:flake8]
deps =
Expand Down

0 comments on commit 1e0e5ff

Please sign in to comment.