1
0
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:
Hitalo Souza 2023-01-05 10:20:15 -03:00 committed by GitHub
parent 8f217c00e5
commit 3598e7d08e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 0 deletions

View File

@ -142,6 +142,7 @@ const (
skip_with_fsanitize_address = [ skip_with_fsanitize_address = [
'vlib/net/websocket/websocket_test.v', 'vlib/net/websocket/websocket_test.v',
'vlib/v/tests/websocket_logger_interface_should_compile_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 = [ skip_with_fsanitize_undefined = [
'do_not_remove', 'do_not_remove',

View File

@ -7,6 +7,7 @@ struct Parent {
} }
struct Child { struct Child {
mut:
id int [primary; sql: serial] id int [primary; sql: serial]
parent_id int parent_id int
name string name string
@ -47,3 +48,68 @@ fn test_orm_array() {
assert parent.children[0].name == 'abc' assert parent.children[0].name == 'abc'
assert parent.children[1].name == 'def' 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
}