Skip to content

Commit 71b30b2

Browse files
committed
Add unit test for lib
Added unit test for data_models, emitter, parser and sc_client
1 parent 2687fff commit 71b30b2

File tree

4 files changed

+494
-0
lines changed

4 files changed

+494
-0
lines changed

spec/lib/data_models_spec.rb

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe DataModels do
4+
include DataModels
5+
6+
describe 'data_models' do
7+
context 'get_ack_object' do
8+
let(:error) { 'error' }
9+
let(:data) { { key: 'value' } }
10+
let(:cid) { [*1..6].sample }
11+
12+
before(:each) do
13+
@ack_object = get_ack_object(error, data, cid)
14+
end
15+
16+
it 'should return an ack object of type hash' do
17+
expect(@ack_object.class).to eq(Hash)
18+
end
19+
20+
it 'should return an error, data and cid in ack object' do
21+
expect(@ack_object.keys.include?(:error)).to be(true)
22+
expect(@ack_object[:error]).to eq(error)
23+
expect(@ack_object.keys.include?(:data)).to be(true)
24+
expect(@ack_object[:data]).to eq(data)
25+
expect(@ack_object.keys.include?(:cid)).to be(true)
26+
expect(@ack_object[:cid]).to eq(cid)
27+
end
28+
end
29+
30+
context 'get_emit_object' do
31+
let(:event) { 'event' }
32+
let(:data) { { key: 'value' } }
33+
34+
before(:each) do
35+
@emit_object = get_emit_object(event, data)
36+
end
37+
38+
it 'should return an emit object of type hash' do
39+
expect(@emit_object.class).to eq(Hash)
40+
end
41+
42+
it 'should return an event and data in emit object' do
43+
expect(@emit_object.keys.include?(:event)).to be(true)
44+
expect(@emit_object[:event]).to eq(event)
45+
expect(@emit_object.keys.include?(:data)).to be(true)
46+
expect(@emit_object[:data]).to eq(data)
47+
end
48+
end
49+
50+
context 'get_emit_ack_object' do
51+
let(:event) { 'event' }
52+
let(:data) { { key: 'value' } }
53+
let(:cid) { [*1..6].sample }
54+
55+
before(:each) do
56+
@emit_ack_object = get_emit_ack_object(event, data, cid)
57+
end
58+
59+
it 'should return an emit ack object of type hash' do
60+
expect(@emit_ack_object.class).to eq(Hash)
61+
end
62+
63+
it 'should return an event, data and cid in emit ack object' do
64+
expect(@emit_ack_object.keys.include?(:event)).to be(true)
65+
expect(@emit_ack_object[:event]).to eq(event)
66+
expect(@emit_ack_object.keys.include?(:data)).to be(true)
67+
expect(@emit_ack_object[:data]).to eq(data)
68+
expect(@emit_ack_object.keys.include?(:cid)).to be(true)
69+
expect(@emit_ack_object[:cid]).to eq(cid)
70+
end
71+
end
72+
73+
context 'get_handshake_object' do
74+
let(:cid) { [*1..6].sample }
75+
76+
before(:each) do
77+
@handshake_object = get_handshake_object(cid)
78+
end
79+
80+
it 'should return a handshake object of type hash' do
81+
expect(@handshake_object.class).to eq(Hash)
82+
end
83+
84+
it 'should return an event, data and cid in handshake object' do
85+
expect(@handshake_object.keys.include?(:event)).to be(true)
86+
expect(@handshake_object[:event]).to eq('#handshake')
87+
expect(@handshake_object.keys.include?(:data)).to be(true)
88+
expect(@handshake_object[:data].keys.include?(:authToken)).to be(true)
89+
expect(@handshake_object.keys.include?(:cid)).to be(true)
90+
expect(@handshake_object[:cid]).to eq(cid)
91+
end
92+
end
93+
94+
context 'get_publish_object' do
95+
let(:channel) { 'channel' }
96+
let(:data) { { key: 'value' } }
97+
let(:cid) { [*1..6].sample }
98+
99+
before(:each) do
100+
@publish_object = get_publish_object(channel, data, cid)
101+
end
102+
103+
it 'should return a publish object of type hash' do
104+
expect(@publish_object.class).to eq(Hash)
105+
end
106+
107+
it 'should return an event, channel, data and cid in publish object' do
108+
expect(@publish_object.keys.include?(:event)).to be(true)
109+
expect(@publish_object[:event]).to eq('#publish')
110+
expect(@publish_object.keys.include?(:data)).to be(true)
111+
expect(@publish_object[:data].keys.include?(:channel)).to be(true)
112+
expect(@publish_object[:data].class).to eq(Hash)
113+
expect(@publish_object.keys.include?(:cid)).to be(true)
114+
expect(@publish_object[:cid]).to be(cid)
115+
end
116+
end
117+
118+
context 'get_subscribe_object' do
119+
let(:channel) { 'channel' }
120+
let(:cid) { [*1..6].sample }
121+
122+
before(:each) do
123+
@subscribe_object = get_subscribe_object(channel, cid)
124+
end
125+
126+
it 'should return a subscribe object of type hash' do
127+
expect(@subscribe_object.class).to eq(Hash)
128+
end
129+
130+
it 'should return an event, data, channel and cid in subscribe object' do
131+
expect(@subscribe_object.keys.include?(:event)).to be(true)
132+
expect(@subscribe_object[:event]).to eq('#subscribe')
133+
expect(@subscribe_object[:data].class).to eq(Hash)
134+
expect(@subscribe_object.keys.include?(:data)).to be(true)
135+
expect(@subscribe_object[:data].keys.include?(:channel)).to be(true)
136+
expect(@subscribe_object[:data][:channel]).to eq(channel)
137+
expect(@subscribe_object.keys.include?(:cid)).to be(true)
138+
expect(@subscribe_object[:cid]).to be(cid)
139+
end
140+
end
141+
142+
context 'get_unsubscribe_object' do
143+
let(:channel) { 'channel' }
144+
let(:cid) { [*1..6].sample }
145+
146+
before(:each) do
147+
@unsubscribe_object = get_unsubscribe_object(channel, cid)
148+
end
149+
150+
it 'should return an unsubscribe object of type hash' do
151+
expect(@unsubscribe_object.class).to eq(Hash)
152+
end
153+
154+
it 'should return an event, data and cid in unsubscribe object' do
155+
expect(@unsubscribe_object.keys.include?(:event)).to be(true)
156+
expect(@unsubscribe_object[:event]).to eq('#unsubscribe')
157+
expect(@unsubscribe_object.keys.include?(:data)).to be(true)
158+
expect(@unsubscribe_object[:data]).to eq(channel)
159+
expect(@unsubscribe_object.keys.include?(:cid)).to be(true)
160+
expect(@unsubscribe_object[:cid]).to be(cid)
161+
end
162+
end
163+
end
164+
end

