Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Split up test and make the State classes a bit better.

  • Loading branch information...
commit 7d041b93944174c88bf8c9342d91203b28041a04 1 parent 028bbd1
Andrew Godwin authored May 18, 2013
37  django/db/migrations/state.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django.db import models
2 2
 from django.db.models.loading import BaseAppCache
  3
+from django.utils.module_loading import import_by_path
3 4
 
4 5
 
5 6
 class ProjectState(object):
@@ -15,12 +16,9 @@ def __init__(self, models=None):
15 16
 
16 17
     def clone(self):
17 18
         "Returns an exact copy of this ProjectState"
18  
-        ps = ProjectState(
  19
+        return ProjectState(
19 20
             models = dict((k, v.copy()) for k, v in self.models.items())
20 21
         )
21  
-        for model in ps.models.values():
22  
-            model.project_state = ps
23  
-        return ps
24 22
 
25 23
     def render(self):
26 24
         "Turns the project state into actual models in a new AppCache"
@@ -33,8 +31,11 @@ def render(self):
33 31
     @classmethod
34 32
     def from_app_cache(cls, app_cache):
35 33
         "Takes in an AppCache and returns a ProjectState matching it"
  34
+        models = {}
36 35
         for model in app_cache.get_models():
37  
-            print model
  36
+            model_state = ModelState.from_model(model)
  37
+            models[(model_state.app_label, model_state.name.lower())] = model_state
  38
+        return cls(models)
38 39
 
39 40
 
40 41
 class ModelState(object):
@@ -44,18 +45,36 @@ class ModelState(object):
44 45
     mutate this one and then render it into a Model as required.
45 46
     """
46 47
 
47  
-    def __init__(self, project_state, app_label, name, fields=None, options=None, bases=None):
48  
-        self.project_state = project_state
  48
+    def __init__(self, app_label, name, fields=None, options=None, bases=None):
49 49
         self.app_label = app_label
50 50
         self.name = name
51 51
         self.fields = fields or []
52 52
         self.options = options or {}
53  
-        self.bases = bases or None
  53
+        self.bases = bases or (models.Model, )
  54
+
  55
+    @classmethod
  56
+    def from_model(cls, model):
  57
+        """
  58
+        Feed me a model, get a ModelState representing it out.
  59
+        """
  60
+        # Deconstruct the fields
  61
+        fields = []
  62
+        for field in model._meta.local_fields:
  63
+            name, path, args, kwargs = field.deconstruct()
  64
+            field_class = import_by_path(path)
  65
+            fields.append((name, field_class(*args, **kwargs)))
  66
+        # Make our record
  67
+        return cls(
  68
+            model._meta.app_label,
  69
+            model._meta.object_name,
  70
+            fields,
  71
+            {},
  72
+            None,
  73
+        )
54 74
 
55 75
     def clone(self):
56 76
         "Returns an exact copy of this ModelState"
57 77
         return self.__class__(
58  
-            project_state = self.project_state,
59 78
             app_label = self.app_label,
60 79
             name = self.name,
61 80
             fields = self.fields,
26  tests/migrations/tests.py → tests/migrations/test_graph.py
@@ -134,29 +134,3 @@ def test_load(self):
134 134
             graph.forwards_plan(("migrations", "0002_second")),
135 135
             [("migrations", "0001_initial"), ("migrations", "0002_second")],
136 136
         )
137  
-
138  
-
139  
-class RecorderTests(TestCase):
140  
-    """
141  
-    Tests the disk and database loader.
142  
-    """
143  
-
144  
-    def test_apply(self):
145  
-        """
146  
-        Tests marking migrations as applied/unapplied.
147  
-        """
148  
-        recorder = MigrationRecorder(connection)
149  
-        self.assertEqual(
150  
-            recorder.applied_migrations(),
151  
-            set(),
152  
-        )
153  
-        recorder.record_applied("myapp", "0432_ponies")
154  
-        self.assertEqual(
155  
-            recorder.applied_migrations(),
156  
-            set([("myapp", "0432_ponies")]),
157  
-        )
158  
-        recorder.record_unapplied("myapp", "0432_ponies")
159  
-        self.assertEqual(
160  
-            recorder.applied_migrations(),
161  
-            set(),
162  
-        )
29  tests/migrations/test_loader.py
... ...
@@ -0,0 +1,29 @@
  1
+from django.test import TestCase
  2
+from django.db import connection
  3
+from django.db.migrations.recorder import MigrationRecorder
  4
+
  5
+
  6
+class RecorderTests(TestCase):
  7
+    """
  8
+    Tests the disk and database loader.
  9
+    """
  10
+
  11
+    def test_apply(self):
  12
+        """
  13
+        Tests marking migrations as applied/unapplied.
  14
+        """
  15
+        recorder = MigrationRecorder(connection)
  16
+        self.assertEqual(
  17
+            recorder.applied_migrations(),
  18
+            set(),
  19
+        )
  20
+        recorder.record_applied("myapp", "0432_ponies")
  21
+        self.assertEqual(
  22
+            recorder.applied_migrations(),
  23
+            set([("myapp", "0432_ponies")]),
  24
+        )
  25
+        recorder.record_unapplied("myapp", "0432_ponies")
  26
+        self.assertEqual(
  27
+            recorder.applied_migrations(),
  28
+            set(),
  29
+        )
43  tests/migrations/test_state.py
... ...
@@ -0,0 +1,43 @@
  1
+from django.test import TestCase
  2
+from django.db import models
  3
+from django.db.models.loading import BaseAppCache
  4
+from django.db.migrations.state import ProjectState
  5
+
  6
+
  7
+class StateTests(TestCase):
  8
+    """
  9
+    Tests state construction, rendering and modification by operations.
  10
+    """
  11
+
  12
+    def test_create(self):
  13
+        """
  14
+        Tests making a ProjectState from an AppCache
  15
+        """
  16
+        new_app_cache = BaseAppCache()
  17
+
  18
+        class Author(models.Model):
  19
+            name = models.CharField(max_length=255)
  20
+            bio = models.TextField()
  21
+            age = models.IntegerField(blank=True, null=True)
  22
+            class Meta:
  23
+                app_label = "migrations"
  24
+                app_cache = new_app_cache
  25
+
  26
+        class Book(models.Model):
  27
+            title = models.CharField(max_length=1000)
  28
+            author = models.ForeignKey(Author)
  29
+            class Meta:
  30
+                app_label = "migrations"
  31
+                app_cache = new_app_cache
  32
+
  33
+        project_state = ProjectState.from_app_cache(new_app_cache)
  34
+        author_state = project_state.models['migrations', 'author']
  35
+        book_state = project_state.models['migrations', 'book']
  36
+        
  37
+        self.assertEqual(author_state.app_label, "migrations")
  38
+        self.assertEqual(author_state.name, "Author")
  39
+        self.assertEqual([x for x, y in author_state.fields], ["id", "name", "bio", "age"])
  40
+        self.assertEqual(author_state.fields[1][1].max_length, 255)
  41
+        self.assertEqual(author_state.fields[2][1].null, False)
  42
+        self.assertEqual(author_state.fields[3][1].null, True)
  43
+        self.assertEqual(author_state.bases, (models.Model, ))

0 notes on commit 7d041b9

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