Giter Club home page Giter Club logo

transient_record's Introduction

Transient Record

transient_record is a gem to define temporary tables and Active Record models for testing purposes. It's a great tool for testing generic Active Record code and libraries.

The library was extracted from active_record_doctor to allow reuse.

Installation

Installing Transient Record is a two-step process.

Step 1: Installing the Gem

You can include Transient Record in your Gemfile:

gem "transient_record", group: :test

The above assumes it'll be used for testing purposes only, hence the test group. However, if you intend to use the gem in other circumstances then you may need to adjust the group accordingly.

If you'd like to use the latest development release then use the line below instead:

gem "transient_record", github: "gregnavis/transient_record", group: :test

After modifying Gemfile, run bundle install.

Step 2: Integrating with the Test Suite

After installing the gem, Transient Record must be integrated with the test suite. TransientRecord.cleanup must be called around every test case: before (to prepare a clean database state for the test case) and after (to leave the database in a clean state).

The snippet below demonstrates integrations with various testing libraries:

# When using Minitest
class TransientRecordTest < Minitest::Test
  def before
    TransientRecord.cleanup
  end

  def after
    TransientRecord.cleanup
  end
end

# When using Minitest::Spec
class TransientRecordTest < Minitest::Spec
  before do
    TransientRecord.cleanup
  end

  after do
    TransientRecord.cleanup
  end
end

# When using RSpec
RSpec.describe TransientRecord do
  before(:each) do
    TransientRecord.cleanup
  end

  after(:each) do
    TransientRecord.cleanup
  end
end

Usage

Transient Record can be used to create temporary tables and, optionally, models backed by them.

A table can be created by calling create_table: a thin wrapper around the method of the same name in Active Record. The only difference is the method in Transient Record implemented a fluent interface that allows calling define_model on the return value.

For example, the statement below creates a table named users with two one string column name and one integer column age:

create_table :users do |t|
  t.string :name, null: false
  t.integer :age, null: false
end

Refer to Ruby on Rails API documentation for details.

In order to define a model backed by that table define_model can be called on the return value of create_table with a block containing the model class body. For example, to define

create_table :users do |t|
  # ...
end.define_model do
  validates :email, presence: true
end

Models are automatically assigned to constants in TransientRecord::Models. The example above creates TransientRecord::Models::User, and is equivalent to:

class TransientRecord::Models::User < ActiveRecord::Base
  validates :email, presence: true
end

Caveats and Limitations

Transient Record does NOT default to using temporary tables (created via CREATE TEMPORARY TABLE) because of their second-class status in Active Record. For example, temporary table are not listed by the tables method. For this reason it was decided to use regular tables with an explicit cleanup step.

Transient Record may not work properly in parallelized test suites, e.g. if two test workers attempt to create a table with the same name then it's likely to result in an error. Full support for parallelism is on the roadmap, so feel free to report any errors and contribute updates.

Author

This gem is developed and maintained by Greg Navis.

transient_record's People

Contributors

gregnavis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.