inline_fixtures 0.0.1 → 0.1.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/README.md +15 -18
- data/lib/inline_fixtures.rb +3 -4
- data/lib/inline_fixtures/version.rb +1 -1
- data/spec/lib/inline_fixtures_spec.rb +28 -18
- data/spec/spec_helper.rb +2 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -23,26 +23,23 @@ That's a trivial example to give you a feel. The real benefit comes when you wan
|
|
23
23
|
|
24
24
|
describe ".last_3_months_revenue" do
|
25
25
|
it "should return the revenue report" do
|
26
|
+
account_ids = inline_fixture :accounts, [:name] { [["Example Account 1"], ["Example Account 2"], ["Example Account 3"]] }
|
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)
|
26
30
|
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
31
|
[
|
35
|
-
[3.months.ago.to_s(:db), "14.00",
|
36
|
-
[two_months_ago, "27.00",
|
37
|
-
[two_months_ago, "88.00",
|
38
|
-
[two_months_ago, "104.00",
|
39
|
-
[last_month, "30.00",
|
40
|
-
[last_month, "120.00",
|
41
|
-
[last_month, "96.00",
|
42
|
-
[this_month, "63.00",
|
43
|
-
[this_month, "144.00",
|
44
|
-
[this_month, "103.00",
|
45
|
-
[1.months.from_now.to_s(:db), "29.00",
|
32
|
+
[3.months.ago.to_s(:db), "14.00", account_ids[1]],
|
33
|
+
[two_months_ago, "27.00", account_ids[0]],
|
34
|
+
[two_months_ago, "88.00", account_ids[1]],
|
35
|
+
[two_months_ago, "104.00", account_ids[2]],
|
36
|
+
[last_month, "30.00", account_ids[0]],
|
37
|
+
[last_month, "120.00", account_ids[1]],
|
38
|
+
[last_month, "96.00", account_ids[2]],
|
39
|
+
[this_month, "63.00", account_ids[0]],
|
40
|
+
[this_month, "144.00", account_ids[1]],
|
41
|
+
[this_month, "103.00", account_ids[2]]
|
42
|
+
[1.months.from_now.to_s(:db), "29.00", account_ids[0]],
|
46
43
|
]
|
47
44
|
end
|
48
45
|
Report.last_3_months_revenue.should == 775.0
|
data/lib/inline_fixtures.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
require "inline_fixtures/version"
|
2
2
|
|
3
3
|
module InlineFixtures
|
4
|
-
def self.included(othermod)
|
5
|
-
puts "hello #{othermod}!"
|
6
|
-
end
|
7
4
|
def inline_fixture(table_name, data_or_columns=nil)
|
8
5
|
|
9
6
|
if block_given? && data_or_columns
|
@@ -17,6 +14,8 @@ module InlineFixtures
|
|
17
14
|
columns = data_or_columns.keys
|
18
15
|
values = [data_or_columns.values]
|
19
16
|
end
|
20
|
-
ActiveRecord::Base.connection.insert_sql("INSERT INTO #{table_name} (#{columns.join(', ')}) VALUES ('#{values.map { |row_vals| row_vals.join("', '") }.join("'), ('")}')")
|
17
|
+
last_insert_id = ActiveRecord::Base.connection.insert_sql("INSERT INTO #{table_name} (#{columns.join(', ')}) VALUES ('#{values.map { |row_vals| row_vals.join("', '") }.join("'), ('")}')")
|
18
|
+
auto_generated_ids = (last_insert_id..(last_insert_id+values.length-1)).to_a
|
19
|
+
auto_generated_ids.length == 1 ? auto_generated_ids.first : auto_generated_ids
|
21
20
|
end
|
22
21
|
end
|
@@ -11,6 +11,7 @@ describe InlineFixtures do
|
|
11
11
|
let(:ar_connection) { mock('ar_connection') }
|
12
12
|
before do
|
13
13
|
ActiveRecord::Base.stub(:connection).and_return(ar_connection)
|
14
|
+
ar_connection.stub(:insert_sql).and_return(19)
|
14
15
|
end
|
15
16
|
describe "#inline_fixture" do
|
16
17
|
context "when a hash is given" do
|
@@ -18,33 +19,42 @@ describe InlineFixtures do
|
|
18
19
|
ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (bar) VALUES ('quux')")
|
19
20
|
ExampleClass.inline_fixture :foos, :bar => "quux"
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
attributes = {:baz => 'grault', :bar => "quux"}
|
23
|
+
key_list = attributes.keys.join(', ')
|
24
|
+
value_list = "'#{attributes.values.join("', '")}'"
|
25
|
+
ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (#{key_list}) VALUES (#{value_list})")
|
26
|
+
ExampleClass.inline_fixture :foos, attributes
|
27
|
+
end
|
28
|
+
it "should return the auto generated id" do
|
29
|
+
ExampleClass.inline_fixture(:foos, :bar => "quux").should == 19
|
23
30
|
end
|
24
31
|
end
|
25
32
|
context "when a block is given with column names" do
|
33
|
+
let(:column_names) { [:first, :second, :third, :fourth, :fifth, :sixth] }
|
34
|
+
let(:fixture_records) { [%w{1 2 3 4 5 6}, %w{x y z a b c}, %w{word word word word word word}] }
|
26
35
|
it "should create and execute sql" do
|
27
36
|
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
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
%w{word word word word word word}
|
33
|
-
]
|
34
|
-
end
|
37
|
+
ExampleClass.inline_fixture(:foos, column_names) { fixture_records }
|
38
|
+
end
|
39
|
+
it "should return the auto generated ids" do
|
40
|
+
ExampleClass.inline_fixture(:foos, column_names) { fixture_records }.should == [19, 20, 21]
|
35
41
|
end
|
36
42
|
end
|
37
43
|
context "when a block is given without column names" do
|
44
|
+
let(:attributes_1) { {:first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :sixth => 6} }
|
45
|
+
let(:attributes_2) { {:first => 'x', :second => 'y', :third => 'z', :fourth => 'a', :fifth => 'b', :sixth => 'c'} }
|
46
|
+
let(:attributes_3) { {:first => 'word', :second => 'word', :third => 'word', :fourth => 'word', :fifth => 'word', :sixth => 'word'} }
|
47
|
+
let(:fixture_records) { [attributes_1, attributes_2, attributes_3] }
|
38
48
|
it "should create and execute sql" do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
49
|
+
key_list = attributes_3.keys.join(', ')
|
50
|
+
value_list = "'#{attributes_1.values.join("', '")}'), ('#{attributes_2.values.join("', '")}'), ('#{attributes_3.values.join("', '")}'"
|
51
|
+
|
52
|
+
ar_connection.should_receive(:insert_sql).with("INSERT INTO foos (#{key_list}) VALUES (#{value_list})")
|
53
|
+
ExampleClass.inline_fixture(:foos) { fixture_records }
|
54
|
+
end
|
55
|
+
it "should return the auto generated ids" do
|
56
|
+
ExampleClass.inline_fixture(:foos) { fixture_records }.should == [19, 20, 21]
|
47
57
|
end
|
48
58
|
end
|
49
59
|
end
|
50
|
-
end
|
60
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'inline_fixtures'))
|
2
|
+
|
1
3
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
4
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
5
|
# Require this file using `require "spec_helper"` to ensure that it is only
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inline_fixtures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jason Edwards
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-01-
|
18
|
+
date: 2013-01-17 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Fast inline fixtures
|