inline_fixtures 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ *.sublime-*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.8.7" > .rvmrc
9
+ environment_id="ruby-1.8.7-p371@inline_fixtures"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.17.3 (latest)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "rspec"
7
+ gem "activerecord"
8
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jason Edwards
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ inline_fixtures
2
+ ==============
3
+
4
+ This adds a helper method to your tests so you can insert test records (fixtures) quickly and without fuss. Here's an example:
5
+
6
+
7
+ describe Post do
8
+ describe ".first" do
9
+ it "should find the first post" do
10
+ first_post_id = inline_fixture :posts, :body => "First post!",
11
+ :posted_at => "2013-01-15 23:08:12"
12
+ second_post_id = inline_fixture :posts, :body => "Second post!",
13
+ :posted_at => "2013-01-15 23:10:53"
14
+
15
+ Post.first.id.should == first_post_id
16
+ end
17
+ end
18
+ end
19
+
20
+ That's a trivial example to give you a feel. The real benefit comes when you want to insert whole bunches of records quickly, e.g.:
21
+
22
+ describe Report do
23
+
24
+ describe ".last_3_months_revenue" do
25
+ it "should return the revenue report" do
26
+ inline_fixture :invoices, [:date, :amount, :account_id] do
27
+ two_months_ago = 2.months.ago.to_s(:db)
28
+ last_month = 1.month.ago.to_s(:db)
29
+ this_month = Date.today.to_s(:db)
30
+ acct_1_id = inline_fixture(:accounts, :name => "Example Account 1")
31
+ acct_2_id = inline_fixture(:accounts, :name => "Example Account 2")
32
+ acct_3_id = inline_fixture(:accounts, :name => "Example Account 3")
33
+
34
+ [
35
+ [3.months.ago.to_s(:db), "14.00", acct_2_id],
36
+ [two_months_ago, "27.00", acct_1_id],
37
+ [two_months_ago, "88.00", acct_2_id],
38
+ [two_months_ago, "104.00", acct_3_id],
39
+ [last_month, "30.00", acct_1_id],
40
+ [last_month, "120.00", acct_2_id],
41
+ [last_month, "96.00", acct_3_id],
42
+ [this_month, "63.00", acct_1_id],
43
+ [this_month, "144.00", acct_2_id],
44
+ [this_month, "103.00", acct_3_id]
45
+ [1.months.from_now.to_s(:db), "29.00", acct_1_id],
46
+ ]
47
+ end
48
+ Report.last_3_months_revenue.should == 775.0
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ Granted this is still a pretty simple example, but the above would result in just 4 inserts, meaning it would be uber fast.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'inline_fixtures/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "inline_fixtures"
8
+ gem.version = InlineFixtures::VERSION
9
+ gem.authors = ["Jason Edwards"]
10
+ gem.email = ["jtanium@gmail.com"]
11
+ gem.description = %q{Fast inline fixtures}
12
+ gem.summary = %q{Don't use external fixture files}
13
+ gem.homepage = "https://github.com/jtanium/inline_fixtures"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,22 @@
1
+ require "inline_fixtures/version"
2
+
3
+ module InlineFixtures
4
+ def self.included(othermod)
5
+ puts "hello #{othermod}!"
6
+ end
7
+ def inline_fixture(table_name, data_or_columns=nil)
8
+
9
+ if block_given? && data_or_columns
10
+ columns = data_or_columns
11
+ values = yield
12
+ elsif block_given?
13
+ data = yield
14
+ columns = data.first.keys
15
+ values = data.map(&:values)
16
+ else
17
+ columns = data_or_columns.keys
18
+ values = [data_or_columns.values]
19
+ end
20
+ ActiveRecord::Base.connection.insert_sql("INSERT INTO #{table_name} (#{columns.join(', ')}) VALUES ('#{values.map { |row_vals| row_vals.join("', '") }.join("'), ('")}')")
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'inline_fixtures'
2
+
3
+ RSpec.configuration.include(InlineFixtures)
@@ -0,0 +1,3 @@
1
+ module InlineFixtures
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ module ActiveRecord
4
+ class Base; end
5
+ end
6
+ class ExampleClass; end
7
+ ExampleClass.class.send(:include, InlineFixtures)
8
+
9
+
10
+ describe InlineFixtures do
11
+ let(:ar_connection) { mock('ar_connection') }
12
+ before do
13
+ ActiveRecord::Base.stub(:connection).and_return(ar_connection)
14
+ end
15
+ describe "#inline_fixture" do
16
+ context "when a hash is given" do
17
+ it "should create and execute sql" do
18
+ ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (bar) VALUES ('quux')")
19
+ ExampleClass.inline_fixture :foos, :bar => "quux"
20
+
21
+ ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (baz, bar) VALUES ('grault', 'quux')")
22
+ ExampleClass.inline_fixture :foos, :baz => 'grault', :bar => "quux"
23
+ end
24
+ end
25
+ context "when a block is given with column names" do
26
+ it "should create and execute sql" do
27
+ ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (first, second, third, fourth, fifth, sixth) VALUES ('1', '2', '3', '4', '5', '6'), ('x', 'y', 'z', 'a', 'b', 'c'), ('word', 'word', 'word', 'word', 'word', 'word')")
28
+ ExampleClass.inline_fixture :foos, [:first, :second, :third, :fourth, :fifth, :sixth] do
29
+ [
30
+ %w{1 2 3 4 5 6},
31
+ %w{x y z a b c},
32
+ %w{word word word word word word}
33
+ ]
34
+ end
35
+ end
36
+ end
37
+ context "when a block is given without column names" do
38
+ it "should create and execute sql" do
39
+ ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (sixth, second, third, fourth, first, fifth) VALUES ('6', '2', '3', '4', '1', '5'), ('c', 'y', 'z', 'a', 'x', 'b'), ('word', 'word', 'word', 'word', 'word', 'word')")
40
+ ExampleClass.inline_fixture :foos do
41
+ [
42
+ {:first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :sixth => 6},
43
+ {:first => 'x', :second => 'y', :third => 'z', :fourth => 'a', :fifth => 'b', :sixth => 'c'},
44
+ {:first => 'word', :second => 'word', :third => 'word', :fourth => 'word', :fifth => 'word', :sixth => 'word'}
45
+ ]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inline_fixtures
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jason Edwards
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-01-16 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Fast inline fixtures
22
+ email:
23
+ - jtanium@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - .rspec
33
+ - .rvmrc
34
+ - Gemfile
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - inline_fixtures.gemspec
39
+ - lib/inline_fixtures.rb
40
+ - lib/inline_fixtures/rspec.rb
41
+ - lib/inline_fixtures/version.rb
42
+ - spec/lib/inline_fixtures_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: https://github.com/jtanium/inline_fixtures
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Don't use external fixture files
77
+ test_files:
78
+ - spec/lib/inline_fixtures_spec.rb
79
+ - spec/spec_helper.rb