Skip to content

Commit

Permalink
Faker doesn't insert random plugins. Fix Kong#29
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Mar 9, 2015
1 parent 6be3430 commit 5d21556
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 63 deletions.
2 changes: 1 addition & 1 deletion scripts/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cli:set_name("db.lua")
cli:add_argument("COMMAND", "{create|migrate|rollback|reset|seed|drop}")
cli:add_option("-c, --config=CONFIG", "configuration file", "kong.yml")
cli:add_option("-n, --name=NAME", "If <create>, sets a name to the migration", "new_migration")
cli:add_flag("-r, --random", "If seeding, also seed random entities (1000 for each collection by default)")
cli:add_option("-r, --random=RANDOM_AMOUNT", "If seeding, specify number of random entities to add")
cli:add_flag("-s, --silent", "No output")

local args = cli:parse(arg)
Expand Down
17 changes: 3 additions & 14 deletions spec/unit/dao/cassandra_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ describe("Cassandra DAO #dao #cassandra", function()
it("should not insert in DB if invalid", function()
-- Without an api_id, it's a schema error
local plugin_t = faker:fake_entity("plugin")
plugin_t.api_id = nil
local plugin, err = dao_factory.plugins:insert(plugin_t)
assert.falsy(plugin)
assert.truthy(err)
Expand Down Expand Up @@ -467,11 +466,6 @@ describe("Cassandra DAO #dao #cassandra", function()
faker:seed()
end)

teardown(function()
dao_factory:drop()
faker:seed()
end)

describe_all_collections(function(type, collection)

it("should return false if there was nothing to delete", function()
Expand Down Expand Up @@ -503,12 +497,7 @@ describe("Cassandra DAO #dao #cassandra", function()

setup(function()
dao_factory:drop()
faker:seed(true, 100)
end)

teardown(function()
dao_factory:drop()
faker:seed()
faker:seed(100)
end)

describe_all_collections(function(type, collection)
Expand Down Expand Up @@ -783,7 +772,8 @@ describe("Cassandra DAO #dao #cassandra", function()
local inserted_plugin

setup(function()
faker:seed(true, 100)
dao_factory:drop()
faker:seed(100)
end)

it("should find distinct plugins", function()
Expand All @@ -809,7 +799,6 @@ describe("Cassandra DAO #dao #cassandra", function()

local plugin_t = faker:fake_entity("plugin")
plugin_t.api_id = api.id
plugin_t.application_id = nil

local plugin, err = dao_factory.plugins:insert(plugin_t)
assert.falsy(err)
Expand Down
22 changes: 4 additions & 18 deletions spec/unit/tools_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ describe("Faker #tools", function()
it("should call insert_from_table()", function()
faker:seed()
assert.spy(faker.insert_from_table).was.called(1)
--assert.spy(faker.insert_from_table).was_called_with(faker, Faker.entities_to_insert)
end)

it("should populate the inserted_entities table for relations", function()
Expand All @@ -294,28 +293,15 @@ describe("Faker #tools", function()
end)

it("should be possible to add some random entities complementing the default hard-coded ones", function()
faker:seed(true)
faker:seed(2000)
assert.spy(faker.insert_from_table).was.called(2)
-- random entities default is 1000 for each collection type
assert.spy(insert_spy).was.called(4000)
end)

it("should be possible to override the amount of random entities", function()
faker:seed(true, 2000)
assert.spy(faker.insert_from_table).was.called(2)
assert.spy(insert_spy).was.called(8000)
end)

it("should throw an error if trying to pass an amount that would result in a negative amount of inserted entities", function()
assert.has_error(function()
faker:seed(true, 2)
end, "Cannot insert a negative number of elements. Too low amount parameter.")
assert.spy(insert_spy).was.called(6018) -- 3*2000 + 18 base entities
end)

it("should create relations between entities_to_insert and inserted entities", function()
faker:seed()

for type, entities in pairs(Faker.entities_to_insert) do
for type, entities in pairs(Faker.FIXTURES) do
for i, entity in ipairs(entities) do
-- assert object has been inserted
local inserted_entity = faker.inserted_entities[type][i]
Expand All @@ -341,7 +327,7 @@ describe("Faker #tools", function()
end
assert.has_error(function()
faker:seed()
end, "Faker failed to insert api entity: "..inspect(Faker.entities_to_insert.api[1]).."\ncannot insert api error test")
end, "Faker failed to insert api entity: "..inspect(Faker.FIXTURES.api[1]).."\ncannot insert api error test")
end)

end)
Expand Down
47 changes: 17 additions & 30 deletions src/kong/tools/faker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ local utils = require "kong.tools.utils"

math.randomseed(os.time())

-- Select a random elements from an array, removes and return it
-- Return a random elements from an array
-- @param {table} t Array to get an element from
-- @return A random element
local function random_from_table(t, remove)
if not t then return {} end

local random_index = math.random(#t)
local random_element = t[random_index]

if remove then
t[random_index] = nil
end

return random_element
return t[math.random(#t)]
end

--
Expand All @@ -30,7 +22,7 @@ function Faker:new(dao_factory)
self.inserted_entities = {}
end

Faker.entities_to_insert = {
Faker.FIXTURES = {
api = {
{ name = "test", public_dns = "test.com", target_url = "http://httpbin.org" },
{ name = "test2", public_dns = "test2.com", target_url = "http://httpbin.org" },
Expand Down Expand Up @@ -60,7 +52,6 @@ Faker.entities_to_insert = {
}

-- Generate a fake entity
--
-- @param {string} type Type of the entity to generate
-- @return {table} An entity schema
function Faker:fake_entity(type)
Expand Down Expand Up @@ -93,39 +84,35 @@ function Faker:fake_entity(type)
return {
name = plugin_type,
value = plugin_value,
api_id = random_from_table(self.inserted_entities.api).id,
application_id = random_from_table(self.inserted_entities.application).id
api_id = nil,
application_id = nil
}
else
error("Entity of type "..type.." cannot be generated.")
end
end

-- Seed the database with a set of hard-coded entities, and optionally random data
--
-- @param {boolean} random If true, will generate random entities
-- @param {number} amount The number of total entity to generate (hard-coded + random)
function Faker:seed(random, amount)
if not amount then
amount = 1000
end

-- @param {number} random_amount The number of random entities to add (apis, accounts, applications)
function Faker:seed(random_amount)
-- reset previously inserted entities
self.inserted_entities = {}

self:insert_from_table(utils.deepcopy(Faker.entities_to_insert), true)
self:insert_from_table(utils.deepcopy(Faker.FIXTURES), true)

if random then
if random_amount then
-- If we ask for random entities, add as many random entities to another table
-- as the difference between total amount requested and hard-coded ones
-- If we ask for 1000 entities, we'll have (1000 - number_of_hard_coded) random entities
--
-- We don't generate any random plugin
local random_entities = {}
for type, entities in pairs(Faker.entities_to_insert) do
local number_to_insert = amount - #entities
for type, entities in pairs(Faker.FIXTURES) do
random_entities[type] = {}
assert(number_to_insert > 0, "Cannot insert a negative number of elements. Too low amount parameter.")
for i = 1, number_to_insert do
table.insert(random_entities[type], self:fake_entity(type))
if type ~= "plugin" then
for i = 1, random_amount do
table.insert(random_entities[type], self:fake_entity(type))
end
end
end

Expand All @@ -140,7 +127,7 @@ end
function Faker:insert_from_table(entities_to_insert, pick_relations)
-- Insert in order (for foreign relashionships)
-- 1. accounts and APIs
-- 2. applications, plugins and metrics which need refereces to inserted apis and accounts
-- 2. applications, which need refereces to inserted apis and accounts
for _, type in ipairs({ "api", "account", "application", "plugin" }) do
for i, entity in ipairs(entities_to_insert[type]) do

Expand Down

0 comments on commit 5d21556

Please sign in to comment.