Commit a9da2b44 authored by adietmue's avatar adietmue
Browse files

Setup basic resource definition and tests.

parent 5f24a17e
"""The main app."""
from os import getcwd
from eve import Eve
from flask import Config
def create_app(settings=None):
"""Super simply bootstrapping for easier testing."""
config = Config(getcwd())
config.from_object('settings')
if settings is not None:
config.update(settings)
return Eve(settings=config)
app = create_app()
Cerberus==0.9.2
click==6.7
Eve==0.7.4
Events==0.2.2
Flask==0.12
Flask-PyMongo==0.5.1
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==0.23
pymongo==3.5.1
simplejson==3.11.1
Werkzeug==0.11.15
"""API Configuration."""
# DB
MONGO_HOST = 'localhost'
MONGO_PORT = 27017
MONGO_USERNAME = 'pvkuser'
MONGO_PASSWORD = 'pvkpass'
MONGO_DBNAME = 'pvk'
RESOURCE_METHODS = ['GET', 'POST']
# A schema for required start/end time tuple
TIMESPAN = {
'type': 'dict',
'schema': {
'start': {
'type': 'datetime',
'nullable': False,
'required': True,
},
'end': {
'type': 'datetime',
'nullable': False,
'required': True,
},
},
}
# Resources
DOMAIN = {
'lectures': {
'schema': {
'title': {
'type': 'string',
'maxlength': 100,
'unique': True,
'required': True,
'nullable': False,
'empty': False,
},
'department': {
'type': 'string',
'allowed': ['itet', 'mavt'],
'required': True,
'nullable': False,
},
'year': {
'type': 'integer',
'min': 1,
'max': 3,
'required': True
},
},
},
'assistants': {
'schema': {
'nethz': {
'type': 'string',
'maxlength': 10,
'unique': True,
'empty': False,
'nullable': False,
'required': True,
},
'name': {
'type': 'string',
'readonly': True,
},
},
},
'courses': {
'schema': {
'lecture': {
'type': 'objectid',
'data_relation': {
'resource': 'lectures',
'field': '_id',
'embeddable': True
},
},
'assistant': {
'type': 'objectid',
'data_relation': {
'resource': 'assistants',
'field': '_id',
'embeddable': True
},
},
'signup': TIMESPAN,
'datetimes': {
'type': 'list',
'schema': TIMESPAN,
},
'room': {
'type': 'string',
'maxlength': 100,
'unique': True,
'required': True,
'nullable': False,
'empty': False,
},
'spots': {
'type': 'integer',
'min': 1,
'required': True,
'nullable': False
}
},
},
'signups': {
'schema': {
'nethz': {
'type': 'string',
'maxlength': 10,
'unique': True,
'empty': False,
'nullable': False,
'required': True,
},
'name': {
'type': 'string',
'readonly': True,
},
'course': {
'type': 'objectid',
'data_relation': {
'resource': 'courses',
'field': '_id',
'embeddable': True
},
},
'status': {
'type': 'string',
'allowed': ['waiting', 'accepted', 'accepted+paid'],
'readonly': True,
},
},
}
}
"""Test fixtures."""
import pytest
import json
from pymongo import MongoClient
from flask.testing import FlaskClient
from app import create_app
TEST_SETTINGS = {
'MONGO_HOST': 'localhost',
'MONGO_PORT': 27017,
'MONGO_DBNAME': 'pvk_test',
'MONGO_USERNAME': 'pvkuser',
'MONGO_PASSWORD': 'pvkpass',
}
class TestClient(FlaskClient):
"""Custom test client for easier json handling."""
def open(self, *args, assert_status=None, **kwargs):
"""Built-in status_code assertion and send/return json."""
if "data" in kwargs:
# Parse data
kwargs['data'] = json.dumps(kwargs['data'])
# Set header
kwargs['content_type'] = "application/json"
response = super(TestClient, self).open(*args, **kwargs)
if assert_status is not None:
assert response.status_code == assert_status, \
response.get_data(as_text=True)
return json.loads(response.get_data(as_text=True))
def dropDB(app):
"""Drop drop drop!"""
connection = MongoClient(app.config['MONGO_HOST'],
app.config['MONGO_PORT'])
connection.drop_database(app.config['MONGO_DBNAME'])
connection.close()
@pytest.fixture(scope='session')
def client():
app = create_app(settings=TEST_SETTINGS)
app.test_client_class = TestClient
yield app.test_client()
dropDB(app)
"""Tests for basic requests to all resources."""
def test_create(client):
"""Test creating everything."""
lecture = {
'title': "Awesome Lecture",
'department': "itet",
'year': 3,
}
lecture_response = client.post('lectures', data=lecture, assert_status=201)
assistant = {'nethz': "Pablo"}
assistant_response = client.post('assistants',
data=assistant,
assert_status=201)
course = {
'lecture': lecture_response['_id'],
'assistant': assistant_response['_id'],
'room': 'ETZ E 6',
'spots': 30,
'signup': {
'start': 'Mon, 01 Jun 2020 01:15:00 GMT',
'end': 'Mon, 01 Jun 2020 04:15:00 GMT',
},
'datetimes': [{
'start': 'Mon, 01 Jun 2020 08:15:00 GMT',
'end': 'Mon, 01 Jun 2020 08:15:00 GMT',
}, {
'start': 'Tue, 02 Jun 2020 08:15:00 GMT',
'end': 'Tue, 02 Jun 2020 08:15:00 GMT',
}],
}
course_response = client.post('courses', data=course, assert_status=201)
signup = {
'nethz': "Pablito",
'course': course_response['_id']
}
client.post('signups', data=signup, assert_status=201)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment