forked from mbleigh/acts-as-taggable-on
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_spec.rb
182 lines (146 loc) · 5.62 KB
/
tag_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#encoding: utf-8
require File.expand_path('../../spec_helper', __FILE__)
describe ActsAsTaggableOn::Tag do
before(:each) do
clean_database!
@tag = ActsAsTaggableOn::Tag.new
@user = TaggableModel.create(:name => "Pablo")
end
describe "named like any" do
before(:each) do
ActsAsTaggableOn::Tag.create(:name => "Awesome")
ActsAsTaggableOn::Tag.create(:name => "awesome")
ActsAsTaggableOn::Tag.create(:name => "epic")
end
it "should find both tags" do
ActsAsTaggableOn::Tag.named_like_any(["awesome", "epic"]).should have(3).items
end
end
describe "find or create by name" do
before(:each) do
@tag.name = "awesome"
@tag.save
end
it "should find by name" do
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("awesome").should == @tag
end
it "should find by name case insensitive" do
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("AWESOME").should == @tag
end
it "should create by name" do
lambda {
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("epic")
}.should change(ActsAsTaggableOn::Tag, :count).by(1)
end
end
unless ActsAsTaggableOn::Tag.using_sqlite?
describe "find or create by unicode name" do
before(:each) do
@tag.name = "привет"
@tag.save
end
it "should find by name" do
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("привет").should == @tag
end
it "should find by name case insensitive" do
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("ПРИВЕТ").should == @tag
end
end
end
describe "find or create all by any name" do
before(:each) do
@tag.name = "awesome"
@tag.save
end
it "should find by name" do
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("awesome").should == [@tag]
end
it "should find by name case insensitive" do
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("AWESOME").should == [@tag]
end
it "should create by name" do
lambda {
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("epic")
}.should change(ActsAsTaggableOn::Tag, :count).by(1)
end
it "should find or create by name" do
lambda {
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("awesome", "epic").map(&:name).should == ["awesome", "epic"]
}.should change(ActsAsTaggableOn::Tag, :count).by(1)
end
it "should return an empty array if no tags are specified" do
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name([]).should == []
end
end
it "should require a name" do
@tag.valid?
@tag.errors[:name].should == ["can't be blank"]
@tag.name = "something"
@tag.valid?
@tag.errors[:name].should == []
end
it "should limit the name length to 255 or less characters" do
@tag.name = "fgkgnkkgjymkypbuozmwwghblmzpqfsgjasflblywhgkwndnkzeifalfcpeaeqychjuuowlacmuidnnrkprgpcpybarbkrmziqihcrxirlokhnzfvmtzixgvhlxzncyywficpraxfnjptxxhkqmvicbcdcynkjvziefqzyndxkjmsjlvyvbwraklbalykyxoliqdlreeykuphdtmzfdwpphmrqvwvqffojkqhlzvinqajsxbszyvrqqyzusxranr"
@tag.valid?
@tag.errors[:name].should == ["is too long (maximum is 255 characters)"]
@tag.name = "fgkgnkkgjymkypbuozmwwghblmzpqfsgjasflblywhgkwndnkzeifalfcpeaeqychjuuowlacmuidnnrkprgpcpybarbkrmziqihcrxirlokhnzfvmtzixgvhlxzncyywficpraxfnjptxxhkqmvicbcdcynkjvziefqzyndxkjmsjlvyvbwraklbalykyxoliqdlreeykuphdtmzfdwpphmrqvwvqffojkqhlzvinqajsxbszyvrqqyzusxran"
@tag.valid?
@tag.errors[:name].should == []
end
it "should equal a tag with the same name" do
@tag.name = "awesome"
new_tag = ActsAsTaggableOn::Tag.new(:name => "awesome")
new_tag.should == @tag
end
it "should return its name when to_s is called" do
@tag.name = "cool"
@tag.to_s.should == "cool"
end
it "have named_scope named(something)" do
@tag.name = "cool"
@tag.save!
ActsAsTaggableOn::Tag.named('cool').should include(@tag)
end
it "have named_scope named_like(something)" do
@tag.name = "cool"
@tag.save!
@another_tag = ActsAsTaggableOn::Tag.create!(:name => "coolip")
ActsAsTaggableOn::Tag.named_like('cool').should include(@tag, @another_tag)
end
describe "escape wildcard symbols in like requests" do
before(:each) do
@tag.name = "cool"
@tag.save
@another_tag = ActsAsTaggableOn::Tag.create!(:name => "coo%")
@another_tag2 = ActsAsTaggableOn::Tag.create!(:name => "coolish")
end
it "return escaped result when '%' char present in tag" do
ActsAsTaggableOn::Tag.named_like('coo%').should_not include(@tag)
ActsAsTaggableOn::Tag.named_like('coo%').should include(@another_tag)
end
end
describe "when using strict_case_match" do
before do
ActsAsTaggableOn.strict_case_match = true
end
after do
ActsAsTaggableOn.strict_case_match = false
end
it "should find by name" do
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("awesome").should == @tag
end
it "should find by name case sensitively" do
tag_count = ActsAsTaggableOn::Tag.count
tag = ActsAsTaggableOn::Tag.find_or_create_with_like_by_name("AWESOME")
ActsAsTaggableOn::Tag.count.should == tag_count + 1
tag.name.should == "AWESOME"
end
it "should have a named_scope named(something) that matches exactly" do
uppercase_tag = ActsAsTaggableOn::Tag.create(:name => "Cool")
@tag.name = "cool"
@tag.save!
ActsAsTaggableOn::Tag.named('cool').should include(@tag)
ActsAsTaggableOn::Tag.named('cool').should_not include(uppercase_tag)
end
end
end