From a107b4fd4e53473fdc14b75db36f1247e661bbad Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 16 Dec 2022 12:38:41 -0800 Subject: [PATCH] add some specs for empty model find --- lapis/db/base_model.lua | 2 +- lapis/db/base_model.moon | 3 ++- spec/model_spec.moon | 27 +++++++++++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lapis/db/base_model.lua b/lapis/db/base_model.lua index 4f6cfcfa..e7df9959 100644 --- a/lapis/db/base_model.lua +++ b/lapis/db/base_model.lua @@ -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 diff --git a/lapis/db/base_model.moon b/lapis/db/base_model.moon index 7cd2af61..9aa23412 100644 --- a/lapis/db/base_model.moon +++ b/lapis/db/base_model.moon @@ -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 (...) diff --git a/spec/model_spec.moon b/spec/model_spec.moon index 5effd881..b6622974 100644 --- a/spec/model_spec.moon +++ b/spec/model_spec.moon @@ -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 @@ -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 { @@ -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", -> @@ -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]]