sync_client 0.0.14 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveRecordClass < ActiveRecord::Base
4
+ include SyncClient::Publisher
5
+
6
+ self.table_name = :players
7
+ publish_changes_of :name, :to => :test
8
+ end
9
+
10
+ class ActiveRecordPublisherTest < ActiveSupport::TestCase
11
+ context "ActiveRecord::Publisher" do
12
+ context "ClassMethods" do
13
+ subject { ActiveRecordClass }
14
+
15
+ should "respond to queue_publisher with defined resource" do
16
+ assert_not_nil subject.queue_publisher.sync_queues
17
+ end
18
+
19
+ should "setup an after_create callback" do
20
+ assert subject._create_callbacks.any? {|cb| cb.filter == :publish_create and cb.kind == :after },
21
+ 'after_create :publish_create was not added'
22
+ end
23
+
24
+ should "setup a before_update callback" do
25
+ assert subject._update_callbacks.any? {|cb| cb.filter == :publish_update and cb.kind == :before },
26
+ 'after_create :publish_update was not added'
27
+ end
28
+
29
+ should "setup an after_destroy callback" do
30
+ assert subject._destroy_callbacks.any? {|cb| cb.filter == :publish_destroy and cb.kind == :after },
31
+ 'after_destroy :publish_destroy was not added'
32
+ end
33
+ end
34
+
35
+ context "InstanceMethods" do
36
+ subject { ActiveRecordClass.new }
37
+
38
+ setup do
39
+ @publisher = SyncClient::QueuePublisher.new
40
+ subject.stubs(:queue_publisher).returns(@publisher)
41
+ end
42
+
43
+ context "#publish_create" do
44
+ should 'send #publish to queue_publisher with :create' do
45
+ @publisher.expects(:publish).with(:create, subject, {}).returns(true)
46
+ subject.publish_create
47
+ end
48
+ end
49
+
50
+ context "#publish_update" do
51
+ should 'send #publish to queue_publisher with :update' do
52
+ @publisher.expects(:publish).with(:update, subject, {}).returns(true)
53
+ subject.publish_update
54
+ end
55
+ end
56
+
57
+ context "#publish_destroy" do
58
+ should 'send #publish to queue_publisher with :destroy' do
59
+ @publisher.expects(:publish).with(:destroy, subject, {}).returns(true)
60
+ subject.publish_destroy
61
+ end
62
+ end
63
+
64
+ context "#any_attributes_changed?" do
65
+ should 'return true if an attribute has changed' do
66
+ subject.name = 'Nick'
67
+ assert_equal true, subject.any_attributes_changed?([:name])
68
+ end
69
+
70
+ should 'return false if no attributes have changed' do
71
+ assert_equal false, subject.any_attributes_changed?([:name])
72
+ end
73
+ end
74
+
75
+ context "#publisher_attributes" do
76
+ should 'return the attributes hash' do
77
+ assert_equal subject.attributes, subject.publisher_attributes
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ class MongoidPublisherTest < ActiveSupport::TestCase
4
+
5
+ context "Mongoid::Publisher" do
6
+ context "ClassMethods" do
7
+ subject { Stat }
8
+
9
+ should "respond to queue_publisher with defined resource" do
10
+ assert_not_nil subject.queue_publisher.sync_queues
11
+ end
12
+
13
+ should "setup an after_create callback" do
14
+ assert subject._create_callbacks.any? {|cb| cb.filter == :publish_create and cb.kind == :after },
15
+ 'after_create :publish_create was not added'
16
+ end
17
+
18
+ should "setup a before_update callback" do
19
+ assert subject._update_callbacks.any? {|cb| cb.filter == :publish_update and cb.kind == :before },
20
+ 'after_create :publish_update was not added'
21
+ end
22
+
23
+ should "setup an after_destroy callback" do
24
+ assert subject._destroy_callbacks.any? {|cb| cb.filter == :publish_destroy and cb.kind == :after },
25
+ 'after_destroy :publish_destroy was not added'
26
+ end
27
+ end
28
+
29
+ context "InstanceMethods" do
30
+ subject { Stat.new }
31
+
32
+ setup do
33
+ @publisher = SyncClient::QueuePublisher.new
34
+ Stat.stubs(:queue_publisher).returns(@publisher)
35
+ end
36
+
37
+ context "#publish_create" do
38
+ should 'send #publish to queue_publisher with :create' do
39
+ @publisher.expects(:publish).with(:create, subject, {}).returns(true)
40
+ subject.publish_create
41
+ end
42
+ end
43
+
44
+ context "#publish_update" do
45
+ should 'send #publish to queue_publisher with :update' do
46
+ @publisher.expects(:publish).with(:update, subject, {}).returns(true)
47
+ subject.publish_update
48
+ end
49
+ end
50
+
51
+ context "#publish_destroy" do
52
+ should 'send #publish to queue_publisher with :destroy' do
53
+ @publisher.expects(:publish).with(:destroy, subject, {}).returns(true)
54
+ subject.publish_destroy
55
+ end
56
+ end
57
+
58
+ context "#any_attributes_changed?" do
59
+ should 'return true if an attribute has changed' do
60
+ subject.values = {:goals => 9001}
61
+ assert_equal true, subject.any_attributes_changed?([:values])
62
+ end
63
+
64
+ should 'return false if no attributes have changed' do
65
+ assert_equal false, subject.any_attributes_changed?([:values])
66
+ end
67
+ end
68
+
69
+ context "#publisher_attributes" do
70
+ should 'format the attributes correctly' do
71
+ subject.player_id = 42
72
+ subject.values = {:goals => 9001}
73
+
74
+ assert_equal({
75
+ 'id' => subject._id,
76
+ 'player_id' => subject.pid,
77
+ 'values' => subject.values,
78
+ }, subject.publisher_attributes)
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ class PoroPublisherTest < ActiveSupport::TestCase
4
+ context "Poro::Publisher" do
5
+ context "ClassMethods" do
6
+ subject { GamePublisher }
7
+
8
+ context "#publish_to" do
9
+ should "create a publisher if one doesn't exist" do
10
+ subject.publish_to :test
11
+ assert_not_nil subject.queue_publisher
12
+ end
13
+
14
+ context "with an existing publisher" do
15
+ setup do
16
+ @publisher = SyncClient::QueuePublisher.new
17
+ subject.instance_variable_set(:@queue_publisher, @publisher)
18
+ end
19
+
20
+ should 'add a publisher to the queue_publisher' do
21
+ @publisher.expects(:add_publisher).with([], {:to => :test, :for => :sync})
22
+ subject.publish_to :test
23
+ end
24
+
25
+ should 'add a publisher to the queue_publisher with custom :for' do
26
+ @publisher.expects(:add_publisher).with([], {:to => :test, :for => [:create, :sync]})
27
+ subject.publish_to :test, :for => [:create, :sync]
28
+ end
29
+
30
+ should 'add multiple publishers to the queue_publisher' do
31
+ @publisher.expects(:add_publisher).with([], {:to => :test1, :for => :sync})
32
+ @publisher.expects(:add_publisher).with([], {:to => :test2, :for => :sync})
33
+ subject.publish_to :test1
34
+ subject.publish_to :test2
35
+ end
36
+
37
+ should 'add multiple publishers in the same line' do
38
+ @publisher.expects(:add_publisher).with([], {:to => :test1, :for => :sync})
39
+ @publisher.expects(:add_publisher).with([], {:to => :test2, :for => :sync})
40
+ subject.publish_to :test1, :test2
41
+ end
42
+ end
43
+ end
44
+
45
+ should "respond to queue_publisher with defined resource" do
46
+ assert_not_nil subject.queue_publisher.sync_queues
47
+ end
48
+ end
49
+ end
50
+
51
+ context "InstanceMethods" do
52
+ subject { GamePublisher.new }
53
+
54
+ setup do
55
+ @publisher = SyncClient::QueuePublisher.new
56
+ GamePublisher.stubs(:queue_publisher).returns(@publisher)
57
+ end
58
+
59
+ context "#sync" do
60
+ should 'send #publish to queue_publisher with :sync' do
61
+ @publisher.expects(:publish).with(:sync, subject).returns(true)
62
+ subject.sync
63
+ end
64
+
65
+ should 'send #publish to queue_publisher with :create if action is passed in' do
66
+ @publisher.expects(:publish).with(:create, subject).returns(true)
67
+ subject.sync(:create)
68
+ end
69
+ end
70
+
71
+ context "publisher_attributes" do
72
+ should 'include all of the instance variables' do
73
+ subject.instance_variable_set(:@game_id, 123)
74
+ subject.score = "12-21"
75
+
76
+ assert_equal({
77
+ 'game_id' => 123,
78
+ 'score' => '12-21',
79
+ },subject.publisher_attributes)
80
+ end
81
+ end
82
+ end
83
+
84
+ end
@@ -22,22 +22,33 @@ class PublisherTest < ActiveSupport::TestCase
22
22
 
