gamera-sequel-fixture 2.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ad175fb0feeef3d7e4fa04a58ffaee472d3bb50
4
+ data.tar.gz: 5bb282e8f9f34cef02086c064a5978c7b2bcc443
5
+ SHA512:
6
+ metadata.gz: 0c8f74bb95113cc35b25440eb5780c421fc18d2891bef2b9658a63552c988a4b43ba178764a2ff8db306497ea116d2d5834b03e9aed91ded64a8d55f0fc6d053
7
+ data.tar.gz: d574389e41b423fc029cc74f1d74a9923b0809d8897d85ff0606c57fd65fae37c152336fca6baa8ff84a3c7e3dfa40e1f8307ecb981cfd319ae70753e862a2d4
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ Gemfile.lock
4
+ .bundle
5
+ .config
6
+ coverage
7
+ vendor/
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ .bin/
16
+ tmp
17
+
18
+ # YARD artifacts
19
+ .yardoc
20
+ _yardoc
21
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.2
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel-fixture.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "rspec", ">=3.2"
8
+ gem "byebug"
9
+ gem "sqlite3"
10
+ gem "rake"
11
+ end
12
+
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (C) 2012 Fetcher
2
+
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ _This is a (hopefully temporary) fork of https://github.com/whitepages/sequel-fixture which incorporates some changes from @diminish7 that we need for https://github.com/gamera-team/gamera_
2
+
3
+ Sequel::Fixture
4
+ ===============
5
+
6
+ Just like Rails fixtures, but for Sequel.
7
+
8
+ This version includes support for defining the fixture schemas
9
+ and uses sqlite3 adapter to inject data into memory.
10
+
11
+ Usage
12
+ =====
13
+ Each fixture file defines the schema and data for a single table which
14
+ is named after the file name.
15
+
16
+ Schema definition is optional, but note that db inserts will fail if the tables do
17
+ not exist.
18
+
19
+ Assuming you have a fixture for the table users with:
20
+ ```yaml
21
+ # fixtures/simple/users.yaml
22
+ schema:
23
+ - name: name
24
+ type: string
25
+ primary_key: true
26
+ - name: last_name
27
+ type: string
28
+ - name: empty
29
+ type: string
30
+ data:
31
+ - name: John
32
+ last_name: Doe
33
+ email: john@doe.com
34
+ - name: Jane
35
+ last_name: Doe
36
+ email: jane@doe.com
37
+ ```
38
+
39
+ and for messages:
40
+ ```yaml
41
+ # fixtures/simple/messages.yaml
42
+ schema:
43
+ - name: sender_id
44
+ type: integer
45
+ primary_key: true
46
+ - name: receiver_id
47
+ type: integer
48
+ - name: text
49
+ type: string
50
+ data:
51
+ - sender_id: 1
52
+ receiver_id: 2
53
+ text: Hi Jane! Long time no see.
54
+ - sender_id: 2
55
+ receiver_id: 1
56
+ text: John! Long time indeed. How are you doing?
57
+ ```
58
+
59
+ and the ruby script
60
+
61
+ ```ruby
62
+ # script.rb
63
+ require "sequel-fixture"
64
+
65
+ DB = Sequel.sqlite # Just a simple example, needs sqlite3 gem
66
+
67
+ ## Set the path of the fixture yaml files to be [script.rb]/fixtures/
68
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
69
+
70
+ fixture = Sequel::Fixture.new :simple, DB # Will load all the data in the fixture into the database
71
+
72
+ fixture.users # == fixture[:users]
73
+ fixture.users[0].name # => "John"
74
+ # The YAML files are parsed into a SymbolMatrix
75
+ # http://github.com/Fetcher/symbolmatrix
76
+ # Each table contains an array of Rows.
77
+ # Within each row the columns can be accessed by name (.name)
78
+
79
+ fixture.rollback # returns users and messages to pristine status ('TRUNCATE')
80
+
81
+
82
+ fixture = Sequel::Fixture.new :simple, DB, false # The `false` flag prevent the constructor to automatically push
83
+ # the fixture into the database
84
+
85
+ fixture.check # Will fail if the user or messages table
86
+ # were already occupied with something
87
+
88
+ fixture.push # Inserts the fixture in the database
89
+
90
+ fixture.rollback # Don't forget to rollback
91
+
92
+ ```
93
+
94
+ ...naturally, `sequel-fixture` makes a lot more sense within some testing framework.
95
+
96
+
97
+ Contributing
98
+ ------------
99
+
100
+ ```
101
+ bundle install --binstubs .bin --path vendor/bundle
102
+ ```
103
+
104
+ Installation
105
+ ------------
106
+
107
+ gem install sequel-fixture
108
+
109
+ ### Or using Bundler
110
+
111
+ gem 'sequel-fixture'
112
+
113
+ And then execute:
114
+
115
+ bundle
116
+
117
+
118
+ ## License
119
+
120
+ Copyright (C) 2012 Whitepages
121
+
122
+ This program is free software: you can redistribute it and/or modify
123
+ it under the terms of the GNU General Public License as published by
124
+ the Free Software Foundation, either version 3 of the License, or
125
+ (at your option) any later version.
126
+
127
+ This program is distributed in the hope that it will be useful,
128
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
129
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130
+ GNU General Public License for more details.
131
+
132
+ You should have received a copy of the GNU General Public License
133
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ desc "Run specs"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.pattern = "./spec/**/*_spec.rb"
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,194 @@
1
+ require "sequel"
2
+ require "symbolmatrix"
3
+ require "fast"
4
+
5
+ require "sequel-fixture/version"
6
+ require "sequel-fixture/exceptions"
7
+ require "sequel-fixture/util"
8
+ require "sequel-fixture/table"
9
+
10
+ module Sequel; end
11
+
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.each do |file|
51
+ key = file.split('.').first.to_sym
52
+ @data ||= {}
53
+ @schema ||= {}
54
+
55
+ file_data = SymbolMatrix.new "#{fixtures_path}/#{fixture}/#{file}"
56
+
57
+ if file_data
58
+ @data[key] = Table.new(file_data[:data]) if file_data.key?(:data)
59
+ @schema[key] = file_data[:schema] if file_data.key?(:schema)
60
+ end
61
+ end
62
+ end
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
104
+
105
+ return @checked = true
106
+ end
107
+
108
+
109
+ # === Description
110
+ # Initializes fixture schema and Inserts the fixture data into the corresponding
111
+ # tables
112
+ #
113
+ def push
114
+ check
115
+
116
+ @schema.each do |table, matrix|
117
+ push_schema(table, matrix)
118
+ end
119
+
120
+ @data.each do |table_name, table_data|
121
+ table_data.rows.each do |values|
122
+ begin
123
+ @connection[table_name].insert(simplify(values.to_h))
124
+ rescue MissingProcessedValueError => m
125
+ rollback
126
+ raise MissingProcessedValueError, "In record '#{values.to_h}' to be inserted into '#{table_name}', the processed value of field '#{m.field}' is missing, aborting."
127
+ rescue NoMethodError => e
128
+ raise IllegalFixtureFormat, "In record '#{values}', data must be formatted as arrays of hashes. Check 'data' section in '#{table_name}.yaml'"
129
+ rescue Sequel::DatabaseError => sde
130
+ raise Sequel::Fixture::DatabaseError, "DB table: #{table_name}, #{sde.message}"
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+
137
+ # === Description
138
+ # Create the schema in our DB connection based on the schema values
139
+ #
140
+ def push_schema(table, values)
141
+ ## Lets passively ignore the schema if the table already exists
142
+ return if @connection.table_exists?(table.to_sym)
143
+
144
+ ## Find the primary key
145
+ pkey_data = nil
146
+ values.each do |column_def|
147
+ pkey_data = column_def if column_def["primary_key"]
148
+ end
149
+
150
+ ## Create the table with the primary key
151
+ @connection.create_table(table) do
152
+ column(pkey_data["name"].to_sym, pkey_data["type"].to_sym)
153
+ end
154
+
155
+ ## Add the rest of the columns
156
+ values.each do |column_def|
157
+ unless column_def["primary_key"]
158
+ @connection.alter_table(table) { add_column(column_def["name"].to_sym, column_def["type"].to_sym) }
159
+ end
160
+ end
161
+ end
162
+
163
+
164
+ # === Description
165
+ # Empties the tables, only if they were empty to begin with
166
+ #
167
+ def rollback
168
+ begin
169
+ check
170
+
171
+ @data.each_key do |table|
172
+ @connection[table].truncate
173
+ end
174
+ rescue TablesNotEmptyError => e
175
+ raise RollbackIllegalError, "The tables weren't empty to begin with, rollback aborted."
176
+ end
177
+ end
178
+
179
+
180
+ # === Description
181
+ # Sets the connection. Raises an ChangingConnectionIllegal exception if this fixture has
182
+ # already been checked
183
+ #
184
+ def connection=(the_connection)
185
+ if @checked
186
+ raise ChangingConnectionIllegal, "Illegal to change connection after check has already been performed"
187
+ end
188
+ @connection = the_connection
189
+ end
190
+
191
+ attr_reader :connection
192
+ attr_reader :data
193
+ attr_reader :schema
194
+ end
@@ -0,0 +1,18 @@
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
+ class DatabaseError < StandardError; end
10
+
11
+ class MissingProcessedValueError < StandardError
12
+ attr_accessor :field
13
+ def initialize message, field = nil
14
+ @field = field
15
+ super message
16
+ end
17
+ end
18
+ 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
@@ -0,0 +1,5 @@
1
+ module Sequel
2
+ class Fixture
3
+ VERSION = "2.0.3"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sequel-fixture/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'gamera-sequel-fixture'
6
+ gem.authors = ['Eric Dobbs', 'Glen Aultman-Bettridge', 'Jillian Rosile']
7
+ gem.email = ['eric@dobbse.net', 'glenab@koansolutions.net', 'jillian.rosile@gmail.com']
8
+ gem.description = %q{Flexible fixtures for the Sequel Gem inspired in Rails 2 fixtures}
9
+ gem.summary = %q{Flexible fixtures for the Sequel Gem inspired in Rails 2 fixtures. Temporary fork of https://github.com/whitepages/sequel-fixture to add ERB support}
10
+ gem.homepage = "http://github.com/gamera-team/sequel-fixture"
11
+
12
+ gem.add_dependency "sequel" # Stating the obvious
13
+ gem.add_dependency "gamera-symbolmatrix" # Because its easy to use
14
+ gem.add_dependency "fast" # Fast was needed. This is a testing gem, there's no problem with extra load
15
+
16
+ gem.add_development_dependency "rspec"
17
+
18
+ gem.files = `git ls-files`.split($\)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ gem.version = Sequel::Fixture::VERSION
23
+ end
@@ -0,0 +1,462 @@
1
+ require "sequel-fixture"
2
+ require "fast"
3
+ require "sequel/exceptions"
4
+
5
+ describe Sequel::Fixture do
6
+ describe ".path" do
7
+ it "returns 'test/fixtures'" do
8
+ expect(Sequel::Fixture.path).to eq "test/fixtures"
9
+ end
10
+
11
+ it "is configurable" do
12
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
13
+ expect(Sequel::Fixture.path).to eq File.join(File.dirname(__FILE__), "fixtures")
14
+ end
15
+ end
16
+
17
+ describe ".new" do
18
+ context "return a DatabaseError" do
19
+ it "calls load_fixture" do
20
+ expect_any_instance_of(Sequel::Fixture).to receive(:simplify).and_raise(Sequel::DatabaseError.new(StandardError.new("created_at: cannot be null")))
21
+
22
+ table = double
23
+ database = double :[] => @table
24
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
25
+ expect {
26
+ Sequel::Fixture.new(:test, database, true)
27
+ }.to raise_error Sequel::Fixture::DatabaseError,
28
+ "DB table: actions, created_at: cannot be null"
29
+ end
30
+ end
31
+
32
+ context "a symbol is sent representing a fixture" do
33
+ it "calls load_fixture" do
34
+ expect_any_instance_of(Sequel::Fixture).to receive(:load).with :test
35
+ Sequel::Fixture.new :test
36
+ end
37
+ end
38
+
39
+ context "a database connection is passed" do
40
+ it "calls push" do
41
+ allow(Sequel).to receive(:connect).and_return Sequel::Database.new
42
+ allow_any_instance_of(Sequel::Fixture).to receive :load
43
+ expect_any_instance_of(Sequel::Fixture).to receive :push
44
+ Sequel::Fixture.new :test, Sequel.connect
45
+ end
46
+ end
47
+
48
+ context "a database is provided but no fixture" do
49
+ it "does not call push" do
50
+ database = double 'database'
51
+ expect_any_instance_of(Sequel::Fixture).to_not receive :push
52
+ Sequel::Fixture.new nil, database
53
+ end
54
+ end
55
+
56
+ context "a database connection and a valid fixture are passed but a false flag is passed at the end" do
57
+ it "does not push" do
58
+ database = double 'database'
59
+ allow_any_instance_of(Sequel::Fixture).to receive :load
60
+ expect_any_instance_of(Sequel::Fixture).to_not receive :push
61
+ Sequel::Fixture.new :test, database, false
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#load" do
67
+ context "there is a valid fixture folder setup" do
68
+ shared_examples_for "valid fixture folder setup" do
69
+
70
+ around do |example|
71
+ Sequel::Fixture.path = "test/fixtures"
72
+ Fast.file! file1
73
+ Fast.file! file2
74
+
75
+ example.run
76
+
77
+ Fast.dir.remove! :test
78
+ end
79
+
80
+ it "loads the fixture YAML files using SymbolMatrix (third-party)" do
81
+ fix = Sequel::Fixture.new
82
+ allow(fix).to receive :check
83
+ expect(SymbolMatrix).to receive(:new).with file1
84
+ expect(SymbolMatrix).to receive(:new).with file2
85
+ fix.load :test
86
+ end
87
+ end
88
+
89
+ context "with files having a .yaml extension" do
90
+ let(:file1) { "test/fixtures/test/users.yaml" }
91
+ let(:file2) { "test/fixtures/test/actions.yaml" }
92
+
93
+ it_behaves_like "valid fixture folder setup"
94
+ end
95
+
96
+ context "with files having a .yml extension" do
97
+ let(:file1) { "test/fixtures/test/users.yml" }
98
+ let(:file2) { "test/fixtures/test/actions.yml" }
99
+
100
+ it_behaves_like "valid fixture folder setup"
101
+ end
102
+
103
+ context "with files having a .erb extension" do
104
+ let(:file1) { "test/fixtures/test/users.yml.erb" }
105
+ let(:file2) { "test/fixtures/test/actions.yml.erb" }
106
+
107
+ it_behaves_like "valid fixture folder setup"
108
+ end
109
+ end
110
+
111
+ context "the check has been performed and I attempt to load another fixture" do
112
+ before do
113
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
114
+ end
115
+
116
+ it "fails" do
117
+ allow_any_instance_of(Sequel::Fixture).to receive :push
118
+ database = double 'database'
119
+ allow(database).to receive(:[]).and_return double(:count => 0 )
120
+ fix = Sequel::Fixture.new :test, database
121
+ fix.check
122
+ expect { fix.load :another
123
+ }.to raise_error Sequel::Fixture::LoadingFixtureIllegal,
124
+ "A check has already been made, loading a different fixture is illegal"
125
+ end
126
+
127
+ after do
128
+ Fast.dir.remove! :test
129
+ end
130
+ end
131
+ end
132
+
133
+ describe "#force_checked!" do
134
+ it "check returns true and does not call [] in the passed database" do
135
+ database = double 'database'
136
+ expect(database).to_not receive :[]
137
+
138
+ allow_any_instance_of(Sequel::Fixture).to receive :load
139
+ fix = Sequel::Fixture.new :anything, database, false
140
+ expect(fix.force_checked!).to eq true
141
+ expect(fix.check).to be_truthy
142
+ end
143
+ end
144
+
145
+ describe "#[]" do
146
+ context "a valid fixture has been loaded" do
147
+ before do
148
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
149
+
150
+ @fix = Sequel::Fixture.new
151
+ allow(@fix).to receive :check
152
+ @fix.load :test
153
+ end
154
+
155
+ context "a table key is passed" do
156
+ it "returns the Fixture::Table containing the same info as in the matching YAML file" do
157
+ expect(@fix[:users]).to be_a Sequel::Fixture::Table
158
+ expect(@fix[:users][0].name).to eq "John Doe"
159
+ expect(@fix[:users][0].last_name).to eq "Wayne"
160
+ expect(@fix[:actions][0].action).to eq "Walks"
161
+ end
162
+ end
163
+
164
+ after do
165
+ Fast.dir.remove! :test
166
+ end
167
+ end
168
+ end
169
+
170
+ describe "#method_missing" do
171
+ context "a valid fixture has been loaded" do
172
+ context "a table key is passed" do
173
+ before do
174
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
175
+ @fix = Sequel::Fixture.new
176
+ allow(@fix).to receive :check
177
+ @fix.load :test
178
+ end
179
+
180
+ it "returns the SymbolMatrix containing the same info as in the matching YAML file" do
181
+ expect(@fix.users).to be_a Sequel::Fixture::Table
182
+ expect(@fix.users[0].name).to eq "John Doe"
183
+ expect(@fix.users[0].last_name).to eq "Wayne"
184
+ expect(@fix.actions[0].action).to eq "Walks"
185
+ end
186
+
187
+ after do
188
+ Fast.dir.remove! :test
189
+ end
190
+ end
191
+ end
192
+
193
+ it "raises no method error if matches nothing" do
194
+ expect { Sequel::Fixture.new.nothing = "hola"
195
+ }.to raise_error NoMethodError
196
+ end
197
+ end
198
+
199
+ describe "#fixtures_path" do
200
+ it "calls Sequel::Fixture.path" do
201
+ expect(Sequel::Fixture).to receive :path
202
+ Sequel::Fixture.new.fixtures_path
203
+ end
204
+ end
205
+
206
+ describe "#check" do
207
+
208
+ context "the check has been done and it passed before" do
209
+ it "returns true even if now tables don't pass" do
210
+ allow_any_instance_of(Sequel::Fixture).to receive :push
211
+
212
+ @counter = double 'counter'
213
+ allow(@counter).to receive :count do
214
+ @amount ||= 0
215
+ @amount += 1
216
+ 0 unless @amount > 5
217
+ end
218
+
219
+ @database = double 'database'
220
+ allow(@database).to receive(:[]).and_return @counter
221
+
222
+ @fix = Sequel::Fixture.new nil, @database
223
+ def @fix.stub_data
224
+ @data = { :users => nil, :tables => nil, :actions => nil, :schemas => nil }
225
+ end
226
+ @fix.stub_data
227
+ expect(@fix.check).to be_truthy
228
+ expect(@fix.check).to be_truthy # This looks confusing: let's explain. The #count method as defined for the mock
229
+ # runs 4 times in the first check. In the second check, it runs 4 times again.
230
+ # After time 6 it returns a large amount, making the check fail.
231
+ # Of course, the fourth time is never reached since the second check is skipped
232
+ end
233
+ end
234
+
235
+ context "no fixture has been loaded" do
236
+ it "fails with a missing fixture exception" do
237
+ fix = Sequel::Fixture.new
238
+ expect { fix.check
239
+ }.to raise_error Sequel::Fixture::MissingFixtureError,
240
+ "No fixture has been loaded, nothing to check"
241
+ end
242
+ end
243
+
244
+ context "a valid fixture has been loaded but no connection has been provided" do
245
+ before do
246
+ Fast.file.write "test/fixtures/test/users.yaml", "jane { name: Jane Doe }"
247
+ end
248
+ it "fails with a missing database connection exception" do
249
+ fix = Sequel::Fixture.new :test
250
+ expect { fix.check
251
+ }.to raise_error Sequel::Fixture::MissingConnectionError,
252
+ "No connection has been provided, impossible to check"
253
+ end
254
+
255
+ after do
256
+ Fast.dir.remove! :test
257
+ end
258
+ end
259
+
260
+ context "a database is provided but no fixture" do
261
+ it "fails with a missing fixture exception" do
262
+ database = double 'database'
263
+ fix = Sequel::Fixture.new nil, database
264
+ expect {fix.check
265
+ }.to raise_error Sequel::Fixture::MissingFixtureError,
266
+ "No fixture has been loaded, nothing to check"
267
+ end
268
+ end
269
+ end
270
+
271
+ describe "#connection" do
272
+ it "returns the Sequel connection passed as argument to the constructor" do
273
+ allow_any_instance_of(Sequel::Fixture).to receive :push
274
+ connection = double
275
+ fix = Sequel::Fixture.new nil, connection
276
+ expect(fix.connection).to eq connection
277
+ end
278
+ end
279
+
280
+ describe "#connection=" do
281
+ it "sets the connection" do
282
+ fix = Sequel::Fixture.new
283
+ connection = double
284
+ fix.connection = connection
285
+ expect(fix.connection).to eq connection
286
+ end
287
+
288
+ context "a check has been performed and I attempt to change the connection" do
289
+ before do
290
+ Fast.file.write "test/fixtures/test/users.yaml", "jane { name: Secret }"
291
+ end
292
+
293
+ it "fails" do
294
+ database = double 'database'
295
+ allow(database).to receive(:[]).and_return double(:count => 0)
296
+ allow_any_instance_of(Sequel::Fixture).to receive :push
297
+ fix = Sequel::Fixture.new :test, database
298
+ fix.check
299
+ expect { fix.connection = double 'database'
300
+ }.to raise_error Sequel::Fixture::ChangingConnectionIllegal,
301
+ "Illegal to change connection after check has already been performed"
302
+ end
303
+
304
+ after do
305
+ Fast.dir.remove! :test
306
+ end
307
+ end
308
+ end
309
+
310
+ describe "#data" do
311
+ context "a fixture has been loaded" do
312
+ before do
313
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
314
+ end
315
+
316
+ it "returns the fixture data" do
317
+ fix = Sequel::Fixture.new :test
318
+ expect(fix.data).to have_key :users
319
+ expect(fix.data[:users]).to be_a Sequel::Fixture::Table
320
+ end
321
+
322
+ after do
323
+ Fast.dir.remove! :test
324
+ end
325
+ end
326
+
327
+ context "no fixture has been loaded" do
328
+ it "returns nil" do
329
+ fix = Sequel::Fixture.new
330
+ expect(fix.data).to be {}
331
+ end
332
+ end
333
+ end
334
+
335
+ describe "#push" do
336
+ it "calls #check" do
337
+ fix = Sequel::Fixture.new
338
+ def fix.stub_data
339
+ @data = {}
340
+ end
341
+ fix.stub_data
342
+ expect(fix).to receive :check
343
+ fix.push
344
+ end
345
+
346
+ context "a valid fixture and a database connection are provided" do
347
+ before do
348
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
349
+
350
+ @table = double
351
+ @database = double :[] => @table
352
+ @fix = Sequel::Fixture.new
353
+ @fix.load :test
354
+ @fix.connection = @database
355
+ end
356
+
357
+ it "attempts to insert the data into the database" do
358
+ allow(@table).to receive(:count).and_return(0)
359
+ expect(@table).to receive(:insert).with "name" => "John Doe", "last_name" => "Wayne"
360
+ expect(@table).to receive(:insert).with "user_id" => 1, "action" => "Walks"
361
+ @fix.push
362
+ end
363
+
364
+ after do
365
+ Fast.dir.remove! :test
366
+ end
367
+ end
368
+
369
+ context "a fixture with a field with a <raw> and a <processed> alternative" do
370
+ before do
371
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
372
+ end
373
+
374
+ it "inserts the <processed> alternative" do
375
+ database = double 'database'
376
+ insertable = double 'table'
377
+ allow(insertable).to receive(:count).and_return(0)
378
+ expect(insertable).to receive(:insert).with "password" => '35ferwt352'
379
+ allow(database).to receive(:[]).and_return insertable
380
+
381
+ fix = Sequel::Fixture.new :processed, database, false
382
+
383
+ fix.push
384
+ end
385
+
386
+ after do
387
+ Fast.dir.remove! :test
388
+ end
389
+ end
390
+
391
+ context "a fixture with a field with alternatives yet missing the <processed> one" do
392
+ before do
393
+ Sequel::Fixture.path = File.join(File.dirname(__FILE__), "fixtures")
394
+ end
395
+
396
+ it "fails" do
397
+ database = double 'database', :[] => double( 'table', :count => 0, :truncate => nil )
398
+ fix = Sequel::Fixture.new :invalid, database, false
399
+
400
+ expect { fix.push }.to raise_error Sequel::Fixture::MissingProcessedValueError
401
+ end
402
+
403
+
404
+ it "calls the rollback" do
405
+ database = double 'database', :[] => double( 'table', :count => 0, :truncate => nil )
406
+ fix = Sequel::Fixture.new :invalid, database, false
407
+ expect(fix).to receive :rollback
408
+ expect { fix.push }.to raise_error Sequel::Fixture::MissingProcessedValueError
409
+ end
410
+
411
+ after do
412
+ Fast.dir.remove! :test
413
+ end
414
+ end
415
+ end
416
+
417
+
418
+ describe "#rollback" do
419
+ it "checks" do
420
+ fix = Sequel::Fixture.new
421
+ def fix.stub_data
422
+ @data = {}
423
+ end
424
+ fix.stub_data
425
+ expect(fix).to receive :check
426
+ fix.rollback
427
+ end
428
+
429
+ context "the check is failing" do
430
+ it "raises a custom error for the rollback" do
431
+ fix = Sequel::Fixture.new
432
+ allow(fix).to receive(:check).and_raise Sequel::Fixture::TablesNotEmptyError
433
+ expect { fix.rollback
434
+ }.to raise_error Sequel::Fixture::RollbackIllegalError,
435
+ "The tables weren't empty to begin with, rollback aborted."
436
+ end
437
+ end
438
+
439
+ context "a check has been done and is passing" do
440
+ before do
441
+ @database = double
442
+ @truncable = double
443
+ allow(@truncable).to receive(:count).and_return(0)
444
+ allow(@database).to receive(:[]).and_return @truncable
445
+
446
+ @fix = Sequel::Fixture.new
447
+ @fix.connection = @database
448
+ def @fix.stub_data
449
+ @data = { :users => nil, :actions => nil, :extras => nil }
450
+ end
451
+ @fix.stub_data
452
+
453
+ expect(@fix.check).to be_truthy
454
+ end
455
+
456
+ it "calls truncate on each of the used tables" do
457
+ expect(@truncable).to receive(:truncate).exactly(3).times
458
+ @fix.rollback
459
+ end
460
+ end
461
+ end
462
+ end
@@ -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 "converts 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
+ expect(simplified).to eq({
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 "raises 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 ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gamera-sequel-fixture
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Eric Dobbs
8
+ - Glen Aultman-Bettridge
9
+ - Jillian Rosile
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-06-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sequel
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: gamera-symbolmatrix
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: fast
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ description: Flexible fixtures for the Sequel Gem inspired in Rails 2 fixtures
72
+ email:
73
+ - eric@dobbse.net
74
+ - glenab@koansolutions.net
75
+ - jillian.rosile@gmail.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - lib/sequel-fixture.rb
87
+ - lib/sequel-fixture/exceptions.rb
88
+ - lib/sequel-fixture/table.rb
89
+ - lib/sequel-fixture/util.rb
90
+ - lib/sequel-fixture/version.rb
91
+ - sequel-fixture.gemspec
92
+ - spec/sequel/fixture_spec.rb
93
+ - spec/sequel/fixtures/another/users.yaml
94
+ - spec/sequel/fixtures/invalid/users.yaml
95
+ - spec/sequel/fixtures/processed/users.yaml
96
+ - spec/sequel/fixtures/test/actions.yaml
97
+ - spec/sequel/fixtures/test/users.yaml
98
+ - spec/sequel/util_spec.rb
99
+ homepage: http://github.com/gamera-team/sequel-fixture
100
+ licenses: []
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Flexible fixtures for the Sequel Gem inspired in Rails 2 fixtures. Temporary
122
+ fork of https://github.com/whitepages/sequel-fixture to add ERB support
123
+ test_files:
124
+ - spec/sequel/fixture_spec.rb
125
+ - spec/sequel/fixtures/another/users.yaml
126
+ - spec/sequel/fixtures/invalid/users.yaml
127
+ - spec/sequel/fixtures/processed/users.yaml
128
+ - spec/sequel/fixtures/test/actions.yaml
129
+ - spec/sequel/fixtures/test/users.yaml
130
+ - spec/sequel/util_spec.rb
131
+ has_rdoc: