Skip to content

Commit 58d214e

Browse files
committed
adds some specs
1 parent 5591dec commit 58d214e

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

lib/resque_bus/server.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def self.included(base)
2626
class Helpers
2727
class << self
2828
def parse_query(query_string)
29+
query_string = query_string.to_s.strip
2930
has_open_brace = query_string.include?("{")
3031
has_close_brace = query_string.include?("}")
3132
has_multiple_lines = query_string.include?("\n")

spec/server_helper_spec.rb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
require 'spec_helper'
2+
require_relative '../lib/resque_bus/server'
3+
4+
describe "Web Server Helper" do
5+
describe ".parse_query" do
6+
it "should pass through valid json" do
7+
input = %Q{
8+
{ "name": "here", "number": 1, "bool": true }
9+
}
10+
output = {"name"=>"here", "number"=>1, "bool"=>true}
11+
check = ::ResqueBus::Server::Helpers.parse_query(input)
12+
check.should == output
13+
end
14+
15+
it "should handle multi-line json" do
16+
input = %Q{
17+
{
18+
"name": "here",
19+
"number": 1,
20+
"bool": true
21+
}
22+
}
23+
output = {"name"=>"here", "number"=>1, "bool"=>true}
24+
check = ::ResqueBus::Server::Helpers.parse_query(input)
25+
check.should == output
26+
end
27+
28+
it "should interpret simple string as bus_event_type" do
29+
input = %Q{ user_created }
30+
output = {"bus_event_type" => "user_created", "more_here" => true}
31+
check = ::ResqueBus::Server::Helpers.parse_query(input)
32+
check.should == output
33+
end
34+
35+
it "should raise error on valid json that's not an Object" do
36+
input = '[{ "name": "here" }]'
37+
lambda {
38+
::ResqueBus::Server::Helpers.parse_query(input)
39+
}.should raise_error("Not a JSON Object")
40+
end
41+
42+
it "should allow array from resque server panel with encoded json string arg array" do
43+
input = '["{\"name\":\"here\"}"]'
44+
output = {"name" => "here"}
45+
check = ::ResqueBus::Server::Helpers.parse_query(input)
46+
check.should == output
47+
end
48+
49+
it "should take in a arg list and make it json" do
50+
input = %Q{
51+
bus_event_type: my_event
52+
user_updated: true
53+
}
54+
output = {"bus_event_type" => "my_event", "user_updated" => "true"}
55+
check = ::ResqueBus::Server::Helpers.parse_query(input)
56+
check.should == output
57+
end
58+
59+
it "should take in an arg list with quoted json and commas" do
60+
input = %Q{
61+
"bus_event_type": "my_event",
62+
"user_updated": true
63+
}
64+
output = {"bus_event_type" => "my_event", "user_updated" => true}
65+
check = ::ResqueBus::Server::Helpers.parse_query(input)
66+
check.should == output
67+
end
68+
69+
it "should parse logged output from event.inspect" do
70+
input = %Q{
71+
{"bus_published_at"=>1563793250, "bus_event_type"=>"user_created", :user_id=>42, :name=>"Brian" }
72+
}
73+
output = {
74+
"bus_published_at" => 1563793250,
75+
"bus_event_type" => "user_created",
76+
"user_id" => 42,
77+
"name" => "Brian"
78+
}
79+
check = ::ResqueBus::Server::Helpers.parse_query(input)
80+
check.should == output
81+
end
82+
83+
it "should throw json parse error when it can't be handled" do
84+
input = '{ "name": "here" q }'
85+
lambda {
86+
::ResqueBus::Server::Helpers.parse_query(input)
87+
}.should raise_error(/unexpected token/)
88+
end
89+
end
90+
91+
describe ".sort_query" do
92+
it "should alphabetize a query hash" do
93+
input = {"cat" => true, "apple" => true, "dog" => true, "bear" => true}
94+
output = {"apple" => true, "bear" => true, "cat" => true, "dog" => true }
95+
check = ::ResqueBus::Server::Helpers.sort_query(input)
96+
check.should == output
97+
end
98+
99+
it "should alphabetize a query sub-hashes but not arrays" do
100+
input = {"cat" => true, "apple" => [
101+
"jackal", "kangaroo", "iguana"
102+
], "dog" => {
103+
"frog" => 11, "elephant" => 12, "hare" => 16, "goat" => 14
104+
}, "bear" => true}
105+
output = {"apple" => [
106+
"jackal", "kangaroo", "iguana"
107+
], "bear" => true, "cat" => true, "dog" => {
108+
"elephant" => 12, "frog" => 11, "goat" => 14, "hare" => 16
109+
}}
110+
check = ::ResqueBus::Server::Helpers.sort_query(input)
111+
check.should == output
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)