acts_as_filesystem_test.rb
4.09 KB
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
require_relative "../test_helper"
class ActsAsFilesystemTest < ActiveSupport::TestCase
  # FIXME shouldn't we test with a non-real model, instead of Article?
  should 'provide a hierarchy list' do
    profile = create_user('testinguser').person
    a1 = profile.articles.create(:name => 'a1')
    a2 = profile.articles.create(:name => 'a2', :parent => a1)
    a3 = profile.articles.create(:name => 'a3', :parent => a2)
    assert_equal [a1, a2, a3], a3.hierarchy
  end
  should 'set ancestry' do
    c1 = create(Category, :name => 'c1')
    c2 = create(Category, :name => 'c2', :parent => c1)
    c3 = create(Category, :name => 'c3', :parent => c2)
    assert_not_nil c1.ancestry
    assert_not_nil c2.ancestry
    assert_equal "%010d.%010d" % [c1.id, c2.id], c3.ancestry
    assert_equal [c1, c2, c3], c3.hierarchy
  end
  should 'provide the level' do
    c1 = create(Category, :name => 'c1')
    c2 = create(Category, :name => 'c2', :parent => c1)
    c3 = create(Category, :name => 'c3', :parent => c2)
    assert_equal 0, c1.level
    assert_equal 1, c2.level
    assert_equal 2, c3.level
  end
  should 'be able to optionally reload the hierarchy' do
    a = Article.new
    list = a.hierarchy
    assert_same list, a.hierarchy
    assert_not_same list, a.hierarchy(true)
  end
  should 'list the full tree' do
    profile = create_user('testinguser').person
    a1 = profile.articles.build(:name => 'a1'); a1.save!
    a1_1 = a1.children.create!(:name => 'a1.1', :profile => profile)
    a1_2 = a1.children.create!(:name => 'a1.2', :profile => profile)
    a1_1_1 = a1_1.children.create!(:name => 'a1.1.1', :profile => profile)
    a1_1_2 = a1_1.children.create!(:name => 'a1.1.2', :profile => profile)
    a1.reload
    assert_equivalent [a1, a1_1, a1_2, a1_1_1, a1_1_2], a1.map_traversal
  end
  should 'list the full tree without the root' do
    profile = create_user('testinguser').person
    a1 = profile.articles.build(:name => 'a1'); a1.save!
    a1_1 = a1.children.create!(:name => 'a1.1', :profile => profile)
    a1_2 = a1.children.create!(:name => 'a1.2', :profile => profile)
    a1_1_1 = a1_1.children.create!(:name => 'a1.1.1', :profile => profile)
    a1_1_2 = a1_1.children.create!(:name => 'a1.1.2', :profile => profile)
    a1.reload
    assert_equivalent [a1_1, a1_2, a1_1_1, a1_1_2].map(&:id), a1.all_children.map(&:id)
  end
  should 'be able to traverse with a block' do
    profile = create_user('testinguser').person
    a1 = profile.articles.build(:name => 'a1'); a1.save!
    a1_1 = a1.children.create!(:name => 'a1.1', :profile => profile)
    a1_2 = a1.children.create!(:name => 'a1.2', :profile => profile)
    a1_1_1 = a1_1.children.create!(:name => 'a1.1.1', :profile => profile)
    a1_1_2 = a1_1.children.create!(:name => 'a1.1.2', :profile => profile)
    a1.reload
    assert_equivalent ['a1', 'a1.1', 'a1.2', 'a1.1.1', 'a1.1.2'], a1.map_traversal { |item| item.name }
  end
  should 'be able to list text articles that are children of a folder' do
    profile = create_user('testinguser').person
    folder = fast_create(Folder, :name => 'folder', :profile_id => profile.id)
    article1 = Article.create!(:name => 'article 1', :profile => profile, :parent => folder)
    article2 = Article.create!(:name => 'article 2', :profile => profile, :parent => folder)
    folder.reload
    items = folder.map_traversal
    assert_includes items, folder
    assert_includes items, article1
    assert_includes items, article2
  end
  should 'allow dots in slug' do
    assert_equal 'test.txt', Article.new(:name => 'test.txt').slug
  end
  should 'provide name without leading parents' do
    a = Article.new
    a.expects(:full_name).with('/').returns('a/b/c/d').times(3)
    assert_equal 'b/c/d', a.full_name_without_leading(1)
    assert_equal 'c/d', a.full_name_without_leading(2)
    assert_equal 'd', a.full_name_without_leading(3)
  end
  should 'cache children count' do
    profile = create_user('testinguser').person
    a1 = profile.articles.create!(:name => 'a1')
    a11 = profile.articles.create!(:name => 'a11', :parent => a1)
    a12 = profile.articles.create!(:name => 'a12', :parent => a1)
  end
end