Commits

Anonymous committed d5ecadc

refactored unit tests to work even if DBINDEXER_BACKENDS isn't included in settings.py

Comments (0)

Files changed (3)

dbindexer/backends.py

     def create_index(self, lookup):
         field_to_index = self.get_field_to_index(lookup.model, lookup.field_name)
         
+        # backend doesn't now how to handle this index definition
         if not field_to_index:
             return 
         
             lookup.model.add_to_class(self.index_name(lookup), index_field)
             self.index_map[lookup] = index_field
             self.add_column_to_name(lookup.model, lookup.field_name)
+        else:
+            if lookup not in self.index_map:
+                self.index_map[lookup] = lookup.model._meta.get_field(
+                    self.index_name(lookup))
+                self.add_column_to_name(lookup.model, lookup.field_name)
+                
+        
 
     def convert_query(self, query):
         '''Converts a database saving query.'''

dbindexer/resolver.py

 class Resolver(object):
     def __init__(self):
         self.backends = []
-        for backend in getattr(settings, 'DBINDEXER_BACKENDS',
+        self.load_backends(getattr(settings, 'DBINDEXER_BACKENDS',
                                ('dbindexer.backends.BaseResolver',
-                                'dbindexer.backends.FKNullFix')):
+                                'dbindexer.backends.FKNullFix')))
+
+    def load_backends(self, backend_paths):
+        for backend in backend_paths:
                 self.backends.append(self.load_backend(backend))
-
+    
     def load_backend(self, path):
         module_name, attr_name = path.rsplit('.', 1)
         try:

dbindexer/tests.py

 from django.test import TestCase
 from dbindexer.api import register_index
 from dbindexer.lookups import StandardLookup
+from dbindexer.resolver import resolver 
 from djangotoolbox.fields import ListField
 from datetime import datetime
 import re
     foreignkey2 = models.ForeignKey(ForeignIndexed2, related_name='idx_set', null=True)
     tags = ListField(models.CharField(max_length=500, null=True))
 
-register_index(Indexed, {
-    'name': ('iexact', 'endswith', 'istartswith', 'iendswith', 'contains',
-             'icontains', re.compile('^i+', re.I), re.compile('^I+'),
-             re.compile('^i\d*i$', re.I)),
-    'published': ('month', 'day', 'year', 'week_day'),
-    'tags': ('iexact', 'icontains', StandardLookup() ),
-#    'foreignkey': 'iexact',
-    'foreignkey__title': 'iexact',
-    'foreignkey__name_fi': 'iexact',
-    'foreignkey__fk__name_fi2': ('iexact', 'endswith'),
-    'foreignkey2__name_fi2': (StandardLookup(), 'iexact'),
-    'foreignkey2__age': (StandardLookup())
-})
-
-register_index(ForeignIndexed, {
-    'title': 'iexact',
-    'name_fi': ('iexact', 'icontains'),
-    'fk__name_fi2': ('iexact', 'endswith'),
-    'fk__age': (StandardLookup()),
-})
-
 # TODO: add test for foreign key with multiple filters via different and equal paths
 # to do so we have to create some entities matching equal paths but not matching
 # different paths
 class TestIndexed(TestCase):
     def setUp(self):
+        self.backends = list(resolver.backends)
+        resolver.backends = []
+        resolver.load_backends(('dbindexer.backends.BaseResolver',
+                      'dbindexer.backends.FKNullFix',
+                      'dbindexer.backends.InMemoryJOINResolver',
+#                      'dbindexer.backends.ConstantFieldJOINResolver',
+        ))
+        self.register_indexex()
+        for backend in resolver.backends:
+            print backend.index_map
+        
         juubi = ForeignIndexed2(name_fi2='Juubi', age=2)
         juubi.save()
         rikudo = ForeignIndexed2(name_fi2='Rikudo', age=200)
                 foreignkey2=juubi).save()
         Indexed(name='I1038593i', tags=('Sharingan'), foreignkey=hachibi,
                 foreignkey2=rikudo).save()
-                
+    
+    def tearDown(self):
+        resolver.backends = self.backends
+        
+    def register_indexex(self):
+        register_index(Indexed, {
+            'name': ('iexact', 'endswith', 'istartswith', 'iendswith', 'contains',
+                     'icontains', re.compile('^i+', re.I), re.compile('^I+'),
+                     re.compile('^i\d*i$', re.I)),
+            'published': ('month', 'day', 'year', 'week_day'),
+            'tags': ('iexact', 'icontains', StandardLookup() ),
+        #    'foreignkey': 'iexact',
+            'foreignkey__title': 'iexact',
+            'foreignkey__name_fi': 'iexact',
+            'foreignkey__fk__name_fi2': ('iexact', 'endswith'),
+            'foreignkey2__name_fi2': (StandardLookup(), 'iexact'),
+            'foreignkey2__age': (StandardLookup())
+        })
+        
+        register_index(ForeignIndexed, {
+            'title': 'iexact',
+            'name_fi': ('iexact', 'icontains'),
+            'fk__name_fi2': ('iexact', 'endswith'),
+            'fk__age': (StandardLookup()),
+        })
         
     # TODO: add tests for created indexes for all backends!
 #    def test_model_fields(self):