spec/lib/emitter_spec.rb

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Emitter do
4+
include Emitter
5+
6+
describe 'emitter' do
7+
context 'initialize_emitter' do
8+
it 'should not have instance variables' do
9+
expect(@events).to eq(nil)
10+
expect(@events_ack).to eq(nil)
11+
end
12+
13+
it 'should initialize instance variables' do
14+
initialize_emitter
15+
16+
expect(@events).to_not eq(nil)
17+
expect(@events_ack).to_not eq(nil)
18+
expect(@events.class).to eq(Hash)
19+
expect(@events_ack.class).to eq(Hash)
20+
end
21+
end
22+
23+
context 'on' do
24+
before(:each) do
25+
initialize_emitter
26+
end
27+
28+
let(:key) { 'ping' }
29+
let(:message) { ->(key, object) { "#{key} #{object}" } }
30+
31+
it 'should not have an event with key' do
32+
expect(@events[key]).to eq(nil)
33+
end
34+
35+
it 'should have an event with key' do
36+
on(key, message)
37+
38+
expect(@events[key]).to be(message)
39+
end
40+
end
41+
42+
context 'onchannel' do
43+
before(:each) do
44+
initialize_emitter
45+
end
46+
47+
let(:key) { 'ping' }
48+
let(:message) { ->(key, object) { "#{key} #{object}" } }
49+
50+
it 'should not have an event with key' do
51+
expect(@events[key]).to eq(nil)
52+
end
53+
54+
it 'should have an event with key' do
55+
onchannel(key, message)
56+
57+
expect(@events[key]).to be(message)
58+
end
59+
end
60+
61+
context 'onack' do
62+
before(:each) do
63+
initialize_emitter
64+
end
65+
66+
let(:key) { 'ping' }
67+
let(:message) { ->(key, object, ack_block) { ack_block.call(key, object) } }
68+
let(:ack_message) { ->(key, object) { "#{key} #{object}" } }
69+
70+
it 'should not have an acknowledgement event with key' do
71+
expect(@events_ack[key]).to eq(nil)
72+
end
73+
74+
it 'should have an acknowledgement event with key' do
75+
onack(key, ack_message)
76+
77+
expect(@events_ack[key]).to be(ack_message)
78+
end
79+
end
80+
81+
context 'execute' do
82+
before(:each) do
83+
initialize_emitter
84+
end
85+
86+
let(:key) { 'ping' }
87+
let(:message) { ->(key, object) { "#{key} #{object}" } }
88+
89+
it 'should not have an event with key' do
90+
expect(@events[key]).to eq(nil)
91+
end
92+
93+
it 'should execute event' do
94+
on(key, message)
95+
96+
expect(execute(key, 'pong')).to eq('ping pong')
97+
end
98+
end
99+
100+
context 'haseventack' do
101+
before(:each) do
102+
initialize_emitter
103+
end
104+
105+
let(:key) { 'ping' }
106+
let(:message) { ->(key, object) { "#{key} #{object}" } }
107+
108+
it 'should not have an event with key' do
109+
expect(@events_ack[key]).to eq(nil)
110+
end
111+
112+
it 'should return an acknowledgment event from key' do
113+
onack(key, message)
114+
115+
expect(haseventack(key)).to eq(message)
116+
end
117+
end
118+
119+
context 'onack' do
120+
before(:each) do
121+
initialize_emitter
122+
end
123+
124+
let(:key) { 'ping' }
125+
let(:message) { ->(key, object, ack_block) { ack_block.call(key, object) } }
126+
let(:ack_message) { ->(key, object) { "#{key} #{object}" } }
127+
128+
it 'should not have an event with key' do
129+
expect(@events_ack[key]).to eq(nil)
130+
end
131+
132+
it 'should expect an acknowledgment event from key' do
133+
onack(key, message)
134+
135+
expect(executeack(key, 'pong', ack_message)).to eq('ping pong')
136+
end
137+
end
138+
end
139+
end

