sequel-seed 0.1.4 → 0.2.1

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: fa4c96943080141c916b61f92f9c1a8757b1de76
4
- data.tar.gz: 1286b7fb1157a44fb6ebaa0e0b6c008b410d370e
3
+ metadata.gz: f5a1af2b07c026c54b028ea8b4153e4523a44faa
4
+ data.tar.gz: 1e1a6ed02f2cf6b09f9f7e30c54e8eb36e2004e3
5
5
  SHA512:
6
- metadata.gz: 31a90821eaeba184b477f362163d1b6e9c4a6fb4e68833097c11d5dab9a3cec1a678f6f0aff776753ad7e2b86239ea4602251ae1bdb898da34e8fdb61f79709c
7
- data.tar.gz: e27f4f75326aa710e6ff337f8d9d99fd0a21285c5f829c13c77a088d284ec32a35a64b1f0282284fdad3aaf5555bdb4c88303f4a76266e47d30b87a55ab9ff0e
6
+ metadata.gz: 5c10ccb47562c8bb4b6436a81220ebb9f8f88d6abca4388e6c6aa2350eb6f86a83feb97e20c7272817ca43479abf6c1086a7c2db476b8c02487ba5ccce85dcca
7
+ data.tar.gz: ba11dee2ccb4b7ccb28b05a0e79897462764e5701a33adc7430e886fe9f664992ae7c6c031034d40fbea2cba9f1f8def3d11159fb4ad462190889e01ae46b62f
data/CHANGELOG CHANGED
@@ -1,5 +1,14 @@
1
1
  ## Changelog
2
2
 
3
+ ### 0.2.1
4
+ - API changes to protect Sequel's namespace
5
+ - `Sequel::Seed.environment = :env` is also `Sequel::Seed.setup(:env)`
6
+ - `Sequel::Seed` class is now `Sequel::Seed::Base`; `Sequel::Seed` is now a module;
7
+ thus, there's no way to proxy the old `Sequel::Seed.apply` to the new `Sequel::Seed::Base.apply`
8
+ - `Sequel::Seeder` and `Sequel::TimestampSeeder` are still the same (no changes in interface as well)
9
+ - Improve test coverage to guarantee backward compatibility
10
+ - Minor hotfixes
11
+
3
12
  ### 0.1.4
4
13
  - Environment references could be a Symbol or String
5
14
  - Improve test coverage
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Sequel::Seed [![Gem Version](https://badge.fury.io/rb/sequel-seed.svg)](http://badge.fury.io/rb/sequel-seed)
1
+ # Sequel::Seed [![Gem Version](https://badge.fury.io/rb/sequel-seed.svg)](http://badge.fury.io/rb/sequel-seed) [![Build Status](https://travis-ci.org/earaujoassis/sequel-seed.svg?branch=master)](https://travis-ci.org/earaujoassis/sequel-seed) [![Coverage Status](https://coveralls.io/repos/earaujoassis/sequel-seed/badge.svg?branch=master&service=github)](https://coveralls.io/github/earaujoassis/sequel-seed?branch=master)
2
2
 
3
3
  > A Sequel extension to make seeds/fixtures manageable like migrations
4
4
 
@@ -22,7 +22,7 @@ end
22
22
  Set the environment
23
23
 
24
24
  ```rb
25
- Sequel::Seed.environment = :development
25
+ Sequel::Seed.setup :development
26
26
  ```
27
27
 
28
28
  Load the extension
@@ -1,9 +1,10 @@
1
1
  ##
2
2
  # Extension based upon Sequel::Migration and Sequel::Migrator
3
3
  #
4
- # Adds the Sequel::Seed and Sequel::Seeder classes, which allow
5
- # the user to easily group entity changes and seed/fixture the database
6
- # to a newer version only (unlike migrations, seeds are not directional).
4
+ # Adds the Sequel::Seed module and the Sequel::Seed::Base and Sequel::Seeder
5
+ # classes, which allow the user to easily group entity changes and seed/fixture
6
+ # the database to a newer version only (unlike migrations, seeds are not
7
+ # directional).
7
8
  #
8
9
  # To load the extension:
9
10
  #
@@ -11,79 +12,109 @@
11
12
  #
12
13
  # It is also important to set the environment:
13
14
  #
14
- # Sequel::Seed.environment = :development
15
+ # Sequel::Seed.setup(:development)
15
16
 
16
17
  module Sequel
17
- class Seed
18
+ class << self
19
+ ##
20
+ # Creates a Seed subclass according to the given +block+.
21
+ #
22
+ # The +env_labels+ lists on which environments the seed should be applicable.
23
+ # If the current environment is not applicable, the seed is ignored. On the
24
+ # other hand, if it is applicable, it will be listed in Seed.descendants and
25
+ # subject to application (if it was not applied yet).
26
+ #
27
+ # Expected seed call:
28
+ #
29
+ # Sequel.seed(:test) do # seed is only applicable to the test environment
30
+ # def run
31
+ # Entity.create attribute: value
32
+ # end
33
+ # end
34
+ #
35
+ # Wildcard seed:
36
+ #
37
+ # Sequel.seed do # seed is applicable to every environment, or no environment
38
+ # def run
39
+ # Entity.create attribute: value
40
+ # end
41
+ # end
42
+ #
43
+
44
+ def seed *env_labels, &block
45
+ return if env_labels.length > 0 && !env_labels.map(&:to_sym).include?(Seed.environment)
46
+
47
+ seed = Class.new(Seed::Base)
48
+ seed.class_eval(&block) if block_given?
49
+ Seed::Base.inherited(seed) unless Seed::Base.descendants.include?(seed)
50
+ seed
51
+ end
52
+ end
53
+
54
+ module Seed
18
55
  class << self
19
56
  attr_reader :environment
20
- end
21
57
 
22
- def self.apply
23
- new.run
24
- end
58
+ ##
59
+ # Sets the Sequel::Seed's environment to +env+ over which the Seeds should be applied
60
+ def setup(env, opts = {})
61
+ @environment = env.to_sym
62
+ @options ||= {}
63
+ @options[:disable_warning] ||= opts[:disable_warning] || false
64
+ end
25
65
 
26
- def self.descendants
27
- @descendants ||= []
28
- end
66
+ ##
67
+ # Keep backward compatibility on how to setup the Sequel::Seed environment
68
+ #
69
+ # Sets the environment +env+ over which the Seeds should be applied
70
+ def environment=(env)
71
+ setup(env)
72
+ end
29
73
 
30
- def self.inherited(base)
31
- descendants << base
32
- end
74
+ ##
75
+ # Keep backward compatibility on how to get Sequel::Seed::Base class descendants
76
+ def descendants
77
+ Base.descendants
78
+ end
33
79
 
34
- def self.environment=(env)
35
- @environment = env.to_sym
80
+ ##
81
+ # Keep backward compatibility on how to append a Sequel::Seed::Base descendant class
82
+ def inherited(base)
83
+ Base.inherited(base)
84
+ end
36
85
  end
37
86
 
38
- def run
39
- end
40
- end
87
+ class Base
88
+ class << self
89
+ def apply
90
+ new.run
91
+ end
41
92
 
42
- ##
43
- # Creates a Seed subclass according to the given +block+.
44
- #
45
- # The +env_labels+ lists on which environments the seed should be applicable.
46
- # If the current environment is not applicable, the seed is ignored. On the
47
- # other hand, if it is applicable, it will be listed in Seed.descendants and
48
- # subject to application (if it was not applied yet).
49
- #
50
- # Expected seed call:
51
- #
52
- # Sequel.seed(:test) do # seed is only applicable to the test environment
53
- # def run
54
- # Entity.create attribute: value
55
- # end
56
- # end
57
- #
58
- # Wildcard seed:
59
- #
60
- # Sequel.seed do # seed is applicable to every environment, or no environment
61
- # def run
62
- # Entity.create attribute: value
63
- # end
64
- # end
65
- #
93
+ def descendants
94
+ @descendants ||= []
95
+ end
66
96
 
67
- def self.seed *env_labels, &block
68
- return if env_labels.length > 0 && !env_labels.map(&:to_sym).include?(Seed.environment)
97
+ def inherited(base)
98
+ descendants << base
99
+ end
100
+ end
69
101
 
70
- seed = Class.new(Seed)
71
- seed.class_eval(&block) if block_given?
72
- Seed.inherited(seed) unless Seed.descendants.include?(seed)
73
- seed
74
- end
102
+ def run
103
+ end
104
+ end
75
105
 
76
- ##
77
- # Class resposible for applying all the seeds related to the current environment,
78
- # if and only if they were not previously applied.
79
- #
80
- # To apply the seeds/fixtures:
81
- #
82
- # Sequel::Seeder.apply(db, directory)
83
- #
84
- # +db+ holds the Sequel database connection
85
- #
86
- # +directory+ the path to the seeds/fixtures files
106
+ ##
107
+ # Class resposible for applying all the seeds related to the current environment,
108
+ # if and only if they were not previously applied.
109
+ #
110
+ # To apply the seeds/fixtures:
111
+ #
112
+ # Sequel::Seeder.apply(db, directory)
113
+ #
114
+ # +db+ holds the Sequel database connection
115
+ #
116
+ # +directory+ the path to the seeds/fixtures files
117
+ end
87
118
 
88
119
  class Seeder
89
120
  SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb|json|yml)\z/i.freeze
@@ -106,7 +137,7 @@ module Sequel
106
137
  next unless SEED_FILE_PATTERN.match(file)
107
138
  return TimestampSeeder if file.split(SEED_SPLITTER, 2).first.to_i > MINIMUM_TIMESTAMP
108
139
  end
109
- raise(Error, "seeder not available for files")
140
+ raise(Error, "seeder not available for files; please checked the directory")
110
141
  else
111
142
  self
112
143
  end
@@ -154,10 +185,10 @@ module Sequel
154
185
  end
155
186
 
156
187
  def remove_seed_classes
157
- Seed.descendants.each do |c|
188
+ Seed::Base.descendants.each do |c|
158
189
  Object.send(:remove_const, c.to_s) rescue nil
159
190
  end
160
- Seed.descendants.clear
191
+ Seed::Base.descendants.clear
161
192
  end
162
193
 
163
194
  def seed_version_from_file(filename)
@@ -230,14 +261,14 @@ module Sequel
230
261
  def get_seed_tuples
231
262
  remove_seed_classes
232
263
  seeds = []
233
- ms = Seed.descendants
264
+ ms = Seed::Base.descendants
234
265
  files.each do |path|
235
266
  f = File.basename(path)
236
267
  fi = f.downcase
237
268
  if !applied_seeds.include?(fi)
238
269
  load(path)
239
270
  el = [ms.last, f]
240
- if ms.last.present? && !seeds.include?(el)
271
+ if !ms.last.nil? && !seeds.include?(el)
241
272
  seeds << [ms.last, f]
242
273
  end
243
274
  end
@@ -1,152 +1,211 @@
1
1
  require 'spec_helper'
2
2
 
3
- Sequel::Seed.environment = :test
4
-
5
3
  describe Sequel.seed do
4
+ before do
5
+ Sequel::Seed.setup(:test)
6
+ end
7
+
6
8
  it 'should create a Seed descendant according to the current environment' do
7
9
  seed = Sequel.seed(:test) {}
8
- expect(Sequel::Seed.descendants).to include seed
10
+ expect(Sequel::Seed::Base.descendants).to include seed
9
11
  end
10
12
 
11
13
  it 'should ignore a Seed not applicable to the current environment' do
12
14
  seed = Sequel.seed(:development) {}
13
- expect(Sequel::Seed.descendants).not_to include seed
15
+ expect(Sequel::Seed::Base.descendants).not_to include seed
14
16
  end
15
17
 
16
18
  it 'should create a Seed applicable to every environment' do
17
19
  seed = Sequel.seed {}
18
- expect(Sequel::Seed.descendants).to include seed
20
+ expect(Sequel::Seed::Base.descendants).to include seed
21
+ end
22
+ end
23
+
24
+ describe Sequel::Seed.environment do
25
+ it 'should be possible to set the environment with Sequel::Seed.setup method' do
26
+ Sequel::Seed.setup(:mock)
27
+ expect(Sequel::Seed.environment).to eq :mock
28
+ Sequel::Seed.setup("test")
29
+ expect(Sequel::Seed.environment).to eq :test
30
+ end
31
+
32
+ it 'should be possible to set the environment with Sequel::Seed.environment= method' do
33
+ Sequel::Seed.environment = :mock
34
+ expect(Sequel::Seed.environment).to eq :mock
35
+ Sequel::Seed.environment = "test"
36
+ expect(Sequel::Seed.environment).to eq :test
37
+ end
38
+ end
39
+
40
+ describe Sequel::Seed do
41
+ describe "to guarantee backward compatibility" do
42
+ it "should point Sequel::Seed.descendants to Sequel::Seed::Base.descendants" do
43
+ Sequel::Seed::Base.descendants << 'hi'
44
+ expect(Sequel::Seed.descendants).to contain_exactly('hi')
45
+ end
46
+
47
+ it "should point Sequel::Seed.inherited() to Sequel::Seed::Base.inherited()" do
48
+ Sequel::Seed::Base.inherited('1')
49
+ Sequel::Seed.inherited('2')
50
+ expect(Sequel::Seed.descendants).to contain_exactly('1', '2')
51
+ end
19
52
  end
20
53
  end
21
54
 
22
55
  describe Sequel::Seeder do
23
56
  let(:DB) {Sequel.sqlite}
57
+ let!(:environment) {"#{Faker::Lorem.word}_#{Faker::Lorem.word}"}
58
+
59
+ it "should raise an error when there is not any seed file to apply" do
60
+ Sequel::Seed.setup environment
61
+
62
+ expect(Sequel::Seed::Base.descendants.length).to be 0
63
+ expect {Sequel::Seeder.apply(DB, '/')}.to raise_error("seeder not available for files; please checked the directory")
64
+ expect(SpecModel.dataset.all.length).to be 0
65
+ end
24
66
 
25
67
  describe 'environment references should be indistinguishable between Symbol and String' do
26
68
  context 'when the environment is defined using a String' do
27
69
  it 'should apply the Seed accordingly' do
28
- Sequel::Seed.environment = "test"
29
-
30
- Sequel.seed(:test) do
31
- def run
32
- SpecModel.create :name => 'environment defined by String'
33
- end
70
+ Sequel::Seed.setup environment
71
+
72
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
73
+ f.puts "Sequel.seed(:#{environment}) do"
74
+ f.puts ' def run'
75
+ f.puts ' SpecModel.create :sentence => \'environment defined by String\''
76
+ f.puts ' end'
77
+ f.puts 'end'
34
78
  end
35
79
 
36
- expect(Sequel::Seed.descendants.length).to be 1
37
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
38
- expect(Sequel::Seed.descendants.length).to be 0
80
+ expect(Sequel::Seed::Base.descendants.length).to be 0
81
+ expect(Sequel::Seeder.seeder_class(seed_test_dir)).to be Sequel::TimestampSeeder
82
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
83
+ expect(Sequel::Seed::Base.descendants.length).to be 1
39
84
  expect(SpecModel.dataset.all.length).to be 1
40
- expect(SpecModel.dataset.first.name).to eq 'environment defined by String'
85
+ expect(SpecModel.dataset.first.sentence).to eq 'environment defined by String'
41
86
  end
42
87
  end
43
88
 
44
89
  context 'when the Seed is defined using a String' do
45
90
  it 'should apply the Seed accordingly' do
46
- Sequel::Seed.environment = :test
47
-
48
- Sequel.seed("test") do
49
- def run
50
- SpecModel.create :name => 'Seed defined by String'
51
- end
91
+ Sequel::Seed.setup environment.to_sym
92
+
93
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
94
+ f.puts "Sequel.seed(\"#{environment}\") do"
95
+ f.puts ' def run'
96
+ f.puts ' SpecModel.create :sentence => \'Seed defined by String\''
97
+ f.puts ' end'
98
+ f.puts 'end'
52
99
  end
53
100
 
54
- expect(Sequel::Seed.descendants.length).to be 1
55
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
56
- expect(Sequel::Seed.descendants.length).to be 0
101
+ expect(Sequel::Seed::Base.descendants.length).to be 0
102
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
103
+ expect(Sequel::Seed::Base.descendants.length).to be 1
57
104
  expect(SpecModel.dataset.all.length).to be 1
58
- expect(SpecModel.dataset.first.name).to eq 'Seed defined by String'
105
+ expect(SpecModel.dataset.first.sentence).to eq 'Seed defined by String'
59
106
  end
60
107
  end
61
108
 
62
109
  context 'when both Seed and environment are defined using a String' do
63
110
  it 'should apply the Seed accordingly' do
64
- Sequel::Seed.environment = "test"
65
-
66
- Sequel.seed("test") do
67
- def run
68
- SpecModel.create :name => 'Seed and environment defined by String'
69
- end
111
+ Sequel::Seed.setup environment
112
+
113
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
114
+ f.puts "Sequel.seed(\"#{environment}\") do"
115
+ f.puts ' def run'
116
+ f.puts ' SpecModel.create :sentence => \'Seed and environment defined by String\''
117
+ f.puts ' end'
118
+ f.puts 'end'
70
119
  end
71
120
 
72
- expect(Sequel::Seed.descendants.length).to be 1
73
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
74
- expect(Sequel::Seed.descendants.length).to be 0
121
+ expect(Sequel::Seed::Base.descendants.length).to be 0
122
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
123
+ expect(Sequel::Seed::Base.descendants.length).to be 1
75
124
  expect(SpecModel.dataset.all.length).to be 1
76
- expect(SpecModel.dataset.first.name).to eq 'Seed and environment defined by String'
125
+ expect(SpecModel.dataset.first.sentence).to eq 'Seed and environment defined by String'
77
126
  end
78
127
  end
79
128
 
80
129
  context 'when both Seed and environment are defined using a Symbol' do
81
130
  it 'should apply the Seed accordingly' do
82
- Sequel::Seed.environment = :test
83
-
84
- Sequel.seed(:test) do
85
- def run
86
- SpecModel.create :name => 'Seed and environment defined by Symbol'
87
- end
131
+ Sequel::Seed.setup environment.to_sym
132
+
133
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
134
+ f.puts "Sequel.seed(:#{environment}) do"
135
+ f.puts ' def run'
136
+ f.puts ' SpecModel.create :sentence => \'Seed and environment defined by Symbol\''
137
+ f.puts ' end'
138
+ f.puts 'end'
88
139
  end
89
140
 
90
- expect(Sequel::Seed.descendants.length).to be 1
91
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
92
- expect(Sequel::Seed.descendants.length).to be 0
141
+ expect(Sequel::Seed::Base.descendants.length).to be 0
142
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
143
+ expect(Sequel::Seed::Base.descendants.length).to be 1
93
144
  expect(SpecModel.dataset.all.length).to be 1
94
- expect(SpecModel.dataset.first.name).to eq 'Seed and environment defined by Symbol'
145
+ expect(SpecModel.dataset.first.sentence).to eq 'Seed and environment defined by Symbol'
95
146
  end
96
147
  end
97
148
 
98
149
  context 'when the environment is defined using a String and we have a wildcard Seed' do
99
150
  it 'should apply the Seed accordingly' do
100
- Sequel::Seed.environment = "test"
101
-
102
- Sequel.seed do
103
- def run
104
- SpecModel.create :name => 'Wildcard Seed and environment defined by String'
105
- end
151
+ Sequel::Seed.setup environment
152
+
153
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
154
+ f.puts 'Sequel.seed do'
155
+ f.puts ' def run'
156
+ f.puts ' SpecModel.create :sentence => \'Wildcard Seed and environment defined by String\''
157
+ f.puts ' end'
158
+ f.puts 'end'
106
159
  end
107
160
 
108
- expect(Sequel::Seed.descendants.length).to be 1
109
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
110
- expect(Sequel::Seed.descendants.length).to be 0
161
+ expect(Sequel::Seed::Base.descendants.length).to be 0
162
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
163
+ expect(Sequel::Seed::Base.descendants.length).to be 1
111
164
  expect(SpecModel.dataset.all.length).to be 1
112
- expect(SpecModel.dataset.first.name).to eq 'Wildcard Seed and environment defined by String'
165
+ expect(SpecModel.dataset.first.sentence).to eq 'Wildcard Seed and environment defined by String'
113
166
  end
114
167
  end
115
168
  end
116
169
 
117
- context 'when there\'s no Seed created' do
118
- it 'should not make any change to the database' do
119
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
120
- expect(SpecModel.dataset.all.length).to be 0
121
- end
122
- end
123
-
124
170
  context 'when there\'s a Seed created' do
125
171
  it 'should change the database accordingly only once' do
126
- Sequel.seed do
127
- def run
128
- SpecModel.create :name => 'should have changed'
129
- end
172
+ Sequel::Seed.setup environment
173
+
174
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
175
+ f.puts 'Sequel.seed do'
176
+ f.puts ' def run'
177
+ f.puts ' SpecModel.create :sentence => \'should have changed\''
178
+ f.puts ' end'
179
+ f.puts 'end'
130
180
  end
131
181
 
132
- expect(Sequel::Seed.descendants.length).to be 1
133
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
134
- expect(Sequel::Seed.descendants.length).to be 0
182
+ expect(Sequel::Seed::Base.descendants.length).to be 0
183
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
184
+ expect(Sequel::Seed::Base.descendants.length).to be 1
185
+ expect(SpecModel.dataset.all.length).to be 1
186
+ expect(SpecModel.dataset.first.sentence).to eq 'should have changed'
187
+ # Once again
188
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
189
+ expect(Sequel::Seed::Base.descendants.length).to be 0
135
190
  expect(SpecModel.dataset.all.length).to be 1
136
- expect(SpecModel.dataset.first.name).to eq 'should have changed'
191
+ expect(SpecModel.dataset.first.sentence).to eq 'should have changed'
137
192
  end
138
193
  end
139
194
 
140
195
  context 'when the specified Seed is not applicable to the current environment' do
141
196
  it 'should not make any change to the database' do
142
- Sequel.seed(:hithere) do
143
- def run
144
- SpecModel.create :name => 'should not have changed'
145
- end
197
+ Sequel::Seed.setup environment
198
+
199
+ File.open("#{seed_test_dir}/#{seed_file_name}", 'w+') do |f|
200
+ f.puts "Sequel.seed(:another_#{Faker::Lorem.word}_word) do"
201
+ f.puts ' def run'
202
+ f.puts ' SpecModel.create :sentence => \'should have changed\''
203
+ f.puts ' end'
204
+ f.puts 'end'
146
205
  end
147
206
 
148
- expect(Sequel::Seed.descendants.length).to be 0
149
- expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
207
+ expect(Sequel::Seed::Base.descendants.length).to be 0
208
+ expect {Sequel::Seeder.apply(DB, seed_test_dir)}.not_to raise_error
150
209
  expect(SpecModel.dataset.all.length).to be 0
151
210
  end
152
211
  end
data/spec/spec_helper.rb CHANGED
@@ -1,56 +1,52 @@
1
1
  require 'bundler/setup'
2
- Bundler.setup(:default, :development)
2
+ Bundler.setup(:default, :development, :test)
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear!
3
6
 
4
7
  require 'sequel'
5
- require 'faker'
6
8
  require File.expand_path(File.dirname(__FILE__) + '/../lib/sequel/extensions/seed.rb')
7
9
 
8
- RSpec.configure do |config|
9
- Sequel.extension :seed
10
-
11
- class Sequel::Seeder
12
- def self.seeder_class(directory)
13
- if self.equal?(Sequel::Seeder)
14
- return Sequel::TimestampSeeder
15
- else
16
- self
17
- end
18
- end
19
- end
10
+ require 'faker'
20
11
 
21
- class Sequel::TimestampSeeder
22
- def run
23
- seed_tuples.each {|s| s.apply}
24
- Sequel::Seed.descendants.clear
12
+ module Sequel::Seed
13
+ module TestHelper
14
+ def seed_test_dir
15
+ @test_dir ||= ENV['TEST_PATH'] || './.testing'
25
16
  end
26
17
 
27
- private
28
-
29
- def get_applied_seeds
30
- []
18
+ def seed_file_name
19
+ "#{Time.now.strftime('%Y%m%d%H%M%S')}_testing_#{Faker::Lorem.word}_#{Faker::Lorem.word}.rb"
31
20
  end
21
+ end
22
+ end
32
23
 
33
- def get_seed_files
34
- []
35
- end
24
+ RSpec.configure do |config|
25
+ include Sequel::Seed::TestHelper
36
26
 
37
- def get_seed_tuples
38
- Sequel::Seed.descendants
39
- end
40
- end
27
+ Sequel.extension :seed
41
28
 
42
29
  DB = Sequel.sqlite
43
30
 
44
31
  DB.create_table(:spec_models) do
45
32
  primary_key :id, :auto_increment => true
46
- String :name
33
+ String :sentence
34
+ end
35
+
36
+ config.before(:suite) do
37
+ FileUtils.mkdir_p(seed_test_dir)
47
38
  end
48
39
 
49
40
  config.before(:each) do
50
41
  SpecModel.dataset.delete
51
- Sequel::Seed.descendants.clear
42
+ Sequel::Seed::Base.descendants.clear
43
+ # QUICK FIX: Somehow the dataset models are not excluded fast enough
44
+ sleep(0.750)
52
45
  end
53
- end
54
46
 
55
- class SpecModel < Sequel::Model
47
+ config.after(:suite) do
48
+ FileUtils.remove_dir(seed_test_dir, true)
49
+ end
56
50
  end
51
+
52
+ class SpecModel < Sequel::Model; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ewerton Assis
@@ -48,7 +48,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
48
  requirements:
49
49
  - - ">="
50
50
  - !ruby/object:Gem::Version
51
- version: 1.9.1
51
+ version: 1.9.3
52
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="