首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoengine + MongoMock不按预期返回数据

Mongoengine + MongoMock不按预期返回数据
EN

Stack Overflow用户
提问于 2016-03-24 14:40:14
回答 1查看 1.5K关注 0票数 0

我正在试着测试控制器的反应。当我在gunicorn vs a testFramework上运行它时,响应是不同的。

我的server.py如下所示:

代码语言:javascript
复制
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)

我的IndexController看起来像:

代码语言:javascript
复制
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
        })

User对象:

代码语言:javascript
复制
from mongoengine import *
import datetime

class User(Document):
    name = StringField(max_length=200, required=True, unique=True)
    location = PointField(default=[0,0])

我的IndexControllerTest

代码语言:javascript
复制
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
        )

通过gunicorn输出,这正是我想要的!:

代码语言:javascript
复制
[
  {
    "name": "Hello",
    "location": [
      52.201962,
      0.128145
    ]
  },
  {
    "name": "World",
    "location": [
      0,
      0
    ]
  }
]

我测试的输出:

代码语言:javascript
复制
...
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'}]

什么?为什么?哪里?谁?@£$@$%和£$(%和£$(& Madness!)

我希望flask_restfull API在这两种情况下都能确保输出是相同的。

EN

回答 1

Stack Overflow用户

发布于 2016-03-26 14:09:34

原来,我的DB从以前的插入中获得了以下内容:

代码语言:javascript
复制
{
        "_id" : ObjectId("56f6966f007502d9952babde"),
        "name" : "World",
        "created" : ISODate("2016-03-26T14:02:23.546Z"),
        "location" : [
                50.201962,
                1.128145
        ]
}

但我希望是这样(测试是正确的):

代码语言:javascript
复制
{
        "_id" : ObjectId("56f6966f007502d9952babde"),
        "name" : "World",
        "created" : ISODate("2016-03-26T14:02:23.546Z"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        50.201962,
                        1.128145
                ]
        }
}

所以我的测试是正确的,但是,我的db反映的不好,因此它不会像预期的那样返回。我认为如果数据是错误的,那么mongoengine会抛出某种错误,但是,很高兴知道它没有。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36202873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档