-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_scopeable.rb
82 lines (60 loc) · 2.77 KB
/
test_scopeable.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
require 'helper'
class AlmostHappy::TestScopeable < Test::Unit::TestCase
with_tables :publishable do
should 'by default not be included' do
assert !(Publishable < AlmostHappy::Scopeable)
end
should 'be added by is_publishable' do
Publishable.is_publishable
assert Publishable < AlmostHappy::Scopeable
end
context 'with a publishable model' do
setup { Publishable.is_publishable }
should 'add a published relationship method' do
assert Publishable.respond_to?(:published)
end
should 'add a unpublished relationship method' do
assert Publishable.respond_to?(:unpublished)
end
should 'add a ordered relationship method' do
assert Publishable.respond_to?(:ordered)
end
should 'add a ordered_for_preview relationship method' do
assert Publishable.respond_to?(:ordered_for_preview)
end
should 'return a relationship from published' do
assert Publishable.published.is_a?(ActiveRecord::Relation)
end
should 'return a relationship from unpublished' do
assert Publishable.unpublished.is_a?(ActiveRecord::Relation)
end
should 'return a relationship from ordered' do
assert Publishable.ordered.is_a?(ActiveRecord::Relation)
end
should 'return a relationship from ordered_for_preview' do
assert Publishable.ordered_for_preview.is_a?(ActiveRecord::Relation)
end
context 'with published items do' do
setup do
@published_a = Publishable.create!(:published_at => 3.weeks.ago)
@published_b = Publishable.create!(:published_at => 1.day.ago)
@unpublished_a = Publishable.create!(:published_at => 3.weeks.from_now)
@unpublished_b = Publishable.create!(:published_at => 2.hours.from_now)
@unpublished_c = Publishable.create!(:published_at => nil)
end
should 'return the correct items for published' do
assert_same_elements [@published_a, @published_b], Publishable.published.all
end
should 'return the correct items for unpublished' do
assert_same_elements [@unpublished_a, @unpublished_b, @unpublished_c], Publishable.unpublished.all
end
should 'have the correct order for ordered' do
assert_equal [@unpublished_a, @unpublished_b, @published_b, @published_a, @unpublished_c], Publishable.ordered.all
end
should 'have the correct order for ordered_for_preview' do
assert_equal [@unpublished_c, @unpublished_a, @unpublished_b, @published_b, @published_a], Publishable.ordered_for_preview.all
end
end
end
end
end