topfunky-ar_fixtures 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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,13 @@
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
+ p.test_globs = ["test/*_test.rb"]
13
+ 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'
@@ -0,0 +1,114 @@
1
+ require 'erb'
2
+
3
+ # Extension to make it easy to read and write data to a file.
4
+ class ActiveRecord::Base
5
+
6
+ class << self
7
+
8
+ # Writes content of this table to db/table_name.yml, or the specified file.
9
+ #
10
+ # Writes all content by default, but can be limited.
11
+ def dump_to_file(path=nil, limit=nil)
12
+ opts = {}
13
+ opts[:limit] = limit if limit
14
+ path ||= "db/#{table_name}.yml"
15
+ write_file(File.expand_path(path, RAILS_ROOT), self.find(:all, opts).to_yaml)
16
+ end
17
+
18
+ ##
19
+ # Delete existing data in database and load fresh from file in db/table_name.yml
20
+ #
21
+ # ERB tags are also possible, as with the rest of Rails fixtures.
22
+ def load_from_file(path=nil)
23
+ path ||= "db/#{table_name}.yml"
24
+
25
+ self.destroy_all
26
+
27
+ if connection.respond_to?(:reset_pk_sequence!)
28
+ connection.reset_pk_sequence!(table_name)
29
+ end
30
+
31
+ rawdata = File.read(File.expand_path(path, RAILS_ROOT))
32
+ erb_data = ERB.new(rawdata).result
33
+ records = YAML::load( erb_data )
34
+
35
+ records.each do |record|
36
+ puts "______________"
37
+ puts record.to_yaml
38
+ puts "______________"
39
+ record_copy = self.new(record.attributes)
40
+ record_copy.id = record.id
41
+
42
+ # For Single Table Inheritance
43
+ klass_col = record.class.inheritance_column.to_sym
44
+ if record[klass_col]
45
+ record_copy.type = record[klass_col]
46
+ end
47
+
48
+ record_copy.save
49
+ end
50
+
51
+ if connection.respond_to?(:reset_pk_sequence!)
52
+ connection.reset_pk_sequence!(table_name)
53
+ end
54
+ end
55
+
56
+ # Write a file that can be loaded with +fixture :some_table+ in tests.
57
+ # Uses existing data in the database.
58
+ #
59
+ # Will be written to +test/fixtures/table_name.yml+. Can be restricted to some number of rows.
60
+ def to_fixture(limit=nil)
61
+ opts = {}
62
+ opts[:limit] = limit if limit
63
+
64
+ write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
65
+ self.find(:all, opts).inject({}) { |hsh, record|
66
+ hsh.merge("#{table_name.singularize}_#{'%05i' % record.id rescue record.id}" => record.attributes)
67
+ }.to_yaml(:SortKeys => true))
68
+ habtm_to_fixture
69
+ end
70
+
71
+ # Write the habtm association table
72
+ def habtm_to_fixture
73
+ joins = self.reflect_on_all_associations.select { |j|
74
+ j.macro == :has_and_belongs_to_many
75
+ }
76
+ joins.each do |join|
77
+ hsh = {}
78
+ connection.select_all("SELECT * FROM #{join.options[:join_table]}").each_with_index { |record, i|
79
+ hsh["join_#{'%05i' % i}"] = record
80
+ }
81
+ write_file(File.expand_path("test/fixtures/#{join.options[:join_table]}.yml", RAILS_ROOT), hsh.to_yaml(:SortKeys => true))
82
+ end
83
+ end
84
+
85
+ # Generates a basic fixture file in test/fixtures that lists the table's field names.
86
+ #
87
+ # You can use it as a starting point for your own fixtures.
88
+ #
89
+ # record_1:
90
+ # name:
91
+ # rating:
92
+ # record_2:
93
+ # name:
94
+ # rating:
95
+ #
96
+ # TODO Automatically add :id field if there is one.
97
+ def to_skeleton
98
+ record = {
99
+ "record_1" => self.new.attributes,
100
+ "record_2" => self.new.attributes
101
+ }
102
+ write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
103
+ record.to_yaml)
104
+ end
105
+
106
+ def write_file(path, content) # :nodoc:
107
+ f = File.new(path, "w+")
108
+ f.puts content
109
+ f.close
110
+ end
111
+
112
+ end
113
+
114
+ 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,70 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'fileutils'
3
+
4
+ require 'beer'
5
+ require 'drunkard'
6
+ require 'glass'
7
+
8
+ # TODO Test limit params
9
+ # TODO Test renaming
10
+ # TODO Examine contents of fixture and skeleton dump
11
+ class ArFixturesTest < Test::Unit::TestCase
12
+ fixtures :beers, :drunkards, :beers_drunkards, :glasses
13
+ include FileUtils
14
+
15
+ def setup
16
+ %w(db test/fixtures).each { |dir| mkdir_p File.join(RAILS_ROOT, dir) }
17
+ end
18
+
19
+ def test_dump_to_file
20
+ %w(pilsner tripel).each {|name| Beer.create(:name => name) }
21
+
22
+ assert_equal 2, Beer.count
23
+ Beer.dump_to_file
24
+ assert File.exist?(File.join(RAILS_ROOT, 'db', 'beers.yml'))
25
+
26
+ Beer.destroy_all
27
+ assert_equal 0, Beer.count
28
+ Beer.load_from_file
29
+ assert_equal 2, Beer.count, "#{Beer.find(:all).to_yaml}"
30
+ end
31
+
32
+ def test_load_from_file
33
+ cp File.join(RAILS_ROOT, 'fixtures', 'glasses.yml'),
34
+ File.join(RAILS_ROOT, 'db', 'glasses.yml')
35
+ assert_equal 0, Glass.count
36
+ Glass.load_from_file
37
+ assert_equal 2, Glass.count
38
+ end
39
+
40
+ def test_to_fixture
41
+ Beer.to_fixture
42
+ assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers.yml'))
43
+ assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers_drunkards.yml'))
44
+ end
45
+
46
+ def test_habtm_to_fixture
47
+ Beer.habtm_to_fixture
48
+ assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers_drunkards.yml'))
49
+ end
50
+
51
+ def test_to_skeleton
52
+ Beer.to_skeleton
53
+ assert File.exist?(File.join(RAILS_ROOT, 'test', 'fixtures', 'beers.yml'))
54
+ end
55
+
56
+ def test_should_use_erb
57
+ Beer.delete_all
58
+
59
+ cp File.join(RAILS_ROOT, 'fixtures', 'beers.yml'),
60
+ File.join(RAILS_ROOT, 'db', 'beers.yml')
61
+ assert_equal 0, Beer.count
62
+ Beer.load_from_file
63
+ assert_equal 2, Beer.count
64
+ end
65
+
66
+ def teardown
67
+ %w(db test).each { |dir| rm_rf File.join(RAILS_ROOT, dir) }
68
+ end
69
+
70
+ 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
@@ -0,0 +1,5 @@
1
+ class Beer < ActiveRecord::Base
2
+
3
+ has_and_belongs_to_many :drunkards
4
+
5
+ end
@@ -0,0 +1,10 @@
1
+ stout:
2
+ id: 1
3
+ name: stout
4
+ rating: 5
5
+ sipped_at: <%= Time.now %>
6
+ ipa:
7
+ id: 2
8
+ name: india pale ale
9
+ rating: 4
10
+ sipped_at: <%= Time.now - 60 %>
@@ -0,0 +1,8 @@
1
+ one:
2
+ beer_id: 1
3
+ drunkard_id: 1
4
+
5
+ two:
6
+ beer_id: 2
7
+ drunkard_id: 1
8
+
@@ -0,0 +1,6 @@
1
+
2
+ class Drunkard < ActiveRecord::Base
3
+
4
+ has_and_belongs_to_many :beers
5
+
6
+ end
@@ -0,0 +1,8 @@
1
+ bert:
2
+ id: 1
3
+ name: bert
4
+
5
+ ernie:
6
+ id: 2
7
+ name: ernie
8
+
@@ -0,0 +1,2 @@
1
+ class Glass < ActiveRecord::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ - !ruby/object:Glass
3
+ attributes:
4
+ name: pilsner
5
+ id: "1"
6
+ - !ruby/object:Glass
7
+ attributes:
8
+ name: snifter
9
+ id: "2"
data/test/schema.rb ADDED
@@ -0,0 +1,22 @@
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
+ t.column :sipped_at, :datetime
7
+ end
8
+
9
+ create_table :glasses do |t|
10
+ t.column :name, :string
11
+ end
12
+
13
+ create_table :drunkards do |t|
14
+ t.column :name, :string
15
+ end
16
+
17
+ create_table :beers_drunkards, :id => false do |t|
18
+ t.column :beer_id, :integer
19
+ t.column :drunkard_id, :integer
20
+ end
21
+
22
+ end
@@ -0,0 +1,35 @@
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 "#{File.dirname(__FILE__)}/../init"
9
+
10
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
11
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
12
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
13
+
14
+ load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
15
+
16
+ Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
17
+ $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
18
+
19
+ class Test::Unit::TestCase #:nodoc:
20
+ def create_fixtures(*table_names)
21
+ if block_given?
22
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
23
+ else
24
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
25
+ end
26
+ end
27
+
28
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
29
+ self.use_transactional_fixtures = true
30
+
31
+ # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
32
+ self.use_instantiated_fixtures = false
33
+
34
+ # Add more helper methods to be used by all tests here...
35
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: topfunky-ar_fixtures
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Geoffrey Grosenbach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.3
23
+ version:
24
+ description: Creates test fixtures from data in the database.
25
+ email: boss AT topfunky.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - History.txt
32
+ - Manifest.txt
33
+ - README.txt
34
+ files:
35
+ - CHANGELOG
36
+ - History.txt
37
+ - MIT-LICENSE
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - about.yml
42
+ - init.rb
43
+ - lib/ar_fixtures.rb
44
+ - tasks/ar_fixtures.rake
45
+ - test/ar_fixtures_test.rb
46
+ - test/database.yml
47
+ - test/fixtures/beer.rb
48
+ - test/fixtures/beers.yml
49
+ - test/fixtures/beers_drunkards.yml
50
+ - test/fixtures/drunkard.rb
51
+ - test/fixtures/drunkards.yml
52
+ - test/fixtures/glass.rb
53
+ - test/fixtures/glasses.yml
54
+ - test/schema.rb
55
+ - test/test_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://rubyforge.org/projects/seattlerb
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --main
61
+ - README.txt
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project: seattlerb
79
+ rubygems_version: 1.0.1
80
+ signing_key:
81
+ specification_version: 2
82
+ summary: Creates test fixtures from data in the database.
83
+ test_files:
84
+ - test/ar_fixtures_test.rb