]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Added a few IDENTITY tests for mssql.
authorMichael Trier <mtrier@gmail.com>
Sat, 31 Jan 2009 21:20:04 +0000 (21:20 +0000)
committerMichael Trier <mtrier@gmail.com>
Sat, 31 Jan 2009 21:20:04 +0000 (21:20 +0000)
test/dialect/mssql.py

index 3ee3ab9972c1cb4c0b5a5b5081b1ca70d9cd84fc..0962f59f3ba6a39fa73f6cdd21e2a69a49043d11 100755 (executable)
@@ -125,6 +125,61 @@ class CompileTest(TestBase, AssertsCompiledSQL):
         self.assert_compile(func.current_date(), "GETDATE()")
         self.assert_compile(func.length(3), "LEN(:length_1)")
 
+
+class IdentityInsertTest(TestBase, AssertsCompiledSQL):
+    __only_on__ = 'mssql'
+    __dialect__ = mssql.MSSQLDialect()
+
+    def setUpAll(self):
+        global metadata, cattable
+        metadata = MetaData(testing.db)
+
+        cattable = Table('cattable', metadata,
+            Column('id', Integer),
+            Column('description', String(50)),
+            PrimaryKeyConstraint('id', name='PK_cattable'),
+        )
+
+    def setUp(self):
+        metadata.create_all()
+
+    def tearDown(self):
+        metadata.drop_all()
+
+    def test_compiled(self):
+        self.assert_compile(cattable.insert().values(id=9, description='Python'), "INSERT INTO cattable (id, description) VALUES (:id, :description)")
+
+    def test_execute(self):
+        cattable.insert().values(id=9, description='Python').execute()
+
+        cats = cattable.select().order_by(cattable.c.id).execute()
+        self.assertEqual([(9, 'Python')], list(cats))
+
+        result = cattable.insert().values(description='PHP').execute()
+        self.assertEqual([10], result.last_inserted_ids())
+        lastcat = cattable.select().order_by(desc(cattable.c.id)).execute()
+        self.assertEqual((10, 'PHP'), lastcat.fetchone())
+
+    def test_executemany(self):
+        cattable.insert().execute([
+            {'id': 89, 'description': 'Python'},
+            {'id': 8, 'description': 'Ruby'},
+            {'id': 3, 'description': 'Perl'},
+            {'id': 1, 'description': 'Java'},
+        ])
+
+        cats = cattable.select().order_by(cattable.c.id).execute()
+        self.assertEqual([(1, 'Java'), (3, 'Perl'), (8, 'Ruby'), (89, 'Python')], list(cats))
+
+        cattable.insert().execute([
+            {'description': 'PHP'},
+            {'description': 'Smalltalk'},
+        ])
+
+        lastcats = cattable.select().order_by(desc(cattable.c.id)).limit(2).execute()
+        self.assertEqual([(91, 'Smalltalk'), (90, 'PHP')], list(lastcats))
+
+
 class ReflectionTest(TestBase):
     __only_on__ = 'mssql'