2023-01-17 21:21:10 +03:00
|
|
|
import db.sqlite
|
|
|
|
|
|
|
|
struct Parent {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
name string
|
|
|
|
children []Child [fkey: 'parent_id']
|
|
|
|
notes []Note [fkey: 'owner_id']
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Child {
|
|
|
|
mut:
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
parent_id int
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Note {
|
|
|
|
mut:
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
owner_id int
|
|
|
|
text string
|
|
|
|
}
|
|
|
|
|
2023-02-16 12:34:16 +03:00
|
|
|
struct Account {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:02:55 +03:00
|
|
|
pub fn insert_parent(db sqlite.DB, mut parent Parent) {
|
2023-01-17 21:21:10 +03:00
|
|
|
sql db {
|
2023-02-11 11:02:55 +03:00
|
|
|
insert parent into Parent
|
2023-01-17 21:21:10 +03:00
|
|
|
}
|
2023-02-11 11:02:55 +03:00
|
|
|
}
|
|
|
|
|
2023-02-16 12:34:16 +03:00
|
|
|
fn test_insert_empty_object() {
|
|
|
|
db := sqlite.connect(':memory:') or { panic(err) }
|
|
|
|
|
|
|
|
account := Account{}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table Account
|
|
|
|
insert account into Account
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts := sql db {
|
|
|
|
select from Account
|
|
|
|
}
|
|
|
|
|
|
|
|
assert accounts.len == 1
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:02:55 +03:00
|
|
|
fn test_orm_insert_mut_object() {
|
|
|
|
db := sqlite.connect(':memory:') or { panic(err) }
|
|
|
|
|
2023-01-17 21:21:10 +03:00
|
|
|
sql db {
|
2023-02-11 11:02:55 +03:00
|
|
|
create table Parent
|
2023-01-17 21:21:10 +03:00
|
|
|
create table Child
|
2023-02-11 11:02:55 +03:00
|
|
|
create table Note
|
|
|
|
}
|
|
|
|
|
|
|
|
mut parent := Parent{
|
|
|
|
name: 'test'
|
2023-01-17 21:21:10 +03:00
|
|
|
}
|
2023-02-11 11:02:55 +03:00
|
|
|
|
|
|
|
insert_parent(db, mut parent)
|
|
|
|
|
|
|
|
parents := sql db {
|
|
|
|
select from Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
assert parents.len == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_orm_insert_with_multiple_child_elements() {
|
|
|
|
mut db := sqlite.connect(':memory:') or { panic(err) }
|
|
|
|
|
2023-01-17 21:21:10 +03:00
|
|
|
sql db {
|
2023-02-11 11:02:55 +03:00
|
|
|
create table Parent
|
|
|
|
create table Child
|
2023-01-17 21:21:10 +03:00
|
|
|
create table Note
|
|
|
|
}
|
|
|
|
|
|
|
|
new_parent := Parent{
|
|
|
|
name: 'test'
|
|
|
|
children: [
|
|
|
|
Child{
|
|
|
|
name: 'Lisa'
|
|
|
|
},
|
|
|
|
Child{
|
|
|
|
name: 'Steve'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
notes: [
|
|
|
|
Note{
|
|
|
|
text: 'First note'
|
|
|
|
},
|
|
|
|
Note{
|
|
|
|
text: 'Second note'
|
|
|
|
},
|
|
|
|
Note{
|
|
|
|
text: 'Third note'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert new_parent into Parent
|
|
|
|
}
|
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
parents := sql db {
|
2023-01-17 21:21:10 +03:00
|
|
|
select from Parent where id == 1
|
|
|
|
}
|
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
parent := parents.first()
|
2023-01-17 21:21:10 +03:00
|
|
|
assert parent.children.len == new_parent.children.len
|
|
|
|
assert parent.notes.len == new_parent.notes.len
|
|
|
|
|
|
|
|
children_count := sql db {
|
|
|
|
select count from Child
|
|
|
|
}
|
|
|
|
assert children_count == new_parent.children.len
|
|
|
|
|
|
|
|
note_count := sql db {
|
|
|
|
select count from Note
|
|
|
|
}
|
|
|
|
assert note_count == new_parent.notes.len
|
|
|
|
|
|
|
|
assert parent.children[0].name == 'Lisa'
|
|
|
|
assert parent.children[1].name == 'Steve'
|
|
|
|
|
|
|
|
assert parent.notes[0].text == 'First note'
|
|
|
|
assert parent.notes[1].text == 'Second note'
|
|
|
|
assert parent.notes[2].text == 'Third note'
|
|
|
|
}
|