1+ from contextlib import suppress
12import unittest
2- import preql
33import time
44from data_diff import database as db
5- from data_diff .diff_tables import TableDiffer , TableSegment , split_space
5+ from data_diff .diff_tables import TableDiffer , TableSegment
66from parameterized import parameterized , parameterized_class
7- from .common import CONN_STRINGS , str_to_checksum
7+ from .common import CONN_STRINGS
88import logging
99
1010logging .getLogger ("diff_tables" ).setLevel (logging .WARN )
1818 "int" : [127 , - 3 , - 9 , 37 , 15 , 127 ],
1919 "datetime_no_timezone" : [
2020 "2020-01-01 15:10:10" ,
21- "2020-01 -01 9:9:9" ,
22- "2022-01 -01 15:10:01.139" ,
23- "2022-01 -01 15:10:02.020409" ,
24- "2022-01 -01 15:10:03.003030" ,
25- "2022-01 -01 15:10:05.009900" ,
21+ "2020-02 -01 9:9:9" ,
22+ "2022-03 -01 15:10:01.139" ,
23+ "2022-04 -01 15:10:02.020409" ,
24+ "2022-05 -01 15:10:03.003030" ,
25+ "2022-06 -01 15:10:05.009900" ,
2626 ],
2727 "float" : [0.0 , 0.1 , 0.10 , 10.0 , 100.98 ],
2828}
101101 # "int",
102102 ],
103103 "datetime_no_timezone" : [
104- # "TIMESTAMP",
104+ "TIMESTAMP" ,
105105 ],
106106 # https://docs.aws.amazon.com/redshift/latest/dg/r_Numeric_types201.html#r_Numeric_types201-floating-point-types
107107 "float" : [
115115 # "int",
116116 ],
117117 "datetime_no_timezone" : [
118- # "timestamp",
119- # "timestamp(6)",
120- # "timestamp(9)",
118+ "timestamp with local time zone " ,
119+ "timestamp(6) with local time zone " ,
120+ "timestamp(9) with local time zone " ,
121121 ],
122122 "float" : [
123123 # "float",
@@ -179,14 +179,29 @@ def expand_params(testcase_func, param_num, param):
179179
180180
181181def _insert_to_table (conn , table , values ):
182- insertion_query = f"INSERT INTO { table } (id, col) VALUES "
183- for j , sample in values :
184- insertion_query += f"({ j } , '{ sample } '),"
185-
186- conn .query (insertion_query [0 :- 1 ], None )
182+ insertion_query = f"INSERT INTO { table } (id, col) "
183+
184+ if isinstance (conn , db .Oracle ):
185+ selects = []
186+ for j , sample in values :
187+ selects .append ( f"SELECT { j } , timestamp '{ sample } ' FROM dual" )
188+ insertion_query += ' UNION ALL ' .join (selects )
189+ else :
190+ insertion_query += ' VALUES '
191+ for j , sample in values :
192+ insertion_query += f"({ j } , '{ sample } '),"
193+ insertion_query = insertion_query [0 :- 1 ]
194+
195+ conn .query (insertion_query , None )
187196 if not isinstance (conn , db .BigQuery ):
188197 conn .query ("COMMIT" , None )
189198
199+ def _drop_table_if_exists (conn , table ):
200+ with suppress (db .QueryError ):
201+ if isinstance (conn , db .Oracle ):
202+ conn .query (f"DROP TABLE { table } " , None )
203+ else :
204+ conn .query (f"DROP TABLE IF EXISTS { table } " , None )
190205
191206class TestDiffCrossDatabaseTables (unittest .TestCase ):
192207 @parameterized .expand (type_pairs , name_func = expand_params )
@@ -204,14 +219,14 @@ def test_types(self, source_db, target_db, source_type, target_type, type_catego
204219 src_table = src_conn .quote ("." .join (src_table_path ))
205220 dst_table = dst_conn .quote ("." .join (dst_table_path ))
206221
207- src_conn . query ( f"DROP TABLE IF EXISTS { src_table } " , None )
208- src_conn .query (f"CREATE TABLE { src_table } (id int, col { source_type } ); " , None )
222+ _drop_table_if_exists ( src_conn , src_table )
223+ src_conn .query (f"CREATE TABLE { src_table } (id int, col { source_type } )" , None )
209224 _insert_to_table (src_conn , src_table , enumerate (sample_values , 1 ))
210225
211226 values_in_source = src_conn .query (f"SELECT id, col FROM { src_table } " , list )
212227
213- dst_conn . query ( f"DROP TABLE IF EXISTS { dst_table } " , None )
214- dst_conn .query (f"CREATE TABLE { dst_table } (id int, col { target_type } ); " , None )
228+ _drop_table_if_exists ( dst_conn , dst_table )
229+ dst_conn .query (f"CREATE TABLE { dst_table } (id int, col { target_type } )" , None )
215230 _insert_to_table (dst_conn , dst_table , values_in_source )
216231
217232 self .table = TableSegment (self .src_conn , src_table_path , "id" , None , ("col" ,), case_sensitive = False )
@@ -235,3 +250,4 @@ def test_types(self, source_db, target_db, source_type, target_type, type_catego
235250
236251 duration = time .time () - start
237252 # print(f"source_db={source_db.__name__} target_db={target_db.__name__} source_type={source_type} target_type={target_type} duration={round(duration * 1000, 2)}ms")
253+
0 commit comments