forked from socrata/soda-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_soda.rb
More file actions
232 lines (184 loc) · 7.54 KB
/
test_soda.rb
File metadata and controls
232 lines (184 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'test/unit'
require 'shoulda'
require 'soda/client'
require 'json'
require 'webmock/test_unit'
# NOTE: These tests are by no means exhaustive, they're just a start
class SODATest < Test::Unit::TestCase
DOMAIN = "fakehost.socrata.com"
APP_TOKEN = "totallyfakenotrealapptoken"
USER = "fakeuser@socrata.com"
PASSWORD = "fakepassword"
# Helpers
def resource(name)
File.new(File.dirname(__FILE__) + "/resources/" + name)
end
context "query strings" do
setup do
@client = SODA::Client.new
end
should "return a proper simple query string" do
assert_equal "foo=bar", @client.send(:query_string, {:foo => "bar"})
end
should "return a proper multi-param query string" do
assert_equal "foo=bar&quo=quux", @client.send(:query_string, {
:foo => "bar",
:quo => "quux"
})
end
should "escape spaces" do
assert_equal "foo=bar+has+spaces+in+it", @client.send(:query_string, {
:foo => "bar has spaces in it"
})
end
should "escape random symbols" do
assert_equal "foo=%21%40%23%24%25%5E%26%2A%28%29_%2B", @client.send(:query_string, {
:foo => '!@#$%^&*()_+'
})
end
end
context "resource paths" do
setup do
@client = SODA::Client.new
end
should "handle a simple resource" do
assert_equal "/resource/644b-gaut.json", @client.send(:resource_path, "644b-gaut")
end
should "handle a custom resource" do
assert_equal "/resource/visitor-records.json", @client.send(:resource_path, "visitor-records")
end
should "allow you to override content type" do
assert_equal "/resource/visitor-records.csv", @client.send(:resource_path, "visitor-records.csv")
end
# NOTE: Will be deprecated later
should "allow you access an old-style SODA1 path" do
assert_equal "/api/views/644b-gaut.json", @client.send(:resource_path, "/api/views/644b-gaut")
end
# NOTE: Will be deprecated later
should "allow you access an old-style SODA1 path with output type" do
assert_equal "/api/views/644b-gaut/rows.csv", @client.send(:resource_path, "/api/views/644b-gaut/rows.csv")
end
end
# Test our response handling directly
context "response objects" do
setup do
@client = SODA::Client.new({:domain => DOMAIN, :app_token => APP_TOKEN})
end
should "handle a 200 with a real JSON payload" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json")
.to_return(resource("earthquakes_50.response"))
uri = URI.parse("https://fakehost.socrata.com/resource/earthquakes.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("X-App-Token", "FAKEAPPTOKEN")
body = @client.send(:handle_response, http.request(request))
assert_equal 50, body.size
end
should "raise on a 500 error" do
stub_request(:get, "https://fakehost.socrata.com/kaboom.json")
.to_return(resource("500_error.response"))
uri = URI.parse("https://fakehost.socrata.com/kaboom.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("X-App-Token", "FAKEAPPTOKEN")
assert_raise RuntimeError do
@client.send(:handle_response, http.request(request))
end
end
end
# Test responses from a real fake dataset
context "earthquakes" do
setup do
@client = SODA::Client.new({:domain => DOMAIN, :app_token => "K6rLY8NBK0Hgm8QQybFmwIUQw" })
end
should "be able to access the earthquakes dataset" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json?")
.to_return(resource("earthquakes_50.response"))
response = @client.get("earthquakes")
assert_equal 50, response.size
end
should "be able to perform a simple equality query" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json?source=uw")
.to_return(resource("earthquakes_uw.response"))
response = @client.get("earthquakes", {:source => "uw"})
assert_equal 26, response.size
end
should "be able to perform a simple $WHERE query" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json?$where=magnitude > 5")
.to_return(resource("earthquakes_where_gt_5.response"))
response = @client.get("earthquakes", {"$where" => "magnitude > 5"})
assert_equal 29, response.size
end
should "be able to combine a $WHERE query with a simple equality" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json?$where=magnitude > 4&source=pr")
.to_return(resource("earthquakes_source_pr_where_gt_4.response"))
response = @client.get("earthquakes", {"$where" => "magnitude > 4", :source => "pr"})
assert_equal 1, response.size
end
should "get the results we expect" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json?$where=magnitude > 4&source=pr")
.to_return(resource("earthquakes_source_pr_where_gt_4.response"))
response = @client.get("earthquakes", {"$where" => "magnitude > 4", :source => "pr"})
assert_equal 1, response.size
quake = response.first
assert_equal "Puerto Rico region", quake.region
assert_equal "4.2", quake.magnitude
assert_equal "17.00", quake.depth
assert quake.region?
end
end
context "authenticated" do
setup do
@client = SODA::Client.new({
:domain => DOMAIN, :app_token => APP_TOKEN,
:username => USER, :password => PASSWORD})
end
context "with user accounts" do
should "be able to read his own email address" do
stub_request(:get, "https://fakeuser%40socrata.com:fakepassword@fakehost.socrata.com/api/users/current.json?")
.to_return(resource("fakeuser.response"))
response = @client.get("/api/users/current.json")
assert_equal USER, response.email
end
end
context "RESTful updates" do
should "be able to POST a set of rows" do
stub_request(:post, "https://fakeuser%40socrata.com:fakepassword@fakehost.socrata.com/resource/earthquakes.json?")
.to_return(resource("earthquakes_create_one_row.response"))
response = @client.post("earthquakes", [ { :source => "uw", :magnitude => 5, :earthquake_id => 42 } ])
assert_equal response["Rows Created"], 1
end
end
end
context "errors" do
setup do
@client = SODA::Client.new({:domain => DOMAIN, :app_token => APP_TOKEN})
end
should "get an error accessing a nonexistent dataset" do
stub_request(:get, "https://fakehost.socrata.com/resource/idontexist.json?")
.to_return(resource("404.response"))
assert_raise RuntimeError do
@client.get("idontexist")
end
end
end
context "raw" do
setup do
@client = SODA::Client.new({:domain => DOMAIN, :app_token => APP_TOKEN})
end
should "be able to retrieve CSV if I so choose" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.csv?")
.to_return(resource("earthquakes.csv.response"))
response = @client.get("earthquakes.csv")
assert response.is_a? String
end
should "be able to retrieve CSV with a full path" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.csv?")
.to_return(resource("earthquakes.csv.response"))
response = @client.get("/resource/earthquakes.csv")
assert response.is_a? String
end
end
end