|
1 | 1 | require "test_helper"
|
2 | 2 |
|
3 | 3 | 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 |
7 | 49 | end
|
0 commit comments