Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendations for fixtures? #122

Open
mwpastore opened this issue Aug 15, 2016 · 3 comments
Open

Recommendations for fixtures? #122

mwpastore opened this issue Aug 15, 2016 · 3 comments

Comments

@mwpastore
Copy link

My apologies for opening an issue to ask a question, but I can't find a Google Group or IRC channel or anything like that mentioned in the README.

What's the best way to get started with fixtures for unit tests when using SequelRails, specifically with Rails 4.2? I've been flailing around with fixture_dependencies without any luck; it appears to not have been updated to work with ActiveSupport::TestCase. Are there any recommendations I can follow to get started here? Thanks in advance.

@tmepple
Copy link

tmepple commented Aug 16, 2016

I'm a newbie who is using fixture_dependencies with sequel-rails with Rails 5 and I got it working fine after a bit of Google research...

  • Add gem 'fixture_dependencies' to the :test group in Gemfile
  • In tests/test_helper.rb file you have to require 'fixture_dependencies/sequel' at the top and add the following code to the ActiveSupport::TestCase class below. This automatically cleans your test database at the end of every test run through a rollback.
  def run(*args, &block)
    FixtureDependencies.fixture_path = 'test/fixtures'
    Sequel::Model.db.transaction(rollback: :always, auto_savepoint: true) { super }
  end
  • If you are including ERB in your YML fixture files you will have to use the .yml.erb extension
  • Also, every time I've run rake db:migrate to change the schema I've been having to run rake test:prepare to re-generate your test database as, unlike AR, they aren't kept in sync automatically that I can tell.

Since all of your generated test classes have require 'test_helper' in them you are good to go. Just load the fixtures in your #setup method (or in the tests themselves) with a construct such as @user = FixtureDependencies.load(:user__michael) to load a single record or FixtureDependencies.load(:users) to load all users.

Hope this helps.

@mwpastore
Copy link
Author

Ah, excellent. Thanks @tmepple. I'm hoping to make the fixtures helper/class method available as well, so I'll see if I can take this work and further it a bit. Thank you!

@iaddict
Copy link

iaddict commented Jul 3, 2018

A special note for those who like to use FixtureDependencies with MS-SQL-Server.
Following my changes/settings made to test/test_helper.rb:

# ...
require 'fixture_dependencies'
require 'fixture_dependencies/sequel'

FixtureDependencies.verbose = 5
FixtureDependencies.fixture_path = 'test/fixtures'

class << FixtureDependencies
  # MS-SqlServer does not allow to insert new records with an already used pk unless
  # IDENTITY_INSERT is ON.
  # Because FixtureDependencies tries to insert once inserted records with its primary key set,
  # it needs to be monkey patched to work with MS-SqlServer.
  alias :model_save_S_org :model_save_S
  def model_save_S(object)
    object.db.run("SET IDENTITY_INSERT #{object.class.table_name} ON") if object.id.present?
    model_save_S_org(object)
    object.db.run("SET IDENTITY_INSERT #{object.class.table_name} OFF") if object.id.present?
  end
end

class ActiveSupport::TestCase
  def run(*args, &block)
    Sequel::Model.db.transaction(rollback: :always, auto_savepoint: true) do
      super
    end
  end

  # ...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants