-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added note_tag factory and NoteTag model test-case
Added registering new factory bot for note_tag and added new unit tests to NoteTagTest for checking if key length is valid, value length is valid, key length is invalid, value length is invalid, orphaned tag is invalid and note_tags are unique.
- Loading branch information
1 parent
2c49d77
commit fbcf7fc
Showing
2 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FactoryBot.define do | ||
factory :note_tag do | ||
sequence(:k) { |n| "Key #{n}" } | ||
sequence(:v) { |n| "Value #{n}" } | ||
|
||
note | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,49 @@ | ||
require "test_helper" | ||
|
||
class NoteTagTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
def test_length_key_valid | ||
tag = create(:note_tag) | ||
[0, 255].each do |i| | ||
tag.k = "k" * i | ||
assert_predicate tag, :valid? | ||
end | ||
end | ||
|
||
def test_length_value_valid | ||
tag = create(:note_tag) | ||
[0, 255].each do |i| | ||
tag.v = "v" * i | ||
assert_predicate tag, :valid? | ||
end | ||
end | ||
|
||
def test_length_key_invalid | ||
tag = create(:note_tag) | ||
tag.k = "k" * 256 | ||
assert_not_predicate tag, :valid?, "Key should be too long" | ||
assert_predicate tag.errors[:k], :any? | ||
end | ||
|
||
def test_length_value_invalid | ||
tag = create(:note_tag) | ||
tag.v = "v" * 256 | ||
assert_not_predicate tag, :valid?, "Value should be too long" | ||
assert_predicate tag.errors[:v], :any? | ||
end | ||
|
||
def test_orphaned_tag_invalid | ||
tag = create(:note_tag) | ||
tag.note = nil | ||
assert_not_predicate tag, :valid?, "Orphaned tag should be invalid" | ||
assert_predicate tag.errors[:note], :any? | ||
end | ||
|
||
def test_uniqueness | ||
existing = create(:note_tag) | ||
tag = build(:note_tag, :note => existing.note, :k => existing.k, :v => existing.v) | ||
assert_predicate tag, :new_record? | ||
assert_not_predicate tag, :valid? | ||
assert_raise(ActiveRecord::RecordInvalid) { tag.save! } | ||
assert_predicate tag, :new_record? | ||
end | ||
end |