Skip to content
This repository
Browse code

Adding tests for db_column

  • Loading branch information...
commit 120ab5c7f9e1267a8250df104edfb863427c2d13 1 parent 08d5d4f
Charles Leifer authored October 02, 2012

Showing 1 changed file with 35 additions and 11 deletions. Show diff stats Hide diff stats

  1. 46  tests.py
46  tests.py
@@ -149,9 +149,18 @@ class NonIntModel(TestModel):
149 149
 class NonIntRelModel(TestModel):
150 150
     non_int_model = ForeignKeyField(NonIntModel, related_name='nr')
151 151
 
  152
+class DBUser(TestModel):
  153
+    user_id = PrimaryKeyField(db_column='db_user_id')
  154
+    username = CharField(db_column='db_username')
  155
+
  156
+class DBBlog(TestModel):
  157
+    blog_id = PrimaryKeyField(db_column='db_blog_id')
  158
+    title = CharField(db_column='db_title')
  159
+    user = ForeignKeyField(DBUser, db_column='db_user')
  160
+
152 161
 
153 162
 MODELS = [User, Blog, Comment, Relationship, NullModel, UniqueModel, OrderedModel, Category, UserCategory,
154  
-          NonIntModel, NonIntRelModel,]
  163
+          NonIntModel, NonIntRelModel, DBUser, DBBlog]
155 164
 
156 165
 def drop_tables(only=None):
157 166
     for model in reversed(MODELS):
@@ -1144,14 +1153,6 @@ def test_non_int_pk(self):
1144 1153
         ])
1145 1154
 
1146 1155
     def test_non_int_fk(self):
1147  
-        """
1148  
-        class NonIntModel(TestModel):
1149  
-            pk = CharField(primary_key=True)
1150  
-            data = CharField()
1151  
-
1152  
-        class NonIntRelModel(TestModel):
1153  
-            non_int_model = ForeignKeyField(NonIntModel)
1154  
-        """
1155 1156
         ni1 = NonIntModel.create(pk='a1', data='ni1')
1156 1157
         ni2 = NonIntModel.create(pk='a2', data='ni2')
1157 1158
 
@@ -1171,8 +1172,31 @@ class NonIntRelModel(TestModel):
1171 1172
 
1172 1173
 
1173 1174
 class DBColumnTestCase(ModelTestCase):
1174  
-    requires = []
1175  
-    # TODO (test fields which specify a db_column)
  1175
+    requires = [DBUser, DBBlog]
  1176
+
  1177
+    def test_select(self):
  1178
+        sq = DBUser.select().where(DBUser.username == 'u1')
  1179
+        self.assertSelect(sq, 'dbuser."db_user_id", dbuser."db_username"', [])
  1180
+        self.assertWhere(sq, 'dbuser."db_username" = ?', ['u1'])
  1181
+
  1182
+        sq = DBUser.select(DBUser.user_id).join(DBBlog).where(DBBlog.title == 'b1')
  1183
+        self.assertSelect(sq, 'dbuser."db_user_id"', [])
  1184
+        self.assertJoins(sq, ['INNER JOIN "dbblog" AS dbblog ON dbuser."db_user_id" = dbblog."db_user"'])
  1185
+        self.assertWhere(sq, 'dbblog."db_title" = ?', ['b1'])
  1186
+
  1187
+    def test_db_column(self):
  1188
+        u1 = DBUser.create(username='u1')
  1189
+        u2 = DBUser.create(username='u2')
  1190
+        u2_db = DBUser.get(user_id=u2.get_id())
  1191
+        self.assertEqual(u2_db.username, 'u2')
  1192
+
  1193
+        b1 = DBBlog.create(user=u1, title='b1')
  1194
+        b2 = DBBlog.create(user=u2, title='b2')
  1195
+        b2_db = DBBlog.get(blog_id=b2.get_id())
  1196
+        self.assertEqual(b2_db.user.user_id, u2.user_id)
  1197
+        self.assertEqual(b2_db.title, 'b2')
  1198
+        
  1199
+        self.assertEqual([b.title for b in u2.dbblog_set], ['b2'])
1176 1200
 
1177 1201
 
1178 1202
 class TransactionTestCase(ModelTestCase):

0 notes on commit 120ab5c

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