ar_fixtures 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +14 -0
- data/History.txt +16 -0
- data/MIT-LICENSE +20 -0
- data/Manifest.txt +21 -0
- data/README.txt +19 -0
- data/Rakefile +12 -0
- data/about.yml +7 -0
- data/init.rb +1 -0
- data/lib/ar_fixtures.rb +103 -0
- data/tasks/ar_fixtures.rake +38 -0
- data/test/ar_fixtures_test.rb +53 -0
- data/test/database.yml +18 -0
- data/test/fixtures/beer.rb +5 -0
- data/test/fixtures/beers.yml +9 -0
- data/test/fixtures/beers_drunkards.yml +8 -0
- data/test/fixtures/drunkard.rb +6 -0
- data/test/fixtures/drunkards.yml +8 -0
- data/test/fixtures/glass.rb +2 -0
- data/test/fixtures/glasses.yml +9 -0
- data/test/schema.rb +21 -0
- data/test/test_helper.rb +37 -0
- metadata +77 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
== June 4, 2006
|
3
|
+
|
4
|
+
* Clarified rake task descriptions to show where data is being dumped.
|
5
|
+
|
6
|
+
== May 20, 2006
|
7
|
+
|
8
|
+
* Added to_skeleton for writing fieldnames only. [Gunther Schmidl]
|
9
|
+
* Added initial tests, thanks to Scott Barron's acts_as_state_machine
|
10
|
+
|
11
|
+
== Jan 12 2006
|
12
|
+
|
13
|
+
* Added code to handle single table inheritance. May need to be modified for non-standard use of STI.
|
14
|
+
|
data/History.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
== 0.0.4 / 2007-06-24
|
2
|
+
|
3
|
+
* Converted to a gem
|
4
|
+
|
5
|
+
== 0.0.3 / June 4, 2006
|
6
|
+
|
7
|
+
* Clarified rake task descriptions to show where data is being dumped.
|
8
|
+
|
9
|
+
== 0.0.2 / May 20, 2006
|
10
|
+
|
11
|
+
* Added to_skeleton for writing fieldnames only. [Gunther Schmidl]
|
12
|
+
* Added initial tests, thanks to Scott Barron's acts_as_state_machine
|
13
|
+
|
14
|
+
== 0.0.1 / Jan 12 2006
|
15
|
+
|
16
|
+
* Added code to handle single table inheritance. May need to be modified for non-standard use of STI.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2005 Geoffrey Grosenbach
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
History.txt
|
3
|
+
MIT-LICENSE
|
4
|
+
Manifest.txt
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
about.yml
|
8
|
+
init.rb
|
9
|
+
lib/ar_fixtures.rb
|
10
|
+
tasks/ar_fixtures.rake
|
11
|
+
test/ar_fixtures_test.rb
|
12
|
+
test/database.yml
|
13
|
+
test/fixtures/beer.rb
|
14
|
+
test/fixtures/beers.yml
|
15
|
+
test/fixtures/beers_drunkards.yml
|
16
|
+
test/fixtures/drunkard.rb
|
17
|
+
test/fixtures/drunkards.yml
|
18
|
+
test/fixtures/glass.rb
|
19
|
+
test/fixtures/glasses.yml
|
20
|
+
test/schema.rb
|
21
|
+
test/test_helper.rb
|
data/README.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= ar_fixtures
|
2
|
+
|
3
|
+
This library makes it easy to save ActiveRecord objects to reloadable files or fixtures. ActiveRecord is required.
|
4
|
+
|
5
|
+
See tasks/ar_fixtures.rake for what can be done from the command-line, or use "rake -T" and look for items in the "db" namespace.
|
6
|
+
|
7
|
+
== Resources
|
8
|
+
|
9
|
+
Subversion
|
10
|
+
|
11
|
+
* http://topfunky.net/svn/plugins/ar_fixtures
|
12
|
+
|
13
|
+
Blog
|
14
|
+
|
15
|
+
* http://nubyonrails.com
|
16
|
+
|
17
|
+
Author
|
18
|
+
|
19
|
+
* Geoffrey Grosenbach boss [at] topfunky [dot] com
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
|
4
|
+
Hoe.new("ar_fixtures", '0.0.4') do |p|
|
5
|
+
p.summary = "Creates test fixtures from data in the database."
|
6
|
+
p.description = "Creates test fixtures from data in the database."
|
7
|
+
p.rubyforge_name = 'seattlerb'
|
8
|
+
p.author = 'Geoffrey Grosenbach'
|
9
|
+
p.email = 'boss AT topfunky.com'
|
10
|
+
p.url = "http://rubyforge.org/projects/seattlerb"
|
11
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
12
|
+
end
|
data/about.yml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
author: topfunky
|
2
|
+
summary: Use the existing data in your database to generate fixtures, fixture skeletons, and reference YAML files.
|
3
|
+
homepage: http://nubyonrails.com
|
4
|
+
plugin: http://topfunky.net/svn/plugins/ar_fixtures
|
5
|
+
license: MIT
|
6
|
+
version: 0.2
|
7
|
+
rails_version: 1.0+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ar_fixtures'
|
data/lib/ar_fixtures.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# Extension to make it easy to read and write data to a file.
|
2
|
+
class ActiveRecord::Base
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Writes content of this table to db/table_name.yml, or the specified file.
|
7
|
+
#
|
8
|
+
# Writes all content by default, but can be limited.
|
9
|
+
def dump_to_file(path=nil, limit=nil)
|
10
|
+
opts = {}
|
11
|
+
opts[:limit] = limit if limit
|
12
|
+
path ||= "db/#{table_name}.yml"
|
13
|
+
write_file(File.expand_path(path, RAILS_ROOT), self.find(:all, opts).to_yaml)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Delete existing data in database and load fresh from file in db/table_name.yml
|
17
|
+
def load_from_file(path=nil)
|
18
|
+
path ||= "db/#{table_name}.yml"
|
19
|
+
|
20
|
+
self.destroy_all
|
21
|
+
|
22
|
+
if connection.respond_to?(:reset_pk_sequence!)
|
23
|
+
connection.reset_pk_sequence!(table_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
records = YAML::load( File.open( File.expand_path(path, RAILS_ROOT) ) )
|
27
|
+
records.each do |record|
|
28
|
+
record_copy = self.new(record.attributes)
|
29
|
+
record_copy.id = record.id
|
30
|
+
|
31
|
+
# For Single Table Inheritance
|
32
|
+
klass_col = record.class.inheritance_column.to_sym
|
33
|
+
if record[klass_col]
|
34
|
+
record_copy.type = record[klass_col]
|
35
|
+
end
|
36
|
+
|
37
|
+
record_copy.save
|
38
|
+
end
|
39
|
+
|
40
|
+
if connection.respond_to?(:reset_pk_sequence!)
|
41
|
+
connection.reset_pk_sequence!(table_name)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Write a file that can be loaded with +fixture :some_table+ in tests.
|
46
|
+
# Uses existing data in the database.
|
47
|
+
#
|
48
|
+
# Will be written to +test/fixtures/table_name.yml+. Can be restricted to some number of rows.
|
49
|
+
def to_fixture(limit=nil)
|
50
|
+
opts = {}
|
51
|
+
opts[:limit] = limit if limit
|
52
|
+
|
53
|
+
write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
|
54
|
+
self.find(:all, opts).inject({}) { |hsh, record|
|
55
|
+
hsh.merge("#{table_name.singularize}_#{'%05i' % record.id}" => record.attributes)
|
56
|
+
}.to_yaml(:SortKeys => true))
|
57
|
+
habtm_to_fixture
|
58
|
+
end
|
59
|
+
|
60
|
+
# Write the habtm association table
|
61
|
+
def habtm_to_fixture
|
62
|
+
joins = self.reflect_on_all_associations.select { |j|
|
63
|
+
j.macro == :has_and_belongs_to_many
|
64
|
+
}
|
65
|
+
joins.each do |join|
|
66
|
+
hsh = {}
|
67
|
+
connection.select_all("SELECT * FROM #{join.options[:join_table]}").each_with_index { |record, i|
|
68
|
+
hsh["join_#{'%05i' % i}"] = record
|
69
|
+
}
|
70
|
+
write_file(File.expand_path("test/fixtures/#{join.options[:join_table]}.yml", RAILS_ROOT), hsh.to_yaml(:SortKeys => true))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Generates a basic fixture file in test/fixtures that lists the table's field names.
|
75
|
+
#
|
76
|
+
# You can use it as a starting point for your own fixtures.
|
77
|
+
#
|
78
|
+
# record_1:
|
79
|
+
# name:
|
80
|
+
# rating:
|
81
|
+
# record_2:
|
82
|
+
# name:
|
83
|
+
# rating:
|
84
|
+
#
|
85
|
+
# TODO Automatically add :id field if there is one.
|
86
|
+
def to_skeleton
|
87
|
+
record = {
|
88
|
+
"record_1" => self.new.attributes,
|
89
|
+
"record_2" => self.new.attributes
|
90
|
+
}
|
91
|
+
write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
|
92
|
+
record.to_yaml)
|
93
|
+
end
|
94
|
+
|
95
|
+
def write_file(path, content) # :nodoc:
|
96
|
+
f = File.new(path, "w+")
|
97
|
+
f.puts content
|
98
|
+
f.close
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
def env_or_raise(var_name, human_name)
|
3
|
+
if ENV[var_name].blank?
|
4
|
+
raise "No #{var_name} value given. Set #{var_name}=#{human_name}"
|
5
|
+
else
|
6
|
+
return ENV[var_name]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def model_or_raise
|
11
|
+
return env_or_raise('MODEL', 'ModelName')
|
12
|
+
end
|
13
|
+
|
14
|
+
def limit_or_nil_string
|
15
|
+
ENV['LIMIT'].blank? ? 'nil' : ENV['LIMIT']
|
16
|
+
end
|
17
|
+
|
18
|
+
namespace :db do
|
19
|
+
namespace :fixtures do
|
20
|
+
desc "Dump data to the test/fixtures/ directory. Use MODEL=ModelName and LIMIT (optional)"
|
21
|
+
task :dump => :environment do
|
22
|
+
eval "#{model_or_raise}.to_fixture(#{limit_or_nil_string})"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
namespace :data do
|
27
|
+
desc "Dump data to the db/ directory. Use MODEL=ModelName and LIMIT (optional)"
|
28
|
+
task :dump => :environment do
|
29
|
+
eval "#{model_or_raise}.dump_to_file(nil, #{limit_or_nil_string})"
|
30
|
+
puts "#{model_or_raise} has been dumped to the db folder."
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Load data from the db/ directory. Use MODEL=ModelName"
|
34
|
+
task :load => :environment do
|
35
|
+
eval "#{model_or_raise}.load_from_file"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
# TODO Test limit params
|
5
|
+
# TODO Test renaming
|
6
|
+
# TODO Examine contents of fixture and skeleton dump
|
7
|
+
class ArFixturesTest < Test::Unit::TestCase
|
8
|
+
fixtures :beers, :drunkards, :beers_drunkards
|
9
|
+
include FileUtils
|
10
|
+
|
11
|
+
def setup
|
12
|
+
%w(db test/fixtures).each { |dir| mkdir_p File.join(RAILS_ROOT, dir) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_dump_to_file
|
16
|
+
Beer.dump_to_file
|
17
|
+
assert File.exist?(File.join(RAILS_ROOT, 'db', 'beers.yml'))
|
18
|
+
|
19
|
+
Beer.destroy_all
|
20
|
+
assert_equal 0, Beer.count
|
21
|
+
Beer.load_from_file
|
22
|
+
assert_equal 2, Beer.count
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_load_from_file
|
26
|
+
cp File.join(RAILS_ROOT, 'fixtures', 'glasses.yml'),
|
27
|
+
File.join(RAILS_ROOT, 'db', 'glasses.yml')
|
28
|
+
assert_equal 0, Glass.count
|
29
|
+
Glass.load_from_file
|
30
|
+
assert_equal 2, Glass.count
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_to_fixture
|
34
|
+
Beer.to_fixture
|
35
|
+
assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers.yml'))
|
36
|
+
assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers_drunkards.yml'))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_habtm_to_fixture
|
40
|
+
Beer.habtm_to_fixture
|
41
|
+
assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers_drunkards.yml'))
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_to_skeleton
|
45
|
+
Beer.to_skeleton
|
46
|
+
assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers.yml'))
|
47
|
+
end
|
48
|
+
|
49
|
+
def teardown
|
50
|
+
%w(db test).each { |dir| rm_rf File.join(RAILS_ROOT, dir) }
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
sqlite:
|
2
|
+
:adapter: sqlite
|
3
|
+
:dbfile: plugin.sqlite.db
|
4
|
+
sqlite3:
|
5
|
+
:adapter: sqlite3
|
6
|
+
:dbfile: ":memory:"
|
7
|
+
postgresql:
|
8
|
+
:adapter: postgresql
|
9
|
+
:username: postgres
|
10
|
+
:password: postgres
|
11
|
+
:database: plugin_test
|
12
|
+
:min_messages: ERROR
|
13
|
+
mysql:
|
14
|
+
:adapter: mysql
|
15
|
+
:host: localhost
|
16
|
+
:username: rails
|
17
|
+
:password:
|
18
|
+
:database: plugin_test
|
data/test/schema.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
|
3
|
+
create_table :beers do |t|
|
4
|
+
t.column :name, :string
|
5
|
+
t.column :rating, :integer
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table :glasses do |t|
|
9
|
+
t.column :name, :string
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :drunkards do |t|
|
13
|
+
t.column :name, :string
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :beers_drunkards, :id => false do |t|
|
17
|
+
t.column :beer_id, :integer
|
18
|
+
t.column :drunkard_id, :integer
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
RAILS_ROOT = File.dirname(__FILE__)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/fixtures'
|
8
|
+
require 'active_support/binding_of_caller'
|
9
|
+
require 'active_support/breakpoint'
|
10
|
+
require "#{File.dirname(__FILE__)}/../init"
|
11
|
+
|
12
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
13
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
14
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
|
15
|
+
|
16
|
+
load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
|
17
|
+
|
18
|
+
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
19
|
+
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
|
20
|
+
|
21
|
+
class Test::Unit::TestCase #:nodoc:
|
22
|
+
def create_fixtures(*table_names)
|
23
|
+
if block_given?
|
24
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
|
25
|
+
else
|
26
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Turn off transactional fixtures if you're working with MyISAM tables in MySQL
|
31
|
+
self.use_transactional_fixtures = true
|
32
|
+
|
33
|
+
# Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
|
34
|
+
self.use_instantiated_fixtures = false
|
35
|
+
|
36
|
+
# Add more helper methods to be used by all tests here...
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: ar_fixtures
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.4
|
7
|
+
date: 2007-06-24 00:00:00 -07:00
|
8
|
+
summary: Creates test fixtures from data in the database.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: boss AT topfunky.com
|
12
|
+
homepage: http://rubyforge.org/projects/seattlerb
|
13
|
+
rubyforge_project: seattlerb
|
14
|
+
description: Creates test fixtures from data in the database.
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Geoffrey Grosenbach
|
31
|
+
files:
|
32
|
+
- CHANGELOG
|
33
|
+
- History.txt
|
34
|
+
- MIT-LICENSE
|
35
|
+
- Manifest.txt
|
36
|
+
- README.txt
|
37
|
+
- Rakefile
|
38
|
+
- about.yml
|
39
|
+
- init.rb
|
40
|
+
- lib/ar_fixtures.rb
|
41
|
+
- tasks/ar_fixtures.rake
|
42
|
+
- test/ar_fixtures_test.rb
|
43
|
+
- test/database.yml
|
44
|
+
- test/fixtures/beer.rb
|
45
|
+
- test/fixtures/beers.yml
|
46
|
+
- test/fixtures/beers_drunkards.yml
|
47
|
+
- test/fixtures/drunkard.rb
|
48
|
+
- test/fixtures/drunkards.yml
|
49
|
+
- test/fixtures/glass.rb
|
50
|
+
- test/fixtures/glasses.yml
|
51
|
+
- test/schema.rb
|
52
|
+
- test/test_helper.rb
|
53
|
+
test_files:
|
54
|
+
- test/test_helper.rb
|
55
|
+
rdoc_options:
|
56
|
+
- --main
|
57
|
+
- README.txt
|
58
|
+
extra_rdoc_files:
|
59
|
+
- History.txt
|
60
|
+
- Manifest.txt
|
61
|
+
- README.txt
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
dependencies:
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: hoe
|
71
|
+
version_requirement:
|
72
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.2.1
|
77
|
+
version:
|