Created
January 12, 2012 15:08
-
-
Save peterhellberg/1601022 to your computer and use it in GitHub Desktop.
External JSON API testing (using minitest/spec, rest-client and yajl-ruby)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
guard 'shell' do | |
watch(/relation_tree_spec\.rb/) { `clear && ruby relation_tree_spec.rb` } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'minitest/spec' | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
require 'rest_client' | |
require 'yajl' | |
describe 'Related products' do | |
before do | |
@base_url = 'http://metadata.ur.se/related_products.json' | |
end | |
it "should return an error if no valid ids were provided" do | |
get_data(100000)['status'].must_equal 400 | |
end | |
it "should only return relations for external products" do | |
get_data([100000, 100001]).keys.must_equal ["100001"] | |
end | |
it "should not return internal products" do | |
data = get_data([100000, 100001]) | |
data.keys.wont_include "100000" | |
data['100001']['packagedvd'][0].must_equal 135136 | |
end | |
it "should have the correct packagedvd" do | |
product_id = 159270 | |
data = get_type_data('packagedvd', [product_id]) | |
data[product_id][0].must_include 159929 | |
end | |
it "should return parts if packageseries_parts parameter is set to true" do | |
data = get_type_data('packageseries_parts', [159270], true) | |
data.keys.must_equal [159270] | |
end | |
it "should have the correct relationships for 163960" do | |
p_id = 163960 | |
s_id = 163955 | |
data = get_data(p_id, true) | |
data[p_id.to_s]["packageseries"][0].must_equal 163955 | |
data[p_id.to_s]["packageseries_parts"]["#{s_id}"].size.must_equal 13 | |
end | |
it "should have the correct relationships for 165085" do | |
product_id = 165085 | |
packageseries_id = 153408 | |
packagedvd_id = 165953 | |
textworksheet_id = 166337 | |
data = get_data(product_id) | |
data["#{product_id}"]["packagedvd"].must_equal [packagedvd_id] | |
data["#{product_id}"]["textworksheet"].must_equal [textworksheet_id] | |
data["#{product_id}"]["packageseries"].must_equal [packageseries_id] | |
end | |
end | |
############################################################################### | |
# Helper methods | |
############################################################################### | |
def get_data(ids, with_parts = false) | |
id_string = ids.respond_to?(:join) ? ids.join(',') : ids | |
query_string = (with_parts) ? '&packageseries_parts=true' : '' | |
url = "#{@base_url}?ur_product_ids=#{id_string}#{query_string}" | |
Yajl::Parser.parse(RestClient.get(url)) | |
end | |
def type_data(product_type, data, id) | |
data["#{id}"][product_type] | |
end | |
def get_type_data(product_type, ids, with_parts = false) | |
data = get_data(ids, with_parts) | |
response_data = {} | |
ids.each do |id| | |
response_data[id] ||= [] | |
response_data[id] << type_data(product_type, data, id) | |
end | |
response_data | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment