This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from postatum/101462172_test_scaffold
Test nefertari_starter scaffold
- Loading branch information
Showing
10 changed files
with
287 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import binascii | ||
import os | ||
import subprocess | ||
|
||
|
Empty file.
123 changes: 123 additions & 0 deletions
123
nefertari/scaffolds/nefertari_starter/+package+/tests/api.raml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
nefertari/scaffolds/nefertari_starter/+package+/tests/items.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
nefertari/scaffolds/nefertari_starter/+package+/tests/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
33 changes: 33 additions & 0 deletions
33
nefertari/scaffolds/nefertari_starter/+package+/tests/test_api.py_tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ releases | |
sphinx | ||
sphinxcontrib-fulltoc | ||
webtest | ||
virtualenv | ||
|
||
-e . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters