database_rewinder 0.4.2 → 0.5.1
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 +4 -4
- data/.gitignore +1 -0
- data/README.md +28 -0
- data/database_rewinder.gemspec +1 -1
- data/lib/database_rewinder.rb +24 -1
- data/spec/active_record_monkey_spec.rb +1 -1
- data/spec/config/database.yml +7 -1
- data/spec/database_rewinder_spec.rb +48 -11
- data/spec/fake_app.rb +17 -0
- data/spec/spec_helper.rb +1 -1
- metadata +16 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b508301ffcd515a842bbaf9b69cb2f259f7d37bf
|
4
|
+
data.tar.gz: 28006751946237e176b620e7bac07527cd99ebd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd07c81ea30272bef5bc22eb0bdeaa1e043ed3eeeab8e5e34a7273ce0ed5bb6fab3876c774c1440a5b2fa10f8b155523ed094d7bc4f11b142f97a2a15bb36cd5
|
7
|
+
data.tar.gz: c34c37f908f7b2e8a2a4181c395a0f23f74605a00005a4776bdc41c53cc65585d736bd4f9c54e0045715c12c66dfb0876398d68d69bd13c0475f7756a2617cb9
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -38,6 +38,8 @@ And then execute:
|
|
38
38
|
|
39
39
|
## Usage
|
40
40
|
|
41
|
+
### Basic configuration
|
42
|
+
|
41
43
|
Do `clean` in `after(:each)`. And do `clean_all` or `clean_with` in `before(:suite)` if you'd like to.
|
42
44
|
|
43
45
|
```ruby
|
@@ -54,6 +56,32 @@ RSpec.configure do |config|
|
|
54
56
|
end
|
55
57
|
```
|
56
58
|
|
59
|
+
### Dealing with multiple DBs
|
60
|
+
|
61
|
+
You can configure multiple DB connections to tell DatabaseRewinder to cleanup all of them after each test.
|
62
|
+
In order to add another connection, use `DatabaseRewinder[]` method.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
RSpec.configure do |config|
|
66
|
+
config.before(:suite) do
|
67
|
+
# simply give the DB connection names that are written in config/database.yml
|
68
|
+
DatabaseRewinder['test']
|
69
|
+
DatabaseRewinder['another_test_db']
|
70
|
+
|
71
|
+
# you could give the DB name with connection: key if you like
|
72
|
+
DatabaseRewinder[connection: 'yet_another_test_db']
|
73
|
+
|
74
|
+
# also with a meaning less something first, then {connection: DB_NAME} as the second argument (DatabaseCleaner compatible)
|
75
|
+
DatabaseRewinder[:active_record, connection: 'an_active_record_db']
|
76
|
+
|
77
|
+
DatabaseRewinder.clean_all
|
78
|
+
end
|
79
|
+
|
80
|
+
config.after(:each) do
|
81
|
+
DatabaseRewinder.clean
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
57
85
|
### Pro Tip
|
58
86
|
|
59
87
|
database\_rewinder is designed to be almost compatible with database\_cleaner.
|
data/database_rewinder.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "database_rewinder"
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '0.5.1'
|
8
8
|
spec.authors = ["Akira Matsuda"]
|
9
9
|
spec.email = ["ronnie@dio.jp"]
|
10
10
|
spec.description = "A minimalist's tiny and ultra-fast database cleaner"
|
data/lib/database_rewinder.rb
CHANGED
@@ -21,7 +21,30 @@ module DatabaseRewinder
|
|
21
21
|
Cleaner.new(config: config, connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
# In order to add another database to cleanup, you can give its connection name in on of the forms below:
|
25
|
+
#
|
26
|
+
# # the simplest form
|
27
|
+
# DatabaseRewinder['the_db_name']
|
28
|
+
#
|
29
|
+
# or
|
30
|
+
#
|
31
|
+
# # with connection: key
|
32
|
+
# DatabaseRewinder[connection: 'the_db_name']
|
33
|
+
#
|
34
|
+
# or
|
35
|
+
#
|
36
|
+
# # DatabaseCleaner compatible
|
37
|
+
# DatabaseRewinder[:active_record, connection: 'the_db_name']
|
38
|
+
#
|
39
|
+
# You can cleanup multiple databases for each test using this configuration.
|
40
|
+
def [](orm, connection: nil, **)
|
41
|
+
if connection.nil?
|
42
|
+
if orm.is_a? String
|
43
|
+
connection = orm
|
44
|
+
elsif orm.is_a?(Hash) && orm.has_key?(:connection)
|
45
|
+
connection = orm[:connection]
|
46
|
+
end
|
47
|
+
end
|
25
48
|
@cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection)
|
26
49
|
end
|
27
50
|
|
@@ -7,7 +7,7 @@ describe 'DatabaseRewinder::InsertRecorder#execute' do
|
|
7
7
|
Bar.connection.execute "insert into bars (name) values ('bar1')"
|
8
8
|
DatabaseRewinder.cleaners
|
9
9
|
end
|
10
|
-
subject { DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == '
|
10
|
+
subject { DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == 'test.sqlite3'} }
|
11
11
|
its(:inserted_tables) { should == %w(foos bars) }
|
12
12
|
its(:pool) { should be }
|
13
13
|
end
|
data/spec/config/database.yml
CHANGED
@@ -6,23 +6,60 @@ describe DatabaseRewinder do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe '.[]' do
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
context 'for connecting to an arbitrary database' do
|
10
|
+
after do
|
11
|
+
DatabaseRewinder.database_configuration = nil
|
12
|
+
end
|
13
|
+
subject { DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name} }
|
14
|
+
|
15
|
+
context 'simply giving a connection name only' do
|
16
|
+
before do
|
17
|
+
DatabaseRewinder.database_configuration = {'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
|
18
|
+
DatabaseRewinder['aaa']
|
19
|
+
end
|
20
|
+
it { should == ['aaa'] }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'giving a connection name via Hash with :connection key' do
|
24
|
+
before do
|
25
|
+
DatabaseRewinder.database_configuration = {'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
|
26
|
+
DatabaseRewinder[connection: 'bbb']
|
27
|
+
end
|
28
|
+
it { should == ['bbb'] }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'the Cleaner compatible syntax' do
|
32
|
+
before do
|
33
|
+
DatabaseRewinder.database_configuration = {'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
|
34
|
+
DatabaseRewinder[:aho, connection: 'ccc']
|
35
|
+
end
|
36
|
+
it { should == ['ccc'] }
|
37
|
+
end
|
12
38
|
end
|
13
|
-
|
14
|
-
|
39
|
+
|
40
|
+
context 'for connecting to multiple databases' do
|
41
|
+
before do
|
42
|
+
DatabaseRewinder[:active_record, connection: 'test']
|
43
|
+
DatabaseRewinder[:active_record, connection: 'test2']
|
44
|
+
|
45
|
+
Foo.create! name: 'foo1'
|
46
|
+
Quu.create! name: 'quu1'
|
47
|
+
|
48
|
+
DatabaseRewinder.clean
|
49
|
+
end
|
50
|
+
it 'should clean all configured databases' do
|
51
|
+
Foo.count.should == 0
|
52
|
+
Quu.count.should == 0
|
53
|
+
end
|
15
54
|
end
|
16
|
-
subject { DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name} }
|
17
|
-
it { should == ['foo'] }
|
18
55
|
end
|
19
56
|
|
20
57
|
describe '.record_inserted_table' do
|
21
58
|
before do
|
22
|
-
DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => 'db/
|
59
|
+
DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => 'db/test_record_inserted_table.sqlite3'}}
|
23
60
|
@cleaner = DatabaseRewinder.create_cleaner 'foo'
|
24
61
|
connection = double('connection').as_null_object
|
25
|
-
connection.instance_variable_set :'@config', {adapter: 'sqlite3', database: File.expand_path('db/
|
62
|
+
connection.instance_variable_set :'@config', {adapter: 'sqlite3', database: File.expand_path('db/test_record_inserted_table.sqlite3', Rails.root) }
|
26
63
|
DatabaseRewinder.record_inserted_table(connection, sql)
|
27
64
|
end
|
28
65
|
after do
|
@@ -41,9 +78,9 @@ describe DatabaseRewinder do
|
|
41
78
|
end
|
42
79
|
end
|
43
80
|
|
44
|
-
context '
|
81
|
+
context 'Database accepts more than one dots in an object notation(exp: SQLServer)' do
|
45
82
|
context 'full joined' do
|
46
|
-
|
83
|
+
let(:sql) { 'INSERT INTO server.database.schema.foos ("name") VALUES (?)' }
|
47
84
|
its(:inserted_tables) { should == ['foos'] }
|
48
85
|
end
|
49
86
|
context 'missing one' do
|
data/spec/fake_app.rb
CHANGED
@@ -15,6 +15,9 @@ end
|
|
15
15
|
class Foo < ActiveRecord::Base; end
|
16
16
|
class Bar < ActiveRecord::Base; end
|
17
17
|
class Baz < ActiveRecord::Base; end
|
18
|
+
class Quu < ActiveRecord::Base
|
19
|
+
establish_connection :test2
|
20
|
+
end
|
18
21
|
|
19
22
|
# migrations
|
20
23
|
class CreateAllTables < ActiveRecord::Migration
|
@@ -22,5 +25,19 @@ class CreateAllTables < ActiveRecord::Migration
|
|
22
25
|
create_table(:foos) {|t| t.string :name }
|
23
26
|
create_table(:bars) {|t| t.string :name }
|
24
27
|
create_table(:bazs) {|t| t.string :name }
|
28
|
+
|
29
|
+
test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
|
30
|
+
test2_connection.create_table(:quus) {|t| t.string :name }
|
31
|
+
ActiveRecord::Base.establish_connection :test
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.down
|
35
|
+
drop_table(:foos) {|t| t.string :name }
|
36
|
+
drop_table(:bars) {|t| t.string :name }
|
37
|
+
drop_table(:bazs) {|t| t.string :name }
|
38
|
+
|
39
|
+
test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
|
40
|
+
test2_connection.drop_table :quus
|
41
|
+
ActiveRecord::Base.establish_connection :test
|
25
42
|
end
|
26
43
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_rewinder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - <
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '2.99'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - <
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.99'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rails
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
description: A minimalist's tiny and ultra-fast database cleaner
|
@@ -87,8 +87,8 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
-
-
|
91
|
-
-
|
90
|
+
- .gitignore
|
91
|
+
- .travis.yml
|
92
92
|
- Gemfile
|
93
93
|
- MIT_LICENSE
|
94
94
|
- README.md
|
@@ -116,12 +116,12 @@ require_paths:
|
|
116
116
|
- lib
|
117
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
118
118
|
requirements:
|
119
|
-
- -
|
119
|
+
- - '>='
|
120
120
|
- !ruby/object:Gem::Version
|
121
121
|
version: '0'
|
122
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
123
|
requirements:
|
124
|
-
- -
|
124
|
+
- - '>='
|
125
125
|
- !ruby/object:Gem::Version
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|