mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-27 12:28:48 +01:00
add some basic tests for tags noew we cannot rely on a tested gem
This commit is contained in:
parent
93494631f1
commit
1a6edbe7c2
5 changed files with 85 additions and 19 deletions
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
# The Tagging join model. This model is automatically generated and added to your app if you run the tagging generator included with has_many_polymorphs.
|
||||
# The Tagging join model.
|
||||
|
||||
class Tagging < ActiveRecord::Base
|
||||
|
||||
|
|
|
|||
46
test/unit/is_taggable_test.rb
Normal file
46
test/unit/is_taggable_test.rb
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
||||
|
||||
class IsTaggableTest < ActiveSupport::TestCase
|
||||
fixtures :todos, :recurring_todos
|
||||
|
||||
def test_models_are_taggable
|
||||
assert Todo.find(:first).respond_to?(:tag_list)
|
||||
assert RecurringTodo.find(:first).respond_to?(:tag_list)
|
||||
end
|
||||
|
||||
def test_test_to_s
|
||||
t = Todo.create(:description => "test", :context => Context.first)
|
||||
t.tag_list = "one, two, three"
|
||||
|
||||
# tags will be sorted alphabetical
|
||||
assert_equal "one, three, two", t.tag_list
|
||||
assert_equal "one, three, two", t.tags.to_s
|
||||
end
|
||||
|
||||
def test_getting_all_tags_except_starred
|
||||
t = Todo.create(:description => "test", :context => Context.first)
|
||||
t.tag_list = "one, two, three"
|
||||
t.toggle_star!
|
||||
|
||||
assert_equal "one, starred, three, two", t.tag_list
|
||||
assert_equal "one, three, two", t.tags.all_except_starred.map(&:name).sort.join(", ")
|
||||
end
|
||||
|
||||
def test_editing_tags
|
||||
t = Todo.create(:description => "test", :context => Context.first)
|
||||
t.tag_list = "a, b, c"
|
||||
|
||||
assert_equal 3, t.tags.count
|
||||
|
||||
t.tag_with "a, b"
|
||||
|
||||
assert_equal "a, b", t.tag_list, "should remove tag c"
|
||||
|
||||
t.tag_with "a, b, c, d"
|
||||
assert_equal "a, b, c, d", t.tag_list, "should add c and d"
|
||||
|
||||
t.tag_with "a, b, e, f"
|
||||
assert_equal "a, b, e, f", t.tag_list, "should add e and f and remove c and d"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -3,7 +3,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|||
class TagTest < ActiveSupport::TestCase
|
||||
fixtures :tags
|
||||
|
||||
# Replace this with your real tests.
|
||||
def test_find_or_create_with_single_word
|
||||
tag = Tag.find_or_create_by_name("test")
|
||||
assert !tag.new_record?
|
||||
|
|
@ -26,5 +25,21 @@ class TagTest < ActiveSupport::TestCase
|
|||
tag = Tag.find_or_create_by_name("8.1.2")
|
||||
assert !tag.new_record?
|
||||
end
|
||||
|
||||
|
||||
def test_tag_name_always_lowercase
|
||||
tag = Tag.find_or_create_by_name("UPPER")
|
||||
assert !tag.new_record?
|
||||
|
||||
upper = Tag.find_by_name("UPPER")
|
||||
assert_not_nil upper
|
||||
assert upper.name == "upper"
|
||||
end
|
||||
|
||||
def test_tag_name_stripped_of_spaces
|
||||
tag = Tag.find_or_create_by_name(" strip spaces ")
|
||||
assert !tag.new_record?
|
||||
|
||||
assert tag.name == "strip spaces"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,10 +1,16 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
||||
|
||||
class TaggingTest < ActiveSupport::TestCase
|
||||
fixtures :taggings
|
||||
fixtures :taggings, :tags
|
||||
|
||||
# Replace this with your real tests.
|
||||
def test_truth
|
||||
assert true
|
||||
def test_removes_unused_tags
|
||||
tag = Tag.create!(:name => "hello")
|
||||
tagging = Tagging.create!(:tag => tag, :taggable_id => 1)
|
||||
|
||||
assert_equal 1, Tagging.find(:all, :conditions => ["tag_id = ?", tag.id]).count
|
||||
|
||||
tagging.destroy
|
||||
|
||||
assert_nil Tag.find_by_name("hello")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -256,11 +256,11 @@ class TodoTest < ActiveSupport::TestCase
|
|||
tag_c = Tag.find_by_name("c")
|
||||
|
||||
todos_with_a = Todo.with_tag(tag_a)
|
||||
assert 1, todos_with_a.count
|
||||
assert_equal 1, todos_with_a.count
|
||||
assert_equal todo.description, todos_with_a.first.description
|
||||
|
||||
todos_with_b = Todo.with_tag(tag_b)
|
||||
assert 1, todos_with_b.count
|
||||
assert_equal 1, todos_with_b.count
|
||||
assert_equal todo.id, todos_with_b.first.id
|
||||
|
||||
todo2 = @not_completed2
|
||||
|
|
@ -270,10 +270,10 @@ class TodoTest < ActiveSupport::TestCase
|
|||
tag_d = Tag.find_by_name("d")
|
||||
|
||||
todos_with_a = Todo.with_tag(tag_a)
|
||||
assert 2, todos_with_a.count
|
||||
assert_equal 2, todos_with_a.count
|
||||
|
||||
todos_with_d = Todo.with_tag(tag_d)
|
||||
assert 1, todos_with_a.count
|
||||
assert_equal 1, todos_with_d.count
|
||||
end
|
||||
|
||||
def test_finding_todos_with_more_tags_using_OR
|
||||
|
|
@ -293,12 +293,12 @@ class TodoTest < ActiveSupport::TestCase
|
|||
# overlapping tags
|
||||
tag_ids = [tag_a.id, tag_c.id]
|
||||
todos_with_a_or_c = Todo.with_tags(tag_ids)
|
||||
assert 2, todos_with_a_or_c.count
|
||||
assert_equal 2, todos_with_a_or_c.count
|
||||
|
||||
# non-overlapping tags
|
||||
tag_ids = [tag_b.id, tag_d.id]
|
||||
todos_with_b_or_d = Todo.with_tags(tag_ids)
|
||||
assert 2, todos_with_b_or_d.count
|
||||
assert_equal 2, todos_with_b_or_d.count
|
||||
end
|
||||
|
||||
def test_finding_todos_with_more_tags_using_AND
|
||||
|
|
@ -314,8 +314,8 @@ class TodoTest < ActiveSupport::TestCase
|
|||
tag_b_id = Tag.find_by_name("b").id
|
||||
|
||||
todos_with_a_and_b = Todo.with_tags([tag_a_id]).with_tags([tag_b_id])
|
||||
assert 1, todos_with_a_and_b.count
|
||||
assert todo1.id, todos_with_a_and_b.first.id
|
||||
assert_equal 1, todos_with_a_and_b.count
|
||||
assert_equal todo1.id, todos_with_a_and_b.first.id
|
||||
end
|
||||
|
||||
def test_finding_todos_with_more_tags_using_AND_and_OR
|
||||
|
|
@ -332,14 +332,14 @@ class TodoTest < ActiveSupport::TestCase
|
|||
tag_c_id = Tag.find_by_name("c").id
|
||||
|
||||
todos_with_aORc_and_b = Todo.with_tags([tag_a_id, tag_c_id]).with_tags([tag_b_id])
|
||||
assert 1, todos_with_aORc_and_b.count
|
||||
assert todo1.id, todos_with_aORc_and_b.first.id
|
||||
assert_equal 1, todos_with_aORc_and_b.count
|
||||
assert_equal todo1.id, todos_with_aORc_and_b.first.id
|
||||
|
||||
# let todo2 fit the expression
|
||||
todo2.tag_list = "a, b, r"
|
||||
todo2.save!
|
||||
todos_with_aORc_and_b = Todo.with_tags([tag_a_id, tag_c_id]).with_tags([tag_b_id])
|
||||
assert 2, todos_with_aORc_and_b.count
|
||||
assert_equal 2, todos_with_aORc_and_b.count
|
||||
end
|
||||
|
||||
# test named_scopes
|
||||
|
|
@ -385,6 +385,5 @@ class TodoTest < ActiveSupport::TestCase
|
|||
assert !older_created_todos.include?(todo_now)
|
||||
assert !recent_created_todos.include?(todo_old)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue