This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
/
activerecord-coders-hstore_spec.rb
123 lines (100 loc) · 3.31 KB
/
activerecord-coders-hstore_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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe ActiveRecord::Coders::Hstore do
describe "#load" do
subject{ ActiveRecord::Coders::Hstore.new.load(value) }
context 'when value is nil and we have a default in the constructor' do
subject{ ActiveRecord::Coders::Hstore.new({'a'=>'a'}).load(nil) }
it{ should eql({'a'=>'a'}) }
end
context 'when key and value have newline char' do
let(:value){ "\"foo\nbar\"=>\"\nnewline\"" }
it{ should eql({"foo\nbar" => "\nnewline"}) }
end
context 'when key and value are empty strings' do
let(:value){ %q(""=>"") }
it{ should eql({'' => ''}) }
end
context 'when value has single quotes' do
let(:value){ %q("'a'"=>"'a'") }
it{ should eql({"'a'" => "'a'"}) }
end
context 'when value is empty hash' do
let(:value){ '' }
it{ should eql({}) }
end
context 'when value is nil' do
let(:value){ nil }
it { should be_nil }
end
context 'when value is a hstore' do
let(:value){ "a=>a" }
it{ should eql({ 'a' => 'a' }) }
end
end
describe "#dump" do
subject{ ActiveRecord::Coders::Hstore.new.dump(value) }
context 'when value is nil and we have a default in the constructor' do
subject{ ActiveRecord::Coders::Hstore.new({'a'=>'a'}).dump(nil) }
it{ should eql('"a"=>"a"') }
end
context 'when key and value have dollar sign char' do
let(:value){ {"foo$bar" => "$ 5.00"} }
it{ should eql("\"foo$bar\"=>\"$ 5.00\"") }
end
context 'when key and value have newline char' do
let(:value){ {"foo\nbar" => "\nnewline"} }
it{ should eql("\"foo\nbar\"=>\"\nnewline\"") }
end
context 'when key and value are empty strings' do
let(:value){ {'' => ''} }
it{ should eql(%q(""=>"")) }
end
context 'when value has single quotes' do
let(:value){ {"'a'" => "'a'"} }
it{ should eql(%q("'a'"=>"'a'")) }
end
context 'when value is empty hash' do
let(:value){ {} }
it{ should eql('') }
end
context 'when value is nil' do
let(:value){ nil }
it{ should be_nil }
end
context "when value is an hstore" do
let(:value){ {'a' => 'a'} }
it{ should eql('"a"=>"a"') }
end
context 'when value has double quotes' do
let(:value){ {"a" => "\"a\""} }
it{ should eql(%q("a"=>"\"a\"")) }
end
# @seamusabshere not sure about this test
# context 'when value has double-escaped double quotes' do
# let(:value){ {"a" => "\\\"a\\\""} }
# it{ should eql(%q("a"=>"\"a\"")) }
# end
end
describe ".load" do
before do
@parameter = 'b=>b'
instance = double("coder instance")
instance.should_receive(:load).with(@parameter)
ActiveRecord::Coders::Hstore.should_receive(:new).and_return(instance)
end
it("should instantiate and call load") do
ActiveRecord::Coders::Hstore.load(@parameter)
end
end
describe ".dump" do
before do
@parameter = {'b' => 'b'}
instance = double("coder instance")
instance.should_receive(:dump).with(@parameter)
ActiveRecord::Coders::Hstore.should_receive(:new).and_return(instance)
end
it("should instantiate and call dump") do
ActiveRecord::Coders::Hstore.dump(@parameter)
end
end
end