-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathstring_ext_spec.rb
158 lines (123 loc) · 4.83 KB
/
string_ext_spec.rb
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
require 'spec_helper'
require 'activerecord-postgres-array/string'
describe "String" do
describe "#valid_postgres_array?" do
it 'returns true for an empty string' do
"".should be_valid_postgres_array
end
it 'returns true for a string consisting only of whitespace' do
" ".should be_valid_postgres_array
end
it 'returns true for a valid postgres integer array' do
"{10000, 10000, 10000, 10000}".should be_valid_postgres_array
end
it 'returns true for a valid postgres float array' do
"{10000.2, .5, 10000, 10000.9}".should be_valid_postgres_array
end
it 'returns true for a valid postgres numerical array with irregular whitespace' do
"{ 10000, 10000 , 10000,10000}".should be_valid_postgres_array
end
it 'returns false for an array with invalid commas' do
"{213,}".should_not be_valid_postgres_array
end
it 'allows enclosing single quotes' do
'\'{"ruby", "on", "rails"}\''.should be_valid_postgres_array
end
it 'returns false for an array without enclosing curly brackets' do
"213, 1234".should_not be_valid_postgres_array
end
it 'returns true for a valid postgres string array' do
'{"ruby", "on", "rails"}'.should be_valid_postgres_array
end
it 'returns true for a postgres string array with escaped double quote' do
'{"ruby", "on", "ra\"ils"}'.should be_valid_postgres_array
end
it 'returns false for a postgres string array with wrong quotation' do
'{"ruby", "on", "ra"ils"}'.should_not be_valid_postgres_array
end
it 'returns true for string array without quotes' do
"{ruby, on, rails}".should be_valid_postgres_array
end
it 'returns false for array consisting of commas' do
"{,,}".should_not be_valid_postgres_array
end
it 'returns false for concatenated strings' do
'{"ruby""on""rails"}'.should_not be_valid_postgres_array
end
it "returns false if single quotes are not closed" do
'\'{"ruby", "on", "rails"}'.should_not be_valid_postgres_array
end
it "returns true for an empty postgres array" do
"{}".should be_valid_postgres_array
end
it "returns false for postgres array beginning with ," do
"{,ruby,on,rails}".should_not be_valid_postgres_array
end
end
describe "#from_postgres_array" do
it 'returns an empty array if string is empty' do
"".from_postgres_array.should == []
end
it 'returns an empty array if empty postgres array is given' do
"{}".from_postgres_array.should == []
end
it 'returns an correct array if a valid postgres array is given' do
"{Ruby,on,Rails}".from_postgres_array.should == ["Ruby", "on", "Rails"]
end
it 'correctly handles commas' do
'{Ruby,on,"Rails,"}'.from_postgres_array.should == ["Ruby", "on", "Rails,"]
end
it 'correctly handles single quotes' do
"{Ruby,on,Ra'ils}".from_postgres_array.should == ["Ruby", "on", "Ra'ils"]
end
it 'correctly handles double quotes' do
"{Ruby,on,\"Ra\\\"ils\"}".from_postgres_array.should == ["Ruby", "on", 'Ra"ils']
end
it 'correctly handles backslashes' do
'\'{"\\\\","\\""}\''.from_postgres_array.should == ["\\","\""]
end
it 'correctly handles multi line content' do
"{A\nB\nC,X\r\nY\r\nZ}".from_postgres_array.should == ["A\nB\nC", "X\r\nY\r\nZ"]
end
it "returns NULL values as nil" do
"{NULL,on,Rails}".from_postgres_array.should == [nil, "on", "Rails"]
end
context "with a base_type of :decimal" do
it "returns decimal numbers" do
"{1.1,2.1,3.1}".from_postgres_array(:decimal).should eql [1.1,2.1,3.1]
end
it "returns NULL values as nil" do
"{NULL,2.1,3.1}".from_postgres_array(:decimal).should eql [nil, 2.1, 3.1]
end
end
context "with a base_type of :integer" do
it "returns integers" do
"{1,2,3}".from_postgres_array(:integer).should eql [1,2,3]
end
it "returns NULL values as nil" do
"{NULL,2,3}".from_postgres_array(:integer).should eql [nil, 2, 3]
end
end
context "with a base_type of :float" do
it "returns floats" do
"{1,2,3}".from_postgres_array(:float).should eql [1.to_f,2.to_f,3.to_f]
end
it "returns NULL values as nil" do
"{NULL,2,3}".from_postgres_array(:float).should eql [nil, 2.to_f, 3.to_f]
end
end
context "with a base_type of timestamp" do
it "returns time objects" do
'{"2004-10-19 10:23:54","2004-10-19 10:23:54"}'.from_postgres_array(:timestamp).should eql [
"2004-10-19 10:23:54".to_time,
"2004-10-19 10:23:54".to_time
]
end
it "returns NULL values as nil" do
'{NULL,"2004-10-19 10:23:54"}'.from_postgres_array(:timestamp).should eql [
nil, "2004-10-19 10:23:54".to_time
]
end
end
end
end