sunstone 5.0.1.3 → 5.0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile.rb +2 -2
- data/ext/active_record/associations.rb +52 -0
- data/ext/active_record/persistence.rb +11 -0
- data/ext/active_record/relation.rb +23 -0
- data/ext/active_record/transactions.rb +12 -4
- data/lib/active_record/connection_adapters/sunstone/database_statements.rb +1 -2
- data/lib/arel/collectors/sunstone.rb +9 -7
- data/lib/arel/visitors/sunstone.rb +118 -93
- data/lib/sunstone.rb +1 -0
- data/lib/sunstone/version.rb +1 -1
- data/sunstone.gemspec +4 -3
- data/test/active_record/associations/has_and_belongs_to_many_test.rb +25 -2
- data/test/active_record/associations/has_many_test.rb +28 -4
- data/test/active_record/eager_loading_test.rb +37 -2
- data/test/active_record/persistance_test.rb +72 -10
- data/test/active_record/preload_test.rb +39 -4
- data/test/active_record/query/all_test.rb +33 -0
- data/test/active_record/query/count_test.rb +38 -0
- data/test/active_record/query/distinct_test.rb +30 -0
- data/test/active_record/query/find_test.rb +37 -0
- data/test/active_record/query/where_test.rb +79 -0
- data/test/active_record/query_test.rb +53 -112
- data/test/schema_mock.rb +117 -0
- data/test/sunstone/connection/configuration_test.rb +1 -1
- data/test/sunstone/connection/cookie_store_test.rb +1 -1
- data/test/sunstone/connection/request_helper_test.rb +1 -1
- data/test/sunstone/connection/send_request_test.rb +1 -1
- data/test/sunstone/connection_test.rb +1 -1
- data/test/test_helper.rb +7 -4
- metadata +23 -26
- data/test/models.rb +0 -115
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveRecord::QueryFindTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
schema do
|
6
|
+
create_table "ships", limit: 100 do |t|
|
7
|
+
t.string "name", limit: 255
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Ship < ActiveRecord::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
test '::find' do
|
15
|
+
webmock(:get, "/ships", { where: {id: 42}, limit: 1 }).to_return({
|
16
|
+
body: [{id: 42}].to_json
|
17
|
+
})
|
18
|
+
|
19
|
+
assert_equal 42, Ship.find(42).id
|
20
|
+
end
|
21
|
+
|
22
|
+
test '::find_each' do
|
23
|
+
requests = []
|
24
|
+
|
25
|
+
requests << webmock(:get, "/ships", { limit: 100, offset: 0, order: [{id: :asc}] }).to_return({
|
26
|
+
body: Array.new(100, { id: 1 }).to_json
|
27
|
+
})
|
28
|
+
requests << webmock(:get, "/ships", { limit: 100, offset: 100, order: [{id: :asc}] }).to_return({
|
29
|
+
body: Array.new(10, { id: 2 }).to_json
|
30
|
+
})
|
31
|
+
|
32
|
+
assert_nil Ship.find_each { |s| s }
|
33
|
+
|
34
|
+
requests.each { |r| assert_requested(r) }
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveRecord::QueryWhereTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
schema do
|
6
|
+
create_table "ships", limit: 100 do |t|
|
7
|
+
t.string "name", limit: 255
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table "fleets" do |t|
|
11
|
+
t.string "name", limit: 255
|
12
|
+
t.integer "fleet_id"
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table "sailors" do |t|
|
16
|
+
t.string "name", limit: 255
|
17
|
+
t.integer "ship_id"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Fleet < ActiveRecord::Base
|
22
|
+
has_many :ships
|
23
|
+
end
|
24
|
+
|
25
|
+
class Ship < ActiveRecord::Base
|
26
|
+
belongs_to :fleet
|
27
|
+
has_many :sailors
|
28
|
+
end
|
29
|
+
|
30
|
+
class Sailor < ActiveRecord::Base
|
31
|
+
belongs_to :ship
|
32
|
+
end
|
33
|
+
|
34
|
+
test '::where on columns' do
|
35
|
+
webmock(:get, "/ships", { where: { id: 10 }, limit: 100, offset: 0 }).to_return(body: [].to_json)
|
36
|
+
|
37
|
+
assert_equal [], Ship.where(id: 10).to_a
|
38
|
+
end
|
39
|
+
|
40
|
+
test '::where column is nil' do
|
41
|
+
webmock(:get, "/ships", { where: { fleet_id: nil }, limit: 100, offset: 0 }).to_return(body: [].to_json)
|
42
|
+
|
43
|
+
assert_equal [], Ship.where(fleet_id: nil).to_a
|
44
|
+
end
|
45
|
+
|
46
|
+
test '::where on belongs_to relation' do
|
47
|
+
webmock(:get, "/ships", where: {fleet: { id: {eq: 1} } }, limit: 100, offset: 0).to_return(body: [].to_json)
|
48
|
+
|
49
|
+
assert_equal [], Ship.where(fleet: {id: 1}).to_a
|
50
|
+
end
|
51
|
+
|
52
|
+
test '::where on has_many relation' do
|
53
|
+
webmock(:get, "/fleets", where: {ships: { id: {eq: 1} } }).to_return(body: [].to_json)
|
54
|
+
|
55
|
+
assert_equal [], Fleet.where(ships: {id: 1}).to_a
|
56
|
+
end
|
57
|
+
|
58
|
+
test '::where on has_and_belongs_to_many relation' do
|
59
|
+
webmock(:get, "/ships", where: {sailors: { id: {eq: 1} } }, limit: 100, offset: 0).to_return(body: [].to_json)
|
60
|
+
|
61
|
+
assert_equal [], Ship.where(sailors: {id: 1}).to_a
|
62
|
+
end
|
63
|
+
|
64
|
+
# Polymorphic
|
65
|
+
test '::where on a has_many throught a polymorphic source' do
|
66
|
+
webmock(:get, "/ships", where: { nations: { id: {eq: 1} } }, limit: 10).to_return(body: [].to_json)
|
67
|
+
|
68
|
+
assert_equal [], Ship.where(nations: {id: 1}).limit(10).to_a
|
69
|
+
end
|
70
|
+
### end polymorphic test
|
71
|
+
|
72
|
+
test '::where on nested relationship' do
|
73
|
+
webmock(:get, "/fleets", where: { ships: {sailors: { id: {eq: 1} } } }, limit: 10).to_return(body: [].to_json)
|
74
|
+
|
75
|
+
assert_equal [], Fleet.where(ships: {sailors: {id: 1}}).limit(10).to_a
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
end
|
@@ -1,135 +1,67 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ActiveRecord::QueryTest <
|
3
|
+
class ActiveRecord::QueryTest < ActiveSupport::TestCase
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
fleet_id: {type: 'integer', primary_key: false, null: true, array: false},
|
15
|
-
name: {type: 'string', primary_key: false, null: true, array: false}
|
16
|
-
}.to_json
|
17
|
-
)
|
18
|
-
|
19
|
-
webmock(:get, "/ships", { where: {id: 42}, limit: 1 }).to_return(body: [{id: 42}].to_json)
|
20
|
-
|
21
|
-
assert_equal 42, Ship.find(42).id
|
22
|
-
|
23
|
-
WebMock::API.remove_request_stub(new_stub)
|
24
|
-
WebMock::StubRegistry.instance.global_stubs.push(replaced_stub)
|
25
|
-
end
|
26
|
-
|
27
|
-
test '::all' do
|
28
|
-
ships = []
|
29
|
-
101.times { |i| ships << Ship.new(id: i) }
|
30
|
-
webmock(:get, "/ships", { limit: 100, offset: 0 }).to_return(body: ships[0..100].to_json)
|
31
|
-
webmock(:get, "/ships", { limit: 100, offset: 100 }).to_return(body: ships[101..-1].to_json)
|
32
|
-
assert_equal ships, Ship.all
|
5
|
+
schema do
|
6
|
+
create_table "ships", limit: 100 do |t|
|
7
|
+
t.string "name", limit: 255
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table "ownerships" do |t|
|
11
|
+
t.string "asset_type"
|
12
|
+
t.integer "asset_id"
|
13
|
+
end
|
33
14
|
end
|
34
15
|
|
35
|
-
|
36
|
-
|
37
|
-
assert_equal [Fleet.new(id: 42)], Fleet.all
|
16
|
+
class Ship < ActiveRecord::Base
|
17
|
+
has_many :ownerships, as: :asset
|
38
18
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
assert_equal 42, Ship.find(42).id
|
19
|
+
|
20
|
+
class Ownership < ActiveRecord::Base
|
21
|
+
belongs_to :asset, polymorphic: true
|
44
22
|
end
|
45
|
-
|
23
|
+
|
46
24
|
test '::first' do
|
47
|
-
webmock(:get, "/ships", { limit: 1, order: [{id: :asc}] }).to_return(
|
25
|
+
webmock(:get, "/ships", { limit: 1, order: [{id: :asc}] }).to_return({
|
26
|
+
body: [].to_json
|
27
|
+
})
|
48
28
|
|
49
29
|
assert_nil Ship.first
|
50
30
|
end
|
51
31
|
|
52
32
|
test '::last' do
|
53
|
-
webmock(:get, "/ships", { limit: 1, order: [{id: :desc}] }).to_return(
|
33
|
+
webmock(:get, "/ships", { limit: 1, order: [{id: :desc}] }).to_return({
|
34
|
+
body: [].to_json
|
35
|
+
})
|
54
36
|
|
55
37
|
assert_nil Ship.last
|
56
38
|
end
|
57
|
-
|
58
|
-
test '::
|
59
|
-
webmock(:get, "/ships", {
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
test '::where on columns' do
|
66
|
-
webmock(:get, "/ships", { where: { id: 10 }, limit: 100, offset: 0 }).to_return(body: [].to_json)
|
67
|
-
|
68
|
-
assert_equal [], Ship.where(:id => 10).to_a
|
69
|
-
end
|
70
|
-
|
71
|
-
test '::where column is nil' do
|
72
|
-
webmock(:get, "/ships", { where: { leased_at: nil }, limit: 100, offset: 0 }).to_return(body: [].to_json)
|
73
|
-
|
74
|
-
assert_equal [], Ship.where(:leased_at => nil).to_a
|
39
|
+
|
40
|
+
test '::where(AND CONDITION)' do
|
41
|
+
webmock(:get, "/ships", { where: {id: 10, name: 'name'}, limit: 1, order: [{id: :asc}] }).to_return({
|
42
|
+
body: [{id: 42}].to_json
|
43
|
+
})
|
44
|
+
|
45
|
+
arel_table = Ship.arel_table
|
46
|
+
assert_equal 42, Ship.where(arel_table[:id].eq(10).and(arel_table[:name].eq('name'))).first.id
|
75
47
|
end
|
48
|
+
|
49
|
+
test '::where(OR CONDITION)' do
|
50
|
+
webmock(:get, "/ships", { where: [{id: 10}, 'OR', {name: 'name'}], limit: 1, order: [{id: :asc}] }).to_return({
|
51
|
+
body: [{id: 42}].to_json
|
52
|
+
})
|
76
53
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
assert_equal [], Ship.where(:fleet => {id: 1}).to_a
|
54
|
+
arel_table = Ship.arel_table
|
55
|
+
assert_equal 42, Ship.where(arel_table[:id].eq(10).or(arel_table[:name].eq('name'))).first.id
|
81
56
|
end
|
82
57
|
|
83
|
-
test '::where
|
84
|
-
webmock(:get, "/
|
58
|
+
test '::where(AND & OR CONDITION)' do
|
59
|
+
webmock(:get, "/ships", { where: [{id: 10}, 'AND', [{id: 10}, 'OR', {name: 'name'}]], limit: 1, order: [{id: :asc}] }).to_return({
|
60
|
+
body: [{id: 42}].to_json
|
61
|
+
})
|
85
62
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
test '::where on has_and_belongs_to_many relation' do
|
90
|
-
webmock(:get, "/ships", where: {sailors: { id: {eq: 1} } }, limit: 100, offset: 0).to_return(body: [].to_json)
|
91
|
-
|
92
|
-
assert_equal [], Ship.where(:sailors => {id: 1}).to_a
|
93
|
-
end
|
94
|
-
|
95
|
-
# Polymorphic
|
96
|
-
test '::where on a has_many throught a polymorphic source' do
|
97
|
-
webmock(:get, "/ships", where: { nations: { id: {eq: 1} } }, limit: 10).to_return(body: [].to_json)
|
98
|
-
|
99
|
-
assert_equal [], Ship.where(nations: {id: 1}).limit(10).to_a
|
100
|
-
end
|
101
|
-
### end polymorphic test
|
102
|
-
|
103
|
-
# Distinct
|
104
|
-
test '::distinct query' do
|
105
|
-
webmock(:get, "/ships", distinct: true, limit: 100, offset: 0).to_return(body: [].to_json)
|
106
|
-
|
107
|
-
assert_equal [], Ship.distinct
|
108
|
-
end
|
109
|
-
|
110
|
-
# TODO: i need arel-extensions....
|
111
|
-
# test '::distinct_on query' do
|
112
|
-
# webmock(:get, "/ships", distinct_on: ['id']).to_return(body: [].to_json)
|
113
|
-
#
|
114
|
-
# assert_equal [], Ship.distinct_on(:id)
|
115
|
-
# end
|
116
|
-
|
117
|
-
test '::count' do
|
118
|
-
webmock(:get, "/ships/calculate", select: [{count: "*"}], limit: 100, offset: 0).to_return(body: [10].to_json)
|
119
|
-
|
120
|
-
assert_equal 10, Ship.count
|
121
|
-
end
|
122
|
-
|
123
|
-
test '::count(:column)' do
|
124
|
-
webmock(:get, "/ships/calculate", select: [{count: "id"}], limit: 100, offset: 0).to_return(body: [10].to_json)
|
125
|
-
|
126
|
-
assert_equal 10, Ship.count(:id)
|
127
|
-
end
|
128
|
-
|
129
|
-
test '::sum(:column)' do
|
130
|
-
webmock(:get, "/ships/calculate", select: [{sum: "weight"}], limit: 100, offset: 0).to_return(body: [10].to_json)
|
131
|
-
|
132
|
-
assert_equal 10, Ship.sum(:weight)
|
63
|
+
arel_table = Ship.arel_table
|
64
|
+
assert_equal 42, Ship.where(arel_table[:id].eq(10).and(arel_table[:id].eq(10).or(arel_table[:name].eq('name')))).first.id
|
133
65
|
end
|
134
66
|
|
135
67
|
test '::where(....big get request turns into post...)' do
|
@@ -141,6 +73,15 @@ class ActiveRecord::QueryTest < Minitest::Test
|
|
141
73
|
|
142
74
|
assert_equal 42, Ship.where(name: name)[0].id
|
143
75
|
end
|
76
|
+
|
77
|
+
test '::where with JOIN' do
|
78
|
+
webmock(:get, "/ships", {where: { ownerships: {id: {eq: 1}} }, limit: 100, offset: 0 }).to_return({
|
79
|
+
body: [{id: 42}].to_json
|
80
|
+
})
|
81
|
+
|
82
|
+
arel = Ownership.arel_table[:id].eq(1)
|
83
|
+
assert_equal 42, Ship.joins(:ownerships).where(arel)[0].id
|
84
|
+
end
|
144
85
|
|
145
86
|
# Relation test
|
146
87
|
|
@@ -149,7 +90,7 @@ class ActiveRecord::QueryTest < Minitest::Test
|
|
149
90
|
end
|
150
91
|
|
151
92
|
test '#to_sql binds correctly when joining' do
|
152
|
-
assert_equal 'SELECT ships.* FROM ships INNER JOIN ownerships ON ownerships.asset_id = ships.id AND ownerships.asset_type = \'Ship\' WHERE ownerships.id = 1', Ship.joins(:ownerships).where({ ownerships: { id: 1 } }).to_sql
|
93
|
+
assert_equal 'SELECT ships.* FROM ships INNER JOIN ownerships ON ownerships.asset_id = ships.id AND ownerships.asset_type = \'ActiveRecord::QueryTest::Ship\' WHERE ownerships.id = 1', Ship.joins(:ownerships).where({ ownerships: { id: 1 } }).to_sql
|
153
94
|
end
|
154
95
|
|
155
96
|
test '#to_sar' do
|
data/test/schema_mock.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
class ActiveSupport::TestCase
|
2
|
+
class Schema
|
3
|
+
|
4
|
+
class Table
|
5
|
+
|
6
|
+
class Column
|
7
|
+
|
8
|
+
def initialize(name, type, options={})
|
9
|
+
@name = name
|
10
|
+
@type = type
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_json
|
15
|
+
{type: @type, primary_key: false, null: true, array: false}.merge(@options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :name, :options, :columns
|
20
|
+
|
21
|
+
def initialize(name, options={}, &block)
|
22
|
+
@name = name
|
23
|
+
@options = options
|
24
|
+
@columns = {}
|
25
|
+
case options[:id]
|
26
|
+
when false
|
27
|
+
else
|
28
|
+
integer('id', primary_key: true, null: false)
|
29
|
+
end
|
30
|
+
|
31
|
+
block.call(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
def string(name, options={})
|
35
|
+
@columns[name] = Column.new(name, :string, options)
|
36
|
+
end
|
37
|
+
|
38
|
+
def datetime(name, options={})
|
39
|
+
@columns[name] = Column.new(name, :datetime, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
def integer(name, options={})
|
43
|
+
@columns[name] = Column.new(name, :integer, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_json
|
47
|
+
json = @options.slice(:limit)
|
48
|
+
json[:columns] = {}
|
49
|
+
@columns.each do |name, column|
|
50
|
+
json[:columns][name] = column.as_json
|
51
|
+
end
|
52
|
+
json.to_json
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
attr_accessor :tables
|
58
|
+
|
59
|
+
def initialize
|
60
|
+
@tables = {}
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.define(&block)
|
64
|
+
i = new
|
65
|
+
i.define(&block)
|
66
|
+
i
|
67
|
+
end
|
68
|
+
|
69
|
+
def define(&block)
|
70
|
+
instance_eval(&block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def create_table(name, options={}, &block)
|
74
|
+
@tables[name] = Table.new(name, options, &block)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.schema(&block)
|
80
|
+
self.class_variable_set(:@@schema, Schema.define(&block))
|
81
|
+
end
|
82
|
+
|
83
|
+
set_callback(:setup, :before) do
|
84
|
+
if !instance_variable_defined?(:@suite_setup_run) && self.class.class_variable_defined?(:@@schema)
|
85
|
+
ActiveRecord::Base.establish_connection(adapter: 'sunstone', url: 'http://example.com')
|
86
|
+
|
87
|
+
req_stub = stub_request(:get, /^http:\/\/example.com/).with do |req|
|
88
|
+
case req.uri.path
|
89
|
+
when '/tables'
|
90
|
+
true
|
91
|
+
when /^\/\w+\/schema$/i
|
92
|
+
true
|
93
|
+
else
|
94
|
+
false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
req_stub.to_return do |req|
|
99
|
+
case req.uri.path
|
100
|
+
when '/tables'
|
101
|
+
{
|
102
|
+
body: self.class.class_variable_get(:@@schema).tables.keys.to_json,
|
103
|
+
headers: { 'StandardAPI-Version' => '5.0.0.5' }
|
104
|
+
}
|
105
|
+
when /^\/(\w+)\/schema$/i
|
106
|
+
{
|
107
|
+
body: self.class.class_variable_get(:@@schema).tables[$1].to_json,
|
108
|
+
headers: { 'StandardAPI-Version' => '5.0.0.5' }
|
109
|
+
}
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
@suite_setup_run = true
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class Sunstone::Connection::ConfigurationTest <
|
3
|
+
class Sunstone::Connection::ConfigurationTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
test "setting the url sets the api_key" do
|
6
6
|
connection = Sunstone::Connection.new(url: 'http://my_api_key@localhost')
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class Sunstone::Connection::CookieStoreTest <
|
3
|
+
class Sunstone::Connection::CookieStoreTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
|
6
6
|
store = CookieStore::HashStore.new
|