Skip to content

Commit

Permalink
Fix try_convert_by_sql in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robozmey committed Dec 2, 2024
1 parent 378fa57 commit f16052b
Show file tree
Hide file tree
Showing 4 changed files with 503 additions and 471 deletions.
13 changes: 8 additions & 5 deletions contrib/try_convert/data/tt_bool.data
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
f
true
t
t
f
false
t
t
f
t
False
t
f
t
True
TRUE
f
f
t
1
0
f
f
FALSE
49 changes: 39 additions & 10 deletions contrib/try_convert/generate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def remove_empty_lines(t):
f' LANGUAGE plpgsql AS\n' \
f'$func$\n' \
f' BEGIN\n' \
f' EXECUTE format(\'SELECT %L::%s\', $1, source_type)\n' \
f' EXECUTE format(\'SELECT %L::{type_name}::%s\', $1, pg_typeof(_out))\n' \
f' INTO _out;\n' \
f' EXCEPTION WHEN others THEN\n' \
f' -- do nothing: _out already carries default\n' \
Expand Down Expand Up @@ -221,18 +221,28 @@ def copy_data(table_name, filename, type_name):
def get_data(type_name):
return f'tt_{type_name}'

def get_len_from_data(type_name):
f = open(f'data/tt_{type_name}')
return(len(f.read().split('\n')))

def get_from_data(type_name, i = None):
f = open(f'data/tt_{type_name}.data')
values = f.read().split('\n')
if i is None:
return content
return values[i]

## TEST

def create_test(source_name, target_name, test_data):
def create_test(source_name, target_name, test_data, default='NULL'):

test_filter = 'not (v1 = v2)' if target_name not in uncomparable_types else 'not (v1::text = v2::text)'
test_filter = 'v1 is distinct from v2' if target_name not in uncomparable_types else 'v1::text is distinct from v2::text'

query = \
f'select * from (' \
f'select ' \
f'try_convert(v, NULL::{target_name}) as v1, ' \
f'try_convert_by_sql(v, NULL::{target_name}) as v2' \
f'try_convert(v, {default}::{target_name}) as v1, ' \
f'try_convert_by_sql(v, {default}::{target_name}) as v2' \
f' from {test_data}' \
f') as t(v1, v2) where {test_filter};'
result = \
Expand Down Expand Up @@ -284,18 +294,37 @@ def create_test(source_name, target_name, test_data):

for source_name, target_name in type_casts:

test_data = get_data(source_name)

if (source_name not in supported_types or target_name not in supported_types):
continue

test_in, test_out = create_test(source_name, target_name, test_data)
d = f'\'{get_from_data(target_name, 0)}\''

for default in ['NULL']:

function_tests_in += [test_in]
function_tests_out += [test_out]
test_data = get_data(source_name)

test_in, test_out = create_test(source_name, target_name, test_data, default)

function_tests_in += [test_in]
function_tests_out += [test_out]

# print(function_tests_in[0])


### DEFAULTS TEST

# for type_name in supported_types:

# query = f'SELECT try_convert({}::{}, {get_from_data(type_name, 0)}::{type_name});'

### ONE MILLION ERRORS

# TODO

### NESTED CASTS

# TODO

### CONSTRUCT TEST

test_str = '\n'.join([
Expand Down
Loading

0 comments on commit f16052b

Please sign in to comment.