1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/db/pg/pg_orm_test.v

227 lines
4.6 KiB
V
Raw Normal View History

2021-07-24 20:49:40 +03:00
module main
import orm
import db.pg
2022-07-27 00:59:32 +03:00
import time
struct TestCustomSqlType {
id int [primary; sql: serial]
custom string [sql_type: 'TEXT']
custom1 string [sql_type: 'VARCHAR(191)']
custom2 string [sql_type: 'TIMESTAMP']
custom3 string [sql_type: 'uuid']
}
struct TestCustomWrongSqlType {
id int [primary; sql: serial]
custom string
custom1 string [sql_type: 'VARCHAR']
custom2 string [sql_type: 'money']
custom3 string [sql_type: 'xml']
}
struct TestTimeType {
mut:
id int [primary; sql: serial]
username string
created_at time.Time [sql_type: 'TIMESTAMP']
updated_at string [sql_type: 'TIMESTAMP']
deleted_at time.Time
}
struct TestDefaultAtribute {
id string [default: 'gen_random_uuid()'; primary; sql_type: 'uuid']
name string
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
}
2021-07-24 20:49:40 +03:00
fn test_pg_orm() {
mut db := pg.connect(
host: 'localhost'
user: 'postgres'
2022-07-27 00:59:32 +03:00
password: 'password'
2021-07-24 20:49:40 +03:00
dbname: 'postgres'
) or { panic(err) }
2022-07-27 00:59:32 +03:00
defer {
db.close()
}
2021-07-24 20:49:40 +03:00
db.create('Test', [
orm.TableField{
name: 'id'
typ: typeof[string]().idx
2022-07-27 00:59:32 +03:00
is_time: false
default_val: ''
is_arr: false
2021-07-24 20:49:40 +03:00
attrs: [
StructAttribute{
name: 'primary'
2022-07-27 00:59:32 +03:00
has_arg: false
arg: ''
kind: .plain
2021-07-24 20:49:40 +03:00
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
2022-07-27 00:59:32 +03:00
kind: .plain
2021-07-24 20:49:40 +03:00
},
]
},
orm.TableField{
name: 'name'
typ: typeof[string]().idx
2022-07-27 00:59:32 +03:00
is_time: false
default_val: ''
is_arr: false
2021-07-24 20:49:40 +03:00
attrs: []
},
orm.TableField{
name: 'age'
typ: typeof[i64]().idx
2022-07-27 00:59:32 +03:00
is_time: false
default_val: ''
is_arr: false
attrs: []
2021-07-24 20:49:40 +03:00
},
]) or { panic(err) }
db.insert('Test', orm.QueryData{
fields: ['name', 'age']
data: [orm.string_to_primitive('Louis'), orm.int_to_primitive(101)]
}) or { panic(err) }
res := db.@select(orm.SelectConfig{
table: 'Test'
2022-07-27 00:59:32 +03:00
is_count: false
2021-07-24 20:49:40 +03:00
has_where: true
2022-07-27 00:59:32 +03:00
has_order: false
order: ''
order_type: .asc
has_limit: false
primary: 'id'
has_offset: false
2021-07-24 20:49:40 +03:00
fields: ['id', 'name', 'age']
types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
2021-07-24 20:49:40 +03:00
}, orm.QueryData{}, orm.QueryData{
2022-07-27 00:59:32 +03:00
fields: ['name', 'age']
data: [orm.Primitive('Louis'), orm.Primitive(101)]
types: []
kinds: [.eq, .eq]
2021-07-24 20:49:40 +03:00
is_and: [true]
}) or { panic(err) }
id := res[0][0]
name := res[0][1]
age := res[0][2]
assert id is int
if id is int {
assert id == 1
}
assert name is string
if name is string {
assert name == 'Louis'
}
assert age is i64
if age is i64 {
assert age == 101
}
2022-07-27 00:59:32 +03:00
/** test orm sql type
* - verify if all type create by attribute sql_type has created
*/
sql db {
create table TestCustomSqlType
}
mut result_custom_sql := db.exec("
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TestCustomSqlType'
ORDER BY ORDINAL_POSITION
") or {
println(err)
panic(err)
}
mut information_schema_data_types_results := []string{}
information_schema_custom_sql := ['integer', 'text', 'character varying',
'timestamp without time zone', 'uuid']
for data_type in result_custom_sql {
information_schema_data_types_results << data_type.vals[0]
}
sql db {
drop table TestCustomSqlType
}
assert information_schema_data_types_results == information_schema_custom_sql
/** test_orm_time_type
* - test time.Time v type with sql_type: 'TIMESTAMP'
* - test string v type with sql_type: 'TIMESTAMP'
* - test time.Time v type without
*/
today := time.parse('2022-07-16 15:13:27') or {
println(err)
panic(err)
}
model := TestTimeType{
username: 'hitalo'
created_at: today
updated_at: today.str()
deleted_at: today
}
sql db {
create table TestTimeType
}
sql db {
insert model into TestTimeType
}
results := sql db {
select from TestTimeType where username == 'hitalo'
}
sql db {
drop table TestTimeType
}
assert results[0].username == model.username
assert results[0].created_at == model.created_at
assert results[0].updated_at == model.updated_at
assert results[0].deleted_at == model.deleted_at
/** test default attribute
*/
sql db {
create table TestDefaultAtribute
}
mut result_defaults := db.exec("
SELECT column_default
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TestDefaultAtribute'
ORDER BY ORDINAL_POSITION
") or {
println(err)
panic(err)
}
mut information_schema_defaults_results := []string{}
for defaults in result_defaults {
information_schema_defaults_results << defaults.vals[0]
}
sql db {
drop table TestDefaultAtribute
}
assert ['gen_random_uuid()', '', 'CURRENT_TIMESTAMP'] == information_schema_defaults_results
2021-07-24 20:49:40 +03:00
}