Skip to content

Commit

Permalink
Testing clean minus error tests.'
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Sep 18, 2014
1 parent 638935b commit 1dcb941
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/bark/request/studies.rb
Expand Up @@ -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 = {
Expand Down
16 changes: 13 additions & 3 deletions lib/bark/response.rb
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions spec/lib/response_spec.rb
Expand Up @@ -10,15 +10,13 @@
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
}

specify 'a good request returns' do
expect(Bark::Response.new(request: request)).to be_truthy
end

specify 'a bad request raises' do
end

end

34 changes: 28 additions & 6 deletions spec/shared_tests.rb
Expand Up @@ -14,24 +14,46 @@ 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
when 'contains'
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
Expand Down
44 changes: 14 additions & 30 deletions 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
Expand All @@ -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":
Expand All @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit 1dcb941

Please sign in to comment.