sequel-fixture 0.0.3 → 2.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.
data/.gitignore CHANGED
@@ -1,17 +1,21 @@
1
1
  *.gem
2
2
  *.rbc
3
+ Gemfile.lock
3
4
  .bundle
4
5
  .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
6
  coverage
10
- doc/
7
+ vendor/
8
+ InstalledFiles
11
9
  lib/bundler/man
12
10
  pkg
13
11
  rdoc
14
12
  spec/reports
15
13
  test/tmp
16
14
  test/version_tmp
15
+ .bin/
17
16
  tmp
17
+
18
+ # YARD artifacts
19
+ .yardoc
20
+ _yardoc
21
+ doc/
data/.travis.yml CHANGED
@@ -8,4 +8,4 @@ rvm:
8
8
  - rbx-18mode
9
9
  - rbx-19mode
10
10
  - ree
11
- bundler_args: --without cucumber
11
+
data/Gemfile CHANGED
@@ -3,13 +3,8 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in sequel-fixture.gemspec
4
4
  gemspec
5
5
 
6
- group :cucumber do
7
- gem "cucumber"
8
- gem "sqlite3"
9
- gem "pry"
10
- end
11
-
12
6
  group :test do
7
+ gem "sqlite3"
13
8
  gem "rake"
14
9
  end
15
10
 
data/README.md CHANGED
@@ -2,35 +2,57 @@ Sequel::Fixture
2
2
  ===============
3
3
  [![Build Status](https://secure.travis-ci.org/Fetcher/sequel-fixture.png)](http://travis-ci.org/Fetcher/sequel-fixture) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/Fetcher/sequel-fixture)
4
4
 
5
- Just like Rails 2 fixtures, but for Sequel.
5
+ Just like Rails fixtures, but for Sequel.
6
6
 
7
- Show off
8
- ========
7
+ This version includes support for defining the fixture schemas
8
+ and uses sqlite3 adapter to inject data into memory.
9
+
10
+ Usage
11
+ =====
12
+ Each fixture file defines the schema and data for a single table which
13
+ is named after the file name.
14
+
15
+ Schema definition is optional, but note that db inserts will fail if the tables do
16
+ not exist.
9
17
 
10
18
  Assuming you have a fixture for the table users with:
11
19
  ```yaml
12
- # test/fixtures/simple/users.yaml
13
- john:
14
- name: John
15
- last_name: Doe
16
- email: john@doe.com
17
- jane:
18
- name: Jane
19
- last_name: Doe
20
- email: jane@doe.com
20
+ # fixtures/simple/users.yaml
21
+ schema:
22
+ - name: name
23
+ type: string
24
+ primary_key: true
25
+ - name: last_name
26
+ type: string
27
+ - name: empty
28
+ type: string
29
+ data:
30
+ - name: John
31
+ last_name: Doe
32
+ email: john@doe.com
33
+ - name: Jane
34
+ last_name: Doe
35
+ email: jane@doe.com
21
36
  ```
22
37
 
23
38
  and for messages:
24
39
  ```yaml
25
- # test/fixtures/simple/messages.yaml
26
- greeting:
27
- sender_id: 1
28
- receiver_id: 2
29
- text: Hi Jane! Long time no see.
30
- long_time:
31
- sender_id: 2
32
- receiver_id: 1
33
- text: John! Long time indeed. How are you doing?
40
+ # fixtures/simple/messages.yaml
41
+ schema:
42
+ - name: sender_id
43
+ type: integer
44
+ primary_key: true
45
+ - name: receiver_id
46
+ type: integer
47
+ - name: text
48
+ type: string
49
+ data:
50
+ - sender_id: 1
51
+ receiver_id: 2
52
+ text: Hi Jane! Long time no see.
53
+ - sender_id: 2
54
+ receiver_id: 1
55
+ text: John! Long time indeed. How are you doing?
34
56
  ```
35
57
 
36
58
  and the ruby script
@@ -41,6 +63,9 @@ require "sequel-fixture"
41
63
 
42
64
  DB = Sequel.sqlite # Just a simple example, needs sqlite3 gem
43
65
 
66
+ ## Set the path of the fixture yaml files to be [script.rb]/fixtures/
67
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
68
+
44
69
  fixture = Sequel::Fixture.new :simple, DB # Will load all the data in the fixture into the database
45
70
 
46
71
  fixture.users # == fixture[:users]
@@ -65,7 +90,13 @@ fixture.rollback # Don't forget to rollback
65
90
 
66
91
  ...naturally, `sequel-fixture` makes a lot more sense within some testing framework.
67
92
 
68
- > **Note**: As of version 0.0.1, the `test/fixtures` path for fixtures is not configurable. Will solve soon.
93
+
94
+ Contributing
95
+ ------------
96
+
97
+ ```
98
+ bundle install --binstubs .bin --path vendor/bundle
99
+ ```
69
100
 
70
101
  Installation
71
102
  ------------
@@ -0,0 +1,17 @@
1
+ class Sequel::Fixture
2
+ class IllegalFixtureFormat < StandardError; end
3
+ class TablesNotEmptyError < StandardError; end
4
+ class RollbackIllegalError < StandardError; end
5
+ class MissingFixtureError < StandardError; end
6
+ class MissingConnectionError < StandardError; end
7
+ class LoadingFixtureIllegal < StandardError; end
8
+ class ChangingConnectionIllegal < StandardError; end
9
+
10
+ class MissingProcessedValueError < StandardError
11
+ attr_accessor :field
12
+ def initialize message, field = nil
13
+ @field = field
14
+ super message
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ module Sequel; end
2
+
3
+ class Sequel::Fixture
4
+
5
+ # === Description
6
+ # Class which represents the actual fixture data in a table
7
+ #
8
+ class Table
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def [](row)
14
+ Sequel::Fixture::Row.new(@data[row])
15
+ end
16
+
17
+ def rows
18
+ @data
19
+ end
20
+ end
21
+
22
+
23
+ # === Description
24
+ # Class which represents a single row in a fixture table.
25
+ #
26
+ class Row
27
+ def initialize(row)
28
+ @data = row
29
+ end
30
+
31
+ # === Description
32
+ # Method missing, for enabling discovery of columns within a row
33
+ #
34
+ def method_missing(s, *args)
35
+ key = s.to_s
36
+ return @data[key] if @data && @data.has_key?(key)
37
+ return super
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ class Sequel::Fixture
2
+
3
+ # === Description
4
+ # Simplifies the hash in order to insert it into the database
5
+ # (Note: I'm well aware that this functionality belongs in a dependency)
6
+ #
7
+ def simplify(the_hash)
8
+ the_returned_hash = {}
9
+
10
+ the_hash.each do |key, value|
11
+ if value.is_a? Hash
12
+ unless value.has_key?("processed") || value.has_key?(:processed)
13
+ raise MissingProcessedValueError.new "The processed value to insert into the db is missing from the field '#{key}', aborting", key
14
+ end
15
+ the_returned_hash[key] = value["processed"] || value[:processed]
16
+ else
17
+ the_returned_hash[key] = value
18
+ end
19
+ end
20
+ return the_returned_hash
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  module Sequel
2
2
  class Fixture
3
- VERSION = "0.0.3"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -3,143 +3,195 @@ require "symbolmatrix"
3
3
  require "fast"
4
4
 
5
5
  require "sequel-fixture/version"
6
+ require "sequel-fixture/exceptions"
7
+ require "sequel-fixture/util"
8
+ require "sequel-fixture/table"
6
9
 
7
- module Sequel
10
+ module Sequel; end
8
11
 
9
- # Fixture managing class for Sequel
10
- class Fixture
11
- ## Class methods
12
-
13
- # Returns the current path to the fixtures folder
14
- def self.path
15
- @@path ||= "test/fixtures"
16
- end
17
-
18
-
19
- ## Instance methods
20
-
21
- # Initializes the fixture handler
22
- # Accepts optionally a symbol as a reference to the fixture
23
- # and a Sequel::Database connection
24
- def initialize fixture = nil, connection = nil, option_push = true
25
- load fixture if fixture
26
-
27
- @connection = connection if connection
28
- push if fixture && connection && option_push
12
+ class Sequel::Fixture
13
+
14
+ # === Description
15
+ # Returns the current path to the fixtures folder
16
+ #
17
+ def self.path
18
+ @@path ||= "test/fixtures"
19
+ end
20
+
21
+
22
+ # === Description
23
+ # Set the current path of the fixtures folder
24
+ #
25
+ def self.path=(path)
26
+ @@path = path
27
+ end
28
+
29
+ # === Description
30
+ # Initializes the fixture handler
31
+ # Accepts optionally a symbol as a reference to the fixture
32
+ # and a Sequel::Database connection
33
+ def initialize(fixture = nil, connection = nil, option_push = true)
34
+ @schema = {}
35
+ @data = {}
36
+
37
+ load(fixture) if fixture
38
+
39
+ @connection = connection if connection
40
+ push if fixture && connection && option_push
41
+ end
42
+
43
+
44
+ # === Description
45
+ # Loads the fixture files into this instance
46
+ #
47
+ def load(fixture)
48
+ raise LoadingFixtureIllegal, "A check has already been made, loading a different fixture is illegal" if @checked
49
+
50
+ Fast.dir("#{fixtures_path}/#{fixture}").files.to.symbols.each do |file|
51
+ @data ||= {}
52
+ @schema ||= {}
53
+
54
+ file_data = SymbolMatrix.new "#{fixtures_path}/#{fixture}/#{file}.yaml"
55
+
56
+ if file_data
57
+ @data[file] = Table.new(file_data[:data]) if file_data.key?(:data)
58
+ @schema[file] = file_data[:schema] if file_data.key?(:schema)
59
+ end
29
60
  end
61
+ end
62
+
63
+
64
+ # === Description
65
+ # Returns the current fixtures path where Sequel::Fixture looks for fixture folders
66
+ #
67
+ def fixtures_path
68
+ Sequel::Fixture.path
69
+ end
70
+
71
+
72
+ # === Description
73
+ # For enabling discovery of tables
74
+ #
75
+ def method_missing(key, *args)
76
+ return @data[key] if @data && @data.has_key?(key)
77
+ return super
78
+ end
79
+
80
+ # === Description
81
+ # Returns the SymbolMatrix with the data referring to that table
82
+ #
83
+ def [](reference)
84
+ @data[reference]
85
+ end
86
+
87
+
88
+ # === Description
89
+ # Forces the check to pass. Dangerous!
90
+ #
91
+ def force_checked!
92
+ @checked = true
93
+ end
94
+
95
+
96
+ # === Description
97
+ # Assures that the tables are empty before proceeding
98
+ #
99
+ def check
100
+ return @checked if @checked # If already checked, it's alright
101
+
102
+ raise MissingFixtureError, "No fixture has been loaded, nothing to check" unless @data.length > 0
103
+ raise MissingConnectionError, "No connection has been provided, impossible to check" unless @connection
30
104
 
31
- # Loads the fixture files into this instance
32
- def load fixture
33
- raise LoadingFixtureIllegal, "A check has already been made, loading a different fixture is illegal" if @checked
34
-
35
- Fast.dir("#{fixtures_path}/#{fixture}").files.to.symbols.each do |file|
36
- @data ||= {}
37
- @data[file] = SymbolMatrix.new "#{fixtures_path}/#{fixture}/#{file}.yaml"
105
+ @data.each_key do |table|
106
+ if @connection[table].count != 0
107
+ raise TablesNotEmptyError, "Table '#{table}' is not empty, tables must be empty prior to testing"
38
108
  end
39
109
  end
40
-
41
- # Returns the current fixtures path where Sequel::Fixtures looks for fixture folders
42
- def fixtures_path
43
- Sequel::Fixture.path
110
+ return @checked = true
111
+ end
112
+
113
+
114
+ # === Description
115
+ # Initializes fixture schema and Inserts the fixture data into the corresponding
116
+ # tables
117
+ #
118
+ def push
119
+ check
120
+
121
+ @schema.each do |table, matrix|
122
+ push_schema(table, matrix)
44
123
  end
45
124
 
46
- # Returns the SymbolMatrix with the data referring to that table
47
- def [] reference
48
- @data[reference]
125
+ @data.each do |table_name, table_data|
126
+ table_data.rows.each do |values|
127
+ begin
128
+ @connection[table_name].insert(simplify(values.to_h))
129
+ rescue MissingProcessedValueError => m
130
+ rollback
131
+ raise MissingProcessedValueError, "In record '#{values.to_h}' to be inserted into '#{table_name}', the processed value of field '#{m.field}' is missing, aborting."
132
+ rescue NoMethodError => e
133
+ raise IllegalFixtureFormat, "In record '#{values}', data must be formatted as arrays of hashes. Check 'data' section in '#{table_name}.yaml'"
134
+ end
135
+ end
49
136
  end
50
-
51
- # Method missing, for enabling discovery of tables
52
- def method_missing s, *args
53
- return @data[s] if @data && @data.has_key?(s)
54
- return super
137
+ end
138
+
139
+
140
+ # === Description
141
+ # Create the schema in our DB connection based on the schema values
142
+ #
143
+ def push_schema(table, values)
144
+ ## Lets passively ignore the schema if the table already exists
145
+ return if @connection.table_exists?(table.to_sym)
146
+
147
+ ## Find the primary key
148
+ pkey_data = nil
149
+ values.each do |column_def|
150
+ pkey_data = column_def if column_def["primary_key"]
55
151
  end
56
152
 
57
- # Forces the check to pass. Dangerous!
58
- def force_checked!
59
- @checked = true
153
+ ## Create the table with the primary key
154
+ @connection.create_table(table) do
155
+ column(pkey_data["name"].to_sym, pkey_data["type"].to_sym)
60
156
  end
61
-
62
- # Assures that the tables are empty before proceeding
63
- def check
64
- return @checked if @checked # If already checked, it's alright
65
157
 
66
- raise MissingFixtureError, "No fixture has been loaded, nothing to check" unless @data
67
- raise MissingConnectionError, "No connection has been provided, impossible to check" unless @connection
68
-
69
- @data.each_key do |table|
70
- raise TablesNotEmptyError, "The table '#{table}' is not empty, all tables should be empty prior to testing" if @connection[table].count != 0
158
+ ## Add the rest of the columns
159
+ values.each do |column_def|
160
+ unless column_def["primary_key"]
161
+ @connection.alter_table(table) { add_column(column_def["name"].to_sym, column_def["type"].to_sym) }
71
162
  end
72
- return @checked = true
73
163
  end
74
-
75
- # Inserts the fixture data into the corresponding tables
76
- def push
164
+ end
165
+
166
+
167
+ # === Description
168
+ # Empties the tables, only if they were empty to begin with
169
+ #
170
+ def rollback
171
+ begin
77
172
  check
78
173
 
79
- @data.each do |table, matrix|
80
- matrix.each do |element, values|
81
- begin
82
- @connection[table].insert simplify values.to_hash
83
- rescue MissingProcessedValueError => m
84
- rollback
85
- raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{table}', the processed value of field '#{m.field}' is missing, aborting"
86
- end
87
- end
88
- end
89
- end
90
-
91
- # Empties the tables, only if they were empty to begin with
92
- def rollback
93
- begin
94
- check
95
-
96
- @data.each_key do |table|
97
- @connection[table].truncate
98
- end
99
- rescue TablesNotEmptyError => e
100
- raise RollbackIllegalError, "The tables weren't empty to begin with, rollback aborted."
101
- end
102
- end
103
-
104
- attr_reader :connection
105
-
106
- # Sets the connection. Raises an ChangingConnectionIllegal exception if this fixture has already been checked
107
- def connection= the_connection
108
- raise ChangingConnectionIllegal, "A check has already been performed, changing the connection now is illegal" if @checked
109
- @connection = the_connection
110
- end
111
-
112
- attr_reader :data
113
-
114
- # Simplifies the hash in order to insert it into the database
115
- # (Note: I'm well aware that this functionality belongs in a dependency)
116
- def simplify the_hash
117
- the_returned_hash = {}
118
- the_hash.each do |key, value|
119
- if value.is_a? Hash
120
- unless value.has_key? :processed
121
- raise MissingProcessedValueError.new "The processed value to insert into the db is missing from the field '#{key}', aborting", key
122
- end
123
- the_returned_hash[key] = value[:processed]
124
- else
125
- the_returned_hash[key] = value
126
- end
174
+ @data.each_key do |table|
175
+ @connection[table].truncate
127
176
  end
128
- return the_returned_hash
177
+ rescue TablesNotEmptyError => e
178
+ raise RollbackIllegalError, "The tables weren't empty to begin with, rollback aborted."
129
179
  end
180
+ end
130
181
 
131
- class TablesNotEmptyError < StandardError; end
132
- class RollbackIllegalError < StandardError; end
133
- class MissingFixtureError < StandardError; end
134
- class MissingConnectionError < StandardError; end
135
- class LoadingFixtureIllegal < StandardError; end
136
- class ChangingConnectionIllegal < StandardError; end
137
- class MissingProcessedValueError < StandardError
138
- attr_accessor :field
139
- def initialize message, field = nil
140
- @field = field
141
- super message
142
- end
182
+
183
+ # === Description
184
+ # Sets the connection. Raises an ChangingConnectionIllegal exception if this fixture has
185
+ # already been checked
186
+ #
187
+ def connection=(the_connection)
188
+ if @checked
189
+ raise ChangingConnectionIllegal, "Illegal to change connection after check has already been performed"
143
190
  end
191
+ @connection = the_connection
144
192
  end
193
+
194
+ attr_reader :connection
195
+ attr_reader :data
196
+ attr_reader :schema
145
197
  end
@@ -2,10 +2,15 @@ require "sequel-fixture"
2
2
  require "fast"
3
3
 
4
4
  describe Sequel::Fixture do
5
- describe ".path" do
5
+ describe ".path" do
6
6
  it "should return 'test/fixtures'" do
7
7
  Sequel::Fixture.path.should == "test/fixtures"
8
8
  end
9
+
10
+ it "should be configurable" do
11
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
12
+ Sequel::Fixture.path.should == File.join(File.dirname(__FILE__), "fixtures")
13
+ end
9
14
  end
10
15
 
11
16
  describe ".new" do
@@ -46,6 +51,7 @@ describe Sequel::Fixture do
46
51
  describe "#load" do
47
52
  context "there is a valid fixture folder setup" do
48
53
  before do
54
+ Sequel::Fixture.path = "test/fixtures"
49
55
  Fast.file! "test/fixtures/test/users.yaml"
50
56
  Fast.file! "test/fixtures/test/actions.yaml"
51
57
  end
@@ -65,8 +71,7 @@ describe Sequel::Fixture do
65
71
 
66
72
  context "the check has been performed and I attempt to load another fixture" do
67
73
  before do
68
- Fast.file.write "test/fixtures/test/users.yaml", "john: { name: John Doe }"
69
- Fast.file.write "test/fixtures/another/users.yaml", "john: { name: John Doe }"
74
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
70
75
  end
71
76
 
72
77
  it "should fail" do
@@ -101,20 +106,19 @@ describe Sequel::Fixture do
101
106
  describe "#[]" do
102
107
  context "a valid fixture has been loaded" do
103
108
  before do
104
- Fast.file.write "test/fixtures/test/users.yaml", "john: { name: John, last_name: Wayne }"
105
- Fast.file.write "test/fixtures/test/actions.yaml", "walk: { user_id: 1, action: Walks }"
109
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
110
+
106
111
  @fix = Sequel::Fixture.new
107
112
  @fix.stub :check
108
113
  @fix.load :test
109
114
  end
110
115
 
111
116
  context "a table key is passed" do
112
- it "should return the SymbolMatrix containing the same info as in the matching YAML file" do
113
- @fix[:users].should be_a SymbolMatrix
114
- @fix[:users].john.name.should == "John"
115
- @fix[:users].john.last_name.should == "Wayne"
116
-
117
- @fix[:actions].walk.action.should == "Walks"
117
+ it "should return the Fixture::Table containing the same info as in the matching YAML file" do
118
+ @fix[:users].should be_a Sequel::Fixture::Table
119
+ @fix[:users][0].name.should == "John Doe"
120
+ @fix[:users][0].last_name.should == "Wayne"
121
+ @fix[:actions][0].action.should == "Walks"
118
122
  end
119
123
  end
120
124
 
@@ -128,19 +132,17 @@ describe Sequel::Fixture do
128
132
  context "a valid fixture has been loaded" do
129
133
  context "a table key is passed" do
130
134
  before do
131
- Fast.file.write "test/fixtures/test/users.yaml", "john: { name: John, last_name: Wayne }"
132
- Fast.file.write "test/fixtures/test/actions.yaml", "walk: { user_id: 1, action: Walks }"
135
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
133
136
  @fix = Sequel::Fixture.new
134
137
  @fix.stub :check
135
138
  @fix.load :test
136
139
  end
137
140
 
138
141
  it "should return the SymbolMatrix containing the same info as in the matching YAML file" do
139
- @fix.users.should be_a SymbolMatrix
140
- @fix.users.john.name.should == "John"
141
- @fix.users.john.last_name.should == "Wayne"
142
-
143
- @fix.actions.walk.action.should == "Walks"
142
+ @fix.users.should be_a Sequel::Fixture::Table
143
+ @fix.users[0].name.should == "John Doe"
144
+ @fix.users[0].last_name.should == "Wayne"
145
+ @fix.actions[0].action.should == "Walks"
144
146
  end
145
147
 
146
148
  after do
@@ -197,9 +199,8 @@ describe Sequel::Fixture do
197
199
  end
198
200
  fix.stub_data
199
201
 
200
- expect { fix.check
201
- }.to raise_error Sequel::Fixture::TablesNotEmptyError,
202
- "The table 'users' is not empty, all tables should be empty prior to testing"
202
+ expect { fix.check }.to raise_error Sequel::Fixture::TablesNotEmptyError,
203
+ "Table 'users' is not empty, tables must be empty prior to testing"
203
204
  end
204
205
 
205
206
  it "should return true if all tables count equals 0" do
@@ -312,7 +313,7 @@ describe Sequel::Fixture do
312
313
  fix.check
313
314
  expect { fix.connection = double 'database'
314
315
  }.to raise_error Sequel::Fixture::ChangingConnectionIllegal,
315
- "A check has already been performed, changing the connection now is illegal"
316
+ "Illegal to change connection after check has already been performed"
316
317
  end
317
318
 
318
319
  after do
@@ -323,14 +324,14 @@ describe Sequel::Fixture do
323
324
 
324
325
  describe "#data" do
325
326
  context "a fixture has been loaded" do
326
- before do
327
- Fast.file.write "test/fixtures/test/users.yaml", "jane { name: Jessica Dore }"
327
+ before do
328
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
328
329
  end
329
330
 
330
331
  it "should return the fixture data" do
331
332
  fix = Sequel::Fixture.new :test
332
333
  fix.data.should have_key :users
333
- fix.data[:users].should be_a SymbolMatrix
334
+ fix.data[:users].should be_a Sequel::Fixture::Table
334
335
  end
335
336
 
336
337
  after do
@@ -341,7 +342,7 @@ describe Sequel::Fixture do
341
342
  context "no fixture has been loaded" do
342
343
  it "should return nil" do
343
344
  fix = Sequel::Fixture.new
344
- fix.data.should be_nil
345
+ fix.data.should be {}
345
346
  end
346
347
  end
347
348
  end
@@ -359,8 +360,8 @@ describe Sequel::Fixture do
359
360
 
360
361
  context "a valid fixture and a database connection are provided" do
361
362
  before do
362
- Fast.file.write "test/fixtures/test/users.yaml", "john: { name: John, last_name: Wayne }"
363
- Fast.file.write "test/fixtures/test/actions.yaml", "walk: { user_id: 1, action: Walks }"
363
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
364
+
364
365
  @table = stub
365
366
  @database = stub :[] => @table
366
367
  @fix = Sequel::Fixture.new
@@ -370,8 +371,8 @@ describe Sequel::Fixture do
370
371
 
371
372
  it "should attempt to insert the data into the database" do
372
373
  @table.stub :count => 0
373
- @table.should_receive(:insert).with :name => "John", :last_name => "Wayne"
374
- @table.should_receive(:insert).with :user_id => 1, :action => "Walks"
374
+ @table.should_receive(:insert).with "name" => "John Doe", "last_name" => "Wayne"
375
+ @table.should_receive(:insert).with "user_id" => 1, "action" => "Walks"
375
376
  @fix.push
376
377
  end
377
378
 
@@ -382,16 +383,18 @@ describe Sequel::Fixture do
382
383
 
383
384
  context "a fixture with a field with a <raw> and a <processed> alternative" do
384
385
  before do
385
- Fast.file.write "test/fixtures/test/users.yaml", "user: { password: { raw: secret, processed: 35ferwt352 } }"
386
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
386
387
  end
387
388
 
388
389
  it "should insert the <processed> alternative" do
389
390
  database = double 'database'
390
391
  insertable = double 'table'
391
392
  insertable.stub :count => 0
392
- insertable.should_receive(:insert).with :password => '35ferwt352'
393
+ insertable.should_receive(:insert).with "password" => '35ferwt352'
393
394
  database.stub(:[]).and_return insertable
394
- fix = Sequel::Fixture.new :test, database, false
395
+
396
+ fix = Sequel::Fixture.new :processed, database, false
397
+
395
398
  fix.push
396
399
  end
397
400
 
@@ -402,24 +405,22 @@ describe Sequel::Fixture do
402
405
 
403
406
  context "a fixture with a field with alternatives yet missing the <processed> one" do
404
407
  before do
405
- Fast.file.write "test/fixtures/test/users.yaml", "hey: { pass: { raw: There } }"
408
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
406
409
  end
407
410
 
408
411
  it "should fail" do
409
412
  database = double 'database', :[] => stub( 'table', :count => 0, :truncate => nil )
410
- fix = Sequel::Fixture.new :test, database, false
411
- expect { fix.push
412
- }.to raise_error Sequel::Fixture::MissingProcessedValueError,
413
- "In record 'hey' to be inserted into 'users', the processed value of field 'pass' is missing, aborting"
413
+ fix = Sequel::Fixture.new :invalid, database, false
414
+
415
+ expect { fix.push }.to raise_error Sequel::Fixture::MissingProcessedValueError
414
416
  end
415
417
 
416
418
 
417
419
  it "should call the rollback" do
418
420
  database = double 'database', :[] => stub( 'table', :count => 0, :truncate => nil )
419
- fix = Sequel::Fixture.new :test, database, false
421
+ fix = Sequel::Fixture.new :invalid, database, false
420
422
  fix.should_receive :rollback
421
- expect { fix.push
422
- }.to raise_error Sequel::Fixture::MissingProcessedValueError
423
+ expect { fix.push }.to raise_error Sequel::Fixture::MissingProcessedValueError
423
424
  end
424
425
 
425
426
  after do
@@ -428,55 +429,6 @@ describe Sequel::Fixture do
428
429
  end
429
430
  end
430
431
 
431
- # This should go in a dependency, pending refactoring TODO
432
- describe "#simplify" do
433
- context "when receiving a multidimensional hash containing a field with raw and processed" do
434
- it "should convert it in a simple hash using the processed value as replacement" do
435
- base_hash = {
436
- :name => "Jane",
437
- :band => "Witherspoons",
438
- :pass => {
439
- :raw => "secret",
440
- :processed => "53oih7fhjdgj3f8="
441
- },
442
- :email => {
443
- :raw => "Jane@gmail.com ",
444
- :processed => "jane@gmail.com"
445
- }
446
- }
447
-
448
- fix = Sequel::Fixture.new
449
- simplified = fix.simplify base_hash
450
- simplified.should == {
451
- :name => "Jane",
452
- :band => "Witherspoons",
453
- :pass => "53oih7fhjdgj3f8=",
454
- :email => "jane@gmail.com"
455
- }
456
- end
457
- end
458
-
459
- context "the multidimensional array is missing the processed part of the field" do
460
- it "should raise an exception" do
461
- base_hash = {
462
- :name => "Jane",
463
- :pass => {
464
- :raw => "secret",
465
- :not_processed => "53oih7fhjdgj3f8="
466
- },
467
- :email => {
468
- :raw => "Jane@gmail.com ",
469
- :processed => "jane@gmail.com"
470
- }
471
- }
472
-
473
- fix = Sequel::Fixture.new
474
- expect { fix.simplify base_hash
475
- }.to raise_error Sequel::Fixture::MissingProcessedValueError,
476
- "The processed value to insert into the db is missing from the field 'pass', aborting"
477
- end
478
- end
479
- end
480
432
 
481
433
  describe "#rollback" do
482
434
  it "should check" do
@@ -0,0 +1,2 @@
1
+ data:
2
+ - name: John Doe
@@ -0,0 +1,4 @@
1
+ data:
2
+ - name: Hey
3
+ pass:
4
+ raw: secret
@@ -0,0 +1,4 @@
1
+ data:
2
+ - password:
3
+ raw: secret
4
+ processed: 35ferwt352
@@ -0,0 +1,4 @@
1
+ data:
2
+ - user_id: 1
3
+ action: Walks
4
+
@@ -0,0 +1,3 @@
1
+ data:
2
+ - name: John Doe
3
+ last_name: Wayne
@@ -0,0 +1,54 @@
1
+ require "sequel-fixture"
2
+ require "fast"
3
+
4
+ describe Sequel::Fixture do
5
+ # This should go in a dependency, pending refactoring TODO
6
+ describe "#simplify" do
7
+ context "when receiving a multidimensional hash containing a field with raw and processed" do
8
+ it "should convert it in a simple hash using the processed value as replacement" do
9
+ base_hash = {
10
+ :name => "Jane",
11
+ :band => "Witherspoons",
12
+ :pass => {
13
+ :raw => "secret",
14
+ :processed => "53oih7fhjdgj3f8="
15
+ },
16
+ :email => {
17
+ :raw => "Jane@gmail.com ",
18
+ :processed => "jane@gmail.com"
19
+ }
20
+ }
21
+
22
+ fix = Sequel::Fixture.new
23
+ simplified = fix.simplify(base_hash)
24
+ simplified.should == {
25
+ :name => "Jane",
26
+ :band => "Witherspoons",
27
+ :pass => "53oih7fhjdgj3f8=",
28
+ :email => "jane@gmail.com"
29
+ }
30
+ end
31
+ end
32
+
33
+ context "the multidimensional array is missing the processed part of the field" do
34
+ it "should raise an exception" do
35
+ base_hash = {
36
+ :name => "Jane",
37
+ :pass => {
38
+ :raw => "secret",
39
+ :not_processed => "53oih7fhjdgj3f8="
40
+ },
41
+ :email => {
42
+ :raw => "Jane@gmail.com ",
43
+ :processed => "jane@gmail.com"
44
+ }
45
+ }
46
+
47
+ fix = Sequel::Fixture.new
48
+ expect { fix.simplify(base_hash)
49
+ }.to raise_error Sequel::Fixture::MissingProcessedValueError,
50
+ "The processed value to insert into the db is missing from the field 'pass', aborting"
51
+ end
52
+ end
53
+ end
54
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-fixture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-07 00:00:00.000000000 Z
12
+ date: 2013-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel
@@ -88,17 +88,19 @@ files:
88
88
  - LICENSE
89
89
  - README.md
90
90
  - Rakefile
91
- - features/configurable_fixtures_folder.feature
92
- - features/play_around_with_fixtures.feature
93
- - features/stepdefs/play_around_with_fixtures/background.rb
94
- - features/stepdefs/play_around_with_fixtures/create_a_simple_fixture_push_it_and_rollback.rb
95
- - features/stepdefs/play_around_with_fixtures/misconfigured_password_field.rb
96
- - features/support/env.rb
97
- - features/support/hooks.rb
98
91
  - lib/sequel-fixture.rb
92
+ - lib/sequel-fixture/exceptions.rb
93
+ - lib/sequel-fixture/table.rb
94
+ - lib/sequel-fixture/util.rb
99
95
  - lib/sequel-fixture/version.rb
100
96
  - sequel-fixture.gemspec
101
97
  - spec/sequel/fixture_spec.rb
98
+ - spec/sequel/fixtures/another/users.yaml
99
+ - spec/sequel/fixtures/invalid/users.yaml
100
+ - spec/sequel/fixtures/processed/users.yaml
101
+ - spec/sequel/fixtures/test/actions.yaml
102
+ - spec/sequel/fixtures/test/users.yaml
103
+ - spec/sequel/util_spec.rb
102
104
  homepage: http://github.com/Fetcher/sequel-fixture
103
105
  licenses: []
104
106
  post_install_message:
@@ -111,24 +113,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
113
  - - ! '>='
112
114
  - !ruby/object:Gem::Version
113
115
  version: '0'
116
+ segments:
117
+ - 0
118
+ hash: -2872414278293677799
114
119
  required_rubygems_version: !ruby/object:Gem::Requirement
115
120
  none: false
116
121
  requirements:
117
122
  - - ! '>='
118
123
  - !ruby/object:Gem::Version
119
124
  version: '0'
125
+ segments:
126
+ - 0
127
+ hash: -2872414278293677799
120
128
  requirements: []
121
129
  rubyforge_project:
122
- rubygems_version: 1.8.21
130
+ rubygems_version: 1.8.25
123
131
  signing_key:
124
132
  specification_version: 3
125
133
  summary: Flexible fixtures for the Sequel Gem inspired in Rails 2 fixtures
126
134
  test_files:
127
- - features/configurable_fixtures_folder.feature
128
- - features/play_around_with_fixtures.feature
129
- - features/stepdefs/play_around_with_fixtures/background.rb
130
- - features/stepdefs/play_around_with_fixtures/create_a_simple_fixture_push_it_and_rollback.rb
131
- - features/stepdefs/play_around_with_fixtures/misconfigured_password_field.rb
132
- - features/support/env.rb
133
- - features/support/hooks.rb
134
135
  - spec/sequel/fixture_spec.rb
136
+ - spec/sequel/fixtures/another/users.yaml
137
+ - spec/sequel/fixtures/invalid/users.yaml
138
+ - spec/sequel/fixtures/processed/users.yaml
139
+ - spec/sequel/fixtures/test/actions.yaml
140
+ - spec/sequel/fixtures/test/users.yaml
141
+ - spec/sequel/util_spec.rb
@@ -1,24 +0,0 @@
1
- Feature: Configurable fixtures folder
2
- In order to load fixtures from arbitrary folders
3
- I want to be able to configure the source of the fixtures
4
-
5
- Background: We have a database connection working
6
- Given a sqlite database connection
7
-
8
- Scenario: The folder is now just "fixtures"
9
- Given a table users with String:name, String:email
10
- And a file "fixtures/configurable/users.yaml" with:
11
- """
12
- xavi:
13
- name: Xavier
14
- email: xavier@via.com
15
- john:
16
- name: Johnny
17
- email: john@doe.com
18
- """
19
- When I set the fixtures path as "fixtures"
20
- And I load the configurable fixture
21
- Then I should see 1 record in users with name "Xavier" and email "xavier@via.com"
22
- And I should see 1 record in users with name "Johnny" and email "john@doe.com"
23
- When I rollback
24
- Then I should see 0 records in users
@@ -1,70 +0,0 @@
1
- Feature: Play around with Fixtures
2
- In order to test if Sequel::Fixture is really practical
3
- As the gem developer
4
- I want to play around with it a little bit
5
-
6
- Background: We have a database connection working
7
- Given a sqlite database connection
8
-
9
- Scenario: Create a simple fixture, push it into a sqlite and rollback
10
- Given a table visitors with String:name, String:email
11
- And a table aliens with String:race
12
- And a table visits with Integer:alien_id, Integer:visitor_id
13
- And a file "test/fixtures/simple/visitors.yaml" with:
14
- """
15
- anonymous:
16
- name: V
17
- email: v@for.vendetta
18
- """
19
- And a file "test/fixtures/simple/aliens.yaml" with:
20
- """
21
- yourfavouritemartian:
22
- race: Zerg
23
- """
24
- And a file "test/fixtures/simple/visits.yaml" with:
25
- """
26
- v2yfm:
27
- alien_id: 1
28
- visitor_id: 1
29
- """
30
- When I load the simple fixture
31
- Then I should see 1 record in visitors with name "V" and email "v@for.vendetta"
32
- And I should see 1 record in aliens with race "Zerg"
33
- And I should see 1 record in visits with alien_id 1 and visitor_id 1
34
- When I rollback
35
- Then I should see 0 records in visitors
36
- And I should see 0 records in aliens
37
- And I should see 0 records in visits
38
-
39
- Scenario: The users table has a password field
40
- Given a table users with String:name, String:password
41
- And a file "test/fixtures/password/users.yaml" with:
42
- """
43
- john:
44
- name: John Wayne
45
- password:
46
- raw: secret
47
- processed: 5bfb52c459cdb07218c176b5ddec9b6215bd5b76
48
- """
49
- When I load the password fixture
50
- Then I should see 1 record in users with name "John Wayne" and password "5bfb52c459cdb07218c176b5ddec9b6215bd5b76"
51
- When I rollback
52
- Then I should see 0 records in users
53
-
54
- Scenario: Misconfigured password field
55
- Given a table users with String:password
56
- And a file "test/fixtures/misconfigured/users.yaml" with:
57
- """
58
- good_entry:
59
- password:
60
- raw: secret
61
- processed: 96bdg756n5sgf9gfs==
62
- wrong_entry:
63
- password:
64
- missing: The field
65
- """
66
- Then the loading of misconfigured fixture should fail
67
- And I should see that the table was "users"
68
- And I should see that the field was "password"
69
- And I should see that the entry was "wrong_entry"
70
- And I should see 0 records in users
@@ -1,5 +0,0 @@
1
- # Background: We have a database connection working
2
- # Given a sqlite database connection
3
- Given /^a sqlite database connection$/ do
4
- @DB = Sequel.sqlite
5
- end
@@ -1,66 +0,0 @@
1
- #Scenario: Create a simple fixture, push it into a sqlite and rollback
2
- # Given a table visitors with String:name, String:email
3
- # And a table aliens with String:race
4
- # And a table visits with Integer:alien_id, Integer:visitor_id
5
- # And a file "test/fixtures/simple/visitors.yaml" with:
6
- # """
7
- # anonymous:
8
- # name: V
9
- # email: v@for.vendetta
10
- # """
11
- # And a file "test/fixtures/simple/aliens.yaml" with:
12
- # """
13
- # yourfavouritemartian:
14
- # race: Zerg
15
- # """
16
- # And a file "test/fixtures/simple/visits.yaml" with:
17
- # """
18
- # v2yfm:
19
- # alien_id: 1
20
- # visitor_id: 1
21
- # """
22
- # When I load the simple fixture
23
- # Then I should see 1 record in visitors with name "V" and email "v@for.vendetta"
24
- # And I should see 1 record in aliens with race "Zerg"
25
- # And I should see 1 record in visits with alien_id 1 and visitor_id 1
26
-
27
- Given /^a table (\w+) with (\w+):(\w+), (\w+):(\w+)$/ do |table, type1, field1, type2, field2|
28
- @DB.create_table table.to_sym do
29
- send :"#{type1}", field1
30
- send :"#{type2}", field2
31
- end
32
- end
33
-
34
- Given /^a table (\w+) with (\w+):(\w+)$/ do |table, type, field|
35
- @DB.create_table table.to_sym do
36
- send :"#{type}", field
37
- end
38
- end
39
-
40
- And /^a file "(.+?)" with:$/ do |path, content|
41
- Fast.file.write path, content
42
- end
43
-
44
- When /^I load the (\w+) fixture$/ do |fixture|
45
- @fixture = Sequel::Fixture.new fixture.to_sym, @DB
46
- end
47
-
48
- Then /^I should see (\d) record in (\w+) with (\w+) "(.+?)" and (\w+) "(.+?)"$/ do |amount, table, field1, data1, field2, data2|
49
- @DB[table.to_sym].where(field1.to_sym => data1, field2.to_sym => data2).count.should == amount.to_i
50
- end
51
-
52
- And /^I should see (\d) record in (\w+) with (\w+) "([^"]+)"$/ do |amount, table, field1, data1|
53
- @DB[table.to_sym].where(field1.to_sym => data1).count.should == amount.to_i
54
- end
55
-
56
- Then /^I should see (\d) record in (\w+) with (\w+) (\d+) and (\w+) (\d+)$/ do |amount, table, field1, data1, field2, data2|
57
- @DB[table.to_sym].where(field1.to_sym => data1.to_i, field2.to_sym => data2.to_i).count.should == amount.to_i
58
- end
59
-
60
- When /^I rollback$/ do
61
- @fixture.rollback
62
- end
63
-
64
- Then /^I should see (\d+) records? in (\w+)$/ do |amount, table|
65
- @DB[table.to_sym].count.should == amount.to_i
66
- end
@@ -1,40 +0,0 @@
1
- #Scenario: Misconfigured password field
2
- # Given a table users with String:password
3
- # And a file "test/fixtures/misconfigured/users.yaml" with:
4
- # """
5
- # good_entry:
6
- # password:
7
- # raw: secret
8
- # processed: 96bdg756n5sgf9gfs==
9
- # wrong_entry:
10
- # password:
11
- # missing: The field
12
- # """
13
- # Then the loading of misconfigured fixture should fail
14
- # And I should see that the table was "users"
15
- # And I should see that the field was "password"
16
- # And I should see that the entry was "wrong_entry"
17
- # And I should see 0 records in users
18
-
19
- # NOTE: most steps have been defined in
20
- # `create_a_simple_fixture_push_it_and_rollback.rb`
21
-
22
- Then /^the loading of (\w+) fixture should fail$/ do |fixture|
23
- begin
24
- Sequel::Fixture.new fixture.to_sym, @DB
25
- rescue Sequel::Fixture::MissingProcessedValueError => e
26
- @exception = e
27
- end
28
- end
29
-
30
- And /^I should see that the table was "(\w+)"$/ do |table|
31
- @exception.message.should include table
32
- end
33
-
34
- And /^I should see that the field was "(\w+)"$/ do |field|
35
- @exception.message.should include field
36
- end
37
-
38
- And /^I should see that the entry was "(\w+)"$/ do |entry|
39
- @exception.message.should include entry
40
- end
@@ -1,6 +0,0 @@
1
- require "fast"
2
- require "pry"
3
-
4
- $LOAD_PATH << File.expand_path("../../../../lib", __FILE__)
5
-
6
- require "sequel-fixture"
@@ -1,6 +0,0 @@
1
- After do
2
- Fast.dir.remove! :test, :fixtures
3
- @DB.tables do |table|
4
- @DB.drop_table table
5
- end
6
- end