From 88555f32cfae151f4d29c379b9a3ce25460711c6 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 Sep 2017 16:44:04 +0800 Subject: [PATCH] Improve specs. --- app/models/draft_sequence.rb | 5 ++--- spec/models/draft_sequence_spec.rb | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/models/draft_sequence.rb b/app/models/draft_sequence.rb index 2e8eeb98c82..460556d004d 100644 --- a/app/models/draft_sequence.rb +++ b/app/models/draft_sequence.rb @@ -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 diff --git a/spec/models/draft_sequence_spec.rb b/spec/models/draft_sequence_spec.rb index 8fe559a77d5..fa74a70400d 100644 --- a/spec/models/draft_sequence_spec.rb +++ b/spec/models/draft_sequence_spec.rb @@ -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