spec/lib/parser_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Parser do
4+
CHECK_AUTHENTICATION = 1
5+
PUBLISH = 2
6+
REMOVE_AUTHENTICATION = 3
7+
SET_AUTHENTICATION = 4
8+
EVENT = 5
9+
ACKNOWLEDGEMENT = 6
10+
11+
describe 'parser'
12+
describe 'parse'
13+
context 'check authentication' do
14+
let(:event) { '' }
15+
let(:response_id) { 1 }
16+
17+
it 'should check if client is authenticated' do
18+
result = Parser.parse(event, response_id)
19+
expect(result).to eq(CHECK_AUTHENTICATION)
20+
end
21+
end
22+
23+
context 'publish' do
24+
let(:event) { '#publish' }
25+
let(:response_id) { [*1..6].sample }
26+
27+
it 'should check if event is publish' do
28+
result = Parser.parse(event, response_id)
29+
expect(result).to eq(PUBLISH)
30+
end
31+
end
32+
33+
context 'remove authentication token' do
34+
let(:event) { '#removeAuthToken' }
35+
let(:response_id) { [*1..6].sample }
36+
37+
it 'should remove authentication token' do
38+
result = Parser.parse(event, response_id)
39+
expect(result).to eq(REMOVE_AUTHENTICATION)
40+
end
41+
end
42+
43+
context 'set authentication' do
44+
let(:event) { '#setAuthToken' }
45+
let(:response_id) { [*1..6].sample }
46+
47+
it 'should set authentication token' do
48+
result = Parser.parse(event, response_id)
49+
expect(result).to eq(SET_AUTHENTICATION)
50+
end
51+
end
52+
53+
context 'random event' do
54+
let(:event) { '#event' }
55+
let(:response_id) { [*1..6].sample }
56+
57+
it 'should check if event is publish' do
58+
result = Parser.parse(event, response_id)
59+
expect(result).to eq(EVENT)
60+
end
61+
end
62+
63+
context 'acknowledgment' do
64+
let(:event) { '' }
65+
let(:response_id) { 6 }
66+
67+
it 'should acknowledge the event' do
68+
result = Parser.parse(event, response_id)
69+
expect(result).to eq(ACKNOWLEDGEMENT)
70+
end
71+
end
72+
end

0 commit comments

Comments
 (0)