arel-extensions 8.1.0 → 9.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -0
- data/README.md +83 -2
- data/lib/active_record/query_methods.rb +30 -36
- data/lib/arel/extensions/version.rb +1 -1
- metadata +14 -40
- data/.github/workflows/ci.yml +0 -267
- data/.gitignore +0 -3
- data/Gemfile +0 -5
- data/Rakefile +0 -14
- data/arel-extensions.gemspec +0 -29
- data/test/binary_value_test.rb +0 -23
- data/test/database.rb +0 -51
- data/test/gis_test.rb +0 -23
- data/test/order_test.rb +0 -83
- data/test/sunstone_test.rb +0 -149
- data/test/test_helper.rb +0 -209
- data/test/ts_test.rb +0 -151
data/test/binary_value_test.rb
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class BinaryValueTest < ActiveSupport::TestCase
|
|
4
|
-
|
|
5
|
-
test 'binary value' do
|
|
6
|
-
query = Property.where( Arel::Nodes::BinaryValue.new("\x01\x01\x00\x00\x00\xC0K\x9B\x84\xD0.<@\b\x96\xA2n\x84%&@") )
|
|
7
|
-
|
|
8
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
9
|
-
SELECT "properties".* FROM "properties"
|
|
10
|
-
WHERE '\\x0101000000c04b9b84d02e3c400896a26e84252640'
|
|
11
|
-
SQL
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
test 'hex encoded binary value' do
|
|
15
|
-
query = Property.where( Arel::Nodes::HexEncodedBinaryValue.new("0101000000c04b9b84d02e3c400896a26e84252640") )
|
|
16
|
-
|
|
17
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
18
|
-
SELECT "properties".* FROM "properties"
|
|
19
|
-
WHERE '\\x0101000000c04b9b84d02e3c400896a26e84252640'
|
|
20
|
-
SQL
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
end
|
data/test/database.rb
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
ActiveRecord::Base.establish_connection({
|
|
2
|
-
adapter: "postgis",
|
|
3
|
-
database: "arel-extensions-test",
|
|
4
|
-
encoding: "utf8"
|
|
5
|
-
})
|
|
6
|
-
|
|
7
|
-
db_config = ActiveRecord::Base.connection_db_config
|
|
8
|
-
task = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(db_config)
|
|
9
|
-
task.drop
|
|
10
|
-
task.create
|
|
11
|
-
|
|
12
|
-
ActiveRecord::Migration.suppress_messages do
|
|
13
|
-
ActiveRecord::Schema.define do
|
|
14
|
-
enable_extension "postgis"
|
|
15
|
-
|
|
16
|
-
create_table "addresses", force: :cascade do |t|
|
|
17
|
-
t.integer "name"
|
|
18
|
-
t.integer "property_id"
|
|
19
|
-
t.geometry "location", limit: {:type=>"Point", :srid=>"4326"}
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
create_table "properties", force: :cascade do |t|
|
|
23
|
-
t.string "name", limit: 255
|
|
24
|
-
t.tsvector 'vector_col'
|
|
25
|
-
t.jsonb 'metadata'
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
class Address < ActiveRecord::Base
|
|
32
|
-
belongs_to :property
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
class Property < ActiveRecord::Base
|
|
36
|
-
has_many :addresses
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
class SunstoneRecord < ActiveRecord::Base
|
|
40
|
-
self.abstract_class = true
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
class SunstoneAddress < SunstoneRecord
|
|
44
|
-
belongs_to :property, class_name: 'SunstoneProperty'
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
class SunstoneProperty < SunstoneRecord
|
|
48
|
-
has_many :addresses, class_name: 'SunstoneAddress'
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
SunstoneRecord.establish_connection(adapter: 'sunstone', url: 'http://example.com')
|
data/test/gis_test.rb
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class GISTest < ActiveSupport::TestCase
|
|
4
|
-
|
|
5
|
-
test 'intersects' do
|
|
6
|
-
query = Address.arel_table['location'].intersects('POINT (28.182869232095754 11.073276002261096)')
|
|
7
|
-
|
|
8
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
9
|
-
ST_Intersects("addresses"."location", '0101000000c04b9b84d02e3c400896a26e84252640')
|
|
10
|
-
SQL
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
test 'within' do
|
|
14
|
-
query = Address.arel_table['location'].within('POINT (28.182869232095754 11.073276002261096)')
|
|
15
|
-
|
|
16
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
17
|
-
ST_Within("addresses"."location", '0101000000c04b9b84d02e3c400896a26e84252640')
|
|
18
|
-
SQL
|
|
19
|
-
|
|
20
|
-
result = Address.where(query).first
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
end
|
data/test/order_test.rb
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class OrderTest < ActiveSupport::TestCase
|
|
4
|
-
|
|
5
|
-
test '::order(column.asc)' do
|
|
6
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc))
|
|
7
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC
|
|
8
|
-
SQL
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
test '::order(column1.asc, column2.asc)' do
|
|
12
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc, Property.arel_table[:name].asc))
|
|
13
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC, "properties"."name" ASC
|
|
14
|
-
SQL
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
test '::order(column.desc)' do
|
|
18
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].desc))
|
|
19
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" DESC
|
|
20
|
-
SQL
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
test '::order(column.asc(:nulls_first))' do
|
|
24
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc(:nulls_first)))
|
|
25
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC NULLS FIRST
|
|
26
|
-
SQL
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
test '::order(column.asc(:nulls_last))' do
|
|
30
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc(:nulls_last)))
|
|
31
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC NULLS LAST
|
|
32
|
-
SQL
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
test '::order(column.desc(:nulls_first))' do
|
|
36
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].desc(:nulls_first)))
|
|
37
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" DESC NULLS FIRST
|
|
38
|
-
SQL
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
test '::order(column.desc(:nulls_last))' do
|
|
42
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].desc(:nulls_last)))
|
|
43
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" DESC NULLS LAST
|
|
44
|
-
SQL
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Test reversing orders
|
|
48
|
-
test '::order(column.asc.reverse)' do
|
|
49
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc.reverse))
|
|
50
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" DESC
|
|
51
|
-
SQL
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
test '::order(column.desc.reverse)' do
|
|
55
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].desc.reverse))
|
|
56
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC
|
|
57
|
-
SQL
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
test '::order(column.asc(:nulls_first).reverse)' do
|
|
61
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc(:nulls_first).reverse))
|
|
62
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" DESC NULLS LAST
|
|
63
|
-
SQL
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
test '::order(column.asc(:nulls_last).reverse)' do
|
|
67
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].asc(:nulls_last).reverse))
|
|
68
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" DESC NULLS FIRST
|
|
69
|
-
SQL
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
test '::order(column.desc(:nulls_first).reverse)' do
|
|
73
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].desc(:nulls_first).reverse))
|
|
74
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC NULLS LAST
|
|
75
|
-
SQL
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
test '::order(column.desc(:nulls_last).reverse)' do
|
|
79
|
-
assert_sql(<<~SQL, Property.order(Property.arel_table[:id].desc(:nulls_last).reverse))
|
|
80
|
-
SELECT "properties".* FROM "properties" ORDER BY "properties"."id" ASC NULLS FIRST
|
|
81
|
-
SQL
|
|
82
|
-
end
|
|
83
|
-
end
|
data/test/sunstone_test.rb
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class SunstoneTest < ActiveSupport::TestCase
|
|
4
|
-
|
|
5
|
-
# schema do
|
|
6
|
-
# create_table "properties", force: :cascade do |t|
|
|
7
|
-
# t.jsonb 'metadata'
|
|
8
|
-
# end
|
|
9
|
-
# end
|
|
10
|
-
|
|
11
|
-
# class Property < ActiveRecord::Base
|
|
12
|
-
# end
|
|
13
|
-
|
|
14
|
-
# test "::filter json_column: STRING throws an error" do
|
|
15
|
-
# assert_raises(ActiveRecord::UnkownFilterError) do
|
|
16
|
-
# Property.filter(metadata: 'string').load
|
|
17
|
-
# end
|
|
18
|
-
# end
|
|
19
|
-
|
|
20
|
-
# test "::filter json_column: {eq: JSON_HASH}" do
|
|
21
|
-
# query = Property.where(metadata: {eq: {json: 'string'}})
|
|
22
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
23
|
-
# SELECT "properties".*
|
|
24
|
-
# FROM "properties"
|
|
25
|
-
# WHERE "properties"."metadata" = '{\"json\":\"string\"}'
|
|
26
|
-
# SQL
|
|
27
|
-
# end
|
|
28
|
-
|
|
29
|
-
test "::filter json_column: {contains: JSON_HASH}" do
|
|
30
|
-
query = SunstoneProperty.where(SunstoneProperty.arel_table['metadata'].contains({json: 'string'}))
|
|
31
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
32
|
-
where: {metadata: {contains: { json: 'string' }}}
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
# query = Property.where(metadata: {contains: {json: 'string'}})
|
|
36
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
37
|
-
# SELECT "properties".*
|
|
38
|
-
# FROM "properties"
|
|
39
|
-
# WHERE "properties"."metadata" @> '{\"json\":\"string\"}'
|
|
40
|
-
# SQL
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# test "::filter json_column: {contained_by: JSON_HASH}" do
|
|
44
|
-
# query = Property.filter(metadata: {contained_by: {json: 'string'}})
|
|
45
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
46
|
-
# SELECT "properties".*
|
|
47
|
-
# FROM "properties"
|
|
48
|
-
# WHERE "properties"."metadata" <@ '{\"json\":\"string\"}'
|
|
49
|
-
# SQL
|
|
50
|
-
# end
|
|
51
|
-
|
|
52
|
-
# test "::filter json_column: {has_key: STRING}" do
|
|
53
|
-
# query = Property.filter(metadata: {has_key: 'string'})
|
|
54
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
55
|
-
# SELECT "properties".*
|
|
56
|
-
# FROM "properties"
|
|
57
|
-
# WHERE "properties"."metadata" ? 'string'
|
|
58
|
-
# SQL
|
|
59
|
-
# end
|
|
60
|
-
|
|
61
|
-
# test "::filter json_column.subkey: {eq: JSON_HASH}" do
|
|
62
|
-
# query = Property.filter("metadata.subkey" => {eq: 'string'})
|
|
63
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
64
|
-
# SELECT "properties".*
|
|
65
|
-
# FROM "properties"
|
|
66
|
-
# WHERE "properties"."metadata"#>'{subkey}' = 'string'
|
|
67
|
-
# SQL
|
|
68
|
-
# end
|
|
69
|
-
|
|
70
|
-
# test "::filter json_column: BOOLEAN" do
|
|
71
|
-
# query = Property.filter(metadata: true)
|
|
72
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
73
|
-
# SELECT "properties".*
|
|
74
|
-
# FROM "properties"
|
|
75
|
-
# WHERE "properties"."metadata" IS NOT NULL
|
|
76
|
-
# SQL
|
|
77
|
-
|
|
78
|
-
# query = Property.filter(metadata: "true")
|
|
79
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
80
|
-
# SELECT "properties".*
|
|
81
|
-
# FROM "properties"
|
|
82
|
-
# WHERE "properties"."metadata" IS NOT NULL
|
|
83
|
-
# SQL
|
|
84
|
-
|
|
85
|
-
# query = Property.filter(metadata: false)
|
|
86
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
87
|
-
# SELECT "properties".*
|
|
88
|
-
# FROM "properties"
|
|
89
|
-
# WHERE "properties"."metadata" IS NULL
|
|
90
|
-
# SQL
|
|
91
|
-
|
|
92
|
-
# query = Property.filter(metadata: "false")
|
|
93
|
-
# assert_equal(<<-SQL.strip.gsub(/\s+/, ' '), query.to_sql.strip)
|
|
94
|
-
# SELECT "properties".*
|
|
95
|
-
# FROM "properties"
|
|
96
|
-
# WHERE "properties"."metadata" IS NULL
|
|
97
|
-
# SQL
|
|
98
|
-
# end
|
|
99
|
-
|
|
100
|
-
test '::order(column.asc)' do
|
|
101
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].asc)
|
|
102
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
103
|
-
order: [{ id: :asc }]
|
|
104
|
-
})
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
test '::order(column1.asc, column2.asc)' do
|
|
108
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].asc, SunstoneProperty.arel_table[:name].asc)
|
|
109
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
110
|
-
order: [{ id: :asc }, { name: :asc }]
|
|
111
|
-
})
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
test '::order(column.desc)' do
|
|
115
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].desc)
|
|
116
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
117
|
-
order: [{ id: :desc }]
|
|
118
|
-
})
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
test '::order(column.asc(:nulls_first))' do
|
|
122
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].asc(:nulls_first))
|
|
123
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
124
|
-
order: [{ id: { asc: :nulls_first } }]
|
|
125
|
-
})
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
test '::order(column.asc(:nulls_last))' do
|
|
129
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].asc(:nulls_last))
|
|
130
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
131
|
-
order: [{ id: { asc: :nulls_last } }]
|
|
132
|
-
})
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
test '::order(column.desc(:nulls_first))' do
|
|
136
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].desc(:nulls_first))
|
|
137
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
138
|
-
order: [{ id: { desc: :nulls_first } }]
|
|
139
|
-
})
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
test '::order(column.desc(:nulls_last))' do
|
|
143
|
-
query = SunstoneProperty.order(SunstoneProperty.arel_table[:id].desc(:nulls_last))
|
|
144
|
-
assert_sar(query, 'GET', '/sunstone_properties', {
|
|
145
|
-
order: [{ id: { desc: :nulls_last } }]
|
|
146
|
-
})
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
end
|
data/test/test_helper.rb
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
# To make testing/debugging easier, test within this source tree versus an
|
|
2
|
-
# installed gem
|
|
3
|
-
$LOAD_PATH << File.expand_path('../lib', __FILE__)
|
|
4
|
-
|
|
5
|
-
# require 'simplecov'
|
|
6
|
-
# SimpleCov.start do
|
|
7
|
-
# add_group 'lib', 'sunstone/lib'
|
|
8
|
-
# add_group 'ext', 'sunstone/ext'
|
|
9
|
-
# end
|
|
10
|
-
|
|
11
|
-
require 'rgeo'
|
|
12
|
-
require 'debug'
|
|
13
|
-
require "minitest/autorun"
|
|
14
|
-
require 'minitest/reporters'
|
|
15
|
-
require 'webmock/minitest'
|
|
16
|
-
# require 'mocha/minitest'
|
|
17
|
-
require 'active_record'
|
|
18
|
-
require 'sunstone'
|
|
19
|
-
require 'arel/extensions'
|
|
20
|
-
require 'activerecord-postgis-adapter'
|
|
21
|
-
|
|
22
|
-
# Setup the test db
|
|
23
|
-
ActiveSupport.test_order = :random
|
|
24
|
-
require File.expand_path('../database', __FILE__)
|
|
25
|
-
|
|
26
|
-
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
|
|
27
|
-
|
|
28
|
-
$debugging = false
|
|
29
|
-
|
|
30
|
-
# File 'lib/active_support/testing/declarative.rb', somewhere in rails....
|
|
31
|
-
class ActiveSupport::TestCase
|
|
32
|
-
include WebMock::API
|
|
33
|
-
|
|
34
|
-
# File 'lib/active_support/testing/declarative.rb'
|
|
35
|
-
def self.test(name, &block)
|
|
36
|
-
test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
|
|
37
|
-
defined = method_defined? test_name
|
|
38
|
-
raise "#{test_name} is already defined in #{self}" if defined
|
|
39
|
-
if block_given?
|
|
40
|
-
define_method(test_name, &block)
|
|
41
|
-
else
|
|
42
|
-
define_method(test_name) do
|
|
43
|
-
skip "No implementation provided for #{name}"
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def webmock(method, path, query=nil)
|
|
49
|
-
query = deep_transform_query(query) if query
|
|
50
|
-
|
|
51
|
-
stub_request(method, /^#{ActiveRecord::Base.connection.instance_variable_get(:@connection).url}/).with do |req|
|
|
52
|
-
if query
|
|
53
|
-
req&.uri&.path == path && req.uri.query && unpack(req.uri.query.sub(/=true$/, '')) == query
|
|
54
|
-
else
|
|
55
|
-
req&.uri&.path == path && req.uri.query.nil?
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def setup
|
|
61
|
-
sunstone_schema = {
|
|
62
|
-
addresses: {
|
|
63
|
-
columns: {
|
|
64
|
-
id: { type: :integer, primary_key: true, null: false, array: false },
|
|
65
|
-
name: { type: :string, primary_key: false, null: true, array: false },
|
|
66
|
-
property_id: { type: :integer, primary_key: false, null: true, array: false }
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
properties: {
|
|
70
|
-
columns: {
|
|
71
|
-
id: { type: :integer, primary_key: true, null: false, array: false },
|
|
72
|
-
name: { type: :string, primary_key: false, null: true, array: false },
|
|
73
|
-
metadata: { type: :json, primary_key: false, null: true, array: false }
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
req_stub = stub_request(:get, /^http:\/\/example.com/).with do |req|
|
|
79
|
-
case req.uri.path
|
|
80
|
-
when '/tables'
|
|
81
|
-
true
|
|
82
|
-
when /^\/\w+\/schema$/i
|
|
83
|
-
true
|
|
84
|
-
else
|
|
85
|
-
false
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
req_stub.to_return do |req|
|
|
90
|
-
case req.uri.path
|
|
91
|
-
when '/tables'
|
|
92
|
-
{
|
|
93
|
-
body: sunstone_schema.keys.to_json,
|
|
94
|
-
headers: { 'StandardAPI-Version' => '5.0.0.5' }
|
|
95
|
-
}
|
|
96
|
-
when /^\/(\w+)\/schema$/i
|
|
97
|
-
{
|
|
98
|
-
body: sunstone_schema[$1.to_sym].to_json,
|
|
99
|
-
headers: { 'StandardAPI-Version' => '5.0.0.5' }
|
|
100
|
-
}
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def debug
|
|
106
|
-
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
|
107
|
-
$debugging = true
|
|
108
|
-
yield
|
|
109
|
-
ensure
|
|
110
|
-
ActiveRecord::Base.logger = nil
|
|
111
|
-
$debugging = false
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
class SQLLogger
|
|
116
|
-
class << self
|
|
117
|
-
attr_accessor :ignored_sql, :log, :log_all
|
|
118
|
-
def clear_log; self.log = []; self.log_all = []; end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
self.clear_log
|
|
122
|
-
|
|
123
|
-
self.ignored_sql = [/^PRAGMA/i, /^SELECT currval/i, /^SELECT CAST/i, /^SELECT @@IDENTITY/i, /^SELECT @@ROWCOUNT/i, /^SAVEPOINT/i, /^ROLLBACK TO SAVEPOINT/i, /^RELEASE SAVEPOINT/i, /^SHOW max_identifier_length/i, /^BEGIN/i, /^COMMIT/i]
|
|
124
|
-
|
|
125
|
-
# FIXME: this needs to be refactored so specific database can add their own
|
|
126
|
-
# ignored SQL, or better yet, use a different notification for the queries
|
|
127
|
-
# instead examining the SQL content.
|
|
128
|
-
oracle_ignored = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from all_triggers/im, /^\s*select .* from all_constraints/im, /^\s*select .* from all_tab_cols/im]
|
|
129
|
-
mysql_ignored = [/^SHOW FULL TABLES/i, /^SHOW FULL FIELDS/, /^SHOW CREATE TABLE /i, /^SHOW VARIABLES /, /^\s*SELECT (?:column_name|table_name)\b.*\bFROM information_schema\.(?:key_column_usage|tables)\b/im]
|
|
130
|
-
postgresql_ignored = [/^\s*select\b.*\bfrom\b.*pg_namespace\b/im, /^\s*select tablename\b.*from pg_tables\b/im, /^\s*select\b.*\battname\b.*\bfrom\b.*\bpg_attribute\b/im, /^SHOW search_path/i]
|
|
131
|
-
sqlite3_ignored = [/^\s*SELECT name\b.*\bFROM sqlite_master/im, /^\s*SELECT sql\b.*\bFROM sqlite_master/im]
|
|
132
|
-
|
|
133
|
-
[oracle_ignored, mysql_ignored, postgresql_ignored, sqlite3_ignored].each do |db_ignored_sql|
|
|
134
|
-
ignored_sql.concat db_ignored_sql
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
attr_reader :ignore
|
|
138
|
-
|
|
139
|
-
def initialize(ignore = Regexp.union(self.class.ignored_sql))
|
|
140
|
-
@ignore = ignore
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
def call(name, start, finish, message_id, values)
|
|
144
|
-
sql = values[:sql]
|
|
145
|
-
|
|
146
|
-
# FIXME: this seems bad. we should probably have a better way to indicate
|
|
147
|
-
# the query was cached
|
|
148
|
-
return if 'CACHE' == values[:name]
|
|
149
|
-
|
|
150
|
-
self.class.log_all << sql
|
|
151
|
-
unless ignore =~ sql
|
|
152
|
-
if $debugging
|
|
153
|
-
puts caller.select { |l| l.starts_with?(File.expand_path('../../lib', __FILE__)) }
|
|
154
|
-
puts "\n\n"
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
self.class.log << sql unless ignore =~ sql
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
ActiveSupport::Notifications.subscribe('sql.active_record', SQLLogger.new)
|
|
161
|
-
|
|
162
|
-
def deep_transform_query(object)
|
|
163
|
-
case object
|
|
164
|
-
when Hash
|
|
165
|
-
object.each_with_object({}) do |(key, value), result|
|
|
166
|
-
result[key.to_s] = deep_transform_query(value)
|
|
167
|
-
end
|
|
168
|
-
when Array
|
|
169
|
-
object.map {|e| deep_transform_query(e) }
|
|
170
|
-
when Symbol
|
|
171
|
-
object.to_s
|
|
172
|
-
else
|
|
173
|
-
object
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
def assert_sar(query, method, path, query_params = {}, body = nil)
|
|
178
|
-
sar = query.to_sar
|
|
179
|
-
|
|
180
|
-
assert_equal method, sar.method
|
|
181
|
-
assert_equal path, sar.path.split('?').first
|
|
182
|
-
assert_equal deep_transform_query(query_params), MessagePack.unpack(CGI::unescape(sar.path.split('?').last))
|
|
183
|
-
if body.nil?
|
|
184
|
-
assert_nil sar.body
|
|
185
|
-
else
|
|
186
|
-
assert_equal body, sar.body
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
def assert_sql(expected, query)
|
|
191
|
-
assert_equal expected.strip, query.to_sql.strip.gsub(/\s+/, ' ')
|
|
192
|
-
end
|
|
193
|
-
# test/unit backwards compatibility methods
|
|
194
|
-
alias :assert_raise :assert_raises
|
|
195
|
-
alias :assert_not_empty :refute_empty
|
|
196
|
-
alias :assert_not_equal :refute_equal
|
|
197
|
-
alias :assert_not_in_delta :refute_in_delta
|
|
198
|
-
alias :assert_not_in_epsilon :refute_in_epsilon
|
|
199
|
-
alias :assert_not_includes :refute_includes
|
|
200
|
-
alias :assert_not_instance_of :refute_instance_of
|
|
201
|
-
alias :assert_not_kind_of :refute_kind_of
|
|
202
|
-
alias :assert_no_match :refute_match
|
|
203
|
-
alias :assert_not_nil :refute_nil
|
|
204
|
-
alias :assert_not_operator :refute_operator
|
|
205
|
-
alias :assert_not_predicate :refute_predicate
|
|
206
|
-
alias :assert_not_respond_to :refute_respond_to
|
|
207
|
-
alias :assert_not_same :refute_same
|
|
208
|
-
|
|
209
|
-
end
|
data/test/ts_test.rb
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class TSTest < ActiveSupport::TestCase
|
|
4
|
-
|
|
5
|
-
test 'tsquery(query)' do
|
|
6
|
-
query = Property.where( Arel::Nodes::TSQuery.new('query') )
|
|
7
|
-
|
|
8
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
9
|
-
SELECT "properties".* FROM "properties"
|
|
10
|
-
WHERE to_tsquery('query')
|
|
11
|
-
SQL
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
test 'tsquery(query, language: lang)' do
|
|
15
|
-
query = Property.where( Arel::Nodes::TSQuery.new('query', language: 'lang') )
|
|
16
|
-
|
|
17
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
18
|
-
SELECT "properties".* FROM "properties"
|
|
19
|
-
WHERE to_tsquery('lang', 'query')
|
|
20
|
-
SQL
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
test 'tsvector(attribute)' do
|
|
24
|
-
query = Property.where( Arel::Nodes::TSVector.new(Property.arel_table[:name]) )
|
|
25
|
-
|
|
26
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
27
|
-
SELECT "properties".* FROM "properties"
|
|
28
|
-
WHERE to_tsvector("properties"."name")
|
|
29
|
-
SQL
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
test 'tsvector(text)' do
|
|
33
|
-
query = Property.where( Arel::Nodes::TSVector.new(
|
|
34
|
-
Arel::Nodes::Quoted.new("my data O'Malley")
|
|
35
|
-
))
|
|
36
|
-
|
|
37
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
38
|
-
SELECT "properties".* FROM "properties"
|
|
39
|
-
WHERE to_tsvector('my data O''Malley')
|
|
40
|
-
SQL
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
test 'tsvector(attribute, language: lang)' do
|
|
44
|
-
query = Property.where( Arel::Nodes::TSVector.new(Property.arel_table[:name], language: 'lang') )
|
|
45
|
-
|
|
46
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
47
|
-
SELECT "properties".* FROM "properties"
|
|
48
|
-
WHERE to_tsvector('lang', "properties"."name")
|
|
49
|
-
SQL
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
test 'ts_match(attrbute, tsquery)' do
|
|
53
|
-
query = Property.where(
|
|
54
|
-
Arel::Nodes::TSMatch.new(
|
|
55
|
-
Property.arel_table[:name],
|
|
56
|
-
Arel::Nodes::TSQuery.new('query')
|
|
57
|
-
)
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
61
|
-
SELECT "properties".* FROM "properties"
|
|
62
|
-
WHERE "properties"."name" @@ to_tsquery('query')
|
|
63
|
-
SQL
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
test 'ts_match(tsvector, tsquery)' do
|
|
67
|
-
query = Property.where(
|
|
68
|
-
Arel::Nodes::TSMatch.new(
|
|
69
|
-
Arel::Nodes::TSVector.new(Property.arel_table[:name]),
|
|
70
|
-
Arel::Nodes::TSQuery.new('query')
|
|
71
|
-
)
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
75
|
-
SELECT "properties".* FROM "properties"
|
|
76
|
-
WHERE to_tsvector("properties"."name") @@ to_tsquery('query')
|
|
77
|
-
SQL
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
test 'ts_rank(attribute, tsquery)' do
|
|
81
|
-
query = Property.where(
|
|
82
|
-
Arel::Nodes::TSRank.new(
|
|
83
|
-
Property.arel_table[:name],
|
|
84
|
-
Arel::Nodes::TSQuery.new('query')
|
|
85
|
-
)
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
89
|
-
SELECT "properties".* FROM "properties"
|
|
90
|
-
WHERE ts_rank("properties"."name", to_tsquery('query'))
|
|
91
|
-
SQL
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
test 'ts_rank(tsvector, tsquery)' do
|
|
95
|
-
query = Property.where(
|
|
96
|
-
Arel::Nodes::TSRank.new(
|
|
97
|
-
Arel::Nodes::TSVector.new(Property.arel_table[:name]),
|
|
98
|
-
Arel::Nodes::TSQuery.new('query')
|
|
99
|
-
)
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
103
|
-
SELECT "properties".* FROM "properties"
|
|
104
|
-
WHERE ts_rank(to_tsvector("properties"."name"), to_tsquery('query'))
|
|
105
|
-
SQL
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
test 'ts_rank_cd(attribute, tsquery)' do
|
|
109
|
-
query = Property.where(
|
|
110
|
-
Arel::Nodes::TSRankCD.new(
|
|
111
|
-
Property.arel_table[:name],
|
|
112
|
-
Arel::Nodes::TSQuery.new('query')
|
|
113
|
-
)
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
117
|
-
SELECT "properties".* FROM "properties"
|
|
118
|
-
WHERE ts_rank_cd("properties"."name", to_tsquery('query'))
|
|
119
|
-
SQL
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
test 'ts_rank_cd(tsvector, tsquery)' do
|
|
123
|
-
query = Property.where(
|
|
124
|
-
Arel::Nodes::TSRankCD.new(
|
|
125
|
-
Arel::Nodes::TSVector.new(Property.arel_table[:name]),
|
|
126
|
-
Arel::Nodes::TSQuery.new('query')
|
|
127
|
-
)
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
131
|
-
SELECT "properties".* FROM "properties"
|
|
132
|
-
WHERE ts_rank_cd(to_tsvector("properties"."name"), to_tsquery('query'))
|
|
133
|
-
SQL
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
test 'ts_rank_cd(tsvector, tsquery, normalization)' do
|
|
137
|
-
query = Property.where(
|
|
138
|
-
Arel::Nodes::TSRankCD.new(
|
|
139
|
-
Arel::Nodes::TSVector.new(Property.arel_table[:name]),
|
|
140
|
-
Arel::Nodes::TSQuery.new('query'),
|
|
141
|
-
5
|
|
142
|
-
)
|
|
143
|
-
)
|
|
144
|
-
|
|
145
|
-
assert_equal(<<~SQL.gsub(/( +|\n)/, ' ').strip, query.to_sql)
|
|
146
|
-
SELECT "properties".* FROM "properties"
|
|
147
|
-
WHERE ts_rank_cd(to_tsvector("properties"."name"), to_tsquery('query'), 5)
|
|
148
|
-
SQL
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
end
|