Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix datetime fraction #541

Merged
merged 20 commits into from
Jul 31, 2019

Conversation

v-makouz
Copy link
Contributor

Fix for issue #540, where when inserting time with shorter precision causes an overflow, because the actual value is not truncated before being sent to the driver

@gordthompson
Copy link
Collaborator

gordthompson commented Mar 12, 2019

Tests for this fix.

(I thought I should be able to submit a PR directly to your fork but GitHub doesn't seem to want to let me.)

From 1c9a5d0efddd6b138c61976bbaac7389e700fde9
     Merge branch 'master' of https://github.com/v-makouz/pyodbc

To   0fe0270c2002588ae889ae60c510d47388bc32fa
     add tests for PR #541

--------------------------- tests2/sqlservertests.py ---------------------------
index eeac68d..730e43f 100755
@@ -448,7 +448,7 @@ class SqlServerTestCase(unittest.TestCase):
     def test_fast_executemany_to_local_temp_table(self):
         if self.driver_type_is('freetds'):
             warn('FREETDS_KNOWN_ISSUE - test_fast_executemany_to_local_temp_table: test cancelled.')
-            return 
+            return
         v = u'Ώπα'
         self.cursor.execute("CREATE TABLE #issue295 (id INT IDENTITY PRIMARY KEY, txt NVARCHAR(50))")
         sql = "INSERT INTO #issue295 (txt) VALUES (?)"
@@ -458,6 +458,18 @@ class SqlServerTestCase(unittest.TestCase):
         self.cursor.executemany(sql, params)
         self.assertEqual(self.cursor.execute("SELECT txt FROM #issue295").fetchval(), v)
 
+    def test_fast_executemany_to_datetime2(self):
+        if self.handle_known_issues_for('freetds', print_reminder=True):
+            warn('FREETDS_KNOWN_ISSUE - test_fast_executemany_to_datetime2: test cancelled.')
+            return
+        v = datetime(2019, 3, 12, 10, 0, 0, 123456)
+        self.cursor.execute("CREATE TABLE ##issue540 (dt2 DATETIME2(2))")
+        sql = "INSERT INTO ##issue540 (dt2) VALUES (?)"
+        params = [(v,)]
+        self.cursor.fast_executemany = True
+        self.cursor.executemany(sql, params)
+        self.assertEqual(self.cursor.execute("SELECT CAST(dt2 AS VARCHAR) FROM ##issue540").fetchval(), '2019-03-12 10:00:00.12')
+
     #
     # binary
     #

--------------------------- tests3/sqlservertests.py ---------------------------
index 4ced98b..db087d9 100644
@@ -440,7 +440,7 @@ class SqlServerTestCase(unittest.TestCase):
     def test_fast_executemany_to_local_temp_table(self):
         if self.handle_known_issues_for('freetds', print_reminder=True):
             warn('FREETDS_KNOWN_ISSUE - test_fast_executemany_to_local_temp_table: test cancelled.')
-            return 
+            return
         v = 'Ώπα'
         self.cursor.execute("CREATE TABLE #issue295 (id INT IDENTITY PRIMARY KEY, txt NVARCHAR(50))")
         sql = "INSERT INTO #issue295 (txt) VALUES (?)"
@@ -450,6 +450,18 @@ class SqlServerTestCase(unittest.TestCase):
         self.cursor.executemany(sql, params)
         self.assertEqual(self.cursor.execute("SELECT txt FROM #issue295").fetchval(), v)
 
+    def test_fast_executemany_to_datetime2(self):
+        if self.handle_known_issues_for('freetds', print_reminder=True):
+            warn('FREETDS_KNOWN_ISSUE - test_fast_executemany_to_datetime2: test cancelled.')
+            return
+        v = datetime(2019, 3, 12, 10, 0, 0, 123456)
+        self.cursor.execute("CREATE TABLE ##issue540 (dt2 DATETIME2(2))")
+        sql = "INSERT INTO ##issue540 (dt2) VALUES (?)"
+        params = [(v,)]
+        self.cursor.fast_executemany = True
+        self.cursor.executemany(sql, params)
+        self.assertEqual(self.cursor.execute("SELECT CAST(dt2 AS VARCHAR) FROM ##issue540").fetchval(), '2019-03-12 10:00:00.12')
+
     #
     # binary
     #

@gordthompson
Copy link
Collaborator

@v-makouz - Can you patch the above tests into your PR or would you prefer that I create a separate PR for mkleehammer/pyodbc ?

@v-makouz
Copy link
Contributor Author

@gordthompson I've added the tests, thanks for making them, I ran them to make sure everything works as expected

@nafisn
Copy link

nafisn commented Jul 30, 2019

When will this be merged?

@v-chojas
Copy link
Contributor

Only @mkleehammer can merge it.

@mkleehammer mkleehammer merged commit d7e2544 into mkleehammer:master Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants