test_dummy 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_dummy', File.dirname(__FILE__))
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.send(:include, TestDummy::TestHelper)
22
- Test::Unit::TestCase.send(:include, TestDummy::TestHelper)
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.before_configuration do
26
- apply!
34
+ config.to_prepare do
35
+ TestDummy::Railtie.apply!
27
36
  end
28
37
  end
29
38
  end
@@ -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] || :"#{attribute.to_s.underscore}_id"
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, options = { })
3
- instance = scope.respond_to?(:build) ? scope.build(options) : scope.new(options)
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.dummy!
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
@@ -3,6 +3,9 @@ class CreateAccounts < ActiveRecord::Migration
3
3
  create_table :accounts do |t|
4
4
  t.string :name
5
5
  t.string :source
6
+ t.string :field_a
7
+ t.string :field_b
8
+ t.string :field_c
6
9
  t.timestamps
7
10
  t.datetime :activated_at
8
11
  t.datetime :closed_at
@@ -0,0 +1,12 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :type
5
+ t.integer :account_id
6
+ t.string :name
7
+ t.string :password_crypt
8
+ t.string :authorization_code
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -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
@@ -10,4 +10,10 @@ class Bill
10
10
 
11
11
  (date >= Date.today) ? Date.today.advance(:days => -1) : date
12
12
  end
13
+
14
+ dummy :only => :with_items, :after => :save do |bill|
15
+ 5.times do
16
+ bill.items.create_dummy
17
+ end
18
+ end
13
19
  end
@@ -0,0 +1 @@
1
+ invalid ruby code here
@@ -0,0 +1,8 @@
1
+ class User
2
+ dummy :name,
3
+ :with => :random_string
4
+
5
+ dummy [ :password, :password_confirmation ] do
6
+ TestDummy::Helper.random_phonetic_string
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class User::Admin
2
+ dummy :authorization_code do
3
+ '%04d' % SecureRandom.random_number(1000)
4
+ end
5
+ end
@@ -1,5 +1,12 @@
1
- require 'rubygems'
2
- require 'test/unit'
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 Test::Unit::TestCase
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
- 'adapter' => "sqlite3",
33
- 'database' => File.expand_path('db/test.sqlite3', base_path)
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
@@ -0,0 +1,3 @@
1
+ class Broken
2
+ # Example class with a broken test/dummy file.
3
+ end
@@ -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
@@ -1,8 +1,12 @@
1
- require File.expand_path('../helper', File.dirname(__FILE__))
1
+ require_relative '../helper'
2
2
 
3
- class TestAccount < ActiveSupport::TestCase
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
@@ -1,12 +1,16 @@
1
- require File.expand_path('../helper', File.dirname(__FILE__))
1
+ require_relative '../helper'
2
2
 
3
- class TestBill < ActiveSupport::TestCase
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.create_dummy
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.create_dummy(:overdue, :account => account)
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