diff --git a/README.md b/README.md index cea37cc..a230365 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Documentation is presently being autogenerate at [RubyDoc.info][1] ## Licence -Bark is open source, the exact licence TBD shortly. +Bark is open source, it is available under the BSD licence. [1]: http://rubydoc.info/github/SpeciesFileGroup/bark/frames [2]: https://github.com/OpenTreeOfLife/hackathon diff --git a/lib/bark/request/studies.rb b/lib/bark/request/studies.rb index 8e23b29..fd479ee 100644 --- a/lib/bark/request/studies.rb +++ b/lib/bark/request/studies.rb @@ -9,8 +9,8 @@ class Bark::Request::Studies < Bark::Request studies_find_studies: %i{property value exact verbose}, studies_find_trees: %i{property value exact verbose}, studies_properties: %i{}, - get_study: %i{study_id}, - get_study_tree: %i{study_id tree_id}, + get_study: %i{study_id}, # GET, not post + get_study_tree: %i{study_id tree_id}, # GET, not post } REQUIRED_PARAMS = { diff --git a/lib/bark/response.rb b/lib/bark/response.rb index 8a82fa9..5dbe001 100644 --- a/lib/bark/response.rb +++ b/lib/bark/response.rb @@ -5,19 +5,29 @@ class Response attr_reader :json def initialize(request: {}) - raise 'No request passed' if request == {} or request.nil? + raise 'No request passed' if request == {} || request.nil? # warn Bark::Error, 'Warning request is not valid, making it anyway.' if !request.valid? @json = {} - req = Net::HTTP::Post.new(request.uri, initheader = {'Content-Type' =>'application/json'}) + if [:get_study, :get_study_tree ].include?( request.method ) + req = Net::HTTP::Get.new(request.uri) + else + req = Net::HTTP::Post.new(request.uri, initheader = {'Content-Type' =>'application/json'}) + end + res = Net::HTTP.start(request.uri.hostname, request.uri.port) do |http| req.body = request.json_payload http.request(req) end + parse_json(res.body) + end + + # Parse the json, and store it in @json. + def parse_json(string) begin - @json = JSON.parse(res.body) + @json = JSON.parse(string) rescue JSON::ParserError => e puts e.message ap request diff --git a/spec/lib/response_spec.rb b/spec/lib/response_spec.rb index 5a40c78..0d155d1 100644 --- a/spec/lib/response_spec.rb +++ b/spec/lib/response_spec.rb @@ -10,6 +10,7 @@ allow(r).to receive(:uri).and_return( uri ) allow(r).to receive(:valid?).and_return(true) allow(r).to receive(:json_payload).and_return({}.to_json) + allow(r).to receive(:method).and_return(:studies_properties) r } @@ -17,8 +18,5 @@ expect(Bark::Response.new(request: request)).to be_truthy end - specify 'a bad request raises' do - end - end diff --git a/spec/shared_tests.rb b/spec/shared_tests.rb index 8bdb402..889861f 100644 --- a/spec/shared_tests.rb +++ b/spec/shared_tests.rb @@ -14,10 +14,10 @@ def run_tests(request_class, tests) end when 'equals' t.tests[k].each_with_index do |subtest, i| - key = t.tests[k][0] - value = t.tests[k][1] - msg = t.tests[k][2] - specify "#{i}- #{k} #{key}" do + key = subtest[0][0] + value = subtest[0][1] + msg = subtest[1] + specify "#{k} #{key}: #{value} (#{i})" do expect(response.json[key]).to eq(value), msg end end @@ -25,13 +25,35 @@ def run_tests(request_class, tests) t.tests[k].each_with_index do |subtest, i| key = subtest[0] msg = subtest[1] - specify "#{i}- #{k} #{key}" do + specify "#{k} #{key} (#{i})" do expect(response.json[key]).to be_truthy end end + when 'length_greater_than' + t.tests[k].each_with_index do |subtest, i| + key = subtest[0][0] + len = subtest[0][1].to_i + msg = subtest[1] + specify "#{k} #{key}: #{len} (#{i})" do + expect(response.json[key].length > len ).to be(true), msg + end + end + when 'deep_equals' + t.tests[k].each_with_index do |subtest, i| + sub_response = response.json + # could eval a string sensu python too + subtest[0][0].each do |k| + sub_response = sub_response[k] + end + value = subtest[0][1] + msg = subtest[1] + specify "#{k} #{subtest[0][0]}: #{value} (#{i})" do + expect(sub_response).to eq(value), msg + end + end else - pending "Test engine for #{k} not yet finished." + pending "Test engine for [ #{k} ] not yet finished." end end end diff --git a/spec/shared_tests_helper.rb b/spec/shared_tests_helper.rb index 195e000..7a602ce 100644 --- a/spec/shared_tests_helper.rb +++ b/spec/shared_tests_helper.rb @@ -1,29 +1,18 @@ -# This module faciliates the use of shared tests used in R, Python, and here. -# Those tests are provided in json format here https://github.com/OpenTreeOfLife/opentree-interfaces/tree/master/python/test -# -# { -# "test_mrca_normal_input": { -# "test_method": "tree_of_life_mrca", -# "test_input": [412129, 536234], -# "test_output": { -# "of_type": "dict", -# "passes": "'nearest_taxon_mrca_rank' == 'superorder'", -# "contains": "nearest_taxon_mrca_ott_id"} -# } -# } +# This module faciliates the use of shared tests used in R, Python, and herein. # +# See https://github.com/OpenTreeOfLife/shared-api-tests # module SharedTestsHelper - + # Instances are used to translate string-keyed hashes to symbolized keys. class SymbolizableHash < Hash include Hashie::Extensions::SymbolizeKeys end - # The location of the .json files - TEST_SOURCE_BASE = 'https://raw.githubusercontent.com/OpenTreeOfLife/opentree-interfaces/master/python/test/' + # The location/ of the .json files + TEST_SOURCE_BASE = ' https://raw.githubusercontent.com/OpenTreeOfLife/shared-api-tests/master/' - # Bade filenames for each test containg .json file + # Base filenames to test with. TEST_FILES = %w{ graph_of_life studies @@ -32,22 +21,18 @@ class SymbolizableHash < Hash tree_of_life } - shared_tests = {} - - # Each test is instantiated as an OtTest. - # This is used internally only in the testing framework. + # Each set of tests is instantiated as an OtTest. class OtTest - # The name of the test, displayed in the specification attr_accessor :name - # The API wrapper method name to to call + # The API wrapper method name to to call. attr_accessor :method # A Hash of input values to be delivered in the JSON payload attr_accessor :input - # A Hash of specifications, by keyword, as provided in 'tests' + # A Hash of specifications, by keyword, i.e. the value of "tests" # # "tests": { # "of_type": @@ -63,14 +48,17 @@ class OtTest # attr_accessor :tests + # Translates specified types to Ruby types RESULT_TYPE_CLASSES = { 'dict' => Hash } - + + # Getter translation of the method attribute. def method @method.to_sym end + # Translate the json result type provided in the json specification to a native Ruby value. def result_type if t = @tests['of_type'] RESULT_TYPE_CLASSES[t.first] @@ -121,11 +109,7 @@ def self.build_test(name, attributes) puts "\n\n!! Warning, unable to build shared tests, some JSON doesn't parse." end - # An Array of OtTest objects + # The Array of OtTest objects SHARED_TESTS = shared_tests - def self.of_type_test(response, ot_test_instance, message) - expect(response.class).to eq(ot_test_instance.result_type, message) - end - end