From 76cb8a8a8d8abd509533673ba30395e857a6cf2f Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Sun, 1 Feb 2015 11:25:44 +0300
Subject: [PATCH] schema: fix exists() functions to accept nil as input

Fix box.schema.user.exists(nil) box.schema.role.exists(nil)
and box.schema.func.exists(nil) to return false (PeterG report).
Add tests.
---
 src/box/lua/schema.lua   |  8 ++++----
 test/box/access.result   | 16 ++++++++++++++++
 test/box/access.test.lua |  4 ++++
 3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index 17a980992b..f0b610f43d 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -79,7 +79,7 @@ local function role_resolve(name_or_id)
     local tuple
     if type(name_or_id) == 'string' then
         tuple = _user.index.name:get{name_or_id}
-    else
+    elseif type(name_or_id) ~= 'nil' then
         tuple = _user:get{name_or_id}
     end
     if tuple == nil or tuple[4] ~= 'role' then
@@ -94,7 +94,7 @@ local function user_resolve(name_or_id)
     local tuple
     if type(name_or_id) == 'string' then
         tuple = _user.index.name:get{name_or_id}
-    else
+    elseif type(name_or_id) ~= 'nil' then
         tuple = _user:get{name_or_id}
     end
     if tuple == nil or tuple[4] ~= 'user' then
@@ -1003,10 +1003,10 @@ end
 
 function box.schema.func.exists(name_or_id)
     local _func = box.space[box.schema.FUNC_ID]
-    local tuple 
+    local tuple = nil
     if type(name_or_id) == 'string' then
         tuple = _func.index.name:get{name_or_id}
-    else
+    elseif type(name_or_id) == 'number' then
         tuple = _func:get{name_or_id}
     end
     return tuple ~= nil
diff --git a/test/box/access.result b/test/box/access.result
index 6ba4f59a0b..917f425539 100644
--- a/test/box/access.result
+++ b/test/box/access.result
@@ -475,6 +475,10 @@ box.schema.user.exists('guest')
 ---
 - true
 ...
+box.schema.user.exists(nil)
+---
+- false
+...
 box.schema.user.exists(0)
 ---
 - true
@@ -520,6 +524,14 @@ box.schema.func.exists('box.schema.user.info')
 ---
 - true
 ...
+box.schema.func.exists()
+---
+- false
+...
+box.schema.func.exists(nil)
+---
+- false
+...
 -- gh-665: user.exists() should nto be true for roles
 box.schema.user.exists('public')
 ---
@@ -529,6 +541,10 @@ box.schema.role.exists('public')
 ---
 - true
 ...
+box.schema.role.exists(nil)
+---
+- false
+...
 -- test if_exists/if_not_exists in grant/revoke
 box.schema.user.grant('guest', 'read,write,execute', 'universe')
 ---
diff --git a/test/box/access.test.lua b/test/box/access.test.lua
index 41fe522ed9..9c3a5400a8 100644
--- a/test/box/access.test.lua
+++ b/test/box/access.test.lua
@@ -197,6 +197,7 @@ box.schema.user.drop('twostep_client')
 -- 
 -- box.schema.user.exists()
 box.schema.user.exists('guest')
+box.schema.user.exists(nil)
 box.schema.user.exists(0)
 box.schema.user.exists(1)
 box.schema.user.exists(100500)
@@ -209,9 +210,12 @@ box.schema.func.exists('guest')
 box.schema.func.exists(1)
 box.schema.func.exists(2)
 box.schema.func.exists('box.schema.user.info')
+box.schema.func.exists()
+box.schema.func.exists(nil)
 -- gh-665: user.exists() should nto be true for roles
 box.schema.user.exists('public')
 box.schema.role.exists('public')
+box.schema.role.exists(nil)
 -- test if_exists/if_not_exists in grant/revoke
 box.schema.user.grant('guest', 'read,write,execute', 'universe')
 box.schema.user.grant('guest', 'read,write,execute', 'universe')
-- 
GitLab