1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# frozen_string_literal: true
require "cases/helper"
require "support/schema_dumping_helper"
if ActiveRecord::Base.connection.supports_extensions?
class PostgresqlHstoreTest < ActiveRecord::PostgreSQLTestCase
include SchemaDumpingHelper
class Hstore < ActiveRecord::Base
self.table_name = "hstores"
store_accessor :settings, :language, :timezone
end
class FakeParameters
def to_unsafe_h
{ "hi" => "hi" }
end
end
def setup
@connection = ActiveRecord::Base.connection
enable_extension!("hstore", @connection)
@connection.transaction do
@connection.create_table("hstores") do |t|
t.hstore "tags", default: ""
t.hstore "payload", array: true
t.hstore "settings"
end
end
Hstore.reset_column_information
@column = Hstore.columns_hash["tags"]
@type = Hstore.type_for_attribute("tags")
end
teardown do
@connection.drop_table "hstores", if_exists: true
disable_extension!("hstore", @connection)
end
def test_hstore_included_in_extensions
assert @connection.respond_to?(:extensions), "connection should have a list of extensions"
assert_includes @connection.extensions, "hstore", "extension list should include hstore"
end
def test_disable_enable_hstore
assert @connection.extension_enabled?("hstore")
@connection.disable_extension "hstore"
assert_not @connection.extension_enabled?("hstore")
@connection.enable_extension "hstore"
assert @connection.extension_enabled?("hstore")
ensure
# Restore column(s) dropped by `drop extension hstore cascade;`
load_schema
end
def test_column
assert_equal :hstore, @column.type
assert_equal "hstore", @column.sql_type
assert_not @column.array?
assert_not @type.binary?
end
def test_default
@connection.add_column "hstores", "permissions", :hstore, default: '"users"=>"read", "articles"=>"write"'
Hstore.reset_column_information
assert_equal({ "users" => "read", "articles" => "write" }, Hstore.column_defaults["permissions"])
assert_equal({ "users" => "read", "articles" => "write" }, Hstore.new.permissions)
ensure
Hstore.reset_column_information
end
def test_change_table_supports_hstore
@connection.transaction do
@connection.change_table("hstores") do |t|
t.hstore "users", default: ""
end
Hstore.reset_column_information
column = Hstore.columns_hash["users"]
assert_equal :hstore, column.type
raise ActiveRecord::Rollback # reset the schema change
end
ensure
Hstore.reset_column_information
end
def test_hstore_migration
hstore_migration = Class.new(ActiveRecord::Migration::Current) do
def change
change_table("hstores") do |t|
t.hstore :keys
end
end
end
hstore_migration.new.suppress_messages do
hstore_migration.migrate(:up)
assert_includes @connection.columns(:hstores).map(&:name), "keys"
hstore_migration.migrate(:down)
assert_not_includes @connection.columns(:hstores).map(&:name), "keys"
end
end
def test_cast_value_on_write
x = Hstore.new tags: { "bool" => true, "number" => 5 }
assert_equal({ "bool" => true, "number" => 5 }, x.tags_before_type_cast)
assert_equal({ "bool" => "true", "number" => "5" }, x.tags)
x.save
assert_equal({ "bool" => "true", "number" => "5" }, x.reload.tags)
end
def test_type_cast_hstore
assert_equal({ "1" => "2" }, @type.deserialize("\"1\"=>\"2\""))
assert_equal({}, @type.deserialize(""))
assert_equal({ "key" => nil }, @type.deserialize("key => NULL"))
assert_equal({ "c" => "}", '"a"' => 'b "a b' }, @type.deserialize(%q(c=>"}", "\"a\""=>"b \"a b")))
end
def test_with_store_accessors
x = Hstore.new(language: "fr", timezone: "GMT")
assert_equal "fr", x.language
assert_equal "GMT", x.timezone
x.save!
x = Hstore.first
assert_equal "fr", x.language
assert_equal "GMT", x.timezone
x.language = "de"
x.save!
x = Hstore.first
assert_equal "de", x.language
assert_equal "GMT", x.timezone
end
def test_duplication_with_store_accessors
x = Hstore.new(language: "fr", timezone: "GMT")
assert_equal "fr", x.language
assert_equal "GMT", x.timezone
y = x.dup
assert_equal "fr", y.language
assert_equal "GMT", y.timezone
end
def test_yaml_round_trip_with_store_accessors
x = Hstore.new(language: "fr", timezone: "GMT")
assert_equal "fr", x.language
assert_equal "GMT", x.timezone
y = YAML.load(YAML.dump(x))
assert_equal "fr", y.language
assert_equal "GMT", y.timezone
end
def test_changes_in_place
hstore = Hstore.create!(settings: { "one" => "two" })
hstore.settings["three"] = "four"
hstore.save!
hstore.reload
assert_equal "four", hstore.settings["three"]
assert_not hstore.changed?
end
def test_dirty_from_user_equal
settings = { "alongkey" => "anything", "key" => "value" }
hstore = Hstore.create!(settings: settings)
hstore.settings = { "key" => "value", "alongkey" => "anything" }
assert_equal settings, hstore.settings
refute hstore.changed?
end
def test_hstore_dirty_from_database_equal
settings = { "alongkey" => "anything", "key" => "value" }
hstore = Hstore.create!(settings: settings)
hstore.reload
assert_equal settings, hstore.settings
hstore.settings = settings
refute hstore.changed?
end
def test_gen1
assert_equal('" "=>""', @type.serialize(" " => ""))
end
def test_gen2
assert_equal('","=>""', @type.serialize("," => ""))
end
def test_gen3
assert_equal('"="=>""', @type.serialize("=" => ""))
end
def test_gen4
assert_equal('">"=>""', @type.serialize(">" => ""))
end
def test_parse1
assert_equal({ "a" => nil, "b" => nil, "c" => "NuLl", "null" => "c" }, @type.deserialize('a=>null,b=>NuLl,c=>"NuLl",null=>c'))
end
def test_parse2
assert_equal({ " " => " " }, @type.deserialize("\\ =>\\ "))
end
def test_parse3
assert_equal({ "=" => ">" }, @type.deserialize("==>>"))
end
def test_parse4
assert_equal({ "=a" => "q=w" }, @type.deserialize('\=a=>q=w'))
end
def test_parse5
assert_equal({ "=a" => "q=w" }, @type.deserialize('"=a"=>q\=w'))
end
def test_parse6
assert_equal({ "\"a" => "q>w" }, @type.deserialize('"\"a"=>q>w'))
end
def test_parse7
assert_equal({ "\"a" => "q\"w" }, @type.deserialize('\"a=>q"w'))
end
def test_rewrite
@connection.execute "insert into hstores (tags) VALUES ('1=>2')"
x = Hstore.first
x.tags = { '"a\'' => "b" }
assert x.save!
end
def test_select
@connection.execute "insert into hstores (tags) VALUES ('1=>2')"
x = Hstore.first
assert_equal({ "1" => "2" }, x.tags)
end
def test_array_cycle
assert_array_cycle([{ "AA" => "BB", "CC" => "DD" }, { "AA" => nil }])
end
def test_array_strings_with_quotes
assert_array_cycle([{ "this has" => 'some "s that need to be escaped"' }])
end
def test_array_strings_with_commas
assert_array_cycle([{ "this,has" => "many,values" }])
end
def test_array_strings_with_array_delimiters
assert_array_cycle(["{" => "}"])
end
def test_array_strings_with_null_strings
assert_array_cycle([{ "NULL" => "NULL" }])
end
def test_contains_nils
assert_array_cycle([{ "NULL" => nil }])
end
def test_select_multikey
@connection.execute "insert into hstores (tags) VALUES ('1=>2,2=>3')"
x = Hstore.first
assert_equal({ "1" => "2", "2" => "3" }, x.tags)
end
def test_create
assert_cycle("a" => "b", "1" => "2")
end
def test_nil
assert_cycle("a" => nil)
end
def test_quotes
assert_cycle("a" => 'b"ar', '1"foo' => "2")
end
def test_whitespace
assert_cycle("a b" => "b ar", '1"foo' => "2")
end
def test_backslash
assert_cycle('a\\b' => 'b\\ar', '1"foo' => "2")
end
def test_comma
assert_cycle("a, b" => "bar", '1"foo' => "2")
end
def test_arrow
assert_cycle("a=>b" => "bar", '1"foo' => "2")
end
def test_quoting_special_characters
assert_cycle("ca" => "cà", "ac" => "àc")
end
def test_multiline
assert_cycle("a\nb" => "c\nd")
end
class TagCollection
def initialize(hash); @hash = hash end
def to_hash; @hash end
def self.load(hash); new(hash) end
def self.dump(object); object.to_hash end
end
class HstoreWithSerialize < Hstore
serialize :tags, TagCollection
end
def test_hstore_with_serialized_attributes
HstoreWithSerialize.create! tags: TagCollection.new("one" => "two")
record = HstoreWithSerialize.first
assert_instance_of TagCollection, record.tags
assert_equal({ "one" => "two" }, record.tags.to_hash)
record.tags = TagCollection.new("three" => "four")
record.save!
assert_equal({ "three" => "four" }, HstoreWithSerialize.first.tags.to_hash)
end
def test_clone_hstore_with_serialized_attributes
HstoreWithSerialize.create! tags: TagCollection.new("one" => "two")
record = HstoreWithSerialize.first
dupe = record.dup
assert_equal({ "one" => "two" }, dupe.tags.to_hash)
end
def test_schema_dump_with_shorthand
output = dump_table_schema("hstores")
assert_match %r[t\.hstore "tags",\s+default: {}], output
end
def test_supports_to_unsafe_h_values
assert_equal("\"hi\"=>\"hi\"", @type.serialize(FakeParameters.new))
end
private
def assert_array_cycle(array)
# test creation
x = Hstore.create!(payload: array)
x.reload
assert_equal(array, x.payload)
# test updating
x = Hstore.create!(payload: [])
x.payload = array
x.save!
x.reload
assert_equal(array, x.payload)
end
def assert_cycle(hash)
# test creation
x = Hstore.create!(tags: hash)
x.reload
assert_equal(hash, x.tags)
# test updating
x = Hstore.create!(tags: {})
x.tags = hash
x.save!
x.reload
assert_equal(hash, x.tags)
end
end
end
|