Skip to content

Commit

Permalink
Allow use of Sequel in addition to ActiveRecord
Browse files Browse the repository at this point in the history
- Allow fixture_path to be set, only set by default when using rails helper
- Update README, use better RDoc format
- Add rdoc directory to clean task
- Add 2008 to copyright year
- Bump version to 1.1.0
  • Loading branch information
jeremyevans committed Aug 25, 2008
1 parent bda31cc commit 63025e7
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 222 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2007 Jeremy Evans
Copyright (c) 2007-2008 Jeremy Evans

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
130 changes: 90 additions & 40 deletions README
@@ -1,31 +1,74 @@
fixture_dependencies is a plugin that changes the way Rails uses fixtures in
the following ways:

- Fixtures can specify associations instead of foreign keys
- Supports belongs_to, has_many, has_one, and habtm associations
- Loads a fixture's dependencies (associations with other fixtures) before the
fixture itself so that foreign key constraints aren't violated
- Can specify individual fixtures to load per test or test suite
- Loads fixtures on every test inside a transaction, so fixture information
is never left in your database
= fixture_dependencies

fixture_dependencies is an advanced fixture loader, allowing the loading of
models from YAML fixtures, along with their entire dependency graph. It has
the following features:

- Fixtures specify association names instead of foreign keys
- Support both Sequel and ActiveRecord
- Supports many_to_one/belongs_to, one_to_many/has_many,
many_to_many/has_and_belongs_to_many, and has_one associations
- Loads a fixture's dependency graph in such a manner that foreign key
constraints aren't violated
- Has a very simple API (FixtureDependencies.load(:model__fixture))
- Handles almost all cyclic dependencies
- Includes Rails and Sequel test helpers for Test::Unit that load fixtures for
every test inside a transaction, so fixture data is never left in your
database

To use, first install the plugin, then add the following to
test/test_helper.rb after "require 'test_help'":
== Installation

require 'fixture_dependencies_test_help'
sudo gem install jeremyevans-fixture_dependencies \
--source http://gems.github.com

== Source

Source is available via github:

http://github.com/jeremyevans/fixture_dependencies

You can check it out with git:

git clone git://github.com/jeremyevans/fixture_dependencies.git

== Usage

=== With Rails/ActiveRecord/Test::Unit:

Add the following to test/test_helper.rb after "require 'test_help'":

require 'fixture_dependencies/test_unit/rails'

This overrides the default test helper to load the fixtures inside transactions
and to use FixtureDependencies to load the fixtures.

fixture_dependencies is available via github:
=== With Sequel/Test::Unit:

Somewhere before the test code is loaded:

require 'fixture_dependencies/test_unit/sequel'

Make sure the test case classes use FixtureDependencies::SequelTestCase:

class ModelTest < FixtureDependencies::SequelTestCase

http://github.com/jeremyevans/fixture_dependencies/tree/master
This runs the test cases inside a Sequel transaction.

Changes to Fixtures:
=== With other testing libraries:

You can just use FixtureDependencies.load to handle the loading of fixtures.
The use of transactions is up to you. It's fairly easy to add support for
running RSpec examples inside transactions. One thing you must do if you are
not using the rails test helper is to set the fixture path for
FixtureDependencies:

FixtureDependencies.fixture_path = '/path/to/fixtures'

== Changes to Rails default fixtures:

fixture_dependencies is designed to require the least possible changes to
fixtures. For example, see the following changes:
the default YAML fixtures used by Rails (well, at least Rails 1.2 and earlier).
For example, see the following changes:

OLD NEW
asset1: asset1:
Expand All @@ -42,7 +85,7 @@ nx7010, and a vendors fixture with the name lxg_computers.

Fixture files still use the table_name of the model.

Changes to the fixtures Class Method:
== Changes to the fixtures Class Method:

fixture_dependencies can still use the fixtures class method in your test:

Expand All @@ -54,12 +97,11 @@ In Rails default testing practices, the arguments to fixtures are table names.
fixture_dependencies changes this to underscored model names. If you are using
Rails' recommended table practices, this shouldn't make a difference.

Another change is that Rails defaults allow you to specify habtm join tables in
fixtures. That doesn't work with fixture dependencies, as there is no
associated model. Instead, you use a has_and_belongs_to_many association name
in the the appropriate model fixtures (see below).
It is recommended that you do not use the fixtures method, and instead load
individual fixtures as needed (see below). This makes your tests much more
robust, in case you want to add or remove individual fixtures at a later date.

Loading Individual Fixtures with fixtures class method:
== Loading individual fixtures with fixtures class method

There is support for loading individual fixtures (and just their dependencies),
using the following syntax:
Expand All @@ -69,9 +111,11 @@ using the following syntax:
end

This would load just the jeremy fixture and its dependencies. I find this is
much better than loading all fixtures in most of my test suites.
much better than loading all fixtures in most of my test suites. Even better
is loading just the fixtures you want instead every test method (see below).
This leads to the most robust testing.

Loading Fixtures Inside Test Methods:
== Loading fixtures inside test methods

I find that it is often better to skip the use of the fixtures method entirely,
and load the fixtures I want manually in each test method. This provides for
Expand All @@ -86,7 +130,7 @@ the loosest coupling possible. Here's an example:

def test_award_statistics
# Load all fixtures in both tables
load(:employee_awards, :awards)
load(:employee_award__jeremy_first, :award__first)
# Test the award_statistics method
# (which pulls data from the tables loaded above)
end
Expand All @@ -95,7 +139,7 @@ the loosest coupling possible. Here's an example:
Don't worry about loading the same fixture twice, if a fixture is already
loaded, it won't attempt to load it again.

has_* Assocations in Fixtures:
== one_to_many/many_to_many/has_many/has_and_belongs_to_many assocations

Here's an example of using has_one (logon_information), has_many (assets), and
has_and_belongs_to_many (groups) associations.
Expand All @@ -120,7 +164,14 @@ groups in the order specified (and their dependencies...). Note that there
is only a load order inside a specific association, associations are stored
in the same hash as attributes and are loaded in an arbitrary order.

Cyclic Dependencies:
== many_to_many/has_and_belongs_to_many join table fixtures

Another change is that Rails defaults allow you to specify habtm join tables in
fixtures. That doesn't work with fixture dependencies, as there is no
associated model. Instead, you use a has_and_belongs_to_many association name
in the the appropriate model fixtures (see above).

== Cyclic dependencies

fixture_dependencies handles almost all cyclic dependencies. It handles all
has_many, has_one, and habtm cyclic dependencies. It handles all
Expand All @@ -145,9 +196,9 @@ division belongs_to head_of_division when the employee is a member of the
division and also the head of the division), even that approach is not
possible.

Known Issues:
== Known issues

Currently, the plugin only supports yaml fixtures, but other types of fixtures
Currently, the plugin only supports YAML fixtures, but other types of fixtures
would be fairly easy to add (send me a patch if you add support for another
fixture type).

Expand All @@ -161,32 +212,31 @@ was rolled back in r2730 due to speed issues. See ticket #2404 on Rails' trac.
Instantiated fixtures are not available with this plugin. Instead, you should
use load(:model__fixture_name).

Troubleshooting:
== Troubleshooting

If you run into problems with loading your fixtures, it can be difficult to see
where the problems are. To aid in debugging an error, add the following to
test/test_helper.rb:

FixtureDependencies.verbose = 2
FixtureDependencies.verbose = 3

This will give a verbose description of the loading and saving of fixtures for
every test, including the recursive loading of all dependencies.
every test, including the recursive loading of the dependency graph.

== Similar Ideas

Similar Ideas:
Rails now supports something similar by default. Honestly, I'm not sure what
the differences are.

fixture_references is a similar plugin. It uses erb inside yaml, and uses the
foreign key numbers inside of the association names, which leads me to believe
it doesn't support has_* associations.

Ticket #6424 on the Rails' trac also implements a similar idea, but it parses
the associations and changes them to foreign keys, which leads me to believe it
doesn't support has_* associations either.

License:
== License

fixture_dependencies is released under the MIT License. See the LICENSE file
for details.

Author:
== Author

Jeremy Evans <code@jeremyevans.net>
4 changes: 3 additions & 1 deletion Rakefile
Expand Up @@ -2,12 +2,14 @@ require 'rake'
require 'rake/clean'
require 'rake/rdoctask'

CLEAN.include ["rdoc"]

Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = "rdoc"
rdoc.options += ["--quiet", "--line-numbers", "--inline-source"]
rdoc.main = "README"
rdoc.title = "fixture_dependencies: Rails fixture loading that works with foreign keys"
rdoc.rdoc_files.add ["README", "LICENSE", "lib/fixture_dependencies.rb", "lib/fixture_dependencies_test_help.rb"]
rdoc.rdoc_files.add ["README", "LICENSE", "lib/**/*.rb"]
end

desc "Package fixture_dependencies"
Expand Down
6 changes: 3 additions & 3 deletions fixture_dependencies.gemspec
@@ -1,11 +1,11 @@
spec = Gem::Specification.new do |s|
s.name = "fixture_dependencies"
s.version = "1.0.0"
s.version = "1.1.0"
s.author = "Jeremy Evans"
s.email = "code@jeremyevans.net"
s.platform = Gem::Platform::RUBY
s.summary = "Rails fixture loading that works with foreign keys"
s.files = ["README", "LICENSE", "lib/fixture_dependencies.rb", "lib/fixture_dependencies_test_help.rb"]
s.summary = "Sequel/ActiveRecord fixture loader that handles dependency graphs"
s.files = ["README", "LICENSE", "lib/fixture_dependencies.rb", "lib/fixture_dependencies_test_help.rb", "lib/fixture_dependencies/sequel.rb", "lib/fixture_dependencies/active_record.rb", "lib/fixture_dependencies/test_unit.rb", "lib/fixture_dependencies/test_unit/rails.rb", "lib/fixture_dependencies/test_unit/sequel.rb"]
s.extra_rdoc_files = ["LICENSE"]
s.require_paths = ["lib"]
s.has_rdoc = true
Expand Down
2 changes: 0 additions & 2 deletions init.rb

This file was deleted.

0 comments on commit 63025e7

Please sign in to comment.