Skip to content

Commit

Permalink
test: add string function test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
richtia committed May 9, 2023
1 parent 2546347 commit 9c13219
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 4 deletions.
17 changes: 15 additions & 2 deletions bft/testers/postgres/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
from bft.cases.types import Case, CaseLiteral
from bft.dialects.types import SqlMapping

type_map = {"i16": "smallint", "i32": "integer", "i64": "bigint", "boolean": "boolean"}
type_map = {
"i16": "smallint",
"i32": "integer",
"i64": "bigint",
"boolean": "boolean",
"string": "text",
}


def type_to_postgres_type(type: str):
Expand Down Expand Up @@ -50,7 +56,14 @@ def run_sql_case(self, case: Case, mapping: SqlMapping) -> SqlCaseResult:

arg_names = [f"arg{idx}" for idx in range(len(case.args))]
joined_arg_names = ",".join(arg_names)
arg_vals = ",".join([literal_to_str(arg) for arg in case.args])
arg_vals = ",".join(
[
"'" + literal_to_str(arg) + "'"
if arg.type == "string" and arg.value is not None
else literal_to_str(arg)
for arg in case.args
]
)
self.conn.execute(
f"INSERT INTO my_table ({joined_arg_names}) VALUES ({arg_vals});"
)
Expand Down
10 changes: 9 additions & 1 deletion bft/testers/sqlite/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"fp32": "REAL",
"fp64": "REAL",
"boolean": "BOOLEAN",
"string": "TEXT",
}


Expand Down Expand Up @@ -46,7 +47,14 @@ def run_sql_case(self, case: Case, mapping: SqlMapping) -> SqlCaseResult:

arg_names = [f"arg{idx}" for idx in range(len(case.args))]
joined_arg_names = ",".join(arg_names)
arg_vals = ",".join([literal_to_str(arg) for arg in case.args])
arg_vals = ",".join(
[
"'" + literal_to_str(arg) + "'"
if arg.type == "string" and arg.value is not None
else literal_to_str(arg)
for arg in case.args
]
)
self.conn.execute(
f"INSERT INTO my_table ({joined_arg_names}) VALUES ({arg_vals});"
)
Expand Down
2 changes: 1 addition & 1 deletion bft/testers/velox/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def is_type_supported(type):
return type in set({"i64", "fp64", "boolean"})
return type in set({"i32", "i64", "fp64", "boolean", "string"})


class VeloxRunner(SqlCaseRunner):
Expand Down
42 changes: 42 additions & 0 deletions cases/string/concat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function: concat
cases:
- group:
id: basic
description: Basic examples without any special cases
args:
- value: 'abcd'
type: string
- value: 'efg'
type: string
result:
value: 'abcdefg'
type: string
- group:
id: null_input
description: Examples with null as input
args:
- value: 'abcd'
type: string
- value: null
type: string
result:
value: null
type: string
- group: null_input
args:
- value: null
type: string
- value: 'abcd'
type: string
result:
value: null
type: string
- group: null_input
args:
- value: null
type: string
- value: null
type: string
result:
value: null
type: string
60 changes: 60 additions & 0 deletions cases/string/like.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function: like
cases:
- group:
id: basic
description: Basic examples without any special cases
args:
- value: 'abcdefg'
type: string
- value: 'abcdefg'
type: string
result:
value: true
type: boolean
- group: basic
args:
- value: 'abcdefg'
type: string
- value: 'abc'
type: string
result:
value: false
type: boolean
- group:
id: wildcard
description: Examples using wildcards
args:
- value: 'abcdefg'
type: string
- value: 'abc%'
type: string
result:
value: true
type: boolean
- group: wildcard
args:
- value: 'abcdefg'
type: string
- value: '%efg'
type: string
result:
value: true
type: boolean
- group: wildcard
args:
- value: 'abcdefg'
type: string
- value: '_bcdefg'
type: string
result:
value: true
type: boolean
- group: wildcard
args:
- value: 'abcdefg'
type: string
- value: 'abc_efg'
type: string
result:
value: true
type: boolean
54 changes: 54 additions & 0 deletions cases/string/substring.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function: substring
cases:
- group:
id: basic
description: Basic examples without any special cases
args:
- value: 'abcdefg'
type: string
- value: 1
type: i32
- value: 5
type: i32
result:
value: 'abcde'
type: string
- group:
id: zero_start
description: Example where start argument is zero
args:
- value: 'abcdefg'
type: string
- value: 0
type: i32
- value: 3
type: i32
result:
value: 'ab'
type: string
- group:
id: negative_start
description: Example where start argument is a negative integer
args:
- value: 'abcdefg'
type: string
- value: -3
type: i32
- value: 2
type: i32
result:
value: 'ef'
type: string
- group:
id: start_greater_than_length
description: Example where start argument greater than the length of the string
args:
- value: 'abcdefg'
type: string
- value: 10
type: i32
- value: 2
type: i32
result:
value: ''
type: string
6 changes: 6 additions & 0 deletions dialects/postgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ scalar_functions:
- name: not
local_name: not
infix: False
- name: substring
- name: concat
local_name: "||"
infix: True
- name: like
infix: True
6 changes: 6 additions & 0 deletions dialects/sqlite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ scalar_functions:
- name: not
local_name: not
infix: False
- name: substring
- name: concat
local_name: "||"
infix: True
- name: like
infix: True
5 changes: 5 additions & 0 deletions dialects/velox_presto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ scalar_functions:
- name: not
local_name: not
infix: False
- name: substring
local_name: substr
- name: concat
- name: like
infix: True

0 comments on commit 9c13219

Please sign in to comment.