test_dummy 0.4.0 → 0.5.0
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.
- checksums.yaml +7 -0
- data/Gemfile +4 -2
- data/README.md +3 -1
- data/Rakefile +7 -4
- data/VERSION +1 -1
- data/lib/test_dummy.rb +55 -306
- data/lib/test_dummy/definition.rb +113 -0
- data/lib/test_dummy/loader.rb +41 -0
- data/lib/test_dummy/operation.rb +364 -0
- data/lib/test_dummy/railtie.rb +15 -6
- data/lib/test_dummy/support.rb +1 -1
- data/lib/test_dummy/test_helper.rb +16 -5
- data/test/db/migrate/0002_create_accounts.rb +3 -0
- data/test/db/migrate/0005_create_users.rb +12 -0
- data/test/dummy/account.rb +9 -1
- data/test/dummy/bill.rb +6 -0
- data/test/dummy/broken.rb +1 -0
- data/test/dummy/user.rb +8 -0
- data/test/dummy/user/admin.rb +5 -0
- data/test/helper.rb +26 -7
- data/test/models/broken.rb +3 -0
- data/test/models/user.rb +45 -0
- data/test/models/user/admin.rb +22 -0
- data/test/unit/test_account.rb +78 -2
- data/test/unit/test_bill.rb +28 -4
- data/test/unit/test_definition.rb +59 -0
- data/test/unit/test_item.rb +13 -2
- data/test/unit/test_loader.rb +23 -0
- data/test/unit/test_operation.rb +570 -0
- data/test/unit/test_support.rb +10 -0
- data/test/unit/test_test_dummy.rb +8 -2
- data/test/unit/test_test_helper.rb +22 -0
- data/test/unit/test_user.rb +18 -0
- data/test_dummy.gemspec +29 -7
- metadata +58 -22
data/lib/test_dummy/railtie.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative '../test_dummy'
|
2
2
|
|
3
3
|
case (Rails::VERSION::MAJOR)
|
4
4
|
when 2
|
@@ -14,16 +14,25 @@ when 2
|
|
14
14
|
else
|
15
15
|
class TestDummy::Railtie < Rails::Railtie
|
16
16
|
def self.apply!
|
17
|
-
if (defined?(ActiveRecord))
|
17
|
+
if (defined?(ActiveRecord) and defined?(ActiveRecord::Base))
|
18
18
|
ActiveRecord::Base.send(:include, TestDummy)
|
19
19
|
end
|
20
20
|
|
21
|
-
ActiveSupport::TestCase
|
22
|
-
|
21
|
+
if (defined?(ActiveSupport) and defined?(ActiveSupport::TestCase))
|
22
|
+
ActiveSupport::TestCase.send(:include, TestDummy::TestHelper)
|
23
|
+
end
|
24
|
+
|
25
|
+
if (defined?(Test) and defined?(Test::Unit))
|
26
|
+
Test::Unit::TestCase.send(:include, TestDummy::TestHelper)
|
27
|
+
end
|
28
|
+
|
29
|
+
if (defined?(MiniTest) and defined?(MiniTest::Unit))
|
30
|
+
MiniTest::Unit::TestCase.send(:include, TestDummy::TestHelper)
|
31
|
+
end
|
23
32
|
end
|
24
33
|
|
25
|
-
config.
|
26
|
-
apply!
|
34
|
+
config.to_prepare do
|
35
|
+
TestDummy::Railtie.apply!
|
27
36
|
end
|
28
37
|
end
|
29
38
|
end
|
data/lib/test_dummy/support.rb
CHANGED
@@ -33,7 +33,7 @@ module TestDummy::Support
|
|
33
33
|
elsif (model_class.respond_to?(:association_reflection) and reflection = model_class.association_reflection(attribute))
|
34
34
|
[
|
35
35
|
reflection[:associated_class],
|
36
|
-
reflection[:key] ||
|
36
|
+
(reflection[:key] || "#{attribute.to_s.underscore}_id").to_sym
|
37
37
|
]
|
38
38
|
end
|
39
39
|
end
|
@@ -1,14 +1,25 @@
|
|
1
1
|
module TestDummy::TestHelper
|
2
|
-
def dummy(scope,
|
3
|
-
|
2
|
+
def dummy(scope, *tags)
|
3
|
+
create_attributes =
|
4
|
+
case (tags.last)
|
5
|
+
when Hash
|
6
|
+
tags.pop
|
7
|
+
else
|
8
|
+
{ }
|
9
|
+
end
|
10
|
+
|
11
|
+
instance = scope.respond_to?(:build) ? scope.build(create_attributes) : scope.new(create_attributes)
|
4
12
|
|
5
13
|
if (block_given?)
|
6
14
|
yield(instance)
|
7
15
|
end
|
8
|
-
|
9
|
-
instance.
|
10
|
-
|
16
|
+
|
17
|
+
instance.class.dummy_definition.apply!(instance, create_attributes, tags)
|
18
|
+
|
11
19
|
instance.save!
|
20
|
+
|
21
|
+
instance.class.dummy_definition.apply_after_save!(instance, create_attributes, tags)
|
22
|
+
|
12
23
|
instance
|
13
24
|
end
|
14
25
|
alias_method :a, :dummy
|
data/test/dummy/account.rb
CHANGED
@@ -9,11 +9,19 @@ class Account
|
|
9
9
|
account.source = 'dummy'
|
10
10
|
end
|
11
11
|
|
12
|
+
dummy [ :field_a, :field_b ] do
|
13
|
+
TestDummy::Helper.random_string(8)
|
14
|
+
end
|
15
|
+
|
16
|
+
dummy :except => :closed do |m|
|
17
|
+
m.field_c = TestDummy::Helper.random_string(8)
|
18
|
+
end
|
19
|
+
|
12
20
|
dummy :activated_at, :except => :unactivated do
|
13
21
|
Time.now - rand(86400 * 365) - 86400
|
14
22
|
end
|
15
23
|
|
16
24
|
dummy :closed_at, :only => [ :closed ] do |m|
|
17
|
-
m.activated_at + rand(86400)
|
25
|
+
(m.activated_at || Time.now) + rand(86400)
|
18
26
|
end
|
19
27
|
end
|
data/test/dummy/bill.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
invalid ruby code here
|
data/test/dummy/user.rb
ADDED
data/test/helper.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
gem 'minitest'
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'minitest/unit'
|
7
|
+
|
8
|
+
gem 'protected_attributes'
|
9
|
+
require 'protected_attributes'
|
3
10
|
|
4
11
|
ENV['RAILS_ENV'] = 'test'
|
5
12
|
|
@@ -9,17 +16,26 @@ $LOAD_PATH.unshift(base_path)
|
|
9
16
|
$LOAD_PATH.unshift(File.join(base_path, '..', 'lib'))
|
10
17
|
$LOAD_PATH.unshift(File.join(base_path, '..', 'test', 'models'))
|
11
18
|
|
12
|
-
gem 'rails'
|
13
|
-
|
14
19
|
require 'rails'
|
15
20
|
require 'active_record'
|
16
21
|
|
17
22
|
require 'test_dummy'
|
18
23
|
|
24
|
+
require 'ostruct'
|
25
|
+
|
19
26
|
TestDummy::Railtie.apply!
|
20
27
|
|
21
|
-
class
|
28
|
+
class Minitest::Unit::TestCase
|
22
29
|
include TestDummy::TestHelper
|
30
|
+
|
31
|
+
def assert_created(model)
|
32
|
+
assert model, "Model should not be nil"
|
33
|
+
|
34
|
+
assert_equal [ ], model.errors.full_messages
|
35
|
+
assert_equal true, model.valid?, "Model is not valid."
|
36
|
+
|
37
|
+
assert_equal false, model.new_record?, "Model has not been saved."
|
38
|
+
end
|
23
39
|
end
|
24
40
|
|
25
41
|
class TestDummy::Application < Rails::Application
|
@@ -29,8 +45,8 @@ end
|
|
29
45
|
TestDummy.dummy_extensions_path = File.expand_path('dummy', File.dirname(__FILE__))
|
30
46
|
|
31
47
|
ActiveRecord::Base.establish_connection(
|
32
|
-
|
33
|
-
|
48
|
+
adapter: "sqlite3",
|
49
|
+
database: File.expand_path('db/test.sqlite3', base_path)
|
34
50
|
)
|
35
51
|
|
36
52
|
ActiveRecord::Migrator.migrate(File.expand_path('db/migrate', base_path))
|
@@ -40,6 +56,9 @@ ActiveSupport::Dependencies.autoload_paths << File.expand_path('models', base_pa
|
|
40
56
|
# Trigger loading the Rails root path here
|
41
57
|
Rails.root
|
42
58
|
|
59
|
+
# Example definitions that are added as extensions to the TestDummy::Helper
|
60
|
+
# module for use by default.
|
61
|
+
|
43
62
|
TestDummy.define do
|
44
63
|
dummy :description,
|
45
64
|
:with => :phonetic_string
|
data/test/models/user.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
# == Extensions ===========================================================
|
5
|
+
|
6
|
+
# == Constants ============================================================
|
7
|
+
|
8
|
+
# == Properties ===========================================================
|
9
|
+
|
10
|
+
attr_accessible :name
|
11
|
+
attr_accessible :password, :password_confirmation
|
12
|
+
|
13
|
+
attr_accessor :password, :password_confirmation
|
14
|
+
|
15
|
+
# == Relationships ========================================================
|
16
|
+
|
17
|
+
belongs_to :account
|
18
|
+
|
19
|
+
# == Callbacks ============================================================
|
20
|
+
|
21
|
+
before_save :encrypt_password
|
22
|
+
|
23
|
+
# == Validations ==========================================================
|
24
|
+
|
25
|
+
validates :password,
|
26
|
+
:confirmation => true
|
27
|
+
|
28
|
+
# == Scopes ===============================================================
|
29
|
+
|
30
|
+
# == Class Methods ========================================================
|
31
|
+
|
32
|
+
# == Instance Methods =====================================================
|
33
|
+
|
34
|
+
protected
|
35
|
+
def encrypt_password
|
36
|
+
# A proper implementation would use bcrypt, but this is just a
|
37
|
+
# non-functional demonstration.
|
38
|
+
self.password_crypt = Digest::SHA1.hexdigest(
|
39
|
+
[
|
40
|
+
@password,
|
41
|
+
SecureRandom.uuid
|
42
|
+
].join('$')
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class User::Admin < User
|
2
|
+
# == Extensions ===========================================================
|
3
|
+
|
4
|
+
# == Constants ============================================================
|
5
|
+
|
6
|
+
# == Properties ===========================================================
|
7
|
+
|
8
|
+
# == Relationships ========================================================
|
9
|
+
|
10
|
+
# == Callbacks ============================================================
|
11
|
+
|
12
|
+
# == Validations ==========================================================
|
13
|
+
|
14
|
+
validates :authorization_code,
|
15
|
+
:presence => true
|
16
|
+
|
17
|
+
# == Scopes ===============================================================
|
18
|
+
|
19
|
+
# == Class Methods ========================================================
|
20
|
+
|
21
|
+
# == Instance Methods =====================================================
|
22
|
+
end
|
data/test/unit/test_account.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
-
|
1
|
+
require_relative '../helper'
|
2
2
|
|
3
|
-
class TestAccount <
|
3
|
+
class TestAccount < MiniTest::Unit::TestCase
|
4
4
|
def test_extension_loaded
|
5
5
|
assert Account.respond_to?(:create_dummy)
|
6
|
+
|
7
|
+
assert TestDummy::Loader.load!(Account)
|
8
|
+
|
9
|
+
assert_equal [ :name, :field_a, :field_b, :activated_at ], Account.dummy_definition.fields
|
6
10
|
end
|
7
11
|
|
8
12
|
def test_create_dummy
|
@@ -12,10 +16,82 @@ class TestAccount < ActiveSupport::TestCase
|
|
12
16
|
|
13
17
|
assert account.name?
|
14
18
|
|
19
|
+
assert_equal 'dummy', account.source
|
20
|
+
|
21
|
+
assert_equal true, account.valid?
|
22
|
+
assert_equal false, account.new_record?
|
23
|
+
|
24
|
+
assert_equal [ ], account.bills.collect(&:ids)
|
25
|
+
assert_equal [ ], account.items.collect(&:ids)
|
26
|
+
|
27
|
+
assert_equal account.field_a, account.field_b
|
28
|
+
|
29
|
+
assert account.field_c?
|
30
|
+
|
31
|
+
assert account.activated_at?
|
32
|
+
assert !account.closed_at?
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_create_dummy_unactivated
|
36
|
+
account = Account.create_dummy(:unactivated)
|
37
|
+
|
38
|
+
assert account
|
39
|
+
|
40
|
+
assert account.name?
|
41
|
+
|
15
42
|
assert_equal true, account.valid?
|
16
43
|
assert_equal false, account.new_record?
|
17
44
|
|
18
45
|
assert_equal [ ], account.bills.collect(&:ids)
|
19
46
|
assert_equal [ ], account.items.collect(&:ids)
|
47
|
+
|
48
|
+
assert_equal account.field_a, account.field_b
|
49
|
+
|
50
|
+
assert account.field_c?
|
51
|
+
|
52
|
+
assert !account.activated_at?
|
53
|
+
assert !account.closed_at?
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_create_dummy_closed
|
57
|
+
account = Account.create_dummy(:closed)
|
58
|
+
|
59
|
+
assert account
|
60
|
+
|
61
|
+
assert account.name?
|
62
|
+
|
63
|
+
assert_equal true, account.valid?
|
64
|
+
assert_equal false, account.new_record?
|
65
|
+
|
66
|
+
assert_equal [ ], account.bills.collect(&:ids)
|
67
|
+
assert_equal [ ], account.items.collect(&:ids)
|
68
|
+
|
69
|
+
assert_equal account.field_a, account.field_b
|
70
|
+
|
71
|
+
assert !account.field_c?
|
72
|
+
|
73
|
+
assert account.activated_at?
|
74
|
+
assert account.closed_at?
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_create_dummy_closed_and_unactivated
|
78
|
+
account = Account.create_dummy(:closed, :unactivated)
|
79
|
+
|
80
|
+
assert account
|
81
|
+
|
82
|
+
assert account.name?
|
83
|
+
|
84
|
+
assert_equal true, account.valid?
|
85
|
+
assert_equal false, account.new_record?
|
86
|
+
|
87
|
+
assert_equal [ ], account.bills.collect(&:ids)
|
88
|
+
assert_equal [ ], account.items.collect(&:ids)
|
89
|
+
|
90
|
+
assert_equal account.field_a, account.field_b
|
91
|
+
|
92
|
+
assert !account.field_c?
|
93
|
+
|
94
|
+
assert !account.activated_at?
|
95
|
+
assert account.closed_at?
|
20
96
|
end
|
21
97
|
end
|
data/test/unit/test_bill.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
-
|
1
|
+
require_relative '../helper'
|
2
2
|
|
3
|
-
class TestBill <
|
3
|
+
class TestBill < MiniTest::Unit::TestCase
|
4
4
|
def test_extension_loaded
|
5
5
|
assert Bill.respond_to?(:create_dummy)
|
6
|
+
|
7
|
+
assert_equal [ :account, :order_date ], Bill.dummy_definition.fields
|
8
|
+
assert_equal [ :account, :order_date, :due_date ], Bill.dummy_definition.fields(:overdue)
|
9
|
+
assert_equal [ :account, :order_date ], Bill.dummy_definition.fields(:with_items)
|
6
10
|
end
|
7
11
|
|
8
12
|
def test_create_dummy
|
9
|
-
bill = Bill
|
13
|
+
bill = a Bill
|
10
14
|
|
11
15
|
assert bill
|
12
16
|
|
@@ -21,6 +25,13 @@ class TestBill < ActiveSupport::TestCase
|
|
21
25
|
assert_equal [ ], bill.items.collect(&:ids)
|
22
26
|
end
|
23
27
|
|
28
|
+
def test_create_with_account
|
29
|
+
account = an Account
|
30
|
+
bill = a Bill, :account => account
|
31
|
+
|
32
|
+
assert_equal account.id, bill.account.id
|
33
|
+
end
|
34
|
+
|
24
35
|
def test_create_dummy_via_association
|
25
36
|
account = an Account
|
26
37
|
|
@@ -38,9 +49,13 @@ class TestBill < ActiveSupport::TestCase
|
|
38
49
|
def test_with_overdue_tag
|
39
50
|
account = an Account
|
40
51
|
|
41
|
-
bill = Bill
|
52
|
+
bill = a Bill, :overdue, :account => account
|
42
53
|
|
43
54
|
assert bill
|
55
|
+
|
56
|
+
assert bill.account
|
57
|
+
assert_equal account.id, bill.account.id
|
58
|
+
|
44
59
|
assert_equal true, bill.valid?
|
45
60
|
assert_equal false, bill.new_record?
|
46
61
|
|
@@ -52,4 +67,13 @@ class TestBill < ActiveSupport::TestCase
|
|
52
67
|
|
53
68
|
assert_equal [ bill.id ], account.bills.collect(&:id)
|
54
69
|
end
|
70
|
+
|
71
|
+
def test_with_items_tag
|
72
|
+
bill = a Bill, :with_items
|
73
|
+
|
74
|
+
assert_created bill
|
75
|
+
|
76
|
+
assert_equal 5, bill.items.count
|
77
|
+
assert_equal 5, bill.items.reject(&:new_record?).length
|
78
|
+
end
|
55
79
|
end
|