Skip to content

Commit

Permalink
First unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Jan 19, 2020
1 parent d0fdf86 commit ead8ad6
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 18 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: python
python: 3.6
branches:
only:
- master
- "/^(release\\/)?v([1-9]\\d*!)?(0|[1-9]\\d*)(\\.(0|[1-9]\\d*))*((a|b|rc)(0|[1-9]\\d*))?(\\.post(0|[1-9]\\d*))?(\\.dev(0|[1-9]\\d*))?$/"
services:
- postgresql
before_install: make ci
install: make install
script: make cover
after_success: coveralls

10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
watch:
gunicorn -t99999 --reload fittonia:app

cover:
pytest tests --cov=fittonia

.PHONY=watch
install:
pip install -e .

ci:
pip install -r requirements-ci.txt

.PHONY=watch cover
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# fittonia

[![Build Status](https://travis-ci.org/pylover/fittonia.svg?branch=master)](https://travis-ci.org/pylover/fittonia)
[![Coverage Status](https://coveralls.io/repos/github/pylover/fittonia/badge.svg?branch=master)](https://coveralls.io/github/pylover/fittonia?branch=master)


Store, Update and get JSON document using URL.

22 changes: 14 additions & 8 deletions fittonia.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import sys

from yhttp import Application, json, statuses
from pony.orm import Required, PrimaryKey, Json, db_session as dbsession
from yhttp.extensions import pony as ponyext, auth
from pony.orm import Required, PrimaryKey, Json, db_session as dbsession, \
Optional
from yhttp.extensions import pony as ponyext, auth as authext


__version__ = '0.1.0'


app = Application()
authenticate = auth.install(app)
authenticate = authext.install(app)
db = ponyext.install(app)
app.settings.merge('''
db:
Expand All @@ -24,13 +25,18 @@
class Resource(db.Entity):
id = PrimaryKey(int, auto=True)
path = Required(str, unique=True)
author = Optional(str)
content = Required(Json)


@app.route(r'/(.*)')
@app.route(r'/users/(.*)(/.*)?')
@dbsession
@json
def get(req, path=None):
def get(req, username, path=None):
path = f'/users/{username}{path or ""}'
if path.endswith('/'):
path = path[:-1]

query = Resource.select(lambda r: r.path == path)
resource = query.first()
if resource is None:
Expand All @@ -39,7 +45,7 @@ def get(req, path=None):
return resource.content


@app.route(r'/(.*)')
@app.route(r'/users/(.*)')
@authenticate()
@dbsession
@json
Expand All @@ -53,7 +59,7 @@ def delete(req, path=None):
return resource.content


@app.route(r'/(.*)')
@app.route(r'/users/(.*)')
@authenticate()
@dbsession
@json
Expand All @@ -67,7 +73,7 @@ def update(req, path=None):
return req.form


@app.route(r'/(.*)')
@app.route(r'/users/(.*)')
@authenticate()
@dbsession
@json
Expand Down
3 changes: 3 additions & 0 deletions requirements-ci.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r requirements-test.txt
coveralls

2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-r requirements-test.txt
gunicorn
pytest-pudb
ipython

3 changes: 3 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bddrest
pytest-cov

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@


dependencies = [
'yhttp-pony >= 1, < 2',
'yhttp-pony >= 1.1.1, < 2',
'yhttp-auth >= 1.1, < 2',
]


Expand Down
16 changes: 9 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import pytest
import functools

import fittonia
import pytest
import bddrest
from yhttp.extensions.pony.testing import freshdb


@pytest.fixture
def app():
dbname = 'fittonia_test'
fittonia.app.settings.merge(f'''
db:
url: postgres://postgres:postgres@localhost/{dbname}
''')
from fittonia import app
return app


@pytest.fixture
def story(app):
return functools.partial(bddrest.Given, app)

29 changes: 29 additions & 0 deletions tests/test_usercontent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from bddrest import when, status, response
from pony.orm import db_session as dbsession

from fittonia import Resource


def test_usercontent(app, story, freshdb):
app.settings.merge(f'''
db:
url: {freshdb}
''')

@dbsession
def mockup():
Resource(path='/users/foo', author='foo', content=dict(foo='bar'))

app.ready()
mockup()

with story('Get a user content by anonymous user', '/users/foo'):
print(response.text)
assert status == 200
assert response.json == {'foo': 'bar'}

when('Try to update a user content by anonymous', verb='POST', form={
'foo': 'baz'
})
assert status == 401

0 comments on commit ead8ad6

Please sign in to comment.