23
23
  should "publish on create" do
24
24
  @new_player.stubs(:queue_publisher).returns(@publisher)
25
- @publisher.expects(:publish).with(:create, @new_player).returns(true)
25
+ @publisher.expects(:publish).with(:create, @new_player, {}).returns(true)
26
26
  @new_player.save
27
27
  end
28
28
 
29
29
  should "publish on destoy" do
30
30
  @player.stubs(:queue_publisher).returns(@publisher)
31
- @publisher.expects(:publish).with(:destroy, @player).returns(true)
31
+ @publisher.expects(:publish).with(:destroy, @player, {}).returns(true)
32
32
  @player.destroy
33
33
  end
34
34
 
35
35
  should "publish on update" do
36
36
  @player.stubs(:queue_publisher).returns(@publisher)
37
- @publisher.expects(:publish).with(:update, @player).returns(true)
37
+ @publisher.expects(:publish).with(:update, @player, {}).returns(true)
38
38
  @player.save
39
39
  end
40
40
 
41
+ context "#any_attributes_changed" do
42
+ should "return false if no attributes changed" do
43
+ assert_equal false, @player.any_attributes_changed?([:id, :name])
44
+ end
45
+
46
+ should "return true if any attribute changed" do
47
+ @player.stubs(:name_changed? => true)
48
+ assert_equal true, @player.any_attributes_changed?([:id, :name])
49
+ end
50
+ end
51
+
41
52
  end
