Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta 'index_specs' resets #113

Closed
danaki opened this issue Sep 4, 2012 · 2 comments
Closed

Meta 'index_specs' resets #113

danaki opened this issue Sep 4, 2012 · 2 comments

Comments

@danaki
Copy link

danaki commented Sep 4, 2012

Here a test case I've prepared (for ME-0.7):

import re
import unittest
from mongoengine import Q, connect, Document
from mongoengine.queryset import QuerySet
from mongoengine.fields import *

class Entry(Document):
    meta = {
        'indexes': ['keywords']
    }

    filename = StringField(primary_key=True)
    body = StringField(max_length=140, required=True)
    keywords = ListField(StringField())

    def save(self, *args, **kwargs):
        self.keywords = re.split('\s+', self.body.lower())
        super(self.__class__, self).save(*args, **kwargs)

class TestTest(unittest.TestCase):
    def setUp(self):
        connect(db='mongoenginetest')

        self.kitty = Entry(
            body='Hello Kitty',
            filename='kitty.txt')
        self.kitty.save()

        self.doggy = Entry(
            body='Hello Doggy',
            filename='doggy.txt')
        self.doggy.save()

    def tearDown(self):
        """Clean all connections cached in the _document_registry."""
        Entry.drop_collection()

        QuerySet._reset_already_indexed()
        from mongoengine.base import _document_registry
        for k, doc in _document_registry.items():
            if not hasattr(doc, '_get_collection') or doc._meta.get('abstract', False):
                continue
            col = doc._get_collection()
            col.database.connection.drop_database(col.database.name)

    def test_1(self):
        pass

    def test_2(self):
        pass

When I run it, it gives:

======================================================================
ERROR: test_2 (__main__.TestTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/test2.py", line 27, in setUp
    self.kitty.save()
  File "tests/test2.py", line 18, in save
    super(self.__class__, self).save(*args, **kwargs)
  File "/home/up/.virtualenvs/flask-mongoengine-test/local/lib/python2.7/site-packages/mongoengine-0.7.0-py2.7.egg/mongoengine/document.py", line 210, in save
    collection = self.__class__.objects._collection
  File "/home/up/.virtualenvs/flask-mongoengine-test/local/lib/python2.7/site-packages/mongoengine-0.7.0-py2.7.egg/mongoengine/queryset.py", line 583, in _collection
    self._ensure_indexes()
  File "/home/up/.virtualenvs/flask-mongoengine-test/local/lib/python2.7/site-packages/mongoengine-0.7.0-py2.7.egg/mongoengine/queryset.py", line 482, in _ensure_indexes
    fields = spec.pop('fields')
KeyError: 'fields'

Using this debug code I've put to mongoengine/queryset.py ~583:

        from pprint import pprint
        import inspect
        print __file__
        print inspect.currentframe().f_back.f_lineno
        pprint(self._document._meta)

I see that items inside meta's index_specs sometimes disappear:

(flask-mongoengine-test)up@lithium ~/flask-mongoengine-test $ python tests/test2.py 
/home/up/.virtualenvs/flask-mongoengine-test/local/lib/python2.7/site-packages/mongoengine-0.7.0-py2.7.egg/mongoengine/queryset.pyc
583
{'abstract': False,
 'allow_inheritance': None,
 'collection': 'entry',
 'delete_rules': None,
 'id_field': 'filename',
 'index_background': False,
 'index_drop_dups': False,
 'index_opts': None,
 'index_specs': [{'fields': [('keywords', 1)]}],
 'indexes': ['keywords'],
 'max_documents': None,
 'max_size': None,
 'ordering': [],
 'unique_indexes': []}
./home/up/.virtualenvs/flask-mongoengine-test/local/lib/python2.7/site-packages/mongoengine-0.7.0-py2.7.egg/mongoengine/queryset.pyc
583
{'abstract': False,
 'allow_inheritance': None,
 'collection': 'entry',
 'delete_rules': None,
 'id_field': 'filename',
 'index_background': False,
 'index_drop_dups': False,
 'index_opts': None,
 'index_specs': [{}],
 'indexes': ['keywords'],
 'max_documents': None,
 'max_size': None,
 'ordering': [],
 'unique_indexes': []}

Tear down code is taken from hmarr#422

@rozza
Copy link
Contributor

rozza commented Sep 4, 2012

Thanks for the test code - not sure what issue you are hitting outside this testcase...

@rozza
Copy link
Contributor

rozza commented Sep 4, 2012

Fixed in the 0.7 branch

@rozza rozza closed this as completed Oct 1, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants