forked from norman/friendly_id
/
history_test.rb
149 lines (127 loc) · 4.06 KB
/
history_test.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
require "helper"
class Manual < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :history
end
class HistoryTest < MiniTest::Unit::TestCase
include FriendlyId::Test
include FriendlyId::Test::Shared::Core
def model_class
Manual
end
test "should insert record in slugs table on create" do
with_instance_of(model_class) {|record| assert record.slugs.any?}
end
test "should not create new slug record if friendly_id is not changed" do
with_instance_of(model_class) do |record|
record.active = true
record.save!
assert_equal 1, FriendlyId::Slug.count
end
end
test "should create new slug record when friendly_id changes" do
with_instance_of(model_class) do |record|
record.name = record.name + "b"
record.save!
assert_equal 2, FriendlyId::Slug.count
end
end
test "should be findable by old slugs" do
with_instance_of(model_class) do |record|
old_friendly_id = record.friendly_id
record.name = record.name + "b"
record.save!
begin
assert model_class.find(old_friendly_id)
assert model_class.exists?(old_friendly_id), "should exist? by old id"
rescue ActiveRecord::RecordNotFound
flunk "Could not find record by old id"
end
end
end
test "should create slug records on each change" do
transaction do
record = model_class.create! :name => "hello"
assert_equal 1, FriendlyId::Slug.count
record = model_class.find("hello")
record.name = "hello again"
record.save!
assert_equal 2, FriendlyId::Slug.count
end
end
test "should not be read only when found by old slug" do
with_instance_of(model_class) do |record|
old_friendly_id = record.friendly_id
record.name = record.name + "b"
record.save!
assert !model_class.find(old_friendly_id).readonly?
end
end
test "should create correct sequence numbers even when some conflicted slugs have changed" do
transaction do
record1 = model_class.create! :name => 'hello'
record2 = model_class.create! :name => 'hello!'
record2.update_attributes :name => 'goodbye'
record3 = model_class.create! :name => 'hello!'
assert_equal 'hello--3', record3.slug
end
end
test "should raise error if used with scoped" do
model_class = Class.new(ActiveRecord::Base) do
self.abstract_class = true
extend FriendlyId
end
assert_raises RuntimeError do
model_class.friendly_id :name, :use => [:history, :scoped]
end
end
test "should handle renames" do
with_instance_of(model_class) do |record|
record.name = 'x'
assert record.save
record.name = 'y'
assert record.save
record.name = 'x'
assert record.save
end
end
test "should not create new slugs that match old slugs" do
transaction do
first_record = model_class.create! :name => "foo"
first_record.name = "bar"
first_record.save!
second_record = model_class.create! :name => "foo"
assert second_record.slug != "foo"
assert second_record.slug == "foo--2"
end
end
test 'should increment the sequence by one for each historic slug' do
transaction do
previous_record = model_class.create! :name => "foo"
first_record = model_class.create! :name => 'another'
second_record = model_class.create! :name => 'another'
assert second_record.slug == "another--2"
end
end
test 'should not fail when updating historic slugs' do
transaction do
first_record = model_class.create! :name => "foo"
second_record = model_class.create! :name => 'another'
second_record.update_attributes :name => 'foo'
assert second_record.slug == "foo--2"
first_record.update_attributes :name => 'another'
assert first_record.slug == "another--2"
end
end
end
class HistoryTestWithSti < HistoryTest
class Journalist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => [:slugged, :history]
end
class Editorialist < Journalist
end
def model_class
Editorialist
end
end