active_record_mocks 1.0.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 +19 -0
- data/License +11 -0
- data/Rakefile +5 -0
- data/Readme.md +149 -0
- data/lib/active_record_mocks/mock/table.rb +148 -0
- data/lib/active_record_mocks/mock.rb +55 -0
- data/lib/active_record_mocks/rails.rb +5 -0
- data/lib/active_record_mocks/rspec.rb +6 -0
- data/lib/active_record_mocks/version.rb +3 -0
- data/lib/active_record_mocks.rb +21 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 88990a18f8fe9b7a17990eb080e7a8e15978997b
|
4
|
+
data.tar.gz: d44b699b89a26142df44a62ed86ab10f774bf1c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aae5f6701fb3066ea610c9b6ce3ea9b0d04e3aab5c8c0094afa46dd0c7865f1bf03191a9e2ea2e64d02487721228fadd301dc9b23c21cba5651b71899443bff7
|
7
|
+
data.tar.gz: 7b7f0ba4382f1e25a4633dd68c634cfbd272bba1546619fcad78f9538b7ba5dff27f29961916ebb54b7a2d9a13fa25bbd824bda7b7ce8b89861687d6b24d13d3
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
gemspec :name => "Gem1"
|
3
|
+
|
4
|
+
group :development do
|
5
|
+
unless ENV["CI"]
|
6
|
+
gem "pry"
|
7
|
+
end
|
8
|
+
|
9
|
+
gem "rake"
|
10
|
+
|
11
|
+
# ---------------------------------------------------------------------------
|
12
|
+
# So we can test a bunch of database platforms.
|
13
|
+
# ---------------------------------------------------------------------------
|
14
|
+
|
15
|
+
gem "pg", :platforms => [:mswin, :mingw, :ruby]
|
16
|
+
gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
|
17
|
+
gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
|
18
|
+
gem "mysql2", :platforms => [:mswin, :mingw, :ruby]
|
19
|
+
end
|
data/License
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Copyright 2013 Jordon Bedwell
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
4
|
+
use this file except in compliance with the License. You may obtain a copy of
|
5
|
+
the License at: http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
9
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
10
|
+
License for the specific language governing permissions and limitations under
|
11
|
+
the License.
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# Active Record Mocks.
|
2
|
+
|
3
|
+
[](https://travis-ci.org/envygeeks/active_record_mocks) [](https://coveralls.io/r/envygeeks/active_record_mocks) [](https://codeclimate.com/github/envygeeks/active_record_mocks) [](https://gemnasium.com/envygeeks/active_record_mocks)
|
4
|
+
|
5
|
+
ActiveRecord Mocks is designed to aide you in testing your ActiveRecord
|
6
|
+
concerns by creating random models (or even named models) that are
|
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.
|
10
|
+
|
11
|
+
## Installing
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem "active_record_mocks"
|
15
|
+
```
|
16
|
+
|
17
|
+
## Using
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
with_mocked_tables do |m|
|
21
|
+
m.enable_extension "uuid-ossp"
|
22
|
+
m.enable_extension "hstore"
|
23
|
+
|
24
|
+
t1 = m.create_table do |t|
|
25
|
+
t.model_name :Foo
|
26
|
+
t.belongs_to :bar
|
27
|
+
|
28
|
+
t.layout do |l|
|
29
|
+
l.integer :bar_id
|
30
|
+
end
|
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
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
---
|
47
|
+
|
48
|
+
### Extensions
|
49
|
+
|
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"
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
---
|
61
|
+
|
62
|
+
### Creating Tables and Layouts
|
63
|
+
|
64
|
+
To create tables you use the `create_table` method when inside of
|
65
|
+
`with_mocked_tables` or `with_mocked_models`, like so:
|
66
|
+
|
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
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
#### Belongs to, Has Many and other methods
|
78
|
+
|
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:
|
82
|
+
|
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
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
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
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Results in:
|
114
|
+
# - Foo (Model)
|
115
|
+
# - foos (Table)
|
116
|
+
```
|
117
|
+
|
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
|
128
|
+
|
129
|
+
# Results in:
|
130
|
+
# - Foo (Model)
|
131
|
+
# - old_foo (Table)
|
132
|
+
```
|
133
|
+
|
134
|
+
#### Model Includes
|
135
|
+
|
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
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
```
|
@@ -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,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,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
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_mocks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordon Bedwell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.14'
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.14'
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.1'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activerecord
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.2'
|
40
|
+
- - <=
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '4.1'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.2'
|
50
|
+
- - <=
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '4.1'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec-expect_error
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.0'
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ~>
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0.0'
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: envygeeks-coveralls
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0.1'
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ~>
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0.1'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: luna-rspec-formatters
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0.4'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ~>
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0.4'
|
95
|
+
description: Mock ActiveRecord tables to test concerns and other code.
|
96
|
+
email: envygeeks@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- Readme.md
|
102
|
+
- License
|
103
|
+
- Rakefile
|
104
|
+
- Gemfile
|
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
|
112
|
+
licenses:
|
113
|
+
- Apache 2.0
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.1.3
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Mock ActiveRecord tables to test.
|
135
|
+
test_files: []
|