fixturease 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +12 -1
  2. data/bin/fixturease.rb +31 -2
  3. data/spec/fixturease_spec.rb +98 -21
  4. metadata +2 -2
data/Rakefile CHANGED
@@ -1,7 +1,13 @@
1
1
  require 'rake/gempackagetask'
2
2
  require 'rubygems'
3
+ require 'spec/rake/spectask'
4
+ require 'rake/rdoctask'
3
5
 
4
- PKG_VERSION="0.2.3"
6
+ desc 'Default: run unit tests.'
7
+ task :default => :spec
8
+
9
+
10
+ PKG_VERSION="0.2.4"
5
11
  PKG_FILES=["bin/fixturease.rb","Rakefile","spec/database.yml","spec/fixturease_spec.rb"]
6
12
 
7
13
  spec = Gem::Specification.new do |s|
@@ -24,4 +30,9 @@ end
24
30
  Rake::GemPackageTask.new(spec) do |pkg|
25
31
  pkg.need_zip = true
26
32
  pkg.need_tar = true
33
+ end
34
+
35
+ desc "Run all specifications"
36
+ Spec::Rake::SpecTask.new('spec') do |t|
37
+ t.spec_files = FileList['spec/*.rb']
27
38
  end
data/bin/fixturease.rb CHANGED
@@ -1,13 +1,36 @@
1
1
  #! /usr/bin/env ruby
2
2
  unless $0.match /^irb(\.bat|\.cmd)?$/
3
+ ENV['LOAD_FIXTURES'] = 'yes' if ARGV.first == "-l"
3
4
  exec "irb -I #{File.dirname(__FILE__)} -r fixturease --simple-prompt"
4
5
  else
5
6
  ENV['RAILS_ENV'] ||= 'test'
6
7
  ENV['RAILS_ROOT'] ||= "."
7
8
  ENV['FIXTURES_ROOT'] ||= "spec/fixtures"
8
9
  puts "Loading Fixturease console for #{ENV['RAILS_ENV']} mode."
9
- require ENV['RAILS_ROOT'] + '/config/boot'
10
10
  require ENV['RAILS_ROOT'] + '/config/environment'
11
+
12
+ $reload_fixtures = false
13
+
14
+ def load_fixtures!
15
+ $reload_fixtures = true
16
+ require 'active_record/fixtures'
17
+ Dir[ENV['RAILS_ROOT']+'/'+ENV['FIXTURES_ROOT']+'/*.yml'].each do |fixture_file|
18
+ Fixtures.create_fixtures(ENV['RAILS_ROOT']+'/'+ENV['FIXTURES_ROOT'], File.basename(fixture_file,".yml"))
19
+ model = File.basename(fixture_file,".yml").singularize.camelize.constantize
20
+ model.find(:all).each {|o| o.save_for_fixturease }
21
+ YAML::load_file(fixture_file).each_pair do |name,obj|
22
+ case name
23
+ when /^#{File.basename(fixture_file,".yml")}$/
24
+ # unnamed, skip
25
+ else
26
+ # if named
27
+ instance_variable_set("@#{name}",model.find(obj["id"]))
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+
11
34
  class ActiveRecord::Base
12
35
  alias_method :save_orig, :save
13
36
  @@fixtures = {}
@@ -18,12 +41,18 @@ else
18
41
  end
19
42
  end
20
43
 
44
+ def save_for_fixturease
45
+ @@fixtures[self.class] ||= {}
46
+ @@fixtures[self.class][self.id] = self unless self.new_record?
47
+ end
48
+
21
49
  def self.fixturease_fixtures
22
50
  @@fixtures
23
51
  end
24
52
  end
25
53
  ActiveRecord::Base.send :increment_open_transactions
26
54
  ActiveRecord::Base.connection.begin_db_transaction
55
+ load_fixtures! if ENV['LOAD_FIXTURES']=='yes'
27
56
  at_exit do
28
57
  vars = {}
29
58
  instance_variables.each {|varname| vars[instance_variable_get(varname)]=varname.gsub(/^@/,"") if instance_variable_get(varname).is_a? ActiveRecord::Base}
@@ -36,7 +65,7 @@ else
36
65
  v = {}
37
66
  val.each_pair {|rec_id,obj| v[rec_id] = obj if k.exists?(rec_id) }
38
67
  puts v.keys.collect{|rec_id| vars[k.find(rec_id)] || rec_id}.to_sentence
39
- f = File.new("#{filename}","a")
68
+ f = File.new("#{filename}",$reload_fixtures ? "w" : "a")
40
69
  f.write "\n\n# Fixturease (#{Time.now}):\n\n"
41
70
  f.write v.values.inject({}) { |hsh, record| hsh.merge(vars[record] || "#{k.table_name}_#{record.id}" => record.attributes) }.to_yaml(:SortKeys => true).gsub(/^---.*\n/,"")
42
71
  f.write "\n# End Fixturease (#{Time.now})\n\n"
@@ -10,6 +10,7 @@ context "Fixturease" do
10
10
  FileUtils.cp File.dirname(__FILE__)+'/database.yml','testfixtureaseproject/config/database.yml'
11
11
  `cd testfixtureaseproject && ./script/generate model TestModel title:string`
12
12
  `cd testfixtureaseproject && rake db:migrate RAILS_ENV=test`
13
+ cleanup_fixtures
13
14
  end
14
15
 
15
16
  teardown do
@@ -25,8 +26,7 @@ context "Fixturease" do
25
26
  end
26
27
 
27
28
 
28
- specify "should create simple model (without columns)" do
29
- `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
29
+ specify "should create empty TestModel" do
30
30
  run_fixturease do |f|
31
31
  f.puts "TestModel.create"
32
32
  f.puts "exit"
@@ -38,8 +38,7 @@ context "Fixturease" do
38
38
  yaml[key]["id"].should_not_be_nil
39
39
  end
40
40
 
41
- specify "should create model (with title)" do
42
- `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
41
+ specify "should create TestModel :title => " do
43
42
  run_fixturease do |f|
44
43
  f.puts "TestModel.create :title => 'test title'"
45
44
  f.puts "exit"
@@ -52,7 +51,6 @@ context "Fixturease" do
52
51
  end
53
52
 
54
53
  specify "should not create destroyed model" do
55
- `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
56
54
  run_fixturease do |f|
57
55
  f.puts "@test1 = TestModel.create :title => 'test title'"
58
56
  f.puts "@test2 = TestModel.create :title => 'test title'"
@@ -65,19 +63,7 @@ context "Fixturease" do
65
63
  yaml.keys.should_have(0).grep(/test2/)
66
64
  end
67
65
 
68
- specify "should ignore non-ActiveRecord instance variables" do
69
- `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
70
- run_fixturease do |f|
71
- f.puts "@test1 = 1"
72
- f.puts "exit"
73
- f.readline until f.eof?
74
- end
75
- yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
76
- yaml.should == false
77
- end
78
-
79
66
  specify "should not create unsaved model" do
80
- `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
81
67
  run_fixturease do |f|
82
68
  f.puts "@test1 = TestModel.new :title => 'test title'"
83
69
  f.puts "exit"
@@ -87,8 +73,17 @@ context "Fixturease" do
87
73
  yaml.should == false
88
74
  end
89
75
 
90
- specify "should create named model" do
91
- `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
76
+ specify "should ignore non-ActiveRecord instance variables" do
77
+ run_fixturease do |f|
78
+ f.puts "@test1 = 1"
79
+ f.puts "exit"
80
+ f.readline until f.eof?
81
+ end
82
+ yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
83
+ yaml.should == false
84
+ end
85
+
86
+ specify "should create named model according to instance variable name" do
92
87
  run_fixturease do |f|
93
88
  f.puts "@test = TestModel.create :title => 'test title'"
94
89
  f.puts "exit"
@@ -100,14 +95,96 @@ context "Fixturease" do
100
95
  yaml[key]["title"].should == 'test title'
101
96
  end
102
97
 
98
+ specify "should rewrite fixtures from scratch if load_fixtures!" do
99
+ run_fixturease do |f|
100
+ f.puts "@test = TestModel.create :title => 'test title'"
101
+ f.puts "exit"
102
+ f.readline until f.eof?
103
+ end
104
+ original_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
105
+ original_mtime = File.mtime("testfixtureaseproject/test/fixtures/test_models.yml")
106
+ run_fixturease do |f|
107
+ f.puts "load_fixtures!"
108
+ f.puts "exit"
109
+ f.readline until f.eof?
110
+ end
111
+ File.mtime("testfixtureaseproject/test/fixtures/test_models.yml").should > original_mtime
112
+ new_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
113
+ new_yaml.should == original_yaml
114
+ end
115
+
116
+ specify "should rewrite fixtures from scratch if -l command line option" do
117
+ run_fixturease do |f|
118
+ f.puts "@test = TestModel.create :title => 'test title'"
119
+ f.puts "exit"
120
+ f.readline until f.eof?
121
+ end
122
+ original_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
123
+ original_mtime = File.mtime("testfixtureaseproject/test/fixtures/test_models.yml")
124
+ run_fixturease("-l") do |f|
125
+ f.puts "exit"
126
+ f.readline until f.eof?
127
+ end
128
+ File.mtime("testfixtureaseproject/test/fixtures/test_models.yml").should > original_mtime
129
+ new_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
130
+ new_yaml.should == original_yaml
131
+ end
132
+
133
+ specify "should rewrite fixtures from scratch with altered models if load_fixtures!" do
134
+ run_fixturease do |f|
135
+ f.puts "@test = TestModel.create :title => 'test title'"
136
+ f.puts "exit"
137
+ f.readline until f.eof?
138
+ end
139
+ original_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
140
+ run_fixturease do |f|
141
+ f.puts "load_fixtures!"
142
+ f.puts "@test = TestModel.find(#{original_yaml['test']['id']})"
143
+ f.puts "@test.title = 'new title'"
144
+ f.puts "@test.save"
145
+ f.puts "exit"
146
+ f.readline until f.eof?
147
+ end
148
+ new_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
149
+ new_yaml.should_not == original_yaml
150
+ new_yaml.keys.should_have(1).grep(/^test$/)
151
+ new_yaml['test']['title'].should == 'new title'
152
+ end
153
+
154
+ specify "should rewrite fixtures from scratch with altered models if load_fixtures! and preserve name" do
155
+ run_fixturease do |f|
156
+ f.puts "@test = TestModel.create :title => 'test title'"
157
+ f.puts "exit"
158
+ f.readline until f.eof?
159
+ end
160
+ original_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
161
+ run_fixturease do |f|
162
+ f.puts "load_fixtures!"
163
+ f.puts "@test1 = TestModel.find(#{original_yaml['test']['id']})"
164
+ f.puts "@test1.title = 'new title'"
165
+ f.puts "@test1.save"
166
+ f.puts "exit"
167
+ f.readline until f.eof?
168
+ end
169
+ new_yaml = YAML::load_file("testfixtureaseproject/test/fixtures/test_models.yml")
170
+ new_yaml.should_not == original_yaml
171
+ new_yaml.keys.should_have(1).grep(/^test$/)
172
+ new_yaml.keys.should_have(0).grep(/^tes1$/)
173
+ new_yaml['test']['title'].should == 'new title'
174
+ end
175
+
103
176
 
104
177
  private
105
178
 
106
- def run_fixturease(&block)
179
+ def cleanup_fixtures()
180
+ `echo -n > testfixtureaseproject/test/fixtures/test_models.yml`
181
+ end
182
+
183
+ def run_fixturease(stropt=nil,&block)
107
184
  ENV['RAILS_ROOT']='testfixtureaseproject'
108
185
  ENV['FIXTURES_ROOT']='test/fixtures'
109
186
  ENV['RAILS_ENV']='test'
110
- @io = IO.popen "#{File.dirname(__FILE__)}/../bin/fixturease.rb", "r+", &block
187
+ @io = IO.popen "#{File.dirname(__FILE__)}/../bin/fixturease.rb #{stropt}", "r+", &block
111
188
  end
112
189
 
113
190
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: fixturease
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.3
7
- date: 2007-01-19 00:00:00 +02:00
6
+ version: 0.2.4
7
+ date: 2007-01-20 00:00:00 +02:00
8
8
  summary: Easy fixture creation tool
9
9
  require_paths:
10
10
  - lib