database_rewinder 0.5.3 → 0.6.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: cc6022f8f8fe45025891e8b927f7513ca025e3f4
4
- data.tar.gz: 6013e2ef376647375b90bce55483d515aeea3306
3
+ metadata.gz: ebe1f8f09158825ddd23fa40e87b2a7fcd819e5a
4
+ data.tar.gz: af86fbfcc87fe2f0bb65323b50c55c14915ce83c
5
5
  SHA512:
6
- metadata.gz: ba2b53e403d1de032782818983c4be4876eb7ba02123cadedf959dda6e744674cd871774c3ff5873034a99aa219e4664a7df3863b811bea656c7257734316525
7
- data.tar.gz: fdc347ce019c3b57afbf93c65563789d5a81336f19dbad54bfe2b0dc2d7038187781cac96bb6b25e31bf3871e025c84867ed105202f5845d4871b5fac61649ad
6
+ metadata.gz: 09900bb841139340bd22a0b40e86cce91c2a41cc4960ce2987001f0285d227e36727f1c8aa11631ad7db656065e6360c1c356c83d2a16556bb8df99fb9bfda23
7
+ data.tar.gz: dc5430759eccca5d9facf567820753fa0c3c92825e0bf489f30627315353aefaccc490ab0eac2597694832e31a89e528f9744c8f7b4188dea1ebf135514c62f2
data/.gitignore CHANGED
@@ -4,6 +4,7 @@
4
4
  .config
5
5
  .yardoc
6
6
  Gemfile.lock
7
+ gemfiles/*.lock
7
8
  InstalledFiles
8
9
  _yardoc
9
10
  coverage
@@ -11,9 +12,10 @@ doc/
11
12
  lib/bundler/man
12
13
  pkg
13
14
  rdoc
14
- spec/reports
15
+ test/reports
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
18
19
  log
19
- spec/*.sqlite3
20
+ test/*.sqlite3
21
+ test/db/*.sqlite3
data/.travis.yml CHANGED
@@ -1,5 +1,23 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
4
  - 2.0.0
4
- - 2.1
5
- - 2.2
5
+ - 2.1.10
6
+ - 2.2.5
7
+ - 2.3.1
8
+
9
+ gemfile:
10
+ - gemfiles/rails_32.gemfile
11
+ - gemfiles/rails_40.gemfile
12
+ - gemfiles/rails_41.gemfile
13
+ - gemfiles/rails_42.gemfile
14
+ - gemfiles/rails_edge.gemfile
15
+
16
+ sudo: false
17
+
18
+ matrix:
19
+ exclude:
20
+ - rvm: 2.0.0
21
+ gemfile: gemfiles/rails_edge.gemfile
22
+ - rvm: 2.1.10
23
+ gemfile: gemfiles/rails_edge.gemfile
data/README.md CHANGED
@@ -22,9 +22,9 @@ This strategy was originally devised and implemented by Shingo Morita (@eudoxa)
22
22
 
23
23
  ## Supported versions
24
24
 
25
- * ActiveRecord 3.2, 4.0, 4.1, 4.2
25
+ * ActiveRecord 3.2, 4.0, 4.1, 4.2, 5.0
26
26
 
27
- * Ruby 2.0, 2.1, 2.2
27
+ * Ruby 2.0, 2.1, 2.2, 2.3
28
28
 
29
29
  ## Installation
30
30
 
@@ -44,13 +44,13 @@ Do `clean` in `after(:each)`. And do `clean_all` or `clean_with` in `before(:sui
44
44
 
45
45
  ```ruby
46
46
  RSpec.configure do |config|
47
- config.before :suite do
47
+ config.before(:suite) do
48
48
  DatabaseRewinder.clean_all
49
49
  # or
50
50
  # DatabaseRewinder.clean_with :any_arg_that_would_be_actually_ignored_anyway
51
51
  end
52
52
 
53
- config.after :each do
53
+ config.after(:each) do
54
54
  DatabaseRewinder.clean
55
55
  end
56
56
  end
data/Rakefile CHANGED
@@ -1,10 +1,13 @@
1
1
  require 'bundler'
2
2
  require 'bundler/setup'
3
3
  require "bundler/gem_tasks"
4
- require 'rspec/core'
5
- require 'rspec/core/rake_task'
4
+ require 'rake/testtask'
6
5
 
7
- task :noop do; end
8
- task :default => :spec
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.warning = true
10
+ t.verbose = true
11
+ end
9
12
 
10
- RSpec::Core::RakeTask.new(:spec => :noop)
13
+ task default: :test
@@ -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.5.3'
7
+ spec.version = '0.6.0'
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"
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.3"
21
21
  spec.add_development_dependency "rake"
22
- spec.add_development_dependency 'rspec', '< 2.99'
22
+ spec.add_development_dependency 'test-unit-rails'
23
23
  spec.add_development_dependency 'rails'
24
24
  spec.add_development_dependency 'sqlite3'
25
25
  end
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 3.2.0'
4
+ gem 'test-unit-rails', '1.0.2'
5
+
6
+ gemspec path: '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.0.0'
4
+
5
+ gemspec path: '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.1.0'
4
+
5
+ gemspec path: '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.2.0'
4
+
5
+ gemspec path: '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 5.0.0.rc1'
4
+
5
+ gemspec path: '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', github: 'rails/rails'
4
+
5
+ gemspec path: '../'
@@ -4,6 +4,12 @@ module DatabaseRewinder
4
4
  cleaners.each {|c| c.clean_with(*args)}
5
5
  end
6
6
 
7
+ def cleaning
8
+ yield
9
+ ensure
10
+ clean
11
+ end
12
+
7
13
  def start; end
8
14
 
9
15
  def strategy=(args)
@@ -12,6 +18,22 @@ module DatabaseRewinder
12
18
  cleaners.each {|c| c.strategy = nil, options}
13
19
  end
14
20
 
21
+ # In order to add another database to cleanup, you can give its connection name in one of the forms below:
22
+ #
23
+ # # the simplest form
24
+ # DatabaseRewinder['the_db_name']
25
+ #
26
+ # or
27
+ #
28
+ # # with connection: key
29
+ # DatabaseRewinder[connection: 'the_db_name']
30
+ #
31
+ # or
32
+ #
33
+ # # DatabaseCleaner compatible
34
+ # DatabaseRewinder[:active_record, connection: 'the_db_name']
35
+ #
36
+ # You can cleanup multiple databases for each test using this configuration.
15
37
  def [](orm, connection: nil, **)
16
38
  if connection.nil?
17
39
  if orm.is_a? String
@@ -21,22 +21,6 @@ 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
- # 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
24
  def [](connection)
41
25
  @cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection)
42
26
  end
@@ -63,7 +47,7 @@ module DatabaseRewinder
63
47
  end
64
48
  end or return
65
49
 
66
- match = sql.match(/\AINSERT(?:\s+IGNORE)?\s+INTO\s+(?:\.*[`"]?([^.\s`"]+)[`"]?)*/i)
50
+ match = sql.match(/\AINSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"]+)[`"]?)*/i)
67
51
  return unless match
68
52
 
69
53
  table = match[1]
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class DatabaseRewinder::InsertRecorderTest < ActiveSupport::TestCase
4
+ setup do
5
+ DatabaseRewinder.init
6
+ Foo.create! name: 'foo1'
7
+ Bar.connection.execute "insert into bars (name) values ('bar1')"
8
+ DatabaseRewinder.cleaners
9
+ end
10
+
11
+ test '#execute' do
12
+ cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == 'test.sqlite3'}
13
+
14
+ assert_equal %w(foos bars), cleaner.inserted_tables
15
+ assert_not_nil cleaner.pool
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class DatabaseRewinder::CleanerTest < ActiveSupport::TestCase
4
+ sub_test_case '#strategy=' do
5
+ setup { @cleaner = DatabaseRewinder::Cleaner.new(only: ['foos'], except: 'bars') }
6
+
7
+ test 'without options' do
8
+ @cleaner.strategy = :truncation
9
+
10
+ # it should keep instance variables
11
+ assert_equal ['foos'], @cleaner.instance_variable_get(:@only)
12
+ assert_equal ['bars'], @cleaner.instance_variable_get(:@except)
13
+ end
14
+
15
+ test 'with options (an array or a string)' do
16
+ @cleaner.strategy = :truncation, { only: ['bars'], except: 'bazs' }
17
+
18
+ # it should overwrite instance variables
19
+ assert_equal ['bars'], @cleaner.instance_variable_get(:@only)
20
+ assert_equal ['bazs'], @cleaner.instance_variable_get(:@except)
21
+ end
22
+
23
+ test 'with options (an empty array or nil)' do
24
+ @cleaner.strategy = :truncation, { only: [], except: nil }
25
+
26
+ # it should overwrite instance variables even if they are empty/nil
27
+ assert_equal [], @cleaner.instance_variable_get(:@only)
28
+ assert_equal [], @cleaner.instance_variable_get(:@except)
29
+ end
30
+ end
31
+ end
File without changes
@@ -0,0 +1,201 @@
1
+ require 'test_helper'
2
+
3
+ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
4
+ setup do
5
+ DatabaseRewinder.init
6
+ end
7
+
8
+ sub_test_case '.[]' do
9
+ teardown do
10
+ DatabaseRewinder.database_configuration = nil
11
+ end
12
+ sub_test_case 'for connecting to an arbitrary database' do
13
+ test 'simply giving a connection name only' do
14
+ DatabaseRewinder.database_configuration = {'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
15
+ DatabaseRewinder['aaa']
16
+ assert_equal ['aaa'], DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
17
+ end
18
+
19
+ test 'giving a connection name via Hash with :connection key' do
20
+ DatabaseRewinder.database_configuration = {'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
21
+ DatabaseRewinder[connection: 'bbb']
22
+ assert_equal ['bbb'], DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
23
+ end
24
+
25
+ test 'the Cleaner compatible syntax' do
26
+ DatabaseRewinder.database_configuration = {'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
27
+ DatabaseRewinder[:aho, connection: 'ccc']
28
+ assert_equal ['ccc'], DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
29
+ end
30
+ end
31
+
32
+ test 'for connecting to multiple databases' do
33
+ DatabaseRewinder[:active_record, connection: 'test']
34
+ DatabaseRewinder[:active_record, connection: 'test2']
35
+
36
+ Foo.create! name: 'foo1'
37
+ Quu.create! name: 'quu1'
38
+
39
+ DatabaseRewinder.clean
40
+
41
+ # it should clean all configured databases
42
+ assert_equal 0, Foo.count
43
+ assert_equal 0, Quu.count
44
+ end
45
+ end
46
+
47
+ sub_test_case '.record_inserted_table' do
48
+ def perform_insert(sql)
49
+ DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => 'test_record_inserted_table.sqlite3'}}
50
+ @cleaner = DatabaseRewinder.create_cleaner 'foo'
51
+
52
+ connection = ::ActiveRecord::Base.sqlite3_connection(adapter: "sqlite3", database: File.expand_path('test_record_inserted_table.sqlite3', Rails.root))
53
+ DatabaseRewinder.record_inserted_table(connection, sql)
54
+ end
55
+ teardown do
56
+ DatabaseRewinder.database_configuration = nil
57
+ end
58
+
59
+ sub_test_case 'common database' do
60
+ test 'include database name' do
61
+ perform_insert 'INSERT INTO "database"."foos" ("name") VALUES (?)'
62
+ assert_equal ['foos'], @cleaner.inserted_tables
63
+ end
64
+ test 'only table name' do
65
+ perform_insert 'INSERT INTO "foos" ("name") VALUES (?)'
66
+ assert_equal ['foos'], @cleaner.inserted_tables
67
+ end
68
+ test 'without "INTO"' do
69
+ perform_insert 'INSERT "foos" ("name") VALUES (?)'
70
+ assert_equal ['foos'], @cleaner.inserted_tables
71
+ end
72
+ end
73
+
74
+ sub_test_case 'Database accepts more than one dots in an object notation (e.g. SQLServer)' do
75
+ test 'full joined' do
76
+ perform_insert 'INSERT INTO server.database.schema.foos ("name") VALUES (?)'
77
+ assert_equal ['foos'], @cleaner.inserted_tables
78
+ end
79
+ test 'missing one' do
80
+ perform_insert 'INSERT INTO database..foos ("name") VALUES (?)'
81
+ assert_equal ['foos'], @cleaner.inserted_tables
82
+ end
83
+
84
+ test 'missing two' do
85
+ perform_insert 'INSERT INTO server...foos ("name") VALUES (?)'
86
+ assert_equal ['foos'], @cleaner.inserted_tables
87
+ end
88
+ end
89
+
90
+ test 'when database accepts INSERT IGNORE INTO statement' do
91
+ perform_insert "INSERT IGNORE INTO `foos` (`name`) VALUES ('alice'), ('bob') ON DUPLICATE KEY UPDATE `foos`.`updated_at`=VALUES(`updated_at`)"
92
+ assert_equal ['foos'], @cleaner.inserted_tables
93
+ end
94
+ end
95
+
96
+ test '.clean' do
97
+ Foo.create! name: 'foo1'
98
+ Bar.create! name: 'bar1'
99
+ DatabaseRewinder.clean
100
+
101
+ assert_equal 0, Foo.count
102
+ assert_equal 0, Bar.count
103
+ end
104
+
105
+ if ActiveRecord::VERSION::STRING >= '4'
106
+ test '.clean_all should not touch AR::SchemaMigration' do
107
+ begin
108
+ ActiveRecord::SchemaMigration.create_table
109
+ ActiveRecord::SchemaMigration.create! version: '001'
110
+ Foo.create! name: 'foo1'
111
+ DatabaseRewinder.clean_all
112
+
113
+ assert_equal 0, Foo.count
114
+ assert_equal 1, ActiveRecord::SchemaMigration.count
115
+
116
+ ensure
117
+ ActiveRecord::SchemaMigration.drop_table
118
+ end
119
+ end
120
+ end
121
+
122
+ sub_test_case '.clean_with' do
123
+ def perform_clean(options)
124
+ @cleaner = DatabaseRewinder.cleaners.first
125
+ @only = @cleaner.instance_variable_get(:@only)
126
+ @except = @cleaner.instance_variable_get(:@except)
127
+ Foo.create! name: 'foo1'
128
+ Bar.create! name: 'bar1'
129
+ DatabaseRewinder.clean_with :truncation, options
130
+ end
131
+
132
+ test 'with only option' do
133
+ perform_clean only: ['foos']
134
+ assert_equal 0, Foo.count
135
+ assert_equal 1, Bar.count
136
+ assert_equal @only, @cleaner.instance_variable_get(:@only)
137
+ end
138
+
139
+ test 'with except option' do
140
+ perform_clean except: ['bars']
141
+ assert_equal 0, Foo.count
142
+ assert_equal 1, Bar.count
143
+ assert_equal @except, @cleaner.instance_variable_get(:@except)
144
+ end
145
+ end
146
+
147
+ sub_test_case '.cleaning' do
148
+ test 'without exception' do
149
+ DatabaseRewinder.cleaning do
150
+ Foo.create! name: 'foo1'
151
+ end
152
+
153
+ assert_equal 0, Foo.count
154
+ end
155
+
156
+ test 'with exception' do
157
+ assert_raises do
158
+ DatabaseRewinder.cleaning do
159
+ Foo.create! name: 'foo1'; fail
160
+ end
161
+ end
162
+ assert_equal 0, Foo.count
163
+ end
164
+ end
165
+
166
+ sub_test_case '.strategy=' do
167
+ sub_test_case 'call first with options' do
168
+ setup do
169
+ DatabaseRewinder.strategy = :truncate, { only: ['foos'], except: ['bars'] }
170
+ end
171
+
172
+ test 'should set options' do
173
+ assert_equal ['foos'], DatabaseRewinder.instance_variable_get(:@only)
174
+ assert_equal ['bars'], DatabaseRewinder.instance_variable_get(:@except)
175
+ end
176
+
177
+ test 'should create cleaner with options' do
178
+ cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
179
+ assert_equal ['foos'], cleaner.instance_variable_get(:@only)
180
+ assert_equal ['bars'], cleaner.instance_variable_get(:@except)
181
+ end
182
+
183
+ sub_test_case 'call again with different options' do
184
+ setup do
185
+ DatabaseRewinder.strategy = :truncate, { only: ['bazs'], except: [] }
186
+ end
187
+
188
+ test 'should overwrite options' do
189
+ assert_equal ['bazs'], DatabaseRewinder.instance_variable_get(:@only)
190
+ assert_equal [], DatabaseRewinder.instance_variable_get(:@except)
191
+ end
192
+
193
+ test 'should overwrite cleaner with new options' do
194
+ cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
195
+ assert_equal ['bazs'], cleaner.instance_variable_get(:@only)
196
+ assert_equal [], cleaner.instance_variable_get(:@except)
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
File without changes
@@ -0,0 +1,20 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
4
+ $LOAD_PATH.unshift(__dir__)
5
+
6
+ require 'rails'
7
+ require 'active_record'
8
+ require 'database_rewinder'
9
+ require 'fake_app'
10
+ require 'test/unit/rails/test_help'
11
+
12
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'foos'
13
+
14
+ module DeleteAllTables
15
+ def teardown
16
+ super
17
+ [Foo, Bar, Baz, Quu].each {|m| m.delete_all }
18
+ end
19
+ end
20
+ ActiveSupport::TestCase.send :prepend, DeleteAllTables
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_rewinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
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-07-21 00:00:00.000000000 Z
11
+ date: 2016-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: test-unit-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "<"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '2.99'
47
+ version: '0'
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
- version: '2.99'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rails
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -88,25 +88,30 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
- - ".rspec"
92
91
  - ".travis.yml"
93
92
  - Gemfile
94
93
  - MIT_LICENSE
95
94
  - README.md
96
95
  - Rakefile
97
96
  - database_rewinder.gemspec
97
+ - gemfiles/rails_32.gemfile
98
+ - gemfiles/rails_40.gemfile
99
+ - gemfiles/rails_41.gemfile
100
+ - gemfiles/rails_42.gemfile
101
+ - gemfiles/rails_50.gemfile
102
+ - gemfiles/rails_edge.gemfile
98
103
  - lib/database_rewinder.rb
99
104
  - lib/database_rewinder/active_record_monkey.rb
100
105
  - lib/database_rewinder/cleaner.rb
101
106
  - lib/database_rewinder/compatibility.rb
102
107
  - lib/database_rewinder/dummy_model.rb
103
108
  - lib/database_rewinder/railtie.rb
104
- - spec/active_record_monkey_spec.rb
105
- - spec/cleaner_spec.rb
106
- - spec/config/database.yml
107
- - spec/database_rewinder_spec.rb
108
- - spec/fake_app.rb
109
- - spec/spec_helper.rb
109
+ - test/active_record_monkey_test.rb
110
+ - test/cleaner_test.rb
111
+ - test/config/database.yml
112
+ - test/database_rewinder_test.rb
113
+ - test/fake_app.rb
114
+ - test/test_helper.rb
110
115
  homepage: https://github.com/amatsuda/database_rewinder
111
116
  licenses:
112
117
  - MIT
@@ -127,14 +132,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
132
  version: '0'
128
133
  requirements: []
129
134
  rubyforge_project:
130
- rubygems_version: 2.4.5
135
+ rubygems_version: 2.6.4
131
136
  signing_key:
132
137
  specification_version: 4
133
138
  summary: A minimalist's tiny and ultra-fast database cleaner
134
139
  test_files:
135
- - spec/active_record_monkey_spec.rb
136
- - spec/cleaner_spec.rb
137
- - spec/config/database.yml
138
- - spec/database_rewinder_spec.rb
139
- - spec/fake_app.rb
140
- - spec/spec_helper.rb
140
+ - test/active_record_monkey_test.rb
141
+ - test/cleaner_test.rb
142
+ - test/config/database.yml
143
+ - test/database_rewinder_test.rb
144
+ - test/fake_app.rb
145
+ - test/test_helper.rb
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --warnings
@@ -1,13 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'DatabaseRewinder::InsertRecorder#execute' do
4
- before do
5
- DatabaseRewinder.init
6
- Foo.create! name: 'foo1'
7
- Bar.connection.execute "insert into bars (name) values ('bar1')"
8
- DatabaseRewinder.cleaners
9
- end
10
- subject { DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == 'test.sqlite3'} }
11
- its(:inserted_tables) { should == %w(foos bars) }
12
- its(:pool) { should be }
13
- end
data/spec/cleaner_spec.rb DELETED
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module DatabaseRewinder
4
- describe Cleaner do
5
- describe '#strategy=' do
6
- before { @cleaner = described_class.new(only: ['foos'], except: 'bars') }
7
-
8
- context 'without options' do
9
- before { @cleaner.strategy = :truncation }
10
-
11
- it 'should keep instance variables' do
12
- expect(@cleaner.instance_variable_get(:@only)).to eq(['foos'])
13
- expect(@cleaner.instance_variable_get(:@except)).to eq(['bars'])
14
- end
15
- end
16
-
17
- context 'with options (an array or a string)' do
18
- before { @cleaner.strategy = :truncation, { only: ['bars'], except: 'bazs' } }
19
-
20
- it 'should overwrite instance variables' do
21
- expect(@cleaner.instance_variable_get(:@only)).to eq(['bars'])
22
- expect(@cleaner.instance_variable_get(:@except)).to eq(['bazs'])
23
- end
24
- end
25
-
26
- context 'with options (an empty array or nil)' do
27
- before { @cleaner.strategy = :truncation, { only: [], except: nil } }
28
-
29
- it 'should overwrite instance variables even if they are empty/nil' do
30
- expect(@cleaner.instance_variable_get(:@only)).to eq([])
31
- expect(@cleaner.instance_variable_get(:@except)).to eq([])
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,193 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DatabaseRewinder do
4
- before do
5
- DatabaseRewinder.init
6
- end
7
-
8
- describe '.[]' do
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
38
- end
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
54
- end
55
- end
56
-
57
- describe '.record_inserted_table' do
58
- before do
59
- DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => 'db/test_record_inserted_table.sqlite3'}}
60
- @cleaner = DatabaseRewinder.create_cleaner 'foo'
61
- connection = double('connection').as_null_object
62
- connection.instance_variable_set :'@config', {adapter: 'sqlite3', database: File.expand_path('db/test_record_inserted_table.sqlite3', Rails.root) }
63
- DatabaseRewinder.record_inserted_table(connection, sql)
64
- end
65
- after do
66
- DatabaseRewinder.database_configuration = nil
67
- end
68
- subject { @cleaner }
69
-
70
- context 'common database' do
71
- context 'include database name' do
72
- let(:sql) { 'INSERT INTO "database"."foos" ("name") VALUES (?)' }
73
- its(:inserted_tables) { should == ['foos'] }
74
- end
75
- context 'only table name' do
76
- let(:sql) { 'INSERT INTO "foos" ("name") VALUES (?)' }
77
- its(:inserted_tables) { should == ['foos'] }
78
- end
79
- end
80
-
81
- context 'Database accepts more than one dots in an object notation (e.g. SQLServer)' do
82
- context 'full joined' do
83
- let(:sql) { 'INSERT INTO server.database.schema.foos ("name") VALUES (?)' }
84
- its(:inserted_tables) { should == ['foos'] }
85
- end
86
- context 'missing one' do
87
- let(:sql) { 'INSERT INTO database..foos ("name") VALUES (?)' }
88
- its(:inserted_tables) { should == ['foos'] }
89
- end
90
-
91
- context 'missing two' do
92
- let(:sql) { 'INSERT INTO server...foos ("name") VALUES (?)' }
93
- its(:inserted_tables) { should == ['foos'] }
94
- end
95
- end
96
-
97
- context 'when database accepts INSERT IGNORE INTO statement' do
98
- let(:sql) { "INSERT IGNORE INTO `foos` (`name`) VALUES ('alice'), ('bob') ON DUPLICATE KEY UPDATE `foos`.`updated_at`=VALUES(`updated_at`)" }
99
- its(:inserted_tables) { should == ['foos'] }
100
- end
101
- end
102
-
103
- describe '.clean' do
104
- before do
105
- Foo.create! name: 'foo1'
106
- Bar.create! name: 'bar1'
107
- DatabaseRewinder.clean
108
- end
109
- it 'should clean' do
110
- Foo.count.should == 0
111
- Bar.count.should == 0
112
- end
113
- end
114
-
115
- describe '.clean_all' do
116
- before do
117
- ActiveRecord::SchemaMigration.create_table
118
- ActiveRecord::SchemaMigration.create! version: '001'
119
- Foo.create! name: 'foo1'
120
- DatabaseRewinder.clean_all
121
- end
122
- after { ActiveRecord::SchemaMigration.drop_table }
123
- it 'should clean except schema_migrations' do
124
- Foo.count.should == 0
125
- ActiveRecord::SchemaMigration.count.should == 1
126
- end
127
- end
128
-
129
- describe '.clean_with' do
130
- before do
131
- @cleaner = DatabaseRewinder.cleaners.first
132
- @only = @cleaner.instance_variable_get(:@only)
133
- @except = @cleaner.instance_variable_get(:@except)
134
- Foo.create! name: 'foo1'
135
- Bar.create! name: 'bar1'
136
- DatabaseRewinder.clean_with :truncation, options
137
- end
138
-
139
- context 'with only option' do
140
- let(:options) { { only: ['foos'] } }
141
- it 'should clean with only option and restore original one' do
142
- Foo.count.should == 0
143
- Bar.count.should == 1
144
- expect(@cleaner.instance_variable_get(:@only)).to eq(@only)
145
- end
146
- end
147
-
148
- context 'with except option' do
149
- let(:options) { { except: ['bars'] } }
150
- it 'should clean with except option and restore original one' do
151
- Foo.count.should == 0
152
- Bar.count.should == 1
153
- expect(@cleaner.instance_variable_get(:@except)).to eq(@except)
154
- end
155
- end
156
- end
157
-
158
- describe '.strategy=' do
159
- context 'call first with options' do
160
- before do
161
- DatabaseRewinder.strategy = :truncate, { only: ['foos'], except: ['bars'] }
162
- end
163
-
164
- it 'should set options' do
165
- expect(DatabaseRewinder.instance_variable_get(:@only)).to eq(['foos'])
166
- expect(DatabaseRewinder.instance_variable_get(:@except)).to eq(['bars'])
167
- end
168
-
169
- it 'should create cleaner with options' do
170
- cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
171
- expect(cleaner.instance_variable_get(:@only)).to eq(['foos'])
172
- expect(cleaner.instance_variable_get(:@except)).to eq(['bars'])
173
- end
174
-
175
- context 'call again with different options' do
176
- before do
177
- DatabaseRewinder.strategy = :truncate, { only: ['bazs'], except: [] }
178
- end
179
-
180
- it 'should overwrite options' do
181
- expect(DatabaseRewinder.instance_variable_get(:@only)).to eq(['bazs'])
182
- expect(DatabaseRewinder.instance_variable_get(:@except)).to eq([])
183
- end
184
-
185
- it 'should overwrite cleaner with new options' do
186
- cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
187
- expect(cleaner.instance_variable_get(:@only)).to eq(['bazs'])
188
- expect(cleaner.instance_variable_get(:@except)).to eq([])
189
- end
190
- end
191
- end
192
- end
193
- end
data/spec/spec_helper.rb DELETED
@@ -1,17 +0,0 @@
1
- ENV['RAILS_ENV'] ||= 'test'
2
-
3
- $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
4
- $LOAD_PATH.unshift(__dir__)
5
-
6
- require 'rails'
7
- require 'database_rewinder'
8
- require 'fake_app'
9
-
10
- RSpec.configure do |config|
11
- config.before :all do
12
- CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'foos'
13
- end
14
- config.after :each do
15
- [Foo, Bar, Baz, Quu].each {|m| m.delete_all }
16
- end
17
- end