arel-extensions 8.1.0 → 9.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.
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