From 3598e7d08e54475f480aabf54f56aeef45156ea5 Mon Sep 17 00:00:00 2001 From: Hitalo Souza <63821277+enghitalo@users.noreply.github.com> Date: Thu, 5 Jan 2023 10:20:15 -0300 Subject: [PATCH] orm: add tests for `fkey:` relationship attribute and retrival of joined rows (#16877) --- cmd/tools/vtest-self.v | 1 + vlib/v/tests/orm_sub_array_struct_test.v | 66 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 924c762836..d0f86c7915 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -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', diff --git a/vlib/v/tests/orm_sub_array_struct_test.v b/vlib/v/tests/orm_sub_array_struct_test.v index cc79e755ca..6643dcf4ed 100644 --- a/vlib/v/tests/orm_sub_array_struct_test.v +++ b/vlib/v/tests/orm_sub_array_struct_test.v @@ -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 +}