Skip to content

Commit

Permalink
Add tests for Node (#6263)
Browse files Browse the repository at this point in the history
  • Loading branch information
seabl authored and jywarren committed Sep 23, 2019
1 parent 1cfd91a commit a7511f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
21 changes: 12 additions & 9 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,20 @@ def path(type = :default)
end

# should only be run at actual creation time --
# or, we should refactor to us node.created instead of Time.now
# or, we should refactor to use node.created instead of Time.now
def generate_path
if type == 'note'
username = User.find_by(id: uid).name
"/notes/#{username}/#{Time.now.strftime('%m-%d-%Y')}/#{title.parameterize}"
elsif type == 'page'
'/wiki/' + title.parameterize
elsif type == 'map'
"/map/#{title.parameterize}/#{Time.now.strftime('%m-%d-%Y')}"
elsif type == 'feature'
time = Time.now.strftime('%m-%d-%Y')

case type
when "note"
username = User.find_by(id: uid).name # name? or username?
"/notes/#{username}/#{time}/#{title.parameterize}"
when "map"
"/map/#{title.parameterize}/#{time}"
when "feature"
"/feature/#{title.parameterize}"
when "page"
"/wiki/#{title.parameterize}"
end
end

Expand Down
42 changes: 26 additions & 16 deletions test/unit/node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,40 @@ def setup
end
end

test 'create a node' do
# in testing, uid and id should be matched, although this is not yet true in production db
node = Node.new(uid: users(:bob).id,
type: 'note',
title: 'My new node for node creation testing')
assert node.save
end

test 'create a feature' do
node = Node.new(uid: users(:admin).id,
type: 'feature',
title: 'header-feature')
assert node.save!
username = users(:bob).username
assert_equal "/feature/#{node.title.parameterize}", node.path
assert_equal 'feature', node.type
assert_equal "/feature/#{node.title.parameterize}", node.path
end

test 'create a map' do
node = Node.new(uid: users(:bob).id,
type: 'map',
title: 'My map')
assert node.save!
assert_equal 'map', node.type
username = users(:bob).username
time = Time.now.strftime('%m-%d-%Y')
title = node.title.parameterize
assert_equal "/map/#{title}/#{time}", node.path
end

test 'create a research note' do
node = Node.new(uid: users(:bob).id,
type: 'note',
title: 'My research note')
assert node.save!
username = users(:bob).username
assert_equal "/notes/#{username}/#{Time.now.strftime('%m-%d-%Y')}/#{node.title.parameterize}", node.path
assert_equal "/questions/#{username}/#{Time.now.strftime('%m-%d-%Y')}/#{node.title.parameterize}", node.path(:question)
assert_equal 'note', node.type
end
username = users(:bob).username
time = Time.now.strftime('%m-%d-%Y')
title = node.title.parameterize
assert_equal "/notes/#{username}/#{time}/#{title}", node.path
assert_equal "/questions/#{username}/#{time}/#{title}",
node.path(:question)
end

test 'edit a research note and check path' do
original_title = 'My research note'
Expand All @@ -132,8 +138,11 @@ def setup
assert node.save!
node.title = 'I changed my mind'
username = users(:bob).username
assert_not_equal "/notes/#{username}/#{Time.now.strftime('%m-%d-%Y')}/#{node.title.parameterize}", node.path
assert_equal "/notes/#{username}/#{Time.now.strftime('%m-%d-%Y')}/#{original_title.parameterize}", node.path
time = Time.now.strftime('%m-%d-%Y')
title = node.title.parameterize
new_title = original_title.parameterize
assert_not_equal "/notes/#{username}/#{time}/#{title}", node.path
assert_equal "/notes/#{username}/#{time}/#{new_title}", node.path
end

# new_note also generates a revision
Expand Down Expand Up @@ -175,6 +184,7 @@ def setup
title: 'My wiki page')
assert node.save!
assert_equal 'page', node.type
assert_equal "/wiki/#{node.title.parameterize}", node.path
end

test 'create a wiki page with new as title' do
Expand Down

0 comments on commit a7511f0

Please sign in to comment.