42
53
  end
43
54
  end
@@ -29,6 +29,15 @@ class QueuePublisherTest < ActiveSupport::TestCase
29
29
 
30
30
  @publisher.publish(:create, @player)
31
31
  end
32
+
33
+ should "queue message on publish if force=true" do
34
+ @sync_queue = @publisher.sync_queues.first
35
+ @sync_queue.stubs(:publishable?).returns(:false)
36
+ @publisher.expects(:queue_message).with(:create, @player, 'foo').returns(@message)
37
+ @message.expects(:publish).returns(true)
38
+
39
+ @publisher.publish(:create, @player, {force: true})
40
+ end
32
41
  end
33
42
  end
34
43
  end
@@ -2,6 +2,13 @@ require 'test_helper'
2
2
 
3
3
  class SyncQueueTest < ActiveSupport::TestCase
4
4
  context "SyncQueue" do
5
+ context "Initialize" do
6
+ should 'force callbacks into an array' do
7
+ sync_queue = SyncClient::SyncQueue.new([:name], :to => :test, :for => :create)
8
+ assert_equal [:create], sync_queue.callbacks
9
+ end
10
+ end
11
+
5
12
  context "Methods" do
6
13
  setup do
7
14
  @attributes = [:name]
data/test/test_helper.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  # Configure Rails Environment
2
2
  ENV["RAILS_ENV"] = "test"
3
3
 
4
+ require 'simplecov'
5
+ require 'simplecov-gem-adapter'
6
+
7
+ SimpleCov.start 'gem'
8
+
4
9
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
10
  require "rails/test_help"
6
11
  require 'test/unit'
7
12
  require 'shoulda'
8
- require 'simplecov'
9
13
  require 'sync_client'
10
- SimpleCov.start 'rails'
11
14
 
12
15
  Rails.backtrace_cleaner.remove_silencers!
13
16
 
@@ -36,4 +39,3 @@ class ActiveSupport::TestCase
36
39
  ::SyncClient.logger.stubs(:info).returns(:true)
37
40
  end
38
41
  end
39
-