ruby on rails - Setup fake data just once -
i'm working on rails application no models. have class in lib, fakemaker
, builds bunch of fake entities display purposed.
i want test out deletion functionality problem fake data set re-initializes every time hit controller.
i'd run data test creator once have 1 set of fake data.
i have tried using ||=
, before_filter
, class methods in fakemaker
, sessions storage seem have issue of reinitializing data set everytime controller hit.
controller code:
class homecontroller < applicationcontroller include fakemaker before_filter :set_up_fake_data def index @workstations = @data[:workstations] @data_sources = @data[:data_sources] end private def set_fake_data @data ||= session[:fake_data] end def initialize_data session[:fake_data] = set_up_fake_data end end
fakemaker in lib:
module fakemaker include actionview::helpers::numberhelper source_cidne = "cidne" source_dcgs = "dcgs" types_cidne = faker::lorem.words(num = 10) types_dcgs = faker::lorem.words(num = 4) def set_up_fake_data @data ||= { workstations: fake_maker("workstation", 8), data_sources: fake_maker("data_source", 2) } end def fake_maker(type, count) fake = [] case type when "report" count.times { fake << fake_report } when "workstation" count.times { fake << fake_workstation } when "data_source" fake = fake_data_source end fake end def fake_report report = { source: [source_cidne, source_dcgs].sample, count: number_with_delimiter(faker::number.number(5), :delimiter => ',') } report[:type] = report[:source] == source_cidne ? types_cidne.sample : types_dcgs.sample.capitalize report end def fake_workstation { name: faker::lorem.word, downloaded: number_with_delimiter(faker::number.number(3), :delimiter => ','), available: number_with_delimiter(faker::number.number(5), :delimiter => ','), last_connect: fake_time, queuejson: fake_queuejson, historyjson: fake_historyjson } end def fake_data_source data_sources = [] ["cidne", "dcgs"].each |source| data_sources << { type: source, name: faker::internet.url, status: ["up", "down"].sample } end data_sources end def fake_historyjson history = [] 12.times { history << fake_history } history end def fake_queuejson queue = [] 35.times { queue << fake_queue } queue end def fake_history { senttimestamp: fake_time, reportid: faker::number.number(5)} end def fake_queue { priority: faker::number.number(3), queuedtimestamp: fake_time, reportid: faker::number.number(5)} end def fake_time random.rand(10.weeks).ago.strftime("%y-%m-%d %h:%m:%s") end end
Comments
Post a Comment