I am trying to test a response made by the controller. The response is different when I am running it through gunicorn vs a testFramework
My server.py
looks like the following:
app = Flask(__name__, static_url_path='/pub')
api = Api(app, catch_all_404s=True)
connect('myapp', host='mongomock://localhost')
api.add_resource(IndexController, "/")
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=debug)
My IndexController
looks like:
from flask_restful import Resource
from myapp.models.User import User
class IndexController(Resource):
"""
This controller shows all the Users
"""
@classmethod
def get(cls):
"""
Simple GET call for /
"""
data = []
for user in User.objects:
data.append({
"name": user.name,
"location": user.location
})
The User
object:
from mongoengine import *
import datetime
class User(Document):
name = StringField(max_length=200, required=True, unique=True)
location = PointField(default=[0,0])
My IndexControllerTest
:
class IndexControllerTest(unittest.TestCase):
"""
This is the default controller Base class which
sets up the server app and has handy asserts to make
life easier when testing the controllers
"""
def setUp(self):
"""
Set up a global instance of the controller
"""
self.app = server.app.test_client()
def tearDown(self):
conn = get_connection()
conn.myapp.user.drop()
#Have to do this because it does not clear the unique index
def assertGet(self, path, data):
"""
This will run a GET request and test the output
"""
response = self.app.get(path)
#print str(response.get_data())
#print flask.json.dumps(response.data)
parsed = flask.json.loads(response.data)
self.assertEqual(parsed, data)
def test_get_returns_one_user(self):
"""
When I call a get I should get Users with thier data
"""
user = User(name="muse")
user.save()
self.assertGet(
"/",
[
{
"name": "muse",
"location": [ 0, 0 ]
}
],
status_code=200
)
Output through gunicorn
which is what I want!:
[
{
"name": "Hello",
"location": [
52.201962,
0.128145
]
},
{
"name": "World",
"location": [
0,
0
]
}
]
Output from my test:
...
First differing element 0:
{u'name': u'muse', u'location': {u'type': u'Point', u'coordinates': [0, 0]}}
{'name': 'muse', 'location': [0, 0]}
- [{u'location': {u'coordinates': [0, 0], u'type': u'Point'}, u'name': u'muse'}]
+ [{'location': [0, 0], 'name': 'muse'}]
What? Why? Where? Who? @£$@$%&£$(%&£$((& Madness!
I would expect the flask_restfull
API to ensure in both cases the output is the same