Improve specs.

This commit is contained in:
Guo Xiang Tan 2017-09-15 16:44:04 +08:00
parent 9540b952fd
commit 88555f32cf
2 changed files with 15 additions and 9 deletions

View File

@ -10,7 +10,7 @@ class DraftSequence < ActiveRecord::Base
c ||= DraftSequence.new(h)
c.sequence ||= 0
c.sequence += 1
c.save
c.save!
exec_sql("DELETE FROM drafts WHERE user_id = :user_id AND draft_key = :draft_key AND sequence < :sequence", draft_key: key, user_id: user_id, sequence: c.sequence)
c.sequence
end
@ -23,8 +23,7 @@ class DraftSequence < ActiveRecord::Base
# perf critical path
r = exec_sql('select sequence from draft_sequences where user_id = ? and draft_key = ?', user_id, key).values
r.length.zero? ? 0 : r[0][0].to_i
r.length.zero? ? 0 : r[0][0]
end
end

View File

@ -1,14 +1,21 @@
require 'rails_helper'
describe DraftSequence do
let(:user) { Fabricate(:user) }
it 'should produce next sequence for a key' do
u = Fabricate(:user)
expect(DraftSequence.next!(u, 'test')).to eq 1
expect(DraftSequence.next!(u, 'test')).to eq 2
expect(DraftSequence.next!(user, 'test')).to eq 1
expect(DraftSequence.next!(user, 'test')).to eq 2
end
it 'should return 0 by default' do
u = Fabricate(:user)
expect(DraftSequence.current(u, 'test')).to eq 0
describe '.current' do
it 'should return 0 by default' do
expect(DraftSequence.current(user, 'test')).to eq 0
end
it 'should return the right sequence' do
expect(DraftSequence.next!(user, 'test')).to eq(1)
expect(DraftSequence.current(user, 'test')).to eq(1)
end
end
end