Replace couchbase counters with drb version

This commit is contained in:
2014-08-05 17:49:16 +02:00
parent c0c25673bf
commit 99a9536c68
20 changed files with 400 additions and 282 deletions
+129
View File
@@ -0,0 +1,129 @@
require 'spec_helper'
describe List do
let(:supplier) { create :supplier }
let(:supplier_password){'secret1'}
let(:supplier) { create :supplier, email: 'supplier@qwaiter.com', password: supplier_password, confirmation_token: 'abc', confirmed_at: Time.now.utc, open: true }
let(:user) { create :user }
let(:section) { create :section, supplier: supplier}
let(:table) { create :table, supplier: supplier}
let(:list_options){ {supplier: supplier, table: table, section: section, user_ids: [user.id]} }
let(:list){ create :list, list_options}
describe 'join requests' do
describe '#send_table_join_request_for_user' do
it "does not add an existing user to join_request_user_ids" do
list.send_table_join_request_for_user! user
expect(list.join_request_user_ids).not_to include user.id
end
it "adds a user to join_request_user_ids" do
other_user = create :user
list.send_table_join_request_for_user! other_user
# test through persistance
list.reload
expect(list.join_request_user_ids).to eq [other_user.id]
end
it "does not add a user multiple times" do
other_user = create :user
2.times { list.send_table_join_request_for_user! other_user }
expect(list.join_request_user_ids).to eq [other_user.id]
end
it "broadcasts it to the user" do
other_user = create :user
expect{
list.send_table_join_request_for_user! other_user
}.to broadcast_to_user(user.id).message('user_join_request').with(
hash_including(:users, :join_request)
)
end
end
describe "#approve_join_request_for_user" do
let(:joining_user){ create :user }
before do
list_options[:join_request_user_ids] = [joining_user.id]
end
it "does not raise error when no user is associated" do
unlinked_user = create :user
expect{ list.approve_join_request_for_user!(unlinked_user) }.not_to raise_error
end
it "removes the user as wanting to join" do
list.approve_join_request_for_user! joining_user
list.join_request_user_ids.should_not include joining_user.id
# persistance check
list.reload
list.join_request_user_ids.should_not include joining_user.id
end
it "sets the active list of the user to the specific list" do
list.approve_join_request_for_user! joining_user
joining_user.active_list_id.should == list.id
# persistance check
joining_user.reload
joining_user.active_list_id.should == list.id
end
it "broadcasts the event to the user itself" do
joining_user
expect{ list.approve_join_request_for_user! joining_user }
.to broadcast_to_user(joining_user).message('join_request_approved')
.with( hash_including(:user) )
end
it "broadcasts the event to other associated users" do
expect{ list.approve_join_request_for_user! joining_user }
.to broadcast_to_user(user).message('join_request_approved')
.with( hash_including(:user) )
end
end
describe "#reject_join_request_for_user" do
let(:joining_user){ create :user }
before do
list_options[:join_request_user_ids] = [joining_user.id]
end
it "does not raise error when no user is associated" do
unlinked_user = create :user
expect{ list.reject_join_request_for_user!(unlinked_user) }.not_to raise_error
end
it "removes the user as wanting to join" do
list.reject_join_request_for_user! joining_user
list.join_request_user_ids.should_not include joining_user.id
# persistance check
list.reload
list.join_request_user_ids.should_not include joining_user.id
end
it "does not set the active list of the user to the specific list" do
list.reject_join_request_for_user! joining_user
joining_user.active_list_id.should_not be_present
end
it "broadcasts the event to the user itself" do
joining_user
expect{ list.reject_join_request_for_user! joining_user }
.to broadcast_to_user(joining_user).message('join_request_rejected')
.with( id: "jr-#{joining_user.id}" )
end
it "broadcasts the event to other associated users" do
expect{ list.reject_join_request_for_user! joining_user }
.to broadcast_to_user(user).message('join_request_rejected')
.with( id: "jr-#{joining_user.id}" )
end
end
end
end
+2 -40
View File
@@ -8,19 +8,12 @@ describe List do
let(:user) { create :user }
let(:section) { create :section, supplier: supplier}
let(:table) { create :table, supplier: supplier}
let(:list){ create :list, supplier: supplier, table: table, section: section, user_ids: [user.id] }
let(:list_options){ {supplier: supplier, table: table, section: section, user_ids: [user.id]} }
let(:list){ create :list, list_options}
let(:product){ create :product, price: 2.22, supplier: supplier }
let(:order) { create :order, user: user, list: list, supplier: supplier, section: section }
let(:product_order ){ create :product_order, order: order, product: product, quantity: 3, price: 2.11 }
subject { list }
describe :as_json do
it 'should include _id in as_json serialization' do
list.as_json.keys.map(&:to_sym).should include :_id
end
it 'should include table_number in as_json serialization' do
list.as_json.keys.should include :table_number
end
end
describe :mark_as_paid do
it "should set paid_at to a time" do
@@ -163,35 +156,4 @@ describe List do
describe 'product order creation'
end
describe 'join requests' do
describe '#send_table_join_request_for_user' do
it "does not add an existing user to join_request_user_ids" do
list.send_table_join_request_for_user! user
expect(list.join_request_user_ids).not_to include user.id
end
it "adds a user to join_request_user_ids" do
other_user = create :user
list.send_table_join_request_for_user! other_user
# test through persistance
list.reload
expect(list.join_request_user_ids).to eq [other_user.id]
end
it "does not add a user multiple times" do
other_user = create :user
2.times { list.send_table_join_request_for_user! other_user }
expect(list.join_request_user_ids).to eq [other_user.id]
end
it "broadcasts it to the user" do
other_user = create :user
expect{
list.send_table_join_request_for_user! other_user
}.to broadcast_to_user(user.id).message('user_join_request').with(
hash_including(:users, :join_request)
)
end
end
end
end
+12 -12
View File
@@ -16,18 +16,18 @@ describe Supplier do
supplier2.orders_placed_count.should == 3
end
it 'cleans counter values if orders are no longer available' do
old_connection = Qwaiter::Counter.connection
# this spec should run on the couchbase database
Qwaiter::Counter.connection = $cb
supplier = create :supplier
Qwaiter::Counter.set "supplier_counter:#{supplier.id}:orders_placed", 9
supplier.orders_placed_count.should == 9
Supplier.reset_counters!
sleep 1
supplier.orders_placed_count.should == 0
Qwaiter::Counter.connection = old_connection
end
# it 'cleans counter values if orders are no longer available', broken: defined?($cb) do
# old_connection = Qwaiter::Counter.connection
# # this spec should run on the couchbase database
# Qwaiter::Counter.connection = $cb
# supplier = create :supplier
# Qwaiter::Counter.set "supplier_counter:#{supplier.id}:orders_placed", 9
# supplier.orders_placed_count.should == 9
# Supplier.reset_counters!
# sleep 1
# supplier.orders_placed_count.should == 0
# Qwaiter::Counter.connection = old_connection
# end
end
describe '#reset_counters!' do