簡體   English   中英

Mongoengine + MongoMock不會按預期返回數據

[英]Mongoengine + MongoMock do not return data as expected

我正在嘗試測試控制器做出的響應。 當我通過gunicorntestFramework運行時,響應是不同的

我的server.py如下所示:

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看起來像:

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對象:

from mongoengine import *
import datetime

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

我的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
        )

通過gunicorn輸出,這是我想要的!:

[
  {
    "name": "Hello",
    "location": [
      52.201962,
      0.128145
    ]
  },
  {
    "name": "World",
    "location": [
      0,
      0
    ]
  }
]

我的測試輸出:

...
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在兩種情況下都能確保輸出相同

事實證明,我的數據庫先前插入的內容如下:

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

但我希望它是(測試是正確的):

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

因此我的測試是正確的,但是我的數據庫反映不佳,因此無法按預期返回。 我以為如果數據錯誤,mongoengine會拋出某種錯誤,但是很高興知道它沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM