fluke/devops-service/spec/connectors/tester_connector/base.rb
2015-12-05 12:34:12 +03:00

53 lines
964 B
Ruby

module TesterConnector
class Base
attr_reader :collection
def initialize
collection_name = self.class.name.demodulize.underscore.pluralize
@collection = SpecSupport.db.collection(collection_name)
@next_id = 1
end
def create(hash={})
collection.insert(create_params(hash))
if block_given?
yield
cleanup
end
end
def create_list(size=2, hash={})
size.times { create(hash) }
if block_given?
yield
cleanup
end
end
def list
collection.find().to_a
end
def show(id)
collection.find({'_id' => id}).to_a.first
end
def cleanup
collection.remove
end
private
def create_params(hash)
params = hash.dup
if params[:id]
params['_id'] = params.delete(:id)
end
unless params['_id']
params['_id'] = @next_id
@next_id += 1
end
params
end
end
end