# 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::Category do
before(:each) do
@cat = AMEE::Data::Category.new
end
it "should have common AMEE object properties" do
@cat.is_a?(AMEE::Data::Object).should be_true
end
it "should have children" do
@cat.should respond_to(:children)
end
it "should have items" do
@cat.should respond_to(:items)
end
it "should initialize AMEE::Object data on creation" do
uid = 'ABCD1234'
@cat = AMEE::Data::Category.new(:uid => uid)
@cat.uid.should == uid
end
it "can be created with hash of data" do
children = ["one", "two"]
items = ["three", "four"]
@cat = AMEE::Data::Category.new(:children => children, :items => items)
@cat.children.should == children
@cat.items.should == items
end
end
describe AMEE::Data::Category, "accessing AMEE V0" do
it "should provide access to root of Data API" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => 'RootMetadatametadataTransporttransportHomehome'))
@root = AMEE::Data::Category.root(connection)
@root.name.should == "Root"
@root.path.should == ""
@root.full_path.should == "/data"
@root.uid.should == "63EA08D29C63"
@root.created.should == DateTime.new(2007,4,16,17,50,43)
@root.modified.should == DateTime.new(2007,4,16,17,50,43)
@root.pager.should be_nil
@root.children.size.should be(3)
@root.children[0][:uid].should == "5376F191D80E"
@root.children[0][:name].should == "Metadata"
@root.children[0][:path].should == "metadata"
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data/home/fuel", {:itemsPerPage => 10}).and_return(flexmock(:body => 'Fuelfuel002.5BiodieselAMEE 200700.13160Biomassdefra 200802.5060Coaldefra 2008002.6287Dieseldefra 20080.53700Electricitydefra 20080.205500Gasdefra 2008002.516Kerosenedefra 2008001.495LPGdefra 2008002.518Oildefra 2008002.3167Petroldefra 2008'))
@data = AMEE::Data::Category.get(connection, "/data/home/fuel")
@data.uid.should == "8B5A3EF67252"
@data.items.size.should be(10)
@data.items[0][:uid].should == "9DC114F06AB2"
@data.items[0][:label].should == "Biodiesel"
@data.items[0][:path].should == "9DC114F06AB2"
@data.items[0][:fuelKgCO2PerLitre].should == "2.5"
@data.pager.should be_nil
end
end
describe AMEE::Data::Category, "with an authenticated XML connection" do
it "should provide access to root of Data API" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.xml')))
@root = AMEE::Data::Category.root(connection)
@root.name.should == "Root"
@root.path.should == ""
@root.full_path.should == "/data"
@root.uid.should == "CD310BEBAC52"
@root.created.should == DateTime.new(2007,7,27,9,30,44)
@root.modified.should == DateTime.new(2007,7,27,9,30,44)
@root.children.size.should be(5)
@root.children[0][:uid].should == "BBA3AC3E795E"
@root.children[0][:name].should == "Home"
@root.children[0][:path].should == "home"
@root.pager.should be_nil
end
it "should provide access to child objects" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.xml')))
connection.should_receive(:get).with("/data/transport", {:itemsPerPage => 10}).and_return(flexmock(:body => '/transportTransporttransportRootBusbusCarcarMotorcyclemotorcycleOtherOtherPlaneplaneTaxitaxiTraintrain'))
@root = AMEE::Data::Category.root(connection)
@transport = @root.child('transport')
@transport.path.should == "/transport"
@transport.itemdef.should == nil
@transport.full_path.should == "/data/transport"
@transport.uid.should == "263FC0186834"
@transport.children.size.should be(7)
@transport.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data/transport/plane/generic", {:itemsPerPage => 10}).and_return(flexmock(:body => '/transport/plane/genericGenericgenericPlaneplane0domestic-AD63A83B4D410.15873domesticone wayFFC7A05D54AD0146domesticreturnF5498AD6FC7500long haul-7D4220DF72F90.105801long haulone way46117F6C0B7E01602long haulreturn96D538B1B24600short haul-9DA419052FDF0.13170short haulone way84B4A14C74240340short haulreturn8DA1BEAA10130019911-1-11109'))
@data = AMEE::Data::Category.get(connection, "/data/transport/plane/generic")
@data.uid.should == "FBA97B70DBDF"
@data.itemdef.should == "441BF4BEA15B"
@data.items.size.should be(9)
@data.items[0][:uid].should == "AD63A83B4D41"
@data.items[0][:label].should == "domestic"
@data.items[0][:path].should == "AD63A83B4D41"
@data.items[0][:source].should == "DfT INAS Division, 29 March 2007"
@data.pager.should_not be_nil
end
it "should be able to get the itemdef" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data/transport/plane/generic", {:itemsPerPage => 10}).and_return(flexmock(:body => '/transport/plane/genericGenericgenericPlaneplane0domestic-AD63A83B4D410.15873domesticone wayFFC7A05D54AD0146domesticreturnF5498AD6FC7500long haul-7D4220DF72F90.105801long haulone way46117F6C0B7E01602long haulreturn96D538B1B24600short haul-9DA419052FDF0.13170short haulone way84B4A14C74240340short haulreturn8DA1BEAA10130019911-1-11109'))
connection.should_receive(:v3_get).with("/3.3/definitions/441BF4BEA15B;full",{}).and_return(fixture('itemdef_441BF4BEA15B.xml')).once
@data = AMEE::Data::Category.get(connection, "/data/transport/plane/generic")
@data.uid.should == "FBA97B70DBDF"
@data.itemdef.should == "441BF4BEA15B"
@itemdef=@data.item_definition
@itemdef.uid.should =="441BF4BEA15B"
end
it "should fail gracefully with bad XML" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.xml').first(12)))
connection.should_receive(:expire).with("/data").once
lambda{AMEE::Data::Category.get(connection, "/data")}.should raise_error(REXML::ParseException)
end
it "should retry if bad XML is received first time" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(2)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.xml').first(12))).twice
connection.should_receive(:expire).with("/data").twice
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.xml'))).once
lambda{AMEE::Data::Category.get(connection, "/data")}.should_not raise_error
end
it "should fail gracefully with bad data in XML" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('empty.xml')))
connection.should_receive(:expire).with("/data").once
lambda{AMEE::Data::Category.get(connection, "/data")}.should raise_error(AMEE::BadData)
end
it "provides access to drilldown resource" 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'))
lambda{
cat = AMEE::Data::Category.get(connection, "/data/transport/car/generic")
cat.drill
}.should_not raise_error
end
end
describe AMEE::Data::Category, "with an authenticated JSON connection" do
it "should provide access to root of Data API" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.json')))
@root = AMEE::Data::Category.root(connection)
@root.name.should == "Root"
@root.path.should == ""
@root.full_path.should == "/data"
@root.uid.should == "CD310BEBAC52"
@root.created.should == DateTime.new(2007,7,27,9,30,44)
@root.modified.should == DateTime.new(2007,7,27,9,30,44)
@root.children.size.should be(5)
@root.children[0][:uid].should == "BBA3AC3E795E"
@root.children[0][:name].should == "Home"
@root.children[0][:path].should == "home"
@root.pager.should be_nil
end
it "should provide access to child objects" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.json')))
connection.should_receive(:get).with("/data/transport", {:itemsPerPage => 10}).and_return(flexmock(:body => '{"dataCategory":{"modified":"2007-07-27 09:30:44.0","created":"2007-07-27 09:30:44.0","dataCategory":{"uid":"CD310BEBAC52","path":"","name":"Root"},"uid":"263FC0186834","environment":{"uid":"5F5887BCF726"},"path":"transport","name":"Transport"},"path":"/transport","children":{"pager":{},"dataCategories":[{"uid":"3C4705614170","path":"bus","name":"Bus"},{"uid":"1D95119FB149","path":"car","name":"Car"},{"uid":"83C4FAF4826A","path":"motorcycle","name":"Motorcycle"},{"uid":"AFB73A5D2E45","path":"Other","name":"Other"},{"uid":"6F3692D81CD9","path":"plane","name":"Plane"},{"uid":"06DE08988C53","path":"taxi","name":"Taxi"},{"uid":"B1A64213FA9D","path":"train","name":"Train"}],"dataItems":{}}}'))
@root = AMEE::Data::Category.root(connection)
@transport = @root.child('transport')
@transport.path.should == "/transport"
@transport.itemdef.should == nil
@transport.full_path.should == "/data/transport"
@transport.uid.should == "263FC0186834"
@transport.children.size.should be(7)
@transport.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data/transport/plane/generic", {:itemsPerPage => 10}).and_return(flexmock(:body => '{"dataCategory":{"modified":"2007-08-01 09:00:23.0","created":"2007-08-01 09:00:23.0","itemDefinition":{"uid":"441BF4BEA15B"},"dataCategory":{"uid":"6F3692D81CD9","path":"plane","name":"Plane"},"uid":"FBA97B70DBDF","environment":{"uid":"5F5887BCF726"},"path":"generic","name":"Generic"},"path":"/transport/plane/generic","children":{"pager":{"to":9,"lastPage":1,"start":0,"nextPage":-1,"items":9,"itemsPerPage":10,"from":1,"previousPage":-1,"requestedPage":1,"currentPage":1,"itemsFound":9},"dataCategories":[],"dataItems":{"rows":[{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"domestic","label":"domestic","uid":"AD63A83B4D41","path":"AD63A83B4D41","size":"-","kgCO2PerPassengerJourney":"0","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0.158"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"domestic","label":"domestic, one way","uid":"FFC7A05D54AD","path":"FFC7A05D54AD","size":"one way","kgCO2PerPassengerJourney":"73","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"domestic","label":"domestic, return","uid":"F5498AD6FC75","path":"F5498AD6FC75","size":"return","kgCO2PerPassengerJourney":"146","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"long haul","label":"long haul","uid":"7D4220DF72F9","path":"7D4220DF72F9","size":"-","kgCO2PerPassengerJourney":"0","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0.105"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"long haul","label":"long haul, one way","uid":"46117F6C0B7E","path":"46117F6C0B7E","size":"one way","kgCO2PerPassengerJourney":"801","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"long haul","label":"long haul, return","uid":"96D538B1B246","path":"96D538B1B246","size":"return","kgCO2PerPassengerJourney":"1602","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"short haul","label":"short haul","uid":"9DA419052FDF","path":"9DA419052FDF","size":"-","kgCO2PerPassengerJourney":"0","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0.13"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"short haul","label":"short haul, one way","uid":"84B4A14C7424","path":"84B4A14C7424","size":"one way","kgCO2PerPassengerJourney":"170","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0"},{"modified":"2007-08-01 09:00:41.0","created":"2007-08-01 09:00:41.0","type":"short haul","label":"short haul, return","uid":"8DA1BEAA1013","path":"8DA1BEAA1013","size":"return","kgCO2PerPassengerJourney":"340","source":"DfT INAS Division, 29 March 2007","kgCO2PerPassengerKm":"0"}],"label":"DataItems"}}}'))
@data = AMEE::Data::Category.get(connection, "/data/transport/plane/generic")
@data.uid.should == "FBA97B70DBDF"
@data.itemdef.should == "441BF4BEA15B"
@data.items.size.should be(9)
@data.items[0][:uid].should == "AD63A83B4D41"
@data.items[0][:label].should == "domestic"
@data.items[0][:path].should == "AD63A83B4D41"
@data.items[0][:source].should == "DfT INAS Division, 29 March 2007"
@data.pager.should_not be_nil
end
it "should fail gracefully with bad JSON" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => "{"))
connection.should_receive(:expire).with("/data").once
lambda{AMEE::Data::Category.get(connection, "/data")}.should raise_error(JSON::ParserError)
end
it "should retry if bad JSON is received first time" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(2)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => "{")).twice
connection.should_receive(:expire).with("/data").twice
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('data.json'))).once
lambda{AMEE::Data::Category.get(connection, "/data")}.should_not raise_error
end
it "should fail gracefully with bad data in json" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_return(flexmock(:body => fixture('empty.json')))
connection.should_receive(:expire).with("/data").once
lambda{AMEE::Data::Category.get(connection, "/data")}.should raise_error(AMEE::BadData)
end
end
describe AMEE::Data::Category, "with an authenticated connection" do
it "should fail gracefully on other GET errors" do
connection = flexmock "connection"
connection.should_receive(:retries).and_return(0)
connection.should_receive(:get).with("/data", {:itemsPerPage => 10}).and_raise(Timeout::Error)
lambda{AMEE::Data::Category.get(connection, "/data")}.should raise_error(Timeout::Error)
end
end