From 50f6392bdd67926874620d47683bd982b85cf79b Mon Sep 17 00:00:00 2001 From: Nenad Vujicic Date: Mon, 11 Nov 2024 12:38:15 +0100 Subject: [PATCH] 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. --- test/factories/note_tags.rb | 8 ++++++ test/models/note_tag_test.rb | 48 +++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/factories/note_tags.rb diff --git a/test/factories/note_tags.rb b/test/factories/note_tags.rb new file mode 100644 index 0000000000..f7d90e002d --- /dev/null +++ b/test/factories/note_tags.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :note_tag do + sequence(:k) { |n| "Key #{n}" } + sequence(:v) { |n| "Value #{n}" } + + note + end +end diff --git a/test/models/note_tag_test.rb b/test/models/note_tag_test.rb index 35a3087b86..058d68eb87 100644 --- a/test/models/note_tag_test.rb +++ b/test/models/note_tag_test.rb @@ -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