2014-11-20 11:29:32 +09:00
|
|
|
require "test_helper"
|
2010-04-20 23:05:11 +00:00
|
|
|
|
|
|
|
class NoteTest < ActiveSupport::TestCase
|
|
|
|
fixtures :users, :posts
|
|
|
|
|
|
|
|
def create_note(params)
|
2024-01-08 19:39:01 +09:00
|
|
|
Note.create({ post_id: 1, user_id: 1, x: 0, y: 0, width: 100, height: 100, is_active: true, ip_addr: "127.0.0.1" }.merge(params))
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_api
|
2024-01-08 19:39:01 +09:00
|
|
|
note = create_note(body: "hello")
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_nothing_raised do
|
|
|
|
note.to_json
|
|
|
|
end
|
|
|
|
assert_nothing_raised do
|
|
|
|
note.to_xml
|
|
|
|
end
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_versioning
|
2024-01-08 19:39:01 +09:00
|
|
|
note = create_note(body: "hello")
|
2010-04-20 23:05:11 +00:00
|
|
|
|
2024-01-08 19:39:01 +09:00
|
|
|
note_v1 = NoteVersion.find_by(note_id: note.id, version: 1)
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_not_nil(note_v1)
|
|
|
|
assert_equal("hello", note_v1.body)
|
|
|
|
|
2024-01-08 19:39:01 +09:00
|
|
|
note.update(body: "hello v2")
|
|
|
|
note_v2 = NoteVersion.find_by(note_id: note.id, version: 2)
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_not_nil(note_v2)
|
|
|
|
assert_equal("hello v2", note_v2.body)
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
note.revert_to!(1)
|
|
|
|
assert_equal(1, note.version)
|
|
|
|
assert_equal("hello", note.body)
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_locking
|
2024-01-08 19:39:01 +09:00
|
|
|
Post.update(1, is_note_locked: true)
|
|
|
|
note = create_note(body: "hello")
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_equal(true, note.errors.any?)
|
2024-01-08 19:39:01 +09:00
|
|
|
assert_equal(0, Note.where(post_id: 1).count)
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2024-01-08 19:39:01 +09:00
|
|
|
Post.update(1, is_note_locked: false)
|
|
|
|
note = create_note(body: "hello")
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_equal(false, note.errors.any?)
|
2024-01-08 19:39:01 +09:00
|
|
|
assert_equal(1, Note.where(post_id: 1).count)
|
2010-04-20 23:05:11 +00:00
|
|
|
|
2024-01-08 19:39:01 +09:00
|
|
|
Post.update(1, is_note_locked: true)
|
|
|
|
note.update(body: "hello v2")
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_equal(true, note.errors.any?)
|
2024-01-08 19:39:01 +09:00
|
|
|
assert_equal(1, Note.where(post_id: 1).count)
|
|
|
|
assert_equal("hello", Note.find_by(post_id: 1).body)
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_last_noted_at
|
|
|
|
assert_nil(Post.find(1).last_noted_at)
|
2024-01-08 19:39:01 +09:00
|
|
|
note_a = create_note(body: "hello")
|
2014-11-20 23:47:58 +09:00
|
|
|
# FIXME: .to_i currently used to take care of rounding of time (float).
|
|
|
|
assert_equal(note_a.updated_at.to_i, Post.find(1).last_noted_at.to_i)
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2014-11-20 23:47:58 +09:00
|
|
|
sleep 2
|
2024-01-08 19:39:01 +09:00
|
|
|
note_b = create_note(body: "hello 2")
|
2014-11-20 23:47:58 +09:00
|
|
|
assert_equal(note_b.updated_at.to_i, Post.find(1).last_noted_at.to_i)
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
end
|