mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
orm: add tests for fkey:
relationship attribute and retrival of joined rows (#16877)
This commit is contained in:
parent
8f217c00e5
commit
3598e7d08e
@ -142,6 +142,7 @@ const (
|
||||
skip_with_fsanitize_address = [
|
||||
'vlib/net/websocket/websocket_test.v',
|
||||
'vlib/v/tests/websocket_logger_interface_should_compile_test.v',
|
||||
'vlib/v/tests/orm_sub_array_struct_test.v',
|
||||
]
|
||||
skip_with_fsanitize_undefined = [
|
||||
'do_not_remove',
|
||||
|
@ -7,6 +7,7 @@ struct Parent {
|
||||
}
|
||||
|
||||
struct Child {
|
||||
mut:
|
||||
id int [primary; sql: serial]
|
||||
parent_id int
|
||||
name string
|
||||
@ -47,3 +48,68 @@ fn test_orm_array() {
|
||||
assert parent.children[0].name == 'abc'
|
||||
assert parent.children[1].name == 'def'
|
||||
}
|
||||
|
||||
fn test_orm_relationship() {
|
||||
mut db := sqlite.connect(':memory:') or { panic(err) }
|
||||
sql db {
|
||||
create table Parent
|
||||
}
|
||||
|
||||
mut child := Child{
|
||||
name: 'abc'
|
||||
}
|
||||
|
||||
par := Parent{
|
||||
name: 'test'
|
||||
children: []
|
||||
}
|
||||
|
||||
sql db {
|
||||
insert par into Parent
|
||||
}
|
||||
|
||||
mut parent := sql db {
|
||||
select from Parent where id == 1
|
||||
}
|
||||
|
||||
child.parent_id = parent.id
|
||||
child.name = 'atum'
|
||||
|
||||
sql db {
|
||||
insert child into Child
|
||||
}
|
||||
|
||||
child.name = 'bacon'
|
||||
|
||||
sql db {
|
||||
insert child into Child
|
||||
}
|
||||
|
||||
assert parent.name == par.name
|
||||
assert parent.children.len == 0
|
||||
|
||||
parent = sql db {
|
||||
select from Parent where id == 1
|
||||
}
|
||||
|
||||
assert parent.name == par.name
|
||||
assert parent.children.len == 2
|
||||
assert parent.children[0].name == 'atum'
|
||||
assert parent.children[1].name == 'bacon'
|
||||
|
||||
mut children := sql db {
|
||||
select from Child
|
||||
}
|
||||
|
||||
assert children.len == 2
|
||||
|
||||
sql db {
|
||||
drop table Parent
|
||||
}
|
||||
|
||||
children = sql db {
|
||||
select from Child
|
||||
}
|
||||
|
||||
assert children.len == 0
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user