2023-01-17 21:21:10 +03:00
|
|
|
import db.sqlite
|
2023-05-09 00:21:42 +03:00
|
|
|
import rand
|
2023-01-17 21:21:10 +03:00
|
|
|
|
|
|
|
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-05-02 11:14:42 +03:00
|
|
|
struct Package {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
name string [unique]
|
|
|
|
author User [fkey: 'id']
|
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
pub mut:
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
username string [unique]
|
|
|
|
}
|
|
|
|
|
2023-05-09 00:21:42 +03:00
|
|
|
struct Entity {
|
|
|
|
uuid string [primary]
|
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EntityWithFloatPrimary {
|
|
|
|
id f64 [primary]
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2023-04-04 08:23:06 +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-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-02-11 11:02:55 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 00:21:42 +03:00
|
|
|
fn test_set_primary_value() {
|
|
|
|
// The primary key is an constraint that ensures each record in a table is unique.
|
|
|
|
// Primary keys must contain unique values and cannot contain `NULL` values.
|
|
|
|
// However, this statement does not imply that a value cannot be inserted by the user.
|
|
|
|
// Therefore, let's allow this.
|
|
|
|
db := sqlite.connect(':memory:')!
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table Child
|
|
|
|
}!
|
|
|
|
|
|
|
|
child := Child{
|
|
|
|
id: 10
|
|
|
|
parent_id: 20
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert child into Child
|
|
|
|
}!
|
|
|
|
|
|
|
|
children := sql db {
|
|
|
|
select from Child
|
|
|
|
}!
|
|
|
|
|
|
|
|
assert children.first() == child
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_uuid_primary_key() {
|
|
|
|
db := sqlite.connect(':memory:')!
|
|
|
|
uuid := rand.uuid_v4()
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table Entity
|
|
|
|
}!
|
|
|
|
|
|
|
|
entity := Entity{
|
|
|
|
uuid: uuid
|
|
|
|
description: 'Test'
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert entity into Entity
|
|
|
|
}!
|
|
|
|
|
|
|
|
entities := sql db {
|
|
|
|
select from Entity where uuid == uuid
|
|
|
|
}!
|
|
|
|
|
|
|
|
mut is_duplicate_inserted := true
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert entity into Entity
|
|
|
|
} or { is_duplicate_inserted = false }
|
|
|
|
|
|
|
|
assert entities.len == 1
|
|
|
|
assert entities.first() == entity
|
|
|
|
assert is_duplicate_inserted == false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_float_primary_key() {
|
|
|
|
db := sqlite.connect(':memory:')!
|
|
|
|
id := 3.14
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table EntityWithFloatPrimary
|
|
|
|
}!
|
|
|
|
|
|
|
|
entity := EntityWithFloatPrimary{
|
|
|
|
id: id
|
|
|
|
name: 'Test'
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert entity into EntityWithFloatPrimary
|
|
|
|
}!
|
|
|
|
|
|
|
|
entities := sql db {
|
|
|
|
select from EntityWithFloatPrimary where id == id
|
|
|
|
}!
|
|
|
|
|
|
|
|
assert entities.len == 1
|
|
|
|
assert entities.first() == entity
|
|
|
|
}
|
|
|
|
|
2023-05-02 11:14:42 +03:00
|
|
|
fn test_does_not_insert_uninitialized_field() {
|
|
|
|
db := sqlite.connect(':memory:')!
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table User
|
|
|
|
create table Package
|
|
|
|
}!
|
|
|
|
|
|
|
|
package := Package{
|
|
|
|
name: 'xml'
|
|
|
|
// author
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert package into Package
|
|
|
|
}!
|
|
|
|
|
|
|
|
users := sql db {
|
|
|
|
select from User
|
|
|
|
}!
|
|
|
|
|
|
|
|
// users must be empty because the package doesn't have an initialized `User` structure.
|
|
|
|
assert users.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_insert_empty_field() {
|
|
|
|
db := sqlite.connect(':memory:')!
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table User
|
|
|
|
create table Package
|
|
|
|
}!
|
|
|
|
|
|
|
|
package := Package{
|
|
|
|
name: 'xml'
|
|
|
|
author: User{}
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert package into Package
|
|
|
|
}!
|
|
|
|
|
|
|
|
users := sql db {
|
|
|
|
select from User
|
|
|
|
}!
|
|
|
|
|
|
|
|
assert users.len == 1
|
|
|
|
}
|
|
|
|
|
2023-02-16 12:34:16 +03:00
|
|
|
fn test_insert_empty_object() {
|
2023-05-02 11:14:42 +03:00
|
|
|
db := sqlite.connect(':memory:')!
|
2023-02-16 12:34:16 +03:00
|
|
|
|
|
|
|
account := Account{}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table Account
|
|
|
|
insert account into Account
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-02-16 12:34:16 +03:00
|
|
|
|
|
|
|
accounts := sql db {
|
|
|
|
select from Account
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-02-16 12:34:16 +03:00
|
|
|
|
|
|
|
assert accounts.len == 1
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:02:55 +03:00
|
|
|
fn test_orm_insert_mut_object() {
|
2023-05-02 11:14:42 +03:00
|
|
|
db := sqlite.connect(':memory:')!
|
2023-02-11 11:02:55 +03:00
|
|
|
|
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
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-02-11 11:02:55 +03:00
|
|
|
|
|
|
|
mut parent := Parent{
|
|
|
|
name: 'test'
|
2023-01-17 21:21:10 +03:00
|
|
|
}
|
2023-02-11 11:02:55 +03:00
|
|
|
|
2023-04-04 08:23:06 +03:00
|
|
|
insert_parent(db, mut parent)!
|
2023-02-11 11:02:55 +03:00
|
|
|
|
|
|
|
parents := sql db {
|
|
|
|
select from Parent
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-02-11 11:02:55 +03:00
|
|
|
|
|
|
|
assert parents.len == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_orm_insert_with_multiple_child_elements() {
|
2023-05-02 11:14:42 +03:00
|
|
|
mut db := sqlite.connect(':memory:')!
|
2023-02-11 11:02:55 +03:00
|
|
|
|
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
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-17 21:21:10 +03:00
|
|
|
|
|
|
|
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-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-17 21:21:10 +03:00
|
|
|
|
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-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-17 21:21:10 +03:00
|
|
|
|
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
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-17 21:21:10 +03:00
|
|
|
assert children_count == new_parent.children.len
|
|
|
|
|
|
|
|
note_count := sql db {
|
|
|
|
select count from Note
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-17 21:21:10 +03:00
|
|
|
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'
|
|
|
|
}
|
2023-07-05 23:25:22 +03:00
|
|
|
|
|
|
|
[table: 'customers']
|
|
|
|
struct Customer {
|
|
|
|
id i64 [primary; sql: serial]
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_i64_primary_field_works_with_insertions_of_id_0() {
|
|
|
|
db := sqlite.connect(':memory:')!
|
|
|
|
sql db {
|
|
|
|
create table Customer
|
|
|
|
}!
|
|
|
|
for i in ['Bob', 'Charlie'] {
|
|
|
|
new_customer := Customer{
|
|
|
|
name: i
|
|
|
|
}
|
|
|
|
sql db {
|
|
|
|
insert new_customer into Customer
|
|
|
|
}!
|
|
|
|
}
|
|
|
|
users := sql db {
|
|
|
|
select from Customer
|
|
|
|
}!
|
|
|
|
assert users.len == 2
|
|
|
|
// println("${users}")
|
|
|
|
}
|