1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

test-cleancode: add vlib/orm (#7577)

This commit is contained in:
Lukas Neubert 2020-12-26 13:40:22 +01:00 committed by GitHub
parent 693e5137eb
commit 6969300f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 27 deletions

View File

@ -24,6 +24,7 @@ const (
'vlib/builtin/array_test.v', 'vlib/builtin/array_test.v',
'vlib/builtin/map.v', 'vlib/builtin/map.v',
'vlib/math/bits/bits.v', 'vlib/math/bits/bits.v',
'vlib/orm/',
'vlib/term/colors.v', 'vlib/term/colors.v',
'vlib/term/term.v', 'vlib/term/term.v',
'vlib/v/ast/', 'vlib/v/ast/',

View File

@ -1,19 +1,19 @@
//import os // import os
//import pg // import pg
//import term // import term
import sqlite import sqlite
struct Module { struct Module {
id int id int
name string name string
nr_downloads int nr_downloads int
} }
struct User { struct User {
id int id int
age int age int
name string name string
is_customer bool is_customer bool
skipped_string string [skip] skipped_string string [skip]
} }
@ -23,11 +23,9 @@ struct Foo {
fn test_orm_sqlite() { fn test_orm_sqlite() {
db := sqlite.connect(':memory:') or { panic(err) } db := sqlite.connect(':memory:') or { panic(err) }
db.exec("drop table if exists User") db.exec('drop table if exists User')
db.exec("create table User (id integer primary key, age int default 0, name text default '', is_customer int default 0);") db.exec("create table User (id integer primary key, age int default 0, name text default '', is_customer int default 0);")
name := 'Peter' name := 'Peter'
db.exec("insert into User (name, age) values ('Sam', 29)") db.exec("insert into User (name, age) values ('Sam', 29)")
db.exec("insert into User (name, age) values ('Peter', 31)") db.exec("insert into User (name, age) values ('Peter', 31)")
db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)") db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)")
@ -99,12 +97,14 @@ fn test_orm_sqlite() {
assert users3[0].age == 29 assert users3[0].age == 29
assert users3[1].age == 31 assert users3[1].age == 31
// //
new_user := User{
new_user := User{name:'New user' age:30} name: 'New user'
age: 30
}
sql db { sql db {
insert new_user into User insert new_user into User
} }
//db.insert<User>(user2) // db.insert<User>(user2)
x := sql db { x := sql db {
select from User where id == 4 select from User where id == 4
} }
@ -197,23 +197,18 @@ fn test_orm_sqlite() {
} }
assert z.len == 2 assert z.len == 2
assert z[0].id == 3 assert z[0].id == 3
oldest := sql db { oldest := sql db {
select from User order by age desc limit 1 select from User order by age desc limit 1
} }
assert oldest.age == 34 assert oldest.age == 34
offs := 1 offs := 1
second_oldest := sql db { second_oldest := sql db {
select from User order by age desc limit 1 offset offs select from User order by age desc limit 1 offset offs
} }
assert second_oldest.age == 31 assert second_oldest.age == 31
sql db { sql db {
delete from User where age == 34 delete from User where age == 34
} }
updated_oldest := sql db { updated_oldest := sql db {
select from User order by age desc limit 1 select from User order by age desc limit 1
} }
@ -221,7 +216,7 @@ fn test_orm_sqlite() {
} }
fn test_orm_pg() { fn test_orm_pg() {
/* /*
dbname := os.getenv('VDB_NAME') dbname := os.getenv('VDB_NAME')
dbuser := os.getenv('VDB_USER') dbuser := os.getenv('VDB_USER')
if dbname == '' || dbuser == '' { if dbname == '' || dbuser == '' {
@ -243,9 +238,8 @@ fn test_orm_pg() {
for mod in mods { for mod in mods {
println(mod) println(mod)
} }
*/ */
/*
/*
mod := db.retrieve<Module>(1) mod := db.retrieve<Module>(1)
mod := db.select from Module where id = 1 mod := db.select from Module where id = 1
@ -259,8 +253,8 @@ fn test_orm_pg() {
nr_modules := db.select count from modules nr_modules := db.select count from modules
nr_modules := db.select from modules nr_modules := db.select from modules
nr_modules := db[:modules].select nr_modules := db[:modules].select
*/ */
/* /*
mod := select from db.modules where id = 1 limit 1 mod := select from db.modules where id = 1 limit 1
println(mod.name) println(mod.name)
top_mods := select from db.modules where nr_downloads > 1000 order by nr_downloads desc limit 10 top_mods := select from db.modules where nr_downloads > 1000 order by nr_downloads desc limit 10
@ -271,5 +265,5 @@ fn test_orm_pg() {
n := db.q_int('select count(*) from modules') n := db.q_int('select count(*) from modules')
println(n) println(n)
*/ */
} }