mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
orm: default attribute (#15221)
This commit is contained in:

committed by
GitHub

parent
c976a691ad
commit
e5e750d533
@ -1,6 +1,7 @@
|
||||
import orm
|
||||
import mysql
|
||||
import time
|
||||
import v.ast
|
||||
|
||||
struct TestCustomSqlType {
|
||||
id int [primary; sql: serial]
|
||||
@ -29,20 +30,28 @@ mut:
|
||||
deleted_at time.Time
|
||||
}
|
||||
|
||||
struct TestDefaultAtribute {
|
||||
id string [primary; sql: serial]
|
||||
name string
|
||||
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
|
||||
}
|
||||
|
||||
fn test_mysql_orm() {
|
||||
mut mdb := mysql.Connection{
|
||||
mut db := mysql.Connection{
|
||||
host: 'localhost'
|
||||
port: 3306
|
||||
username: 'root'
|
||||
password: ''
|
||||
dbname: 'mysql'
|
||||
}
|
||||
mdb.connect() or { panic(err) }
|
||||
db := orm.Connection(mdb)
|
||||
db.connect() or { panic(err) }
|
||||
defer {
|
||||
db.close()
|
||||
}
|
||||
db.create('Test', [
|
||||
orm.TableField{
|
||||
name: 'id'
|
||||
typ: 7
|
||||
typ: ast.int_type_idx
|
||||
attrs: [
|
||||
StructAttribute{
|
||||
name: 'primary'
|
||||
@ -57,12 +66,12 @@ fn test_mysql_orm() {
|
||||
},
|
||||
orm.TableField{
|
||||
name: 'name'
|
||||
typ: 20
|
||||
typ: ast.string_type_idx
|
||||
attrs: []
|
||||
},
|
||||
orm.TableField{
|
||||
name: 'age'
|
||||
typ: 7
|
||||
typ: ast.int_type_idx
|
||||
},
|
||||
]) or { panic(err) }
|
||||
|
||||
@ -75,11 +84,11 @@ fn test_mysql_orm() {
|
||||
table: 'Test'
|
||||
has_where: true
|
||||
fields: ['id', 'name', 'age']
|
||||
types: [7, 20, 8]
|
||||
types: [ast.int_type_idx, ast.string_type_idx, ast.i64_type_idx]
|
||||
}, orm.QueryData{}, orm.QueryData{
|
||||
fields: ['name', 'age']
|
||||
data: [orm.Primitive('Louis'), i64(101)]
|
||||
types: [20, 8]
|
||||
types: [ast.string_type_idx, ast.i64_type_idx]
|
||||
is_and: [true, true]
|
||||
kinds: [.eq, .eq]
|
||||
}) or { panic(err) }
|
||||
@ -88,8 +97,6 @@ fn test_mysql_orm() {
|
||||
name := res[0][1]
|
||||
age := res[0][2]
|
||||
|
||||
mdb.close()
|
||||
|
||||
assert id is int
|
||||
if id is int {
|
||||
assert id == 1
|
||||
@ -104,22 +111,10 @@ fn test_mysql_orm() {
|
||||
if age is i64 {
|
||||
assert age == 101
|
||||
}
|
||||
}
|
||||
|
||||
fn test_orm() {
|
||||
mut db := mysql.Connection{
|
||||
host: 'localhost'
|
||||
port: 3306
|
||||
username: 'root'
|
||||
password: ''
|
||||
dbname: 'mysql'
|
||||
}
|
||||
|
||||
db.connect() or {
|
||||
println(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
/** test orm sql type
|
||||
* - verify if all type create by attribute sql_type has created
|
||||
*/
|
||||
sql db {
|
||||
create table TestCustomSqlType
|
||||
}
|
||||
@ -168,27 +163,19 @@ fn test_orm() {
|
||||
sql db {
|
||||
drop table TestCustomSqlType
|
||||
}
|
||||
db.close()
|
||||
|
||||
assert result_custom_sql.maps() == information_schema_custom_sql
|
||||
}
|
||||
|
||||
fn test_orm_time_type() ? {
|
||||
mut db := mysql.Connection{
|
||||
host: 'localhost'
|
||||
port: 3306
|
||||
username: 'root'
|
||||
password: ''
|
||||
dbname: 'mysql'
|
||||
}
|
||||
|
||||
db.connect() or {
|
||||
/** 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)
|
||||
}
|
||||
|
||||
today := time.parse('2022-07-16 15:13:27')?
|
||||
|
||||
model := TestTimeType{
|
||||
username: 'hitalo'
|
||||
created_at: today
|
||||
@ -212,10 +199,38 @@ fn test_orm_time_type() ? {
|
||||
drop table TestTimeType
|
||||
}
|
||||
|
||||
db.close()
|
||||
|
||||
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.query("
|
||||
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{}
|
||||
|
||||
sql db {
|
||||
drop table TestDefaultAtribute
|
||||
}
|
||||
|
||||
information_schema_column_default_sql := [{
|
||||
'COLUMN_DEFAULT': ''
|
||||
}, {
|
||||
'COLUMN_DEFAULT': ''
|
||||
}, {
|
||||
'COLUMN_DEFAULT': 'CURRENT_TIMESTAMP'
|
||||
}]
|
||||
assert information_schema_column_default_sql == result_defaults.maps()
|
||||
}
|
||||
|
Reference in New Issue
Block a user