Browse Source

fixed the tests

master
Julio Biason 11 years ago
parent
commit
e466071888
  1. 4
      luncho/blueprints/users.py
  2. 8
      luncho/helpers.py
  3. 13
      tests/users_tests.py

4
luncho/blueprints/users.py

@ -5,14 +5,14 @@
from flask import Blueprint from flask import Blueprint
from flask import request from flask import request
# from flask import jsonify from flask import jsonify
# from flask import current_app # from flask import current_app
from pony.orm import commit from pony.orm import commit
from luncho.helpers import ForceJSON from luncho.helpers import ForceJSON
from luncho.database import User from luncho.server import User
users = Blueprint('users', __name__) users = Blueprint('users', __name__)

8
luncho/helpers.py

@ -8,6 +8,7 @@ from functools import wraps
from flask import request from flask import request
from flask import jsonify from flask import jsonify
class ForceJSON(object): class ForceJSON(object):
def __init__(self, required=None): def __init__(self, required=None):
self.required = required or [] self.required = required or []
@ -22,14 +23,15 @@ class ForceJSON(object):
# now we have the JSON, let's check if all the fields are here. # now we have the JSON, let's check if all the fields are here.
missing = [] missing = []
for field in required or []: for field in self.required or []:
if not field in json: if not field in json:
missing.append(field) missing.append(field)
if missing: if missing:
fields = ', '.join(missing)
error = 'Missing fields: {fields}'.format(fields=fields)
return jsonify(status='ERROR', return jsonify(status='ERROR',
error='Missing fields: {fields}'.format( error=error)
fields=', '.join(missing)))
return func(*args, **kwargs) return func(*args, **kwargs)
return check_json return check_json

13
tests/users_tests.py

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import os
import tempfile import tempfile
import unittest import unittest
import json import json
@ -13,17 +12,23 @@ class TestUsers(unittest.TestCase):
"""Test users request.""" """Test users request."""
def setUp(self): def setUp(self):
(_, server.app.config['SQLITE_FILENAME']) = tempfile.mkstemp() (_, name) = tempfile.mkstemp()
server.app.config['SQLITE_FILENAME'] = name
server.app.config['TESTING'] = True
print server.app.config['SQLITE_FILENAME']
self.app = server.app.test_client() self.app = server.app.test_client()
def tearDown(self): def tearDown(self):
os.unlick(server.app.config['SQLITE_FILENAME']) # os.unlink(server.app.config['SQLITE_FILENAME'])
pass
def test_create_user(self): def test_create_user(self):
request = {'username': 'username', request = {'username': 'username',
'full_name': 'full name', 'full_name': 'full name',
'password': 'hash'} 'password': 'hash'}
rv = self.app.put('/users/', rv = self.app.put('/user/',
data=json.dumps(request), data=json.dumps(request),
content_type='application/json') content_type='application/json')
print rv.data print rv.data

Loading…
Cancel
Save