Skip to content

Commit

Permalink
Added note_tag factory and NoteTag model test-case
Browse files Browse the repository at this point in the history
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
nenad-vujicic committed Nov 14, 2024
1 parent 54e517b commit ea120b0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
8 changes: 8 additions & 0 deletions test/factories/note_tags.rb
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
48 changes: 45 additions & 3 deletions test/models/note_tag_test.rb
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

0 comments on commit ea120b0

Please sign in to comment.