test/yao/resources/test_restfully_accessible.rb in yao-0.15.0 vs test/yao/resources/test_restfully_accessible.rb in yao-0.16.0
- old
+ new
@@ -1,8 +1,20 @@
class TestRestfullyAccesible < Test::Unit::TestCase
include RestfullyAccessibleStub
class Test
+ def initialize(data_via_json)
+ @data = data_via_json
+ end
+
+ def id
+ @data['id']
+ end
+
+ def name
+ @data['name']
+ end
+
class << self
attr_accessor :client
end
extend Yao::Resources::RestfullyAccessible
end
@@ -43,11 +55,11 @@
name = "dummy"
uuid = "00112233-4455-6677-8899-aabbccddeeff"
stub_get_request_not_found([@url, @resources_name, name].join('/'))
stub_get_request([@url, "#{@resources_name}?name=dummy"].join('/'), @resources_name)
- mock(Test).new("dummy_resource") { Struct.new(:id).new(uuid) }
+ mock(Test).new("dummy_resource") { Struct.new(:id, :name).new(uuid, name) }
stub_get_request([@url, @resources_name, uuid].join('/'), @resources_name)
mock(Test).new("dummy_resource") { "OK" }
assert_equal("OK", Test.get(name))
end
@@ -58,11 +70,11 @@
uuid = "00112233-4455-6677-8899-aabbccddeeff"
body = {@resources_name => [@resources_name]}
stub_get_request_not_found([@url, @resources_name, name].join('/'))
stub_get_request_with_json_response([@url, "#{@resources_name}?name=dummy"].join('/'), body)
- mock(Test).new("dummy_resources") { Struct.new(:id).new(uuid) }
+ mock(Test).new("dummy_resources") { Struct.new(:id, :name).new(uuid, name) }
stub_get_request([@url, @resources_name, uuid].join('/'), @resource_name)
mock(Test).new("dummy_resource") { "OK" }
assert_equal("OK", Test.get(name))
end
@@ -82,12 +94,21 @@
end
end
sub_test_case 'get!' do
test 'not found' do
- stub_get_request_not_found("https://example.com/dummy_resource")
- assert_equal(nil, Test.get!("https://example.com/dummy_resource"))
+ Test.return_single_on_querying = false
+ name = "dummy"
+ uuid = "00112233-4455-6677-8899-aabbccddeeff"
+ body = {@resources_name => []}
+
+ stub1 = stub_get_request_not_found([@url, @resources_name, name].join('/'))
+ stub2 = stub_get_request_with_json_response([@url, "#{@resources_name}?name=#{name}"].join('/'), body)
+
+ assert_equal(nil, Test.get!(name))
+ assert_requested(stub1)
+ assert_requested(stub2)
end
test 'found' do
uuid = "00112233-4455-6677-8899-aabbccddeeff"
stub_get_request([@url, @resources_name, uuid].join('/'), @resource_name)
@@ -101,10 +122,27 @@
Test.get!("https://example.com/dummy_resource")
end
end
end
+ sub_test_case 'get_by_name' do
+ test 'multiple found' do
+ name = 'dummy'
+ uuid = '00112233-4455-6677-8899-aabbccddeeff'
+ list_body = { @resources_name => [
+ { 'name' => 'dummy', 'id' => uuid },
+ { 'name' => 'dummy2', 'id' => '308cb410-9c84-40ec-a3eb-583001aaa7fd' }
+ ]}
+ get_body = { @resource_name => { 'name' => 'dummy', 'id' => uuid } }
+ stub1 = stub_get_request_not_found([@url, @resources_name, name].join('/'))
+ stub2 = stub_get_request_with_json_response([@url, "#{@resources_name}?name=#{name}"].join('/'), list_body)
+ stub3 = stub_get_request_with_json_response([@url, @resources_name, name].join('/'), get_body)
+
+ assert_equal(uuid, Test.send(:get_by_name, 'dummy').body[@resource_name]['id'])
+ end
+ end
+
def test_find_by_name
mock(Test).list({"name" => "dummy"}) { "dummy" }
assert_equal("dummy", Test.find_by_name("dummy"))
end
@@ -113,7 +151,11 @@
assert_equal(true, Test.send(:uuid?, "00112233-4455-6677-8899-aabbccddeeff"))
# not uuid
assert_equal(false, Test.send(:uuid?, "dummy resource"))
assert_equal(false, Test.send(:uuid?, "00112233445566778899aabbccddeeff"))
+ end
+
+ def test_delete
+ assert_equal(Test.method(:delete), Test.method(:destroy))
end
end