Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
import push, pop from require "lapis.environment" | ||
import set_backend, init_logger from require "lapis.nginx.postgres" | ||
|
||
setup_db = (opts) -> | ||
push "test", { | ||
postgres: { | ||
backend: "pgmoon" | ||
database: "lapis_test" | ||
} | ||
} | ||
|
||
set_backend "pgmoon" | ||
init_logger! | ||
|
||
teardown_db = -> | ||
pop! | ||
|
||
{:setup_db, :teardown_db} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
|
||
import setup_db, teardown_db from require "spec_postgres.helpers" | ||
|
||
import drop_tables from require "lapis.spec.db" | ||
|
||
import Model, enum from require "lapis.db.model" | ||
import types, create_table from require "lapis.db.schema" | ||
|
||
describe "model", -> | ||
setup -> | ||
setup_db! | ||
|
||
teardown -> | ||
teardown_db! | ||
|
||
describe "basic model", -> | ||
class Things extends Model | ||
|
||
before_each -> | ||
drop_tables Things | ||
create_table Things\table_name!, { | ||
{"id", types.serial} | ||
{"name", types.text} | ||
"PRIMARY KEY (id)" | ||
} | ||
|
||
it "should find on empty table", -> | ||
nothing = Things\find 1 | ||
assert.falsy nothing | ||
|
||
it "should insert new rows with autogenerated id", -> | ||
first = Things\create { name: "first" } | ||
second = Things\create { name: "second" } | ||
|
||
assert.same 1, first.id | ||
assert.same "first", first.name | ||
|
||
assert.same 2, second.id | ||
assert.same "second", second.name | ||
|
||
describe "with some rows", -> | ||
local first, second | ||
|
||
before_each -> | ||
first = Things\create { name: "first" } | ||
second = Things\create { name: "second" } | ||
|
||
it "should should find existing row", -> | ||
assert.same first, Things\find first.id | ||
assert.same second, Things\find second.id | ||
assert.same second, Things\find name: "second" | ||
assert.falsy Things\find name: "second", id: 1 | ||
assert.same first, Things\find id: "1" | ||
|
||
it "it should select rows", -> | ||
things = Things\select! | ||
assert.same 2, #things | ||
|
||
things = Things\select "order by name desc" | ||
assert "second", things[1].name | ||
assert "first", things[2].name | ||
|
||
it "it should only select specified fields", -> | ||
things = Things\select "order by id asc", fields: "id" | ||
assert.same {{id: 1}, {id: 2}}, things | ||
|
||
it "it should find all", -> | ||
things = Things\find_all {1,3} | ||
assert.same {first}, things | ||
|
||
things = Things\find_all {1,2}, where: { | ||
name: "second" | ||
} | ||
|
||
assert.same {second}, things | ||
|
||
describe "timestamp model", -> | ||
class Posts extends Model | ||
@timestamp: true | ||
|
||
before_each -> | ||
drop_tables Posts | ||
create_table Posts\table_name!, { | ||
{"id", types.serial} | ||
{"title", types.text null: false} | ||
{"body", types.text null: false} | ||
{"created_at", types.time} | ||
{"updated_at", types.time} | ||
"PRIMARY KEY (id)" | ||
} | ||
|
||
it "should fail to create without required types", -> | ||
assert.has_error -> | ||
Posts\create {} | ||
|
||
it "should create model", -> | ||
post = Posts\create { | ||
title: "Hello world" | ||
body: "Greetings" | ||
} | ||
|
||
assert.truthy post.created_at | ||
assert.truthy post.updated_at | ||
assert.same post.created_at, post.updated_at | ||
|
||
describe "with row", -> | ||
local post, other_post | ||
|
||
before_each -> | ||
post = Posts\create { | ||
title: "Hello world" | ||
body: "Greetings" | ||
} | ||
|
||
other_post = Posts\create { | ||
title: "Meetings" | ||
body: "Say all" | ||
} | ||
|
||
it "should update post", -> | ||
res = post\update { | ||
title: "Another world" | ||
} | ||
|
||
-- this is undocumented | ||
assert.same {affected_rows: 1}, res | ||
assert.same "Another world", post.title | ||
|
||
it "should delete post", -> | ||
assert.truthy (post\delete!) | ||
assert.falsy (post\delete!) | ||
assert.same {other_post}, Posts\select! | ||
|