mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871)
This commit is contained in:
82
vlib/orm/orm_result_test.v
Normal file
82
vlib/orm/orm_result_test.v
Normal file
@@ -0,0 +1,82 @@
|
||||
import db.sqlite
|
||||
|
||||
struct Account {
|
||||
id int [primary; sql: serial]
|
||||
name string
|
||||
}
|
||||
|
||||
struct Note {
|
||||
id int [primary; sql: serial]
|
||||
content string
|
||||
}
|
||||
|
||||
fn test_catch_table_is_not_created() {
|
||||
mut db := sqlite.connect(':memory:')!
|
||||
|
||||
mut is_inserted := true
|
||||
|
||||
account := Account{}
|
||||
|
||||
sql db {
|
||||
insert account into Account
|
||||
} or { is_inserted = false }
|
||||
|
||||
assert !is_inserted
|
||||
}
|
||||
|
||||
fn test_catch_one_of_queries() {
|
||||
mut db := sqlite.connect(':memory:')!
|
||||
|
||||
sql db {
|
||||
create table Account
|
||||
}!
|
||||
|
||||
account := Account{}
|
||||
|
||||
sql db {
|
||||
insert account into Account
|
||||
}!
|
||||
|
||||
mut are_updated := true
|
||||
|
||||
sql db {
|
||||
update Account set name = 'test' where id == 1
|
||||
update Note set content = 'test' where id == 1
|
||||
} or { are_updated = false }
|
||||
|
||||
assert !are_updated
|
||||
}
|
||||
|
||||
fn test_print_results() {
|
||||
mut db := sqlite.connect(':memory:')!
|
||||
|
||||
sql db {
|
||||
create table Account
|
||||
}!
|
||||
|
||||
account := Account{}
|
||||
|
||||
sql db {
|
||||
insert account into Account
|
||||
}!
|
||||
|
||||
count := sql db {
|
||||
select count from Account
|
||||
}!
|
||||
|
||||
println(count)
|
||||
|
||||
user := sql db {
|
||||
select from Account
|
||||
}!.first()
|
||||
|
||||
println(user)
|
||||
|
||||
users := sql db {
|
||||
select from Account
|
||||
}!
|
||||
|
||||
println(users)
|
||||
|
||||
assert true
|
||||
}
|
||||
Reference in New Issue
Block a user