v2 / vlib / orm / orm_nested_struct_test.v
132 lines · 120 sloc · 2.52 KB · 99be39cbd15d4bbb5ab14d2f870199908c00bc8d
Raw
1// vtest retry: 3
2import db.sqlite
3
4struct Address {
5 id int @[primary; sql: serial]
6 parent_id int
7 create_at string
8 update_at string
9 street string
10 city string
11 state string
12 zip_code string
13 proximity string
14}
15
16@[table: 'RealStates']
17struct RealState {
18 id string @[primary; sql_type: 'uuid']
19 parent_id int
20 name string @[sql_type: 'varchar(80)']
21 cnpj string @[sql_type: 'varchar(14)']
22 address []Address @[fkey: 'parent_id']
23}
24
25@[table: 'Realtors']
26struct Realtor {
27 id ?string @[primary; sql_type: 'uuid']
28 first_name string @[sql_type: 'VARCHAR(30)']
29 last_name string @[sql_type: 'VARCHAR(30)']
30 creci string @[sql_type: 'VARCHAR(8)']
31 cnpj ?string @[sql_type: 'VARCHAR(15)']
32 cpf ?string @[sql_type: 'VARCHAR(12)']
33 phone string @[sql_type: 'VARCHAR(15)']
34 real_state RealState @[fkey: 'id']
35}
36
37fn test_orm_nested_struct() {
38 mut db := sqlite.connect(':memory:')!
39
40 data := Realtor{
41 first_name: 'John'
42 last_name: 'Doe'
43 creci: '12345678'
44 cnpj: '12345678901234'
45 cpf: '12345678901'
46 phone: '1234567890'
47 real_state: RealState{
48 name: 'Teste'
49 cnpj: '12345678901234'
50 address: [
51 Address{
52 street: 'Teste'
53 city: 'Teste'
54 state: 'Teste'
55 zip_code: 'Teste'
56 proximity: 'Teste'
57 },
58 ]
59 }
60 }
61
62 sql db {
63 create table Address
64 create table RealState
65 create table Realtor
66 }!
67
68 sql db {
69 insert data into Realtor
70 }!
71
72 x1 := sql db {
73 select from Address
74 }!
75 assert x1.str() == "[Address{
76 id: 1
77 parent_id: 0
78 create_at: ''
79 update_at: ''
80 street: 'Teste'
81 city: 'Teste'
82 state: 'Teste'
83 zip_code: 'Teste'
84 proximity: 'Teste'
85}]"
86
87 x2 := sql db {
88 select from RealState
89 }!
90 assert x2.str() == "[RealState{
91 id: ''
92 parent_id: 0
93 name: 'Teste'
94 cnpj: '12345678901234'
95 address: [Address{
96 id: 1
97 parent_id: 0
98 create_at: ''
99 update_at: ''
100 street: 'Teste'
101 city: 'Teste'
102 state: 'Teste'
103 zip_code: 'Teste'
104 proximity: 'Teste'
105 }]
106}]"
107
108 x3 := sql db {
109 select from Realtor
110 }!
111
112 // FIXME!
113 // I think this result is not correct.
114 assert x3.str() == "[Realtor{
115 id: Option(none)
116 first_name: 'John'
117 last_name: 'Doe'
118 creci: '12345678'
119 cnpj: Option('12345678901234')
120 cpf: Option('12345678901')
121 phone: '1234567890'
122 real_state: RealState{
123 id: ''
124 parent_id: 0
125 name: ''
126 cnpj: ''
127 address: []
128 }
129}]"
130
131 db.close()!
132}
133