forked from tlconnor/activerecord-postgres-array
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_spec.rb
135 lines (118 loc) · 4.37 KB
/
integration_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
require 'spec_helper'
describe Article do
describe ".create" do
it "builds valid arrays" do
article = Article.create(:languages => ["English", "German"], :author_ids => [1,2,3])
article.reload
article.languages_before_type_cast.should == "{English,German}"
article.languages.should == ["English", "German"]
article.author_ids_before_type_cast.should == "{1,2,3}"
article.author_ids.should == [1,2,3]
end
it "escapes single quotes correctly" do
article = Article.create(:languages => ["English", "Ger'man"])
article.reload
article.languages_before_type_cast.should == "{English,Ger''man}"
article.languages.should == ["English", "Ger'man"]
end
it "escapes double quotes correctly" do
article = Article.create(:languages => ["English", "Ger\"man"])
article.reload
article.languages_before_type_cast.should == "{English,\"Ger\\\"man\"}"
article.languages.should == ["English", "Ger\"man"]
end
it "handles commas correctly" do
article = Article.create(:languages => ["English", "Ger,man"])
article.reload
article.languages_before_type_cast.should == "{English,\"Ger,man\"}"
article.languages.should == ["English", "Ger,man"]
end
it "handles backslashes correctly" do
article = Article.create(:languages => ["\\","\""])
article.reload
article.languages_before_type_cast.should == '{"\\\\","\\""}'
article.languages.should == ["\\","\""]
end
it 'does not interfere with YAML serialization' do
article = Article.create!(
:languages => ['a','b'],
:author_ids => [1, 2, 3],
:prices => [1.1, 2.2, 3.3],
:serialized_column => {:a => 1, :b => 2}
)
article.reload
article.serialized_column.should == {:a => 1, :b => 2}
article.languages.should == ['a', 'b']
article.author_ids.should == [1, 2, 3]
article.prices.should == [1.1, 2.2, 3.3]
end
it 'does not interfere with hstore serialization' do
article = Article.create!(
:languages => ['a','b'],
:author_ids => [1, 2, 3],
:prices => [1.1, 2.2, 3.3],
:hstore_column => {:a => 1, :b => 2}
)
article.reload
article.hstore_column['a'].to_s.should == '1'
article.hstore_column['b'].to_s.should == '2'
article.languages.should == ['a', 'b']
article.author_ids.should == [1, 2, 3]
article.prices.should == [1.1, 2.2, 3.3]
end
end
describe ".update" do
before(:each) do
@article = Article.create
end
it "builds valid arrays" do
@article.languages = ["English", "German"]
@article.prices = [1.2, 1.3]
@article.save
@article.reload
@article.languages_before_type_cast.should == "{English,German}"
@article.prices_before_type_cast.should == "{1.2,1.3}"
@article.languages.should == ["English", "German"]
@article.prices.should == [1.2, 1.3]
end
it "escapes single quotes correctly" do
@article.languages = ["English", "Ger'man"]
@article.save
@article.reload
@article.languages_before_type_cast.should == "{English,Ger'man}"
@article.languages.should == ["English", "Ger'man"]
end
it "escapes double quotes correctly" do
@article.languages = ["English", "Ger\"man"]
@article.save
@article.reload
@article.languages_before_type_cast.should == "{English,\"Ger\\\"man\"}"
@article.languages.should == ["English", "Ger\"man"]
end
it "handles commas correctly" do
@article.languages = ["English", "Ger,man"]
@article.save
@article.reload
@article.languages_before_type_cast.should == "{English,\"Ger,man\"}"
@article.languages.should == ["English", "Ger,man"]
end
it "handles backslashes correctly" do
@article.languages = ["\\","\""]
@article.save
@article.reload
@article.languages_before_type_cast.should == '{"\\\\","\\""}'
@article.languages.should == ["\\","\""]
end
it 'is able to be contatenated with new arrays' do
article = Article.new
article.languages = (article.languages || []) + ['a', 'b', 'c']
article.save!
article.reload
article.languages.should == ['a', 'b', 'c']
article.languages += ['d']
article.save!
article.reload
article.languages.should == ['a', 'b', 'c', 'd']
end
end
end