Skip to content

Commit

Permalink
fix(model): Adds missing db.close on model:fild_all
Browse files Browse the repository at this point in the history
Method call was missing. Discovered due to new tests added. Opening and closing of db will be tested automatically from now on.
  • Loading branch information
Etiene committed Oct 11, 2015
1 parent 6322a36 commit e07960f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/sailor/model.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--------------------------------------------------------------------------------
-- model.lua, v0.7: basic model creator, uses db module
-- model.lua, v0.7.1: basic model creator, uses db module
-- This file is a part of Sailor project
-- Copyright (c) 2014 Etiene Dalcol <[email protected]>
-- License: MIT
Expand All @@ -19,6 +19,7 @@ function model:new(obj)
obj = util.deepcopy(obj)

setmetatable(obj,self)
-- REWRITE
self.__index = function (table, key)
if key ~= "attributes" and key ~= "@name" and key ~= "relations" and key ~= "loaded_relations" and key ~= "db" and not model[key] and key ~= "errors" then
if obj.relations and obj.relations[key] then
Expand Down Expand Up @@ -254,7 +255,7 @@ function model:find_all(where_string)
where_string = ''
end
local res = db.query("select * from "..self.db.table..where_string..";")

db.close()
if not res then return {} end

for k,_ in ipairs(res) do
Expand Down
33 changes: 31 additions & 2 deletions test/dev-app/tests/unit/model.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
local sailor = require "sailor"
local helper = require "tests.helper"
local access = require "sailor.access"
local db = require "sailor.db"


describe("Testing #UserModel", function()
--helper.blah()
local User = sailor.model('user')
local fixtures = require "tests.fixtures.user" or {}
local users = User:find_all()
local u, count_before
local db_spies = {}
local db_spies_count = 0

local function assert_db_close(n)
n = n or 1
db_spies_count = db_spies_count + n
assert.spy(db_spies[1]).was_called(db_spies_count)
assert.spy(db_spies[2]).was_called(db_spies_count)
end

setup(function()

table.insert(db_spies, spy.on(db,'connect',function() print "hey" end))
table.insert(db_spies, spy.on(db,'close'))
end)

it("should cound objects", function()
it("should count objects", function()
assert.is_equal(#users,User:count())
assert_db_close()
end)

it("should create different objects", function()
Expand All @@ -33,23 +46,31 @@ describe("Testing #UserModel", function()
assert.is_equal(User:count(),count_before+1)
assert.spy(s).was_not_called()
assert.spy(s2).was_called()
assert_db_close(3)
end)

it("should update object", function()
local s = spy.on(User,'update')
u.username = users[2].username or nil
assert.is_true(u:save(false))

-- On update it is opening and closing db connection twice
-- first for verifying if the entry exists
-- maybe this should be revised
assert_db_close(2)
assert.spy(s).was_called()
end)

it("should delete object", function()
u:delete()
assert.is_equal(User:count(),count_before)
assert_db_close(2)
end)

it("should find object by id", function()
u = User:find_by_id(1)
assert.are_same(u.id,users[1].id)
assert_db_close()
end)

it("should find user relations", function()
Expand All @@ -63,21 +84,25 @@ describe("Testing #UserModel", function()
end
end
assert.is_equal(amount,#(u.posts))
assert_db_close(2)
end)

it("should not find object by id", function()
local u = User:find_by_id(42)
assert.is_false(u)
assert_db_close()
end)

it("should find object by attributes", function()
local u = User:find_by_attributes({username = users[1].username})
assert.are_same(u.id,users[1].id)
assert_db_close()
end)

it("should not find object by attributes", function()
local u = User:find_by_attributes({username = ''})
assert.is_false(u)
assert_db_close()
end)

it("should validate object", function()
Expand All @@ -90,19 +115,23 @@ describe("Testing #UserModel", function()

it("should find all objects", function()
assert.is_equal(User:count(),#(User:find_all()))
assert_db_close(2)
end)

it("should find some objects", function()
assert.is_equal(2,#(User:find_all("password LIKE '12345%'")))
assert_db_close()
end)

it("should find one object", function()
u = User:find("password LIKE '12345%'")
assert.is_equal(users[1].id,u.id)
assert_db_close()
end)

it("should login", function()
assert.is_true(User.authenticate(fixtures[1].username,fixtures[1].password,false))
assert_db_close()
end)

it("should know the user is logged in", function()
Expand Down

0 comments on commit e07960f

Please sign in to comment.