Question

I am following this http://pythonhosted.org/Flask-MongoKit/ as an example

I am just trying to get an instance of Document for writing unit test, but it isn't working. Here's the code for test:

import unittest
from tests import app, db, ctx
from word.models import Word

class ModelWordTestCase(unittest.TestCase):

    def setUp(self):
        pass

    def test_model_word(self):
        print db.Word

        word = db.Word()

        self.assertIsNotNone(word)

    def tearDown(self):
        pass

Word Class

from flask.ext.mongokit import Document
from core import db

@db.register
class Word(Document):
    __collection__ = 'words'
    use_dot_notation = True

    STATUS = {
        "approved" : 1,
        "pending" : 0,
        "rejected" : -1,
    }

    structure = {
        'lang': unicode,
        'local': unicode,
        'pronunciation' : unicode,
        'meaning': unicode,
        }

Surprisingly db.Word exists when printed with print db.Word statement, but it can't be called to create a new instance as done in tutorial I mentioned above. Here's the output of test:

Collection(Database(MongoClient('localhost', 27017), u'words_test'), u'Word')

E
======================================================================
ERROR: test_model_word (tests.model_tests.ModelWordTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/model_tests.py", line 14, in test_model_word
    word = db.Word()
  File "/usr/local/lib/python2.7/dist-packages/mongokit/collection.py", line 64, in __call__
    self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'Word' method on a 'Database' object it is failing because no such method exists.

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

How can I fix this and get instance of Word document so that I can create and save a record.

Was it helpful?

Solution

I think you are using the wrong db instance in your test. You should use the same db instance you used to register your models. So, I don't know how your code is structured but, at first glance, you should import the db from your models:

>>> from word.models import db
>>> db.Word.find()

or maybe

>>> from core import db
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top