Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix state tests a little

  • Loading branch information...
commit 38a8cf1cdccb937297c7735aa73f6c02d468e7df 1 parent 7d041b9
Andrew Godwin authored May 18, 2013
11  django/db/migrations/state.py
@@ -14,6 +14,9 @@ def __init__(self, models=None):
14 14
         self.models = models or {}
15 15
         self.app_cache = None
16 16
 
  17
+    def add_model_state(self, model_state):
  18
+        self.models[(model_state.app_label, model_state.name.lower())] = model_state
  19
+
17 20
     def clone(self):
18 21
         "Returns an exact copy of this ProjectState"
19 22
         return ProjectState(
@@ -24,7 +27,7 @@ def render(self):
24 27
         "Turns the project state into actual models in a new AppCache"
25 28
         if self.app_cache is None:
26 29
             self.app_cache = BaseAppCache()
27  
-            for model in self.model.values:
  30
+            for model in self.models.values():
28 31
                 model.render(self.app_cache)
29 32
         return self.app_cache
30 33
 
@@ -90,10 +93,6 @@ def render(self, app_cache):
90 93
         meta = type("Meta", tuple(), meta_contents)
91 94
         # Then, work out our bases
92 95
         # TODO: Use the actual bases
93  
-        if self.bases:
94  
-            raise NotImplementedError("Custom bases not quite done yet!")
95  
-        else:
96  
-            bases = [models.Model]
97 96
         # Turn fields into a dict for the body, add other bits
98 97
         body = dict(self.fields)
99 98
         body['Meta'] = meta
@@ -101,6 +100,6 @@ def render(self, app_cache):
101 100
         # Then, make a Model object
102 101
         return type(
103 102
             self.name,
104  
-            tuple(bases),
  103
+            tuple(self.bases),
105 104
             body,
106 105
         )
31  tests/migrations/test_state.py
... ...
@@ -1,7 +1,7 @@
1 1
 from django.test import TestCase
2 2
 from django.db import models
3 3
 from django.db.models.loading import BaseAppCache
4  
-from django.db.migrations.state import ProjectState
  4
+from django.db.migrations.state import ProjectState, ModelState
5 5
 
6 6
 
7 7
 class StateTests(TestCase):
@@ -13,6 +13,7 @@ def test_create(self):
13 13
         """
14 14
         Tests making a ProjectState from an AppCache
15 15
         """
  16
+
16 17
         new_app_cache = BaseAppCache()
17 18
 
18 19
         class Author(models.Model):
@@ -41,3 +42,31 @@ class Meta:
41 42
         self.assertEqual(author_state.fields[2][1].null, False)
42 43
         self.assertEqual(author_state.fields[3][1].null, True)
43 44
         self.assertEqual(author_state.bases, (models.Model, ))
  45
+        
  46
+        self.assertEqual(book_state.app_label, "migrations")
  47
+        self.assertEqual(book_state.name, "Book")
  48
+        self.assertEqual([x for x, y in book_state.fields], ["id", "title", "author"])
  49
+        self.assertEqual(book_state.fields[1][1].max_length, 1000)
  50
+        self.assertEqual(book_state.fields[2][1].null, False)
  51
+        self.assertEqual(book_state.bases, (models.Model, ))
  52
+
  53
+    def test_render(self):
  54
+        """
  55
+        Tests rendering a ProjectState into an AppCache.
  56
+        """
  57
+        project_state = ProjectState()
  58
+        project_state.add_model_state(ModelState(
  59
+            "migrations",
  60
+            "Tag",
  61
+            [
  62
+                ("id", models.AutoField(primary_key=True)),
  63
+                ("name", models.CharField(max_length=100)),
  64
+                ("hidden", models.BooleanField()),
  65
+            ],
  66
+            {},
  67
+            None,
  68
+        ))
  69
+
  70
+        new_app_cache = project_state.render()
  71
+        self.assertEqual(new_app_cache.get_model("migrations", "Tag")._meta.get_field_by_name("name")[0].max_length, 100)
  72
+        self.assertEqual(new_app_cache.get_model("migrations", "Tag")._meta.get_field_by_name("hidden")[0].null, False)

0 notes on commit 38a8cf1

Please sign in to comment.
Something went wrong with that request. Please try again.