rspec-active_record_mocks 0.1.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7916c465a0bd34487754d11b36daec7aa98b6bf7
4
- data.tar.gz: 08aed740399bf1acc91fb9f8667eac66590ed2d8
3
+ metadata.gz: 6e35438d49c6384a1ff0f6f50e20be1d24ba5275
4
+ data.tar.gz: 3647113e60f410c852aa188b5b960b6a29a4acf2
5
5
  SHA512:
6
- metadata.gz: b0ebb4c40c62875ab92a27619f1427a770de89d87c8b5b757f10d8deb6cffe033f40bd6e70d7d6514a926e4f867e6901f6f0a03ab2cfe849542796e1925aeb64
7
- data.tar.gz: 1a7786439a49a411e9d1c4041797ef1e3aaadd9caeed8b8404be76c87e9917ff2d7ac2491b49613fb7b460cc2e6387bf10585c9de3dfe2ee1963efcb65924571
6
+ metadata.gz: 96ff970256537da82849bc5812e57da8ad3b7ffa7acff26deefeac9c773cc925097c7d025f9adabe68d330b363e50200fb6a62e3838ee7e600059ea07eacad7a
7
+ data.tar.gz: d3a6e82279c6eacf1aef4cdc8e6b7cedcb65e12d1e179fb9b5c449669aa0662f9b45cd8c8af3c054fc5860039327ea9e88cef48363b3ea0ed5a23a03253d76d1
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source "https://rubygems.org"
2
- gemspec
2
+ gemspec :name => "Gem1"
3
3
 
4
4
  group :development do
5
5
  unless ENV["CI"]
data/Readme.md CHANGED
@@ -1,114 +1,148 @@
1
- # Important note
2
-
3
- *Over the next few days this gem will be transformed to simply be
4
- `active_record_mocks` so it works with Test::Unit, MiniTest and RSpec.
5
- This gem will be a complete alias of the other one and will still be
6
- published but your work flow might change a bit depending on how I
7
- decide to go about the work flow, this means that you could end up
8
- using it in an entirely different way than before.*
9
-
10
- # RSpec Active Record Mocks.
1
+ # Active Record Mocks.
11
2
 
12
3
  [![Build Status](https://travis-ci.org/envygeeks/active_record_mocks.png?branch=master)](https://travis-ci.org/envygeeks/active_record_mocks) [![Coverage Status](https://coveralls.io/repos/envygeeks/active_record_mocks/badge.png?branch=master)](https://coveralls.io/r/envygeeks/active_record_mocks) [![Code Climate](https://codeclimate.com/github/envygeeks/active_record_mocks.png)](https://codeclimate.com/github/envygeeks/active_record_mocks) [![Dependency Status](https://gemnasium.com/envygeeks/active_record_mocks.png)](https://gemnasium.com/envygeeks/active_record_mocks)
13
4
 
14
5
  ActiveRecord Mocks is designed to aide you in testing your ActiveRecord
15
6
  concerns by creating random models (or even named models) that are
16
- removed after each test.
7
+ removed after each test. It was originally concieved to test concerns,
8
+ includes and other types of things that normally aren't tied to a
9
+ model specifically.
17
10
 
18
11
  ## Installing
19
12
 
20
13
  ```ruby
21
- gem "rspec-active_record_mocks"
14
+ gem "active_record_mocks"
22
15
  ```
23
16
 
24
17
  ## Using
25
18
 
26
- `RSpec::ActiveRecordMocks` supports `before` with `:all` or `:each`, it
27
- can also be used directly inside the it. It's designed to try and be a
28
- little bit flexible in how you try to use it.
29
-
30
19
  ```ruby
20
+ with_mocked_tables do |m|
21
+ m.enable_extension "uuid-ossp"
22
+ m.enable_extension "hstore"
31
23
 
32
- # ----------------------------------------------------------------------------
33
- # One Line Usage.
34
- # ----------------------------------------------------------------------------
24
+ t1 = m.create_table do |t|
25
+ t.model_name :Foo
26
+ t.belongs_to :bar
35
27
 
36
- describe TestConcern do
37
- it "should work as expected" do
38
- expect(mock_active_record_model(:include => TestConcern).test_column).to eq "value"
28
+ t.layout do |l|
29
+ l.integer :bar_id
30
+ end
39
31
  end
32
+
33
+ t2 = m.create_table do |t|
34
+ t.model_name :Bar
35
+ t.has_many :foo
36
+
37
+ t.layout do |l|
38
+ l.text :bar_text
39
+ end
40
+ end
41
+
42
+ # Do Work Here
40
43
  end
41
44
  ```
42
45
 
43
- ```ruby
46
+ ---
44
47
 
45
- # ----------------------------------------------------------------------------
46
- # An example using extensions!
47
- # ----------------------------------------------------------------------------
48
+ ### Extensions
48
49
 
49
- describe TestConcern do
50
- it "should work with extensions" do
51
- mock_active_record_model(:extensions => :hstore)
52
- expect(ActiveRecord::Base.connection.extensions).to include "hstore"
53
- end
50
+ You can enable PostgreSQL extensions inside of your models using the
51
+ `enable_extension` method when inside of `with_mocked_tables` or
52
+ `with_mocked_models` like so:
53
+
54
+ ```ruby
55
+ with_mocked_tables do |m|
56
+ m.enable_extension "extension-name"
54
57
  end
55
58
  ```
56
59
 
57
- ```ruby
60
+ ---
58
61
 
59
- # ----------------------------------------------------------------------------
60
- # Before :all example with :include and a migration.
61
- # Also works with `before :each`
62
- # ----------------------------------------------------------------------------
62
+ ### Creating Tables and Layouts
63
63
 
64
- describe TestConcern do
65
- before :all do
66
- @test_model = mock_active_record_model(:include => TestConcern) do
67
- table.string(:test_column)
68
- end
69
- end
64
+ To create tables you use the `create_table` method when inside of
65
+ `with_mocked_tables` or `with_mocked_models`, like so:
70
66
 
71
- it "should have the concerns method" do
72
- expect(@test_model.new).to respond_to :test_method
67
+ ```ruby
68
+ with_mocked_tables do |m|
69
+ m.create_table migration_arguments do |t|
70
+ t.layout do |l|
71
+ l.text :foo_text
72
+ end
73
73
  end
74
74
  end
75
75
  ```
76
76
 
77
- ```ruby
77
+ #### Belongs to, Has Many and other methods
78
78
 
79
- # ----------------------------------------------------------------------------
80
- # Before :all example that does a class_eval to include.
81
- # Also works with `before :each`
82
- # ----------------------------------------------------------------------------
79
+ Any method that `ActiveRecordMocks` does not know or understand is
80
+ passed on to the model itself, so if you need for example `belongs_to`
81
+ then you would simply use belongs to when creating your table:
83
82
 
84
- describe TestConcern do
85
- before :all do
86
- @model = mock_active_record_model.class_eval do
87
- include MyActiveRecordConcern
83
+ ```ruby
84
+ with_mocked_tables do |m|
85
+ m.create_table migration_arguments do |t|
86
+ t.belongs_to :bar_model
87
+ t.layout do |l|
88
+ l.text :foo_text
88
89
  end
89
90
  end
91
+ end
92
+ ```
90
93
 
91
- it "should work" do
92
- expect(@model.concern_method).to eq "concern_value"
94
+ #### Named models and tables
95
+
96
+ If you need a named model or a named table or a model whose table is
97
+ different than it's model you can use the methods `model_name` and
98
+ `table_name`, if you simply need a named model and you use standard
99
+ naming conventions than you can simply leave out the `table_name`
100
+ when using model name and `ActiveRecordMocks` will tabelize the name
101
+ of your model automatically the same as `Rails` would.
102
+
103
+ ```ruby
104
+ with_mocked_tables do |m|
105
+ t1 = m.create_table migration_arguments do |t|
106
+ t.model_name :Foo
107
+ t.layout do |l|
108
+ l.text :foo_text
109
+ end
93
110
  end
94
111
  end
112
+
113
+ # Results in:
114
+ # - Foo (Model)
115
+ # - foos (Table)
95
116
  ```
96
117
 
97
118
  ```ruby
119
+ with_mocked_tables do |m|
120
+ t1 = m.create_table migration_arguments do |t|
121
+ t.table_name :old_foo
122
+ t.model_name :Foo
123
+ t.layout do |l|
124
+ l.text :foo_text
125
+ end
126
+ end
127
+ end
98
128
 
99
- # ----------------------------------------------------------------------------
100
- # Completely random example in a before :all.
101
- # Also works with `before :each`
102
- # ----------------------------------------------------------------------------
129
+ # Results in:
130
+ # - Foo (Model)
131
+ # - old_foo (Table)
132
+ ```
103
133
 
104
- describe MyActiveRecordConcern do
105
- before :all do
106
- @model = mock_active_record_model do |table|
107
- table.string(:column)
108
- end
134
+ #### Model Includes
109
135
 
110
- @model.class_eval do
111
- include MyActiveRecordConcern
136
+ If you need to include anything into your model you can use the
137
+ `includes` method when inside of `with_mocked_models` or
138
+ `with_mocked_tables`, like so:
139
+
140
+ ```ruby
141
+ with_mocked_tables do |m|
142
+ m.create_table migration_arguments do |t|
143
+ t.includes Bar1, Bar2
144
+ t.layout do |l|
145
+ l.text :foo_text
112
146
  end
113
147
  end
114
148
  end
@@ -0,0 +1,21 @@
1
+ require "active_record"
2
+
3
+ module ActiveRecordMocks
4
+ require_relative "active_record_mocks/mock"
5
+
6
+ module IncludeMe
7
+ def mocked_active_record
8
+ Mock.new
9
+ end
10
+
11
+ def with_mocked_tables(&block)
12
+ if block_given?
13
+ mocked_active_record.tap do |o|
14
+ block.call(o)
15
+ o.delete_tables
16
+ end
17
+ end
18
+ nil
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ require_relative "mock/table"
2
+ require "active_record"
3
+
4
+ module ActiveRecordMocks
5
+ class Mock
6
+ class ExtensionsUnsupported < StandardError
7
+ def initialize
8
+ super "Your database server does not support extensions."
9
+ end
10
+ end
11
+
12
+ attr_accessor :tables
13
+
14
+ def enable_extension(ext)
15
+ raise_if_extensions_unsupported!
16
+ ActiveRecord::Migration.tap do |o|
17
+ o.suppress_messages do
18
+ o.enable_extension(ext)
19
+ end
20
+ end
21
+ end
22
+
23
+ def create_table(*args, &block)
24
+ Table.new(*args).tap do |o|
25
+ if block_given?
26
+ block.call(o)
27
+ end
28
+
29
+ o.setup_mocking!
30
+ tables.push(o)
31
+ return o.model
32
+ end
33
+ end
34
+
35
+ def initialize
36
+ @tables = [
37
+ ]
38
+ end
39
+
40
+ def delete_tables
41
+ tables.each do |t|
42
+ Object.send(:remove_const, t.model_name)
43
+ ActiveRecord::Base.connection.drop_table(t.table_name)
44
+ end
45
+ nil
46
+ end
47
+
48
+ private
49
+ def raise_if_extensions_unsupported!
50
+ if ! ActiveRecord::Base.connection.respond_to?(:enable_extension)
51
+ raise ExtensionsUnsupported
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,148 @@
1
+ require "active_support/core_ext/string/inflections"
2
+
3
+ module ActiveRecordMocks
4
+ class Mock
5
+ class Table
6
+ attr_reader :model_methods
7
+ attr_reader :args
8
+
9
+ def initialize(*args, &block)
10
+ @model_methods = []
11
+ @table_name = nil
12
+ @includes = []
13
+ @args = args
14
+ @layout = nil
15
+ @model_name = nil
16
+ end
17
+
18
+ # ---------------------------------------------------------------
19
+ # Tells us if we have already setup this model and object so
20
+ # that we don't keep setting stuff up.
21
+ # ---------------------------------------------------------------
22
+
23
+ def setup?
24
+ @already_setup ? true : false
25
+ end
26
+
27
+ # ---------------------------------------------------------------
28
+ # Gives the proper object of the model for you.
29
+ # ---------------------------------------------------------------
30
+
31
+ def model
32
+ if setup?
33
+ Object.const_get(@model_name)
34
+ end
35
+ end
36
+
37
+ # ---------------------------------------------------------------
38
+ # Allows you to set the files that should be included into the
39
+ # model, you must use t.includes because t.include is already
40
+ # a method on the object you are in.
41
+ # ---------------------------------------------------------------
42
+
43
+ def includes(*incs)
44
+ if setup? || incs.size == 0
45
+ @includes
46
+ else
47
+ incs.each do |i|
48
+ unless i.blank?
49
+ @includes.push(i)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ # ---------------------------------------------------------------
56
+ # Allows you to set the layout for the table you are building.
57
+ # ---------------------------------------------------------------
58
+
59
+ def layout(&block)
60
+ setup? || ! block_given? ? @layout ||= nil : @layout = block
61
+ end
62
+
63
+ # ---------------------------------------------------------------
64
+ # Allows the setting of or setuping up of and returning of the
65
+ # name of the table that is being used for the model. If
66
+ # you do not customize this then it will be a tabelized name
67
+ # of the model, the same way that normal active_record would do.
68
+ # ---------------------------------------------------------------
69
+
70
+ def table_name(tname = nil)
71
+ if setup? || (! tname && @table_name)
72
+ @table_name
73
+ else
74
+ @table_name = \
75
+ tname ? tname : model_name.to_s.tableize
76
+ end
77
+ end
78
+
79
+ # ---------------------------------------------------------------
80
+ # Allows for the setting of or setup of and returning of the name
81
+ # of the model being used, this should not be confused with model
82
+ # which returns the actual object. The model need not match the
83
+ # table and sometimes it won't if you chose to be that way.
84
+ # ---------------------------------------------------------------
85
+
86
+ def model_name(mname = nil)
87
+ if setup? || (! mname && @model_name)
88
+ @model_name
89
+ else
90
+ @model_name = mname ? mname : \
91
+ SecureRandom.hex(10).tr("^a-z", "").capitalize
92
+ end
93
+ end
94
+
95
+ def setup_mocking!
96
+ if ! setup?
97
+ setup_table!
98
+ setup_model!
99
+ @already_setup = true
100
+ end
101
+ end
102
+
103
+ private
104
+ def setup_table!
105
+ ActiveRecord::Migration.tap do |o|
106
+ o.suppress_messages do
107
+ o.create_table table_name, *args do |t|
108
+ layout.call(t) if layout.is_a?(Proc)
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ private
115
+ def setup_model!
116
+ Object.const_set(model_name,
117
+ Class.new(ActiveRecord::Base)).tap do |o|
118
+ o.table_name = table_name
119
+ setup_includes(o)
120
+ run_model_methods(o)
121
+ end
122
+ end
123
+
124
+ private
125
+ def setup_includes(obj)
126
+ includes.each do |i|
127
+ obj.send(:include, i)
128
+ end
129
+ end
130
+
131
+ private
132
+ def run_model_methods(obj)
133
+ model_methods.each do |m|
134
+ obj.send(m[:method], *m[:args], &m[:block])
135
+ end
136
+ end
137
+
138
+ public
139
+ def method_missing(methud, *args, &block)
140
+ model_methods.push({
141
+ :block => block,
142
+ :method => methud,
143
+ :args => args
144
+ })
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "../active_record_mocks"
2
+
3
+ class ActiveSupport::TestCase
4
+ include ActiveRecordMocks::IncludeMe
5
+ end
@@ -0,0 +1,6 @@
1
+ require_relative "../active_record_mocks"
2
+ require "rspec"
3
+
4
+ RSpec.configure do |config|
5
+ config.include ActiveRecordMocks::IncludeMe
6
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordMocks
2
+ VERSION = "1.0.0"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-active_record_mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordon Bedwell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-22 00:00:00.000000000 Z
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -102,9 +102,13 @@ files:
102
102
  - License
103
103
  - Rakefile
104
104
  - Gemfile
105
- - lib/rspec/active_record_mocks.rb
106
- - lib/rspec/active_record_mocks/version.rb
107
- homepage: https://github.com/envygeeks/rspec-active_record_mocks
105
+ - lib/active_record_mocks.rb
106
+ - lib/active_record_mocks/rails.rb
107
+ - lib/active_record_mocks/version.rb
108
+ - lib/active_record_mocks/mock/table.rb
109
+ - lib/active_record_mocks/mock.rb
110
+ - lib/active_record_mocks/rspec.rb
111
+ homepage: https://github.com/envygeeks/active_record_mocks
108
112
  licenses:
109
113
  - Apache 2.0
110
114
  metadata: {}
@@ -1,98 +0,0 @@
1
- require "active_support/core_ext/string/inflections"
2
- require "active_record"
3
- require "rspec"
4
-
5
- module RSpec
6
- module ActiveRecordMocks
7
- class ExtensionsUnsupportedError < StandardError
8
- def initialize
9
- super "Your adapter does not support PostgreSQL extensions"
10
- end
11
- end
12
-
13
- # Allow people to mock ActiveRecord while giving them the flexibility.
14
-
15
- def mock_active_record_model(opts = {}, &block)
16
- tbl, ext = opts.delete(:name), opts.delete(:extensions)
17
- tbl = create_active_record_table_for_mocking(tbl, ext, &block)
18
- Object.const_set(tbl.camelize, Class.new(ActiveRecord::Base)).class_eval do
19
- include opts.delete(:include) if opts[:include]
20
- self.table_name = tbl and self
21
- end
22
- end
23
-
24
- # Roll through each one of the created tables and destroy them.
25
-
26
- def clean_tables_for_active_record_mocking
27
- if mocked_active_record_options[:mocked_active_record_tables]
28
- mocked_active_record_options.delete(:mocked_active_record_tables).each do |tbl|
29
- ActiveRecord::Base.connection.drop_table(tbl) if active_record_tables.include?(tbl)
30
- if Object.const_defined?(tbl.camelize)
31
- Object.send(:remove_const, tbl.camelize)
32
- end
33
- end
34
- end
35
- end
36
-
37
- # Aliases ActiveRecord::Base.connection.tables to active_record_tables to
38
- # a geniunely useful method that can be used by anybody doing db testing.
39
-
40
- def active_record_tables
41
- ActiveRecord::Base.connection.tables
42
- end
43
-
44
- # Allows us to access options for either the class or the test itself as
45
- # to allow users to either work on the class or work in the test allowing
46
- # us to cleanup without affecting the other.
47
-
48
- private
49
- def mocked_active_record_options
50
- (example.nil?) ? (@mocked_active_record_options ||= {}) : example.options
51
- end
52
-
53
- # Creates a temporary table inside of the database using ActiveRecord.
54
-
55
- private
56
- def create_active_record_table_for_mocking(tbl, ext, &block)
57
- tbl = (tbl || SecureRandom.hex(30).tr('^a-z', '')).to_s
58
- setup_active_record_mocking_table(tbl, ext, &block)
59
- tbl
60
- end
61
-
62
- # Sets up the table using an ActiveRecord migration.
63
-
64
- private
65
- def setup_active_record_mocking_table(tbl, ext, &block)
66
- (mocked_active_record_options[:mocked_active_record_tables] ||= []).push(tbl)
67
- ActiveRecord::Migration.suppress_messages do
68
- setup_active_record_mocking_extensions(ext)
69
- ActiveRecord::Migration.create_table tbl do |obj|
70
- block.call(obj) if block_given?
71
- end
72
- end
73
- end
74
-
75
- # Sets up the extensions for PostgreSQL.
76
-
77
- private
78
- def setup_active_record_mocking_extensions(ext)
79
- ext = [ext].delete_if { |value| value.blank? }.flatten
80
- if ext.size > 0 && ! ActiveRecord::Base.connection.respond_to?(:enable_extension)
81
- raise ExtensionsUnsupportedError
82
- else
83
- ext.each do |extension|
84
- ActiveRecord::Migration.enable_extension(extension)
85
- end
86
- end
87
- end
88
- end
89
- end
90
-
91
- RSpec.configure do |config|
92
- config.include RSpec::ActiveRecordMocks
93
- [:all, :each].each do |type|
94
- config.after(type) do
95
- clean_tables_for_active_record_mocking
96
- end
97
- end
98
- end
@@ -1,5 +0,0 @@
1
- module RSpec
2
- module ActiveRecordMocks
3
- VERSION = "0.1.4"
4
- end
5
- end