# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
require File.dirname(__FILE__) + '/spec_helper.rb'
describe AMEE::Data::DrillDown do
before(:each) do
@drill = AMEE::Data::DrillDown.new :choice_name => 'choice'
end
it "should have common AMEE object properties" do
@drill.is_a?(AMEE::Data::Object).should be_true
end
it "should have choices" do
@drill.should respond_to(:choices)
end
it "should have a choice name" do
@drill.should respond_to(:choice_name)
end
it "should have selections" do
@drill.should respond_to(:selections)
end
it "provides access to data item uid" do
@drill.should respond_to(:data_item_uid)
end
it "should initialize AMEE::Object data on creation" do
uid = 'ABCD1234'
@drill = AMEE::Data::DrillDown.new(:uid => uid, :choice_name => 'choice')
@drill.uid.should == uid
end
it "can be created with hash of data" do
choices = ["one", "two"]
@drill = AMEE::Data::DrillDown.new(:choices => choices, :choice_name => 'choice')
@drill.choices.should == choices
end
end
describe AMEE::Data::DrillDown, "accessing AMEE V0" do
it "loads drilldown resource" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/transport/drill?transportType=Car1").and_return(flexmock(:body => fixture('v0_data_transport_transport_drill_transportType_Car1.xml')))
drill = AMEE::Data::DrillDown.get(connection, "/data/transport/transport/drill?transportType=Car1")
drill.choice_name.should == "transportStyle"
drill.choices.size.should be(1)
drill.choices[0].should == "-"
drill.selections.size.should be(1)
drill.selections['transportType'].should == 'Car1'
drill.data_item_uid.should be_nil
end
it "provides simple access to uid" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/transport/drill?transportType=Car1&transportStyle=-&transportSize=large&transportFuel=Diesel").and_return(flexmock(:body => 'transportTypeCar1transportStyle-transportSizelargetransportFuelDieseluidAC6DA76D96EEAC6DA76D96EE'))
drill = AMEE::Data::DrillDown.get(connection, "/data/transport/transport/drill?transportType=Car1&transportStyle=-&transportSize=large&transportFuel=Diesel")
drill.choice_name.should == "uid"
drill.choices.size.should be(1)
drill.selections.size.should be(4)
drill.data_item_uid.should == "AC6DA76D96EE"
end
end
describe AMEE::Data::DrillDown, "with an authenticated XML connection" do
it "loads drilldown resource" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel").and_return(flexmock(:body => fixture('data_transport_car_generic_drill_fuel_diesel.xml')))
drill = AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel")
drill.choice_name.should == "size"
drill.choices.size.should be(3)
drill.choices[0].should == "large"
drill.choices[1].should == "medium"
drill.choices[2].should == "small"
drill.selections.size.should be(1)
drill.selections['fuel'].should == 'diesel'
drill.data_item_uid.should be_nil
end
it "should fail gracefully with incorrect data" do
connection = flexmock "connection"
xml = ''
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel").and_return(flexmock(:body => xml))
lambda{AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel")}.should raise_error(AMEE::BadData)
end
it "provides simple access to uid" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel&size=large").and_return(flexmock(:body => 'Genericgenericfueldieselsizelargeuid4F6CBCEE95F74F6CBCEE95F7'))
drill = AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel&size=large")
drill.choice_name.should == "uid"
drill.choices.size.should be(1)
drill.selections.size.should be(2)
drill.data_item_uid.should == "4F6CBCEE95F7"
end
end
describe AMEE::Data::DrillDown, "with an authenticated JSON connection" do
it "loads drilldown resource" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel").and_return(flexmock(:body => '{"itemDefinition":{"uid":"123C4A18B5D6"},"dataCategory":{"modified":"2007-07-27 09:30:44.0","created":"2007-07-27 09:30:44.0","itemDefinition":{"uid":"123C4A18B5D6"},"dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"uid":"87E55DA88017","environment":{"uid":"5F5887BCF726"},"path":"generic","name":"Generic"},"selections":[{"value":"diesel","name":"fuel"}],"choices":{"choices":[{"value":"large","name":"large"},{"value":"medium","name":"medium"},{"value":"small","name":"small"}],"name":"size"}}'))
drill = AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel")
drill.choice_name.should == "size"
drill.choices.size.should be(3)
drill.choices[0].should == "large"
drill.choices[1].should == "medium"
drill.choices[2].should == "small"
drill.selections.size.should be(1)
drill.selections['fuel'].should == 'diesel'
drill.data_item_uid.should be_nil
end
it "should fail gracefully with incorrect data" do
connection = flexmock "connection"
json = '{}'
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel").and_return(flexmock(:body => json))
lambda{AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel")}.should raise_error(AMEE::BadData)
end
it "provides simple access to uid" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel&size=large").and_return(flexmock(:body => '{"itemDefinition":{"uid":"123C4A18B5D6"},"dataCategory":{"modified":"2007-07-27 09:30:44.0","created":"2007-07-27 09:30:44.0","itemDefinition":{"uid":"123C4A18B5D6"},"dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"uid":"87E55DA88017","environment":{"uid":"5F5887BCF726"},"path":"generic","name":"Generic"},"selections":[{"value":"diesel","name":"fuel"},{"value":"large","name":"size"}],"choices":{"choices":[{"value":"4F6CBCEE95F7","name":"4F6CBCEE95F7"}],"name":"uid"}}'))
drill = AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel&size=large")
drill.choice_name.should == "uid"
drill.choices.size.should be(1)
drill.selections.size.should be(2)
drill.data_item_uid.should == "4F6CBCEE95F7"
end
end
describe AMEE::Data::DrillDown, "with data" do
it "should fail gracefully on other GET errors" do
connection = flexmock "connection"
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel").and_raise("unidentified error")
lambda{AMEE::Data::DrillDown.get(connection, "/data/transport/car/generic/drill?fuel=diesel")}.should raise_error(AMEE::BadData)
end
it "enables drilling down through the levels" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data/transport/car/generic", {:itemsPerPage => 10}).and_return(flexmock(:body => '/transport/car/genericGenericgenericCarcarElectricelectricdiesel0.230.23large4F6CBCEE95F7diesel0.1630.163medium7E2B2426C927diesel0.1310.131small57E6AC080BF4petrol0.2570.349largeCEA465039777petrol0.1880.27medium9A9E8852220Bpetrol0.1590.224small66DB66447D2Fpetrol hybrid0.1950.195large69A44DCA9845petrol hybrid0.110.11medium7DC4C91CD8DA018811-1-11108'))
connection.should_receive(:get).with("/data/transport/car/generic/drill").and_return(flexmock(:body => 'Genericgenericfueldieseldieselpetrolpetrolpetrol hybridpetrol hybrid'))
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel").and_return(flexmock(:body => 'Genericgenericfueldieselsizelargelargemediummediumsmallsmall'))
connection.should_receive(:get).with("/data/transport/car/generic/drill?fuel=diesel&size=large").and_return(flexmock(:body => 'Genericgenericfueldieselsizelargeuid4F6CBCEE95F74F6CBCEE95F7'))
category = AMEE::Data::Category.get(connection, "/data/transport/car/generic")
drill = category.drill
drill.choice_name.should == "fuel"
drill = drill.choose "diesel"
drill.choice_name.should == "size"
drill = drill.choose "large"
drill.data_item_uid.should == "4F6CBCEE95F7"
end
end