fixture_background 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fixture_background/background.rb +21 -5
- data/lib/fixture_background/generator.rb +4 -10
- data/lib/fixture_background/version.rb +1 -1
- data/lib/fixture_background.rb +1 -1
- data/lib/mini_test/test_case.rb +26 -0
- data/test/rails3/Gemfile.lock +1 -1
- data/test/rails3/app/models/post.rb +2 -0
- data/test/rails3/db/migrate/20110406091431_create_posts.rb +13 -0
- data/test/rails3/db/schema.rb +7 -1
- data/test/rails3/test/test_helper.rb +1 -1
- data/test/rails3/test/unit/person_test.rb +23 -6
- metadata +8 -3
@@ -1,4 +1,13 @@
|
|
1
1
|
module FixtureBackground
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def clean_database!
|
5
|
+
(ActiveRecord::Base.connection.tables - ["schema_migrations"]).each do |table_name|
|
6
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
2
11
|
class Background
|
3
12
|
|
4
13
|
class << self
|
@@ -20,14 +29,21 @@ module FixtureBackground
|
|
20
29
|
klass.class_eval <<-EOT
|
21
30
|
cattr_accessor :fixture_background
|
22
31
|
@@background_generated = false
|
32
|
+
@@fixtures_enabled = false
|
23
33
|
|
24
34
|
def initialize(*args)
|
25
35
|
super
|
26
36
|
|
27
|
-
if
|
28
|
-
|
29
|
-
|
30
|
-
|
37
|
+
if background = fixture_background
|
38
|
+
if !@@background_generated
|
39
|
+
background.generate!
|
40
|
+
@@background_generated = true
|
41
|
+
end
|
42
|
+
if !@@fixtures_enabled
|
43
|
+
self.class.fixtures :all
|
44
|
+
self.class.teardown_suite { FixtureBackground.clean_database! }
|
45
|
+
@@fixtures_enabled = true
|
46
|
+
end
|
31
47
|
end
|
32
48
|
end
|
33
49
|
EOT
|
@@ -53,7 +69,7 @@ module FixtureBackground
|
|
53
69
|
@full_class_name = full_class_name
|
54
70
|
@parent = parent
|
55
71
|
@background_block = blk
|
56
|
-
|
72
|
+
FixtureBackground.clean_database!
|
57
73
|
@generator = Generator.new(@full_class_name, background_signature, fixture_path, ancestors_and_own_background_blocks, @test_unit_class) unless background_valid?
|
58
74
|
end
|
59
75
|
|
@@ -9,14 +9,6 @@ module FixtureBackground
|
|
9
9
|
create_background_dir
|
10
10
|
|
11
11
|
transaction_with_rollback do
|
12
|
-
(ActiveRecord::Base.connection.tables - ["schema_migrations"]).each do |table_name|
|
13
|
-
begin
|
14
|
-
klass = table_name.classify.constantize
|
15
|
-
klass.delete_all
|
16
|
-
rescue NameError # do not die on tables that have no model
|
17
|
-
puts "cannot delete from #{table_name} as no model exists"
|
18
|
-
end
|
19
|
-
end
|
20
12
|
|
21
13
|
bm = Benchmark.realtime do
|
22
14
|
dump_ivars do |klass|
|
@@ -39,6 +31,7 @@ module FixtureBackground
|
|
39
31
|
end
|
40
32
|
|
41
33
|
private
|
34
|
+
|
42
35
|
def transaction_with_rollback
|
43
36
|
ActiveRecord::Base.connection.increment_open_transactions
|
44
37
|
ActiveRecord::Base.connection.begin_db_transaction
|
@@ -77,9 +70,10 @@ module FixtureBackground
|
|
77
70
|
(ActiveRecord::Base.connection.tables - ["schema_migrations"]).each do |table_name|
|
78
71
|
begin
|
79
72
|
klass = table_name.classify.constantize
|
80
|
-
|
73
|
+
|
81
74
|
records = klass.all
|
82
|
-
|
75
|
+
next if records.empty?
|
76
|
+
|
83
77
|
fixtures = {}
|
84
78
|
records.each do |record|
|
85
79
|
fixtures[table_name + record.id.to_s] = record.instance_variable_get(:@attributes)
|
data/lib/fixture_background.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
class ArrayWithTeardownSuiteCallback < Array
|
2
|
+
|
3
|
+
def each
|
4
|
+
super do |suite|
|
5
|
+
yield suite
|
6
|
+
suite.teardown_suite_blocks.each(&:call) if suite.teardown_suite_blocks
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
class_inheritable_accessor :teardown_suite_blocks
|
14
|
+
|
15
|
+
class << self
|
16
|
+
alias_method :test_suites_without_teardown, :test_suites
|
17
|
+
|
18
|
+
def test_suites
|
19
|
+
ArrayWithTeardownSuiteCallback.new(test_suites_without_teardown)
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown_suite(&block)
|
23
|
+
(self.teardown_suite_blocks ||= []) << block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/rails3/Gemfile.lock
CHANGED
data/test/rails3/db/schema.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
#
|
11
11
|
# It's strongly recommended to check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema.define(:version =>
|
13
|
+
ActiveRecord::Schema.define(:version => 20110406091431) do
|
14
14
|
|
15
15
|
create_table "people", :force => true do |t|
|
16
16
|
t.string "name"
|
@@ -18,4 +18,10 @@ ActiveRecord::Schema.define(:version => 20110127093735) do
|
|
18
18
|
t.datetime "updated_at"
|
19
19
|
end
|
20
20
|
|
21
|
+
create_table "posts", :force => true do |t|
|
22
|
+
t.string "title"
|
23
|
+
t.datetime "created_at"
|
24
|
+
t.datetime "updated_at"
|
25
|
+
end
|
26
|
+
|
21
27
|
end
|
@@ -3,14 +3,25 @@ require 'test_helper'
|
|
3
3
|
class PersonTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
background do
|
6
|
-
|
6
|
+
test_helper_returning_one
|
7
7
|
instance_test_helper_defined_after_background_returning_one
|
8
8
|
@hase = Person.create(:name => "bunny")
|
9
9
|
end
|
10
10
|
|
11
|
+
should "without context" do
|
12
|
+
assert @hase
|
13
|
+
assert_nil @thies
|
14
|
+
assert_nil @manuel
|
15
|
+
assert_nil @norman
|
16
|
+
end
|
17
|
+
|
18
|
+
should "not create post.yml" do
|
19
|
+
assert !File.exist?(File.dirname(__FILE__) + '/../backgrounds/person_test/posts.yml')
|
20
|
+
end
|
21
|
+
|
11
22
|
context "with thies" do
|
12
23
|
background do
|
13
|
-
|
24
|
+
test_helper_returning_one
|
14
25
|
instance_test_helper_defined_after_background_returning_one
|
15
26
|
@thies = Person.create(:name => "thies")
|
16
27
|
end
|
@@ -59,9 +70,15 @@ class PersonTest < ActiveSupport::TestCase
|
|
59
70
|
assert_equal 2, Person.count
|
60
71
|
end
|
61
72
|
end
|
73
|
+
|
74
|
+
private
|
75
|
+
def instance_test_helper_defined_after_background_returning_one
|
76
|
+
1
|
77
|
+
end
|
78
|
+
end
|
62
79
|
|
63
|
-
|
64
|
-
|
80
|
+
class ZZZEmptyDatabaseTest < ActiveSupport::TestCase
|
81
|
+
should "have a clean database" do
|
82
|
+
assert_equal 0, Person.count
|
65
83
|
end
|
66
|
-
|
67
|
-
end
|
84
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
version: 0.9.
|
8
|
+
- 7
|
9
|
+
version: 0.9.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Thies C. Arntzen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-06 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/fixture_background/ivars.rb
|
44
44
|
- lib/fixture_background/shoulda.rb
|
45
45
|
- lib/fixture_background/version.rb
|
46
|
+
- lib/mini_test/test_case.rb
|
46
47
|
- test/rails3/.gitignore
|
47
48
|
- test/rails3/Gemfile
|
48
49
|
- test/rails3/Gemfile.lock
|
@@ -54,6 +55,7 @@ files:
|
|
54
55
|
- test/rails3/app/helpers/application_helper.rb
|
55
56
|
- test/rails3/app/helpers/people_helper.rb
|
56
57
|
- test/rails3/app/models/person.rb
|
58
|
+
- test/rails3/app/models/post.rb
|
57
59
|
- test/rails3/app/views/layouts/application.html.erb
|
58
60
|
- test/rails3/app/views/people/_form.html.erb
|
59
61
|
- test/rails3/app/views/people/edit.html.erb
|
@@ -76,6 +78,7 @@ files:
|
|
76
78
|
- test/rails3/config/locales/en.yml
|
77
79
|
- test/rails3/config/routes.rb
|
78
80
|
- test/rails3/db/migrate/20110127093735_create_people.rb
|
81
|
+
- test/rails3/db/migrate/20110406091431_create_posts.rb
|
79
82
|
- test/rails3/db/schema.rb
|
80
83
|
- test/rails3/db/seeds.rb
|
81
84
|
- test/rails3/doc/README_FOR_APP
|
@@ -147,6 +150,7 @@ test_files:
|
|
147
150
|
- test/rails3/app/helpers/application_helper.rb
|
148
151
|
- test/rails3/app/helpers/people_helper.rb
|
149
152
|
- test/rails3/app/models/person.rb
|
153
|
+
- test/rails3/app/models/post.rb
|
150
154
|
- test/rails3/app/views/layouts/application.html.erb
|
151
155
|
- test/rails3/app/views/people/_form.html.erb
|
152
156
|
- test/rails3/app/views/people/edit.html.erb
|
@@ -169,6 +173,7 @@ test_files:
|
|
169
173
|
- test/rails3/config/locales/en.yml
|
170
174
|
- test/rails3/config/routes.rb
|
171
175
|
- test/rails3/db/migrate/20110127093735_create_people.rb
|
176
|
+
- test/rails3/db/migrate/20110406091431_create_posts.rb
|
172
177
|
- test/rails3/db/schema.rb
|
173
178
|
- test/rails3/db/seeds.rb
|
174
179
|
- test/rails3/doc/README_FOR_APP
|