Skip to content

Commit fbcf7fc

Browse files
committed
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.
1 parent 2c49d77 commit fbcf7fc

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

test/factories/note_tags.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :note_tag do
3+
sequence(:k) { |n| "Key #{n}" }
4+
sequence(:v) { |n| "Value #{n}" }
5+
6+
note
7+
end
8+
end

test/models/note_tag_test.rb

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
require "test_helper"
22

33
class NoteTagTest < ActiveSupport::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
def test_length_key_valid
5+
tag = create(:note_tag)
6+
[0, 255].each do |i|
7+
tag.k = "k" * i
8+
assert_predicate tag, :valid?
9+
end
10+
end
11+
12+
def test_length_value_valid
13+
tag = create(:note_tag)
14+
[0, 255].each do |i|
15+
tag.v = "v" * i
16+
assert_predicate tag, :valid?
17+
end
18+
end
19+
20+
def test_length_key_invalid
21+
tag = create(:note_tag)
22+
tag.k = "k" * 256
23+
assert_not_predicate tag, :valid?, "Key should be too long"
24+
assert_predicate tag.errors[:k], :any?
25+
end
26+
27+
def test_length_value_invalid
28+
tag = create(:note_tag)
29+
tag.v = "v" * 256
30+
assert_not_predicate tag, :valid?, "Value should be too long"
31+
assert_predicate tag.errors[:v], :any?
32+
end
33+
34+
def test_orphaned_tag_invalid
35+
tag = create(:note_tag)
36+
tag.note = nil
37+
assert_not_predicate tag, :valid?, "Orphaned tag should be invalid"
38+
assert_predicate tag.errors[:note], :any?
39+
end
40+
41+
def test_uniqueness
42+
existing = create(:note_tag)
43+
tag = build(:note_tag, :note => existing.note, :k => existing.k, :v => existing.v)
44+
assert_predicate tag, :new_record?
45+
assert_not_predicate tag, :valid?
46+
assert_raise(ActiveRecord::RecordInvalid) { tag.save! }
47+
assert_predicate tag, :new_record?
48+
end
749
end

0 commit comments

Comments
 (0)