Project

kibutsu

0.0
No commit activity in last 3 years
No release in over 3 years
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!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.7
~> 0.55
~> 1.3

Runtime

~> 5.18
 Project Readme

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:

gem 'kibutsu'

And then execute:

$ 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:

config.before(:suite) do
  Kibutsu.load_fixtures!(ENV['DATABASE_URL'], Hanami.root.join('spec', 'fixtures'))
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:

config.around(:each) do |example|
  Hanami::Model.configuration.connection.transaction(savepoint: true) do
    example.run
    raise Sequel::Rollback
  end
end

Fixture files

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

spec/fixtures/authors.yml

authors: # name of the database table
  adams: # name of this fixture, to be referenced in other fixtures
    first_name: Douglas
    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

books:
  hitchhiker:
    title: The Hitchhiker's Guide to the Galaxy
    author: adams # we reference another fixture here (this assumes there is a column books.author_id)
    page_count: <%= 179 %>

  restaurant:
    title: The Restaurant at the End of the Universe
    author: adams
    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:

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.