From a7511f0ced12cebf793723ceae71ceb84572e669 Mon Sep 17 00:00:00 2001 From: Sea Date: Mon, 23 Sep 2019 18:56:39 +0400 Subject: [PATCH] Add tests for Node (#6263) --- app/models/node.rb | 21 ++++++++++++--------- test/unit/node_test.rb | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index fed667217b..ace4e71a5b 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -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 diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index 0db4a68450..50deae4b73 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -95,22 +95,25 @@ 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 @@ -118,11 +121,14 @@ def setup 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' @@ -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 @@ -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