Skip to content

Commit

Permalink
add some specs for empty model find
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Dec 16, 2022
1 parent c878536 commit a107b4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lapis/db/base_model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ do
self.find = function(self, ...)
local first = select(1, ...)
if first == nil then
error(tostring(self:table_name()) .. " trying to find with no conditions")
error("Model.find: " .. tostring(self:table_name()) .. ": trying to find with no conditions")
end
local cond
if "table" == type(first) then
Expand Down
3 changes: 2 additions & 1 deletion lapis/db/base_model.moon
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ class BaseModel
-- find by primary key, or by table of conds
@find: (...) =>
first = select 1, ...
error "#{@table_name!} trying to find with no conditions" if first == nil
if first == nil
error "Model.find: #{@table_name!}: trying to find with no conditions"

cond = if "table" == type first
@db.encode_clause (...)
Expand Down
27 changes: 23 additions & 4 deletions spec/model_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ describe "lapis.db.model", ->


describe "find", ->
it "handles empty clause", ->
class Things extends Model

assert.has_error(
-> Things\find {}
"db.encode_clause: passed an empty table"
)

assert.has_error(
-> Things\find nil
"Model.find: things: trying to find with no conditions"
)

assert.has_error(
-> Things\find!
"Model.find: things: trying to find with no conditions"
)


it "basic", ->
class Things extends Model

Expand Down Expand Up @@ -203,13 +222,13 @@ describe "lapis.db.model", ->
assert_queries {
[[SELECT * FROM "things" WHERE "dad" IN (1, 2, 4)]]
}

it "with fields option", ->
Things\find_all { 1,2,4 }, fields: "hello"
assert_queries {
[[SELECT hello FROM "things" WHERE "id" IN (1, 2, 4)]]
}

it "with multiple field and key option", ->
Things\find_all { 1,2,4 }, fields: "hello, world", key: "dad"
assert_queries {
Expand Down Expand Up @@ -360,7 +379,7 @@ describe "lapis.db.model", ->
[[INSERT INTO "things" ("color") VALUES ('blue') RETURNING "id"]]
[[INSERT INTO "timed_things" ("created_at", "hello", "updated_at") VALUES ('2013-08-13 06:56:40', 'world', '2013-08-13 06:56:40') RETURNING "id"]]
[[INSERT INTO "other_things" ("height", "id_a") VALUES ('400px', 120) RETURNING "id_a", "id_b"]]

}

it "should create model with options", ->
Expand Down Expand Up @@ -488,7 +507,7 @@ describe "lapis.db.model", ->

assert_queries {
[[UPDATE "things" SET "color" = 'green', "height" = 100 WHERE "id" = 12]]

[[UPDATE "things" SET "age" = 2000 WHERE "id" IS NULL]]
[[UPDATE "timed_things" SET "great" = TRUE, "updated_at" = '2013-08-13 06:56:40' WHERE "a" = 2 AND "b" = 3]]
[[UPDATE "timed_things" SET "hello" = 'world' WHERE "a" = 2 AND "b" = 3]]
Expand Down

0 comments on commit a107b4f

Please sign in to comment.