项目作者: nerdinand

项目描述 :
A fixture library for sequel
高级语言: Ruby
项目地址: git://github.com/nerdinand/kibutsu.git
创建时间: 2017-11-11T16:23:06Z
项目社区:https://github.com/nerdinand/kibutsu

开源协议:

下载


kibutsu

Gem Version Build Status

Kibutsu is an easy-to-use database fixture library.

The only dependency kibutsu has is the sequel gem. Thus, you may use it with any project that uses sequel, e.g. with a hanami app!

It features Rails-style database fixtures with

  • ERB-preprocessing capability
  • cross-referencing of fixtures to simplify work with foreign keys
  • automatic filling of created_at and updated_at, if necessary

Installation

Add this line to your application’s Gemfile:

  1. gem 'kibutsu'

And then execute:

  1. $ bundle

Usage

Kibutsu can be used in any project that uses the sequel gem. The public API is accessible through the Kibutsu module.

Usage with RSpec and Hanami

In your spec_helper.rb, add the following:

  1. config.before(:suite) do
  2. Kibutsu.load_fixtures!(ENV['DATABASE_URL'], Hanami.root.join('spec', 'fixtures'))
  3. end

This will tell Kibutsu to load the fixture files saved in spec/fixtures/ in your project. It will then import that fixture data into the database specified in the DATABASE_URL environment variable.

It’s also recommended that you reset the status of the database after each individual test, for example via a transaction configured in spec_helper.rb:

  1. config.around(:each) do |example|
  2. Hanami::Model.configuration.connection.transaction(savepoint: true) do
  3. example.run
  4. raise Sequel::Rollback
  5. end
  6. end

Fixture files

The fixture file format is almost exactly like the one used for Rails. Here’s an example:

spec/fixtures/authors.yml

  1. authors: # name of the database table
  2. adams: # name of this fixture, to be referenced in other fixtures
  3. first_name: Douglas
  4. last_name: Adams

If a fixture file ends in .yml.erb instead of .yml, it is being run through ERB partial processing:

spec/fixtures/books.yml.erb

  1. books:
  2. hitchhiker:
  3. title: The Hitchhiker's Guide to the Galaxy
  4. author: adams # we reference another fixture here (this assumes there is a column books.author_id)
  5. page_count: <%= 179 %>
  6. restaurant:
  7. title: The Restaurant at the End of the Universe
  8. author: adams
  9. page_count: <%= 200 %>

In case you need to reference a particular fixture from a test, you can use Kibutsu.fixture_name_to_id to get the id of a named fixture like so:

  1. AuthorRepository.new.find(Kibutsu.fixture_name_to_id(:adams))

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nerdinand/kibutsu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.