vostok 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vostok/import.rb +9 -3
- data/lib/vostok/version.rb +1 -1
- data/spec/vostok_spec.rb +36 -25
- metadata +1 -1
data/lib/vostok/import.rb
CHANGED
@@ -2,10 +2,16 @@ require 'pg'
|
|
2
2
|
|
3
3
|
module Vostok
|
4
4
|
class Import
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :pg_connection, :table
|
6
6
|
|
7
7
|
def initialize(connection)
|
8
|
-
|
8
|
+
raise ArgumentError, 'Connection can not be null' unless connection
|
9
|
+
raise ArgumentError, 'Connection must be a Hash or a PG::Connection' unless (connection.is_a?(::Hash) || connection.is_a?(PG::Connection))
|
10
|
+
if connection.is_a? ::Hash
|
11
|
+
@pg_connection = PG::Connection.new(connection)
|
12
|
+
else
|
13
|
+
@pg_connection = connection
|
14
|
+
end
|
9
15
|
@options = {batch_size: 1000}
|
10
16
|
end
|
11
17
|
|
@@ -13,7 +19,7 @@ module Vostok
|
|
13
19
|
validate_args(columns, values)
|
14
20
|
@table = table
|
15
21
|
begin
|
16
|
-
@pg_connection
|
22
|
+
@pg_connection.reset
|
17
23
|
values.each_slice(options[:batch_size]) do |slice|
|
18
24
|
sql = generate_sql(table, columns, slice)
|
19
25
|
@pg_connection.exec(sql)
|
data/lib/vostok/version.rb
CHANGED
data/spec/vostok_spec.rb
CHANGED
@@ -1,23 +1,43 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'vostok' do
|
4
|
+
before {
|
5
|
+
connection_stub = stub 'connection', reset: true, close: true
|
6
|
+
connection_stub.stub(:is_a?).with(PG::Connection).and_return(true)
|
7
|
+
connection_stub.stub(:is_a?).with(Hash).and_return(false)
|
8
|
+
PG::Connection.stub(:new).and_return(connection_stub)
|
9
|
+
}
|
10
|
+
let(:import){import = Vostok::Import.new(host: 'localhost', dbname: 'db1')}
|
11
|
+
|
4
12
|
it 'should return version' do
|
5
13
|
Vostok::VERSION.should_not be_nil
|
6
14
|
end
|
7
15
|
|
8
|
-
|
9
|
-
it 'should
|
16
|
+
|
17
|
+
it 'should throw argument error when no connection is given' do
|
18
|
+
->{Vostok::Import.new(nil)}.should raise_error ArgumentError
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should throw argument error when given connection is not a hash or a PG::Connection' do
|
22
|
+
->{Vostok::Import.new(1)}.should raise_error ArgumentError
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should accept connection hash in constructor' do
|
26
|
+
import = Vostok::Import.new(host: 'localhost', dbname: 'db1')
|
10
27
|
import.should_not be_nil
|
11
28
|
end
|
12
29
|
|
13
|
-
it 'should
|
14
|
-
|
30
|
+
it 'should accept existing PG connection in constructor' do
|
31
|
+
pg_connection = PG::Connection.new(host: 'localhost', dbname: 'db1')
|
32
|
+
import = Vostok::Import.new(pg_connection)
|
33
|
+
import.should_not be_nil
|
15
34
|
end
|
16
35
|
|
17
|
-
it 'should assign
|
18
|
-
import.
|
36
|
+
it 'should assign pg_connection' do
|
37
|
+
import.pg_connection.should_not be_nil
|
19
38
|
end
|
20
39
|
|
40
|
+
|
21
41
|
context 'importing' do
|
22
42
|
it 'should throw argument error when no table name given' do
|
23
43
|
->{import.start(nil, nil, nil)}.should raise_error ArgumentError
|
@@ -36,10 +56,9 @@ describe 'vostok' do
|
|
36
56
|
->{import.start(:customers, [:a, :b], [[1,2,3], [1,2,3]])}.should raise_error ArgumentError
|
37
57
|
end
|
38
58
|
|
39
|
-
it 'should
|
40
|
-
|
41
|
-
|
42
|
-
PG::Connection.should_receive(:open).and_return mock_connection
|
59
|
+
it 'should reset pg_connection if it is not yet open' do
|
60
|
+
import.pg_connection.stub(:exec)
|
61
|
+
import.pg_connection.should_receive(:reset)
|
43
62
|
import.start(:customers, [:a], [[1]])
|
44
63
|
end
|
45
64
|
|
@@ -47,9 +66,7 @@ describe 'vostok' do
|
|
47
66
|
sql = <<-eos
|
48
67
|
insert into "customers" ("a","b") values('1','2'),('3','4')
|
49
68
|
eos
|
50
|
-
|
51
|
-
PG::Connection.stub!(:open).and_return mock_connection
|
52
|
-
mock_connection.should_receive(:exec).with(sql.strip)
|
69
|
+
import.pg_connection.should_receive(:exec).with(sql.strip)
|
53
70
|
import.start(:customers, [:a, :b], [[1,2], [3,4]])
|
54
71
|
end
|
55
72
|
|
@@ -64,26 +81,20 @@ describe 'vostok' do
|
|
64
81
|
sql3 = <<-eos
|
65
82
|
insert into "customers" ("a","b") values('9','10')
|
66
83
|
eos
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
mock_connection.should_receive(:exec).with(sql2.strip).ordered
|
71
|
-
mock_connection.should_receive(:exec).with(sql3.strip).ordered
|
84
|
+
import.pg_connection.should_receive(:exec).with(sql1.strip).ordered
|
85
|
+
import.pg_connection.should_receive(:exec).with(sql2.strip).ordered
|
86
|
+
import.pg_connection.should_receive(:exec).with(sql3.strip).ordered
|
72
87
|
import.start(:customers, [:a, :b], data, batch_size: 2)
|
73
88
|
end
|
74
89
|
|
75
90
|
it 'should return number of inserted rows' do
|
76
|
-
|
77
|
-
PG::Connection.stub!(:open).and_return mock_connection
|
78
|
-
mock_connection.stub!(:exec)
|
91
|
+
import.pg_connection.stub(:exec)
|
79
92
|
import.start(:customers, [:a, :b], [[1,2]]).should == 1
|
80
93
|
end
|
81
94
|
|
82
95
|
it 'should close the connection' do
|
83
|
-
|
84
|
-
|
85
|
-
mock_connection.stub!(:exec)
|
86
|
-
mock_connection.should_receive(:close)
|
96
|
+
import.pg_connection.stub(:exec)
|
97
|
+
import.pg_connection.should_receive(:close)
|
87
98
|
import.start(:customers, [:a, :b], [[1,2]])
|
88
99
|
end
|
89
100
|
end
|