require File.dirname(__FILE__) + '/spec_helper.rb'
describe AMEE::Profile::Category do
before(:each) do
@cat = AMEE::Profile::Category.new
end
it "should have common AMEE object properties" do
@cat.is_a?(AMEE::Profile::Object).should be_true
end
it "should have a total co2 amount and unit" do
@cat.should respond_to(:total_amount)
@cat.should respond_to(:total_amount_unit)
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::Profile::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::Profile::Category.new(:children => children, :items => items)
@cat.children.should == children
@cat.items.should == items
end
end
describe AMEE::Profile::Category, "accessing AMEE V0" do
it "should load Profile Category" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/D4B9755F671D/home", {}).and_return(flexmock(:body => 'HomehomeTelevisiontelevisionSeasonal Billingseasonal_billingHeatingheatingFuelfuelLightinglightingFuel Pricefuel_priceAppliancesappliancesCookingcooking200902'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/D4B9755F671D/home")
@cat.profile_uid.should == "D4B9755F671D"
@cat.profile_date.should == DateTime.new(2009, 2)
@cat.name.should == "Home"
@cat.path.should == "/home"
@cat.full_path.should == "/profiles/D4B9755F671D/home"
@cat.children.size.should be(8)
@cat.children[0][:uid].should == "3A72CC3F2AC9"
@cat.children[0][:name].should == "Television"
@cat.children[0][:path].should == "television"
@cat.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/D4B9755F671D/transport/transport", {}).and_return(flexmock(:body => 'Transporttransport20090201falsefalsefalsefalse1false021.42false25A341E24BCE0.0833333333333333Flights, Short haul, Return20090201falsefalsefalsefalse14.16false1340.83333333333286.39false3CAFBEA600F40Car1, Fuel manufacturer consumption, Diesel200902307.81'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/D4B9755F671D/transport/transport")
@cat.total_amount.should be_close(307.81, 1e-9)
@cat.total_amount_unit.should == "kg/month"
@cat.items.size.should be(2)
@cat.items[0][:uid].should == "05BB9D4AF86D"
@cat.items[0][:name].should be_nil
@cat.items[0][:path].should == "05BB9D4AF86D"
@cat.items[0][:dataItemLabel].should == "Flights, Short haul, Return"
@cat.items[0][:dataItemUid].should == "25A341E24BCE"
@cat.items[0][:validFrom].should == DateTime.new(2009, 2, 1)
@cat.items[0][:end].should == false
@cat.items[0][:amountPerMonth].should be_close(21.42, 1e-9)
@cat.items[0][:values].size.should be(8)
@cat.items[0][:values][:transportNumberOfJourneys].should == "0.0833333333333333"
@cat.items[0][:values][:transportEcoDriving].should == "false"
@cat.pager.should be_nil
end
end
describe AMEE::Profile::Category, "with an authenticated XML connection" do
it "should load Profile Category" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home", {}).and_return(flexmock(:body => '/home200809HomehomeAppliancesappliancesEnergyenergyHeatingheatingLightinglighting'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E/home")
@cat.profile_uid.should == "E54C5525AA3E"
@cat.profile_date.should == DateTime.new(2008, 9)
@cat.name.should == "Home"
@cat.path.should == "/home"
@cat.full_path.should == "/profiles/E54C5525AA3E/home"
@cat.children.size.should be(4)
@cat.children[0][:uid].should == "427DFCC65E52"
@cat.children[0][:name].should == "Appliances"
@cat.children[0][:path].should == "appliances"
@cat.pager.should be_nil
end
it "should provide access to child objects" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home", {}).and_return(flexmock(:body => '/home200809HomehomeAppliancesappliancesEnergyenergyHeatingheatingLightinglighting'))
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home/appliances", {}).and_return(flexmock(:body => '/home/appliances200809AppliancesappliancesComputerscomputersCookingcookingEntertainmententertainmentKitchenkitchenTelevisionstelevisions'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E/home")
@child = @cat.child('appliances')
@child.path.should == "/home/appliances"
@child.full_path.should == "/profiles/E54C5525AA3E/home/appliances"
@child.children.size.should be(5)
@child.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home/energy/quantity", {}).and_return(flexmock(:body => '/home/energy/quantity200809Quantityquantity20080902false122.47266056991EE230FB07247AD9370gasgas20080901false500103.00066056991EE230D9CBCDED44C50gas2gas012211-1-11102105.472'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E/home/energy/quantity")
@cat.total_amount.should be_close(105.472, 1e-9)
@cat.total_amount_unit.should == "kg/month"
@cat.items.size.should be(2)
@cat.items[0][:uid].should == "FB07247AD937"
@cat.items[0][:name].should == "gas"
@cat.items[0][:path].should == "FB07247AD937"
@cat.items[0][:dataItemLabel].should == "gas"
@cat.items[0][:dataItemUid].should == "66056991EE23"
@cat.items[0][:validFrom].should == DateTime.new(2008, 9, 2)
@cat.items[0][:end].should == false
@cat.items[0][:amountPerMonth].should be_close(2.472, 1e-9)
@cat.items[0][:values].size.should be(3)
@cat.items[0][:values][:kWhPerMonth].should == "12"
@cat.items[0][:values][:kgPerMonth].should == "0"
@cat.items[0][:values][:litresPerMonth].should == "0"
@cat.pager.should_not be_nil
end
it "should fail gracefully with incorrect data" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
xml = ''
connection.should_receive(:get).with("/profiles/E54C5525AA3E", {}).and_return(flexmock(:body => xml))
lambda{AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E")}.should raise_error(AMEE::BadData)
end
it "parses recursive GET requests" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/BE22C1732952/transport/car", {:recurse=>true}).and_return(flexmock(:body => '/transport/car200901Carcar/transport/car/bandsBandsbands/transport/car/genericGenericgeneric/transport/car/generic/electricElectricelectric120090101falsetruefalsefalse0false0.265-104F6CBCEE95F7true8450D6D97D2Ddiesel, large0.265/transport/car/specificSpecificspecific'))
cat = AMEE::Profile::Category.get(connection, "/profiles/BE22C1732952/transport/car", :recurse => true)
cat.items.size.should == 0
cat.children.size.should == 3
cat.children[1][:name].should == "Generic"
cat.children[1][:path].should == "generic"
cat.children[1][:uid].should == "87E55DA88017"
cat.children[1][:totalAmountPerMonth].should == 0.265
cat.children[1][:children].size.should == 1
cat.children[1][:children][0][:name].should == "Electric"
cat.children[1][:children][0][:path].should == "electric"
cat.children[1][:children][0][:uid].should == "417DD367E9AA"
cat.children[1][:items].size.should == 1
cat.children[1][:items][0][:amountPerMonth].should == 0.265
cat.children[1][:items][0][:dataItemLabel].should == "diesel, large"
cat.children[1][:items][0][:dataItemUid].should == "4F6CBCEE95F7"
cat.children[1][:items][0][:values][:airconTypical].should == "true"
cat.children[1][:items][0][:uid].should == "8450D6D97D2D"
cat.pager.should be_nil
end
end
describe AMEE::Profile::Category, "with an authenticated version 2 XML connection" do
it "should load Profile Category" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/26532D8EFA9D/home", {}).and_return(flexmock(:body => ' /homeAMEE1010HomehomeAppliancesappliancesEnergyenergyHeatingheatingLightinglightingWastewasteWaterwater'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/26532D8EFA9D/home")
@cat.profile_uid.should == "26532D8EFA9D"
@cat.name.should == "Home"
@cat.path.should == "/home"
@cat.full_path.should == "/profiles/26532D8EFA9D/home"
@cat.children.size.should be(6)
@cat.children[0][:uid].should == "427DFCC65E52"
@cat.children[0][:name].should == "Appliances"
@cat.children[0][:path].should == "appliances"
@cat.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/26532D8EFA9D/home/energy/quantity", {}).and_return(flexmock(:body => ' /home/energy/quantityAMEE1010QuantityquantitygaspaymentFrequencyPayment frequencypaymentFrequencyPayment frequencytextTEXTtruefalsegreenTariffGreen tariffgreenTariffGreen tarifftextTEXTtruefalseseasonSeasonseasonSeasontextTEXTtruefalseincludesHeatingIncludes HeatingfalseincludesHeatingIncludes HeatingtextTEXTtruefalsemassPerTimeMass Per TimekgyearmassPerTimeMass Per TimeamountDOUBLEkgyeartruefalseenergyConsumptionEnergy Consumption13kWhmonthenergyConsumptionEnergy ConsumptionamountDOUBLEkWhyeartruefalsecurrentReadingCurrent ReadingkWhcurrentReadingCurrent ReadingamountDOUBLEkWhtruefalselastReadingLast ReadingkWhlastReadingLast ReadingamountDOUBLEkWhtruefalsevolumePerTimeVolume Per TimeLyearvolumePerTimeVolume Per TimeamountDOUBLELyeartruefalsedeliveriesNumber of deliveriesyeardeliveriesNumber of deliveriesamountDOUBLEyeartruefalse29.66429.6642008-09-02T01:00+0100BC0B730255FBpaymentFrequencyPayment frequencypaymentFrequencyPayment frequencytextTEXTtruefalsegreenTariffGreen tariffgreenTariffGreen tarifftextTEXTtruefalseseasonSeasonseasonSeasontextTEXTtruefalseincludesHeatingIncludes HeatingfalseincludesHeatingIncludes HeatingtextTEXTtruefalsemassPerTimeMass Per TimekgyearmassPerTimeMass Per TimeamountDOUBLEkgyeartruefalseenergyConsumptionEnergy ConsumptionkWhyearenergyConsumptionEnergy ConsumptionamountDOUBLEkWhyeartruefalsecurrentReadingCurrent ReadingkWhcurrentReadingCurrent ReadingamountDOUBLEkWhtruefalselastReadingLast ReadingkWhlastReadingLast ReadingamountDOUBLEkWhtruefalsevolumePerTimeVolume Per TimeLyearvolumePerTimeVolume Per TimeamountDOUBLELyeartruefalsedeliveriesNumber of deliveriesyeardeliveriesNumber of deliveriesamountDOUBLEyeartruefalse0.0002009-02-11T14:13+0000012211-1-111021265.664'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/26532D8EFA9D/home/energy/quantity")
@cat.total_amount.should be_close(1265.664, 1e-9)
@cat.total_amount_unit.should == "kg/year"
@cat.items.size.should be(2)
@cat.items[0][:uid].should == "30C00AD33033"
@cat.items[0][:name].should == "gas"
@cat.items[0][:path].should == "30C00AD33033"
@cat.items[0][:dataItemLabel].should == "gas"
@cat.items[0][:dataItemUid].should == "66056991EE23"
@cat.items[0][:startDate].should == DateTime.new(2008, 9, 2)
@cat.items[0][:endDate].should be_nil
@cat.items[0][:amount].should be_close(29.664, 1e-9)
@cat.items[0][:amount_unit].should == "kg/year"
@cat.items[0][:values].size.should be(10)
@cat.items[0][:values][:energyConsumption][:value].should == "13"
@cat.items[0][:values][:energyConsumption][:unit].should == "kWh"
@cat.items[0][:values][:energyConsumption][:per_unit].should == "month"
@cat.items[0][:values][:massPerTime][:value].should == "0"
@cat.items[0][:values][:volumePerTime][:value].should == "0"
@cat.pager.should_not be_nil
end
it "should parse multiple return values" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/F8C2D1052BF7/home/energy/quantity", {}).and_return(flexmock(:body => '/home/energy/quantityAMEE1010QuantityquantityfalsegascurrentReadingCurrent Reading0kWhyear2010-07-16T10:44:00-04:00currentReadingCurrent ReadingamountDECIMALkWhyeartruefalsefalseenergyConsumptionEnergy Consumption13kWhmonth2010-07-16T10:44:00-04:00energyConsumptionEnergy ConsumptionamountDECIMALkWhyeartruefalsefalseincludesHeatingIncludes Heatingfalse2010-07-16T10:44:00-04:00includesHeatingIncludes HeatingtextTEXTtruefalsefalseseasonSeason2010-07-16T10:44:00-04:00seasonSeasontextTEXTtruefalsefalsegreenTariffGreen tariff2010-07-16T10:44:00-04:00greenTariffGreen tarifftextTEXTtruefalsefalsepaymentFrequencyPayment frequency2010-07-16T10:44:00-04:00paymentFrequencyPayment frequencytextTEXTtruefalsefalsemassPerTimeMass Per Time0kgyear2010-07-16T10:44:00-04:00massPerTimeMass Per TimeamountDECIMALkgyeartruefalsefalselastReadingLast Reading0kWhyear2010-07-16T10:44:00-04:00lastReadingLast ReadingamountDECIMALkWhyeartruefalsefalsevolumePerTimeVolume Per Time0Lyear2010-07-16T10:44:00-04:00volumePerTimeVolume Per TimeamountDECIMALLyeartruefalsefalsedeliveriesNumber of deliveriesyear2010-07-16T10:44:00-04:00deliveriesNumber of deliveriesamountDECIMALyeartruefalsefalse31.777231.77724.2This is a text nodeThis is another text node2010-07-16T10:44:00-04:00011111-1-1110131.7772'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/F8C2D1052BF7/home/energy/quantity")
@cat.total_amount.should be_close(31.7772, 1e-4)
@cat.total_amount_unit.should == 'kg/year'
item = @cat.items[0]
item[:amounts].size.should be(2)
item[:amounts][0][:type].should == "CO2"
item[:amounts][0][:value].should be_close(31.7772, 1e-4)
item[:amounts][0][:unit].should == 'kg'
item[:amounts][0][:per_unit].should == 'year'
item[:amounts][0][:default].should be_true
item[:amounts][1][:type].should == "CH4"
item[:amounts][1][:value].should be_close(4.2, 1e-2)
item[:amounts][1][:unit].should == 'kg'
item[:amounts][1][:per_unit].should be_nil
item[:notes].size.should be(2)
item[:notes][0][:type].should == 'comment'
item[:notes][0][:value].should == 'This is a text node'
item[:notes][1][:type].should == 'comment'
item[:notes][1][:value].should == 'This is another text node'
end
it "should fail gracefully with incorrect data" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
xml = ''
connection.should_receive(:get).with("/profiles/E54C5525AA3E", {}).and_return(flexmock(:body => xml))
lambda{AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E")}.should raise_error(AMEE::BadData)
end
# it "parses recursive GET requests" do
# connection = flexmock "connection"
# connection.should_receive(:version).and_return(2.0)
# connection.should_receive(:get).with("/profiles/BE22C1732952/transport/car", {:recurse=>true}).and_return('')
# cat = AMEE::Profile::Category.get(connection, "/profiles/BE22C1732952/transport/car", :recurse => true)
# cat.items.size.should == 0
# cat.children.size.should == 3
# cat.children[1][:name].should == "Generic"
# cat.children[1][:path].should == "generic"
# cat.children[1][:uid].should == "87E55DA88017"
# cat.children[1][:totalAmountPerMonth].should == 0.265
# cat.children[1][:children].size.should == 1
# cat.children[1][:children][0][:name].should == "Electric"
# cat.children[1][:children][0][:path].should == "electric"
# cat.children[1][:children][0][:uid].should == "417DD367E9AA"
# cat.children[1][:items].size.should == 1
# cat.children[1][:items][0][:amountPerMonth].should == 0.265
# cat.children[1][:items][0][:dataItemLabel].should == "diesel, large"
# cat.children[1][:items][0][:dataItemUid].should == "4F6CBCEE95F7"
# cat.children[1][:items][0][:values][:airconTypical].should == "true"
# cat.children[1][:items][0][:uid].should == "8450D6D97D2D"
# end
end
describe AMEE::Profile::Category, "with an authenticated version 2 Atom connection" do
it "should load Profile Category" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/FD89B8D07D4F/home", {}).and_return(flexmock(:body => ' Profile FD89B8D07D4F, Category Home urn:dataCategory:BBA3AC3E795E AMEE FD89B8D07D4F Home 0.000000 1970-01-01T00:00:00.000Z '))
@cat = AMEE::Profile::Category.get(connection, "/profiles/FD89B8D07D4F/home")
@cat.profile_uid.should == "FD89B8D07D4F"
@cat.name.should == "Home"
@cat.path.should == "/home"
@cat.full_path.should == "/profiles/FD89B8D07D4F/home"
#@cat.children.size.should be(6)
#@cat.children[0][:uid].should == "427DFCC65E52"
#@cat.children[0][:name].should == "Appliances"
#@cat.children[0][:path].should == "appliances"
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/9BFB0C1CD78A/home/energy/quantity", {}).and_return(flexmock(:body => 'Profile 9BFB0C1CD78A, Category /home/energy/quantityurn:dataCategory:A92693A99BADAMEE9BFB0C1CD78AQuantity1265.6642009-02-11T14:13:28.000ZgasTue, 2 Sep 2008 01:00:00 BSTurn:item:30C00AD330332008-09-02T00:00:00.000Z2008-09-02T00:00:00.000Z2008-09-02T01:00+010029.664Payment frequencyN/AGreen tariffN/ASeasonN/AIncludes HeatingfalseMass Per TimeN/AkgyearEnergy Consumption13kWhmonthCurrent ReadingN/AkWhLast ReadingN/AkWhVolume Per TimeN/ALyearNumber of deliveriesN/Ayeargas<div class="vevent"><div class="summary">0.000 kg/year</div><abbr class="dtstart" title="2008-09-02T01:00+0100"> Tue, 2 Sep 2008 01:00:00 BST</abbr></div>BC0B730255FBWed, 11 Feb 2009 14:13:00 GMTurn:item:BC0B730255FB2009-02-11T14:13:00.000Z2009-02-11T14:13:00.000Z2009-02-11T14:13+00000.000Payment frequencyN/AGreen tariffN/ASeasonN/AIncludes HeatingfalseMass Per TimeN/AkgyearEnergy ConsumptionN/AkWhyearCurrent ReadingN/AkWhLast ReadingN/AkWhVolume Per TimeN/ALyearNumber of deliveriesN/Ayear<div class="vevent"><div class="summary">0.000 kg/year</div><abbr class="dtstart" title="2009-02-11T14:13+0000"> Wed, 11 Feb 2009 14:13:00 GMT</abbr></div>'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/9BFB0C1CD78A/home/energy/quantity")
@cat.total_amount.should be_close(1265.664, 1e-9)
@cat.total_amount_unit.should == "kg/year"
@cat.items.size.should be(2)
@cat.items[0][:uid].should == "30C00AD33033"
@cat.items[0][:name].should == "gas"
@cat.items[0][:path].should == "30C00AD33033"
#@cat.items[0][:dataItemLabel].should == "gas"
#@cat.items[0][:dataItemUid].should == "66056991EE23"
@cat.items[0][:startDate].should == DateTime.new(2008, 9, 2)
@cat.items[0][:endDate].should be_nil
@cat.items[0][:amount].should be_close(29.664, 1e-9)
@cat.items[0][:amount_unit].should == "kg/year"
@cat.items[0][:values].size.should be(10)
@cat.items[0][:values][:energyConsumption][:value].should == "13"
@cat.items[0][:values][:energyConsumption][:unit].should == "kWh"
@cat.items[0][:values][:energyConsumption][:per_unit].should == "month"
@cat.items[0][:values][:massPerTime][:value].should == "0"
@cat.items[0][:values][:volumePerTime][:value].should == "0"
end
it "should fail gracefully with incorrect data" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
atom = ''
connection.should_receive(:get).with("/profiles/E54C5525AA3E", {}).and_return(flexmock(:body => atom))
lambda{AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E")}.should raise_error(AMEE::BadData)
end
# it "parses recursive GET requests" do
# connection = flexmock "connection"
# connection.should_receive(:version).and_return(2.0)
# connection.should_receive(:get).with("/profiles/BE22C1732952/transport/car", {:recurse=>true}).and_return(flexmock(:body => ''))
# cat = AMEE::Profile::Category.get(connection, "/profiles/BE22C1732952/transport/car", :recurse => true)
# cat.items.size.should == 0
# cat.children.size.should == 3
# cat.children[1][:name].should == "Generic"
# cat.children[1][:path].should == "generic"
# cat.children[1][:uid].should == "87E55DA88017"
# cat.children[1][:totalAmountPerMonth].should == 0.265
# cat.children[1][:children].size.should == 1
# cat.children[1][:children][0][:name].should == "Electric"
# cat.children[1][:children][0][:path].should == "electric"
# cat.children[1][:children][0][:uid].should == "417DD367E9AA"
# cat.children[1][:items].size.should == 1
# cat.children[1][:items][0][:amountPerMonth].should == 0.265
# cat.children[1][:items][0][:dataItemLabel].should == "diesel, large"
# cat.children[1][:items][0][:dataItemUid].should == "4F6CBCEE95F7"
# cat.children[1][:items][0][:values][:airconTypical].should == "true"
# cat.children[1][:items][0][:uid].should == "8450D6D97D2D"
# end
end
describe AMEE::Profile::Category, "with an authenticated JSON connection" do
it "should load Profile Category" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home", {}).and_return(flexmock(:body => '{"totalAmountPerMonth":"0","dataCategory":{"uid":"BBA3AC3E795E","path":"home","name":"Home"},"profileDate":"200809","path":"/home","profile":{"uid":"E54C5525AA3E"},"children":{"pager":{},"dataCategories":[{"uid":"427DFCC65E52","path":"appliances","name":"Appliances"},{"uid":"30BA55A0C472","path":"energy","name":"Energy"},{"uid":"A46ECFA19333","path":"heating","name":"Heating"},{"uid":"150266DD4434","path":"lighting","name":"Lighting"}],"profileItems":{}}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E/home")
@cat.profile_uid.should == "E54C5525AA3E"
@cat.profile_date.should == DateTime.new(2008, 9)
@cat.name.should == "Home"
@cat.path.should == "/home"
@cat.full_path.should == "/profiles/E54C5525AA3E/home"
@cat.children.size.should be(4)
@cat.children[0][:uid].should == "427DFCC65E52"
@cat.children[0][:name].should == "Appliances"
@cat.children[0][:path].should == "appliances"
@cat.pager.should be_nil
end
it "should provide access to child objects" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home", {}).and_return(flexmock(:body => '{"totalAmountPerMonth":"0","dataCategory":{"uid":"BBA3AC3E795E","path":"home","name":"Home"},"profileDate":"200809","path":"/home","profile":{"uid":"E54C5525AA3E"},"children":{"pager":{},"dataCategories":[{"uid":"427DFCC65E52","path":"appliances","name":"Appliances"},{"uid":"30BA55A0C472","path":"energy","name":"Energy"},{"uid":"A46ECFA19333","path":"heating","name":"Heating"},{"uid":"150266DD4434","path":"lighting","name":"Lighting"}],"profileItems":{}}}'))
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home/appliances", {}).and_return(flexmock(:body => '{"totalAmountPerMonth":"0","dataCategory":{"uid":"427DFCC65E52","path":"appliances","name":"Appliances"},"profileDate":"200809","path":"/home/appliances","profile":{"uid":"E54C5525AA3E"},"children":{"pager":{},"dataCategories":[{"uid":"3FE23FDC8CEA","path":"computers","name":"Computers"},{"uid":"54C8A44254AA","path":"cooking","name":"Cooking"},{"uid":"75AD9B83B7BF","path":"entertainment","name":"Entertainment"},{"uid":"4BD595E1873A","path":"kitchen","name":"Kitchen"},{"uid":"700D0771870A","path":"televisions","name":"Televisions"}],"profileItems":{}}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E/home")
@child = @cat.child('appliances')
@child.path.should == "/home/appliances"
@child.full_path.should == "/profiles/E54C5525AA3E/home/appliances"
@child.children.size.should be(5)
@child.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/E54C5525AA3E/home/energy/quantity", {}).and_return(flexmock(:body => '{"totalAmountPerMonth":105.472,"dataCategory":{"uid":"A92693A99BAD","path":"quantity","name":"Quantity"},"profileDate":"200809","path":"/home/energy/quantity","profile":{"uid":"E54C5525AA3E"},"children":{"pager":{"to":2,"lastPage":1,"start":0,"nextPage":-1,"items":2,"itemsPerPage":10,"from":1,"previousPage":-1,"requestedPage":1,"currentPage":1,"itemsFound":2},"dataCategories":[],"profileItems":{"rows":[{"created":"2008-09-03 11:37:35.0","kgPerMonth":"0","dataItemLabel":"gas","end":"false","uid":"FB07247AD937","modified":"2008-09-03 11:38:12.0","dataItemUid":"66056991EE23","validFrom":"20080902","amountPerMonth":"2.472","label":"ProfileItem","litresPerMonth":"0","path":"FB07247AD937","kWhPerMonth":"12","name":"gas"},{"created":"2008-09-03 11:40:44.0","kgPerMonth":"0","dataItemLabel":"gas","end":"false","uid":"D9CBCDED44C5","modified":"2008-09-03 11:41:54.0","dataItemUid":"66056991EE23","validFrom":"20080901","amountPerMonth":"103.000","label":"ProfileItem","litresPerMonth":"0","path":"D9CBCDED44C5","kWhPerMonth":"500","name":"gas2"}],"label":"ProfileItems"}}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E/home/energy/quantity")
@cat.total_amount.should be_close(105.472, 1e-9)
@cat.total_amount_unit.should == "kg/month"
@cat.items.size.should be(2)
@cat.items[0][:uid].should == "FB07247AD937"
@cat.items[0][:name].should == "gas"
@cat.items[0][:path].should == "FB07247AD937"
@cat.items[0][:dataItemLabel].should == "gas"
@cat.items[0][:dataItemUid].should == "66056991EE23"
@cat.items[0][:validFrom].should == DateTime.new(2008, 9, 2)
@cat.items[0][:end].should == false
@cat.items[0][:amountPerMonth].should be_close(2.472, 1e-9)
@cat.items[0][:values].size.should be(3)
@cat.items[0][:values][:kWhPerMonth].should == "12"
@cat.items[0][:values][:kgPerMonth].should == "0"
@cat.items[0][:values][:litresPerMonth].should == "0"
@cat.pager.should_not be_nil
end
it "should fail gracefully with incorrect data" do
connection = flexmock "connection"
json = '{"apiVersion":"2.0"}'
connection.should_receive(:get).with("/profiles/E54C5525AA3E", {}).and_return(flexmock(:body => json))
lambda{AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E")}.should raise_error(AMEE::BadData)
end
it "parses recursive GET requests" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(1.0)
connection.should_receive(:get).with("/profiles/BE22C1732952/transport/car", {:recurse => true}).and_return(flexmock(:body => '{"totalAmountPerMonth":"0","dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"profileDate":"200901","path":"/transport/car","profile":{"uid":"BE22C1732952"},"children":{"pager":{},"dataCategories":[{"dataCategory":{"modified":"2008-04-21 16:42:10.0","created":"2008-04-21 16:42:10.0","itemDefinition":{"uid":"C6BC60C55678"},"dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"uid":"883ADD27228F","environment":{"uid":"5F5887BCF726"},"path":"bands","name":"Bands"},"path":"/transport/car/bands"},{"totalAmountPerMonth":0.265,"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"},"path":"/transport/car/generic","children":{"dataCategories":[{"dataCategory":{"modified":"2008-09-23 12:18:03.0","created":"2008-09-23 12:18:03.0","itemDefinition":{"uid":"E6D0BB09578A"},"dataCategory":{"uid":"87E55DA88017","path":"generic","name":"Generic"},"uid":"417DD367E9AA","environment":{"uid":"5F5887BCF726"},"path":"electric","name":"Electric"},"path":"/transport/car/generic/electric"}],"profileItems":{"rows":[{"created":"2009-01-05 13:58:52.0","ecoDriving":"false","tyresUnderinflated":"false","dataItemLabel":"diesel, large","kmPerLitre":"0","distanceKmPerMonth":"1","end":"false","uid":"8450D6D97D2D","modified":"2009-01-05 13:59:05.0","airconFull":"false","dataItemUid":"4F6CBCEE95F7","validFrom":"20090101","amountPerMonth":"0.265","kmPerLitreOwn":"0","country":"","label":"ProfileItem","occupants":"-1","airconTypical":"true","path":"8450D6D97D2D","name":"","regularlyServiced":"true"}],"label":"ProfileItems"}}},{"dataCategory":{"modified":"2007-07-27 09:30:44.0","created":"2007-07-27 09:30:44.0","itemDefinition":{"uid":"07EBA32512DF"},"dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"uid":"95E76249584D","environment":{"uid":"5F5887BCF726"},"path":"specific","name":"Specific"},"path":"/transport/car/specific"}],"profileItems":{}}}'))
cat = AMEE::Profile::Category.get(connection, "/profiles/BE22C1732952/transport/car", :recurse => true)
cat.items.size.should == 0
cat.children.size.should == 3
cat.children[1][:name].should == "Generic"
cat.children[1][:path].should == "generic"
cat.children[1][:uid].should == "87E55DA88017"
cat.children[1][:totalAmountPerMonth].should == 0.265
cat.children[1][:children].size.should == 1
cat.children[1][:children][0][:name].should == "Electric"
cat.children[1][:children][0][:path].should == "electric"
cat.children[1][:children][0][:uid].should == "417DD367E9AA"
cat.children[1][:items].size.should == 1
cat.children[1][:items][0][:amountPerMonth].should == 0.265
cat.children[1][:items][0][:dataItemLabel].should == "diesel, large"
cat.children[1][:items][0][:dataItemUid].should == "4F6CBCEE95F7"
cat.children[1][:items][0][:values][:airconTypical].should == "true"
cat.children[1][:items][0][:uid].should == "8450D6D97D2D"
cat.pager.should be_nil
end
end
describe AMEE::Profile::Category, "with an authenticated V2 JSON connection" do
it "should load Profile Category" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/447C40EB29FB/home", {}).and_return(flexmock(:body => '{"apiVersion":"2.0","pager":{},"profileCategories":[{"uid":"427DFCC65E52","name":"Appliances","path":"appliances"},{"uid":"30BA55A0C472","name":"Energy","path":"energy"},{"uid":"A46ECFA19333","name":"Heating","path":"heating"},{"uid":"150266DD4434","name":"Lighting","path":"lighting"},{"uid":"6553150F96CE","name":"Waste","path":"waste"},{"uid":"07362DCC9E7B","name":"Water","path":"water"}],"dataCategory":{"uid":"BBA3AC3E795E","dataCategory":{"uid":"CD310BEBAC52","name":"Root","path":""},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10},"created":"2007-07-27 09:30:44.0","name":"Home","path":"home","modified":"2007-07-27 09:30:44.0"},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"created":"2007-07-27 09:30:44.0","description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10,"modified":"2007-07-27 09:30:44.0"},"totalAmount":"0","path":"/home","profileItemActions":{"allowCreate":true,"allowView":true,"allowList":true,"allowModify":true,"allowDelete":true},"actions":{"allowCreate":true,"allowView":true,"allowList":true,"allowModify":true,"allowDelete":true},"profileItems":{},"profile":{"uid":"447C40EB29FB"}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/447C40EB29FB/home")
@cat.profile_uid.should == "447C40EB29FB"
@cat.name.should == "Home"
@cat.path.should == "/home"
@cat.full_path.should == "/profiles/447C40EB29FB/home"
@cat.children.size.should be(6)
@cat.children[0][:uid].should == "427DFCC65E52"
@cat.children[0][:name].should == "Appliances"
@cat.children[0][:path].should == "appliances"
@cat.pager.should be_nil
end
it "should provide access to child objects" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/447C40EB29FB/home", {}).and_return(flexmock(:body => '{"apiVersion":"2.0","pager":{},"profileCategories":[{"uid":"427DFCC65E52","name":"Appliances","path":"appliances"},{"uid":"30BA55A0C472","name":"Energy","path":"energy"},{"uid":"A46ECFA19333","name":"Heating","path":"heating"},{"uid":"150266DD4434","name":"Lighting","path":"lighting"},{"uid":"6553150F96CE","name":"Waste","path":"waste"},{"uid":"07362DCC9E7B","name":"Water","path":"water"}],"dataCategory":{"uid":"BBA3AC3E795E","dataCategory":{"uid":"CD310BEBAC52","name":"Root","path":""},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10},"created":"2007-07-27 09:30:44.0","name":"Home","path":"home","modified":"2007-07-27 09:30:44.0"},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"created":"2007-07-27 09:30:44.0","description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10,"modified":"2007-07-27 09:30:44.0"},"totalAmount":"0","path":"/home","profileItemActions":{"allowCreate":true,"allowView":true,"allowList":true,"allowModify":true,"allowDelete":true},"actions":{"allowCreate":true,"allowView":true,"allowList":true,"allowModify":true,"allowDelete":true},"profileItems":{},"profile":{"uid":"447C40EB29FB"}}'))
connection.should_receive(:get).with("/profiles/447C40EB29FB/home/appliances", {}).and_return(flexmock(:body => '{"apiVersion":"2.0","pager":{},"profileCategories":[{"uid":"3FE23FDC8CEA","name":"Computers","path":"computers"},{"uid":"54C8A44254AA","name":"Cooking","path":"cooking"},{"uid":"75AD9B83B7BF","name":"Entertainment","path":"entertainment"},{"uid":"4BD595E1873A","name":"Kitchen","path":"kitchen"},{"uid":"700D0771870A","name":"Televisions","path":"televisions"}],"dataCategory":{"uid":"427DFCC65E52","dataCategory":{"uid":"BBA3AC3E795E","name":"Home","path":"home"},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10},"created":"2007-07-27 09:30:44.0","name":"Appliances","path":"appliances","modified":"2007-07-27 09:30:44.0"},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"created":"2007-07-27 09:30:44.0","description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10,"modified":"2007-07-27 09:30:44.0"},"totalAmount":"0","path":"/home/appliances","profileItemActions":{"allowCreate":true,"allowView":true,"allowList":true,"allowModify":true,"allowDelete":true},"actions":{"allowCreate":true,"allowView":true,"allowList":true,"allowModify":true,"allowDelete":true},"profileItems":{},"profile":{"uid":"447C40EB29FB"}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/447C40EB29FB/home")
@child = @cat.child('appliances')
@child.path.should == "/home/appliances"
@child.full_path.should == "/profiles/447C40EB29FB/home/appliances"
@child.children.size.should be(5)
@child.pager.should be_nil
end
it "should parse data items" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/447C40EB29FB/home/energy/quantity", {}).and_return(flexmock(:body => '{"apiVersion":"2.0","pager":{"to":1,"lastPage":1,"nextPage":-1,"items":1,"start":0,"itemsFound":1,"requestedPage":1,"currentPage":1,"from":1,"itemsPerPage":10,"previousPage":-1},"profileCategories":[],"dataCategory":{"uid":"A92693A99BAD","dataCategory":{"uid":"30BA55A0C472","deprecated":false,"name":"Energy","path":"energy"},"deprecated":false,"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10},"created":"2007-07-27 08:30:44.0","name":"Quantity","path":"quantity","itemDefinition":{"uid":"212C818D8F16","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","name":"Energy Quantity","drillDown":"type","modified":"2007-07-27 08:30:44.0"},"modified":"2007-07-27 08:30:44.0"},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"created":"2007-07-27 08:30:44.0","description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10,"modified":"2007-07-27 08:30:44.0"},"totalAmount":{"unit":"kg/year","value":"31.7772"},"path":"/home/energy/quantity","profileItems":[{"amount":{"unit":"kg/year","value":31.7772},"uid":"B3D15D776EB9","startDate":"2010-06-28T13:14:00-04:00","itemValues":[{"itemValueDefinition":{"perUnit":"year","uid":"67B7476423C0","unit":"kWh","name":"Current Reading","fromData":false,"path":"currentReading","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"048EE4994F5B","startDate":"2010-06-28T13:14:00-04:00","unit":"kWh","name":"Current Reading","value":"0","path":"currentReading","displayPath":"currentReading","displayName":"Current Reading"},{"itemValueDefinition":{"perUnit":"year","uid":"068A0D6D03F7","unit":"kWh","name":"Energy Consumption","fromData":false,"path":"energyConsumption","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"month","uid":"2E52C3842819","startDate":"2010-06-28T13:14:00-04:00","unit":"kWh","name":"Energy Consumption","value":"13","path":"energyConsumption","displayPath":"energyConsumption","displayName":"Energy Consumption"},{"itemValueDefinition":{"uid":"1740E500BDAB","choices":"true=true,false=false","name":"Includes Heating","fromData":false,"path":"includesHeating","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"98D6B86F928E","startDate":"2010-06-28T13:14:00-04:00","unit":"","name":"Includes Heating","value":"false","path":"includesHeating","displayPath":"includesHeating","displayName":"Includes Heating"},{"itemValueDefinition":{"uid":"527AADFB3B65","name":"Season","fromData":false,"path":"season","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"142422F25D4E","startDate":"2010-06-28T13:14:00-04:00","unit":"","name":"Season","value":"","path":"season","displayPath":"season","displayName":"Season"},{"itemValueDefinition":{"uid":"63005554AE8A","name":"Green tariff","fromData":false,"path":"greenTariff","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"3BBD254BA9BA","startDate":"2010-06-28T13:14:00-04:00","unit":"","name":"Green tariff","value":"","path":"greenTariff","displayPath":"greenTariff","displayName":"Green tariff"},{"itemValueDefinition":{"uid":"E0EFED6FD7E6","name":"Payment frequency","fromData":false,"path":"paymentFrequency","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"4E57D4DEFC2A","startDate":"2010-06-28T13:14:00-04:00","unit":"","name":"Payment frequency","value":"","path":"paymentFrequency","displayPath":"paymentFrequency","displayName":"Payment frequency"},{"itemValueDefinition":{"perUnit":"year","uid":"CF12B3EBAD02","unit":"kg","name":"Mass Per Time","fromData":false,"path":"massPerTime","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"F0AE74C87EEE","startDate":"2010-06-28T13:14:00-04:00","unit":"kg","name":"Mass Per Time","value":"0","path":"massPerTime","displayPath":"massPerTime","displayName":"Mass Per Time"},{"itemValueDefinition":{"perUnit":"year","uid":"40AB7E90980C","unit":"kWh","name":"Last Reading","fromData":false,"path":"lastReading","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"99B9FDA5D1A8","startDate":"2010-06-28T13:14:00-04:00","unit":"kWh","name":"Last Reading","value":"0","path":"lastReading","displayPath":"lastReading","displayName":"Last Reading"},{"itemValueDefinition":{"perUnit":"year","uid":"177D4642A07A","name":"Number of deliveries","fromData":false,"path":"deliveries","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"EE0151C2C22C","startDate":"2010-06-28T13:14:00-04:00","unit":"","name":"Number of deliveries","value":"","path":"deliveries","displayPath":"deliveries","displayName":"Number of deliveries"},{"itemValueDefinition":{"perUnit":"year","uid":"324EC808AC30","unit":"L","name":"Volume Per Time","fromData":false,"path":"volumePerTime","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"35B94788B050","startDate":"2010-06-28T13:14:00-04:00","unit":"L","name":"Volume Per Time","value":"0","path":"volumePerTime","displayPath":"volumePerTime","displayName":"Volume Per Time"}],"amounts":{"amount":[{"perUnit":"year","unit":"kg","default":"true","value":31.7772,"type":"CO2"}]},"created":"2010-06-28T17:14:18Z","name":"gas","endDate":"","dataItem":{"uid":"66056991EE23","Label":"gas"},"modified":"2010-06-28T17:40:35Z"}],"profile":{"uid":"B6FB1C01BAA7"}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/447C40EB29FB/home/energy/quantity")
@cat.total_amount.should be_close(31.7772, 1e-9)
@cat.total_amount_unit.should == "kg/year"
@cat.items.size.should be(1)
@cat.items[0][:uid].should == "B3D15D776EB9"
@cat.items[0][:name].should == "gas"
@cat.items[0][:path].should == "B3D15D776EB9"
@cat.items[0][:dataItemLabel].should == "gas"
@cat.items[0][:dataItemUid].should == "66056991EE23"
@cat.items[0][:startDate].should == DateTime.new(2010, 6, 28, 17, 14)
@cat.items[0][:endDate].should be_nil
@cat.items[0][:amount].should be_close(31.7772, 1e-9)
@cat.items[0][:amount_unit].should == "kg/year"
@cat.items[0][:values].size.should be(10)
@cat.items[0][:values][:energyConsumption][:value].should == "13"
@cat.items[0][:values][:energyConsumption][:unit].should == "kWh"
@cat.items[0][:values][:energyConsumption][:per_unit].should == "month"
@cat.items[0][:values][:massPerTime][:value].should == "0"
@cat.items[0][:values][:volumePerTime][:value].should == "0"
@cat.pager.should_not be_nil
end
it "should parse multiple return values" do
connection = flexmock "connection"
connection.should_receive(:version).and_return(2.0)
connection.should_receive(:get).with("/profiles/F8C2D1052BF7/home/energy/quantity", {}).and_return(flexmock(:body => '{"apiVersion":"2.0","pager":{"to":1,"lastPage":1,"nextPage":-1,"items":1,"start":0,"itemsFound":1,"requestedPage":1,"currentPage":1,"from":1,"itemsPerPage":10,"previousPage":-1},"profileCategories":[],"dataCategory":{"uid":"A92693A99BAD","dataCategory":{"uid":"30BA55A0C472","deprecated":false,"name":"Energy","path":"energy"},"deprecated":false,"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10},"created":"2007-07-27 08:30:44.0","name":"Quantity","path":"quantity","itemDefinition":{"uid":"212C818D8F16","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","name":"Energy Quantity","drillDown":"type","modified":"2007-07-27 08:30:44.0"},"modified":"2007-07-27 08:30:44.0"},"environment":{"uid":"5F5887BCF726","itemsPerFeed":10,"created":"2007-07-27 08:30:44.0","description":"","name":"AMEE","owner":"","path":"","itemsPerPage":10,"modified":"2007-07-27 08:30:44.0"},"totalAmount":{"unit":"kg/year","value":"31.7772"},"path":"/home/energy/quantity","profileItems":[{"amount":{"unit":"kg/year","value":31.7772},"uid":"3BCDB0BB2B96","startDate":"2010-07-16T10:44:00-04:00","itemValues":[{"itemValueDefinition":{"perUnit":"year","uid":"67B7476423C0","unit":"kWh","name":"Current Reading","fromData":false,"path":"currentReading","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"06F553CED9F7","startDate":"2010-07-16T10:44:00-04:00","unit":"kWh","name":"Current Reading","value":"0","path":"currentReading","displayPath":"currentReading","displayName":"Current Reading"},{"itemValueDefinition":{"perUnit":"year","uid":"068A0D6D03F7","unit":"kWh","name":"Energy Consumption","fromData":false,"path":"energyConsumption","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"month","uid":"4CF7691084DB","startDate":"2010-07-16T10:44:00-04:00","unit":"kWh","name":"Energy Consumption","value":"13","path":"energyConsumption","displayPath":"energyConsumption","displayName":"Energy Consumption"},{"itemValueDefinition":{"uid":"1740E500BDAB","choices":"true=true,false=false","name":"Includes Heating","fromData":false,"path":"includesHeating","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"1CA0D40208F0","startDate":"2010-07-16T10:44:00-04:00","unit":"","name":"Includes Heating","value":"false","path":"includesHeating","displayPath":"includesHeating","displayName":"Includes Heating"},{"itemValueDefinition":{"uid":"527AADFB3B65","name":"Season","fromData":false,"path":"season","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"1ACEB4784A42","startDate":"2010-07-16T10:44:00-04:00","unit":"","name":"Season","value":"","path":"season","displayPath":"season","displayName":"Season"},{"itemValueDefinition":{"uid":"63005554AE8A","name":"Green tariff","fromData":false,"path":"greenTariff","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"036262B5D69D","startDate":"2010-07-16T10:44:00-04:00","unit":"","name":"Green tariff","value":"","path":"greenTariff","displayPath":"greenTariff","displayName":"Green tariff"},{"itemValueDefinition":{"uid":"E0EFED6FD7E6","name":"Payment frequency","fromData":false,"path":"paymentFrequency","fromProfile":true,"valueDefinition":{"uid":"CCEB59CACE1B","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"text","valueType":"TEXT","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"","uid":"A568643CD1D7","startDate":"2010-07-16T10:44:00-04:00","unit":"","name":"Payment frequency","value":"","path":"paymentFrequency","displayPath":"paymentFrequency","displayName":"Payment frequency"},{"itemValueDefinition":{"perUnit":"year","uid":"CF12B3EBAD02","unit":"kg","name":"Mass Per Time","fromData":false,"path":"massPerTime","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"990F69A6BD48","startDate":"2010-07-16T10:44:00-04:00","unit":"kg","name":"Mass Per Time","value":"0","path":"massPerTime","displayPath":"massPerTime","displayName":"Mass Per Time"},{"itemValueDefinition":{"perUnit":"year","uid":"40AB7E90980C","unit":"kWh","name":"Last Reading","fromData":false,"path":"lastReading","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"A5BB73E6CBD2","startDate":"2010-07-16T10:44:00-04:00","unit":"kWh","name":"Last Reading","value":"0","path":"lastReading","displayPath":"lastReading","displayName":"Last Reading"},{"itemValueDefinition":{"perUnit":"year","uid":"324EC808AC30","unit":"L","name":"Volume Per Time","fromData":false,"path":"volumePerTime","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"DA93374B62C2","startDate":"2010-07-16T10:44:00-04:00","unit":"L","name":"Volume Per Time","value":"0","path":"volumePerTime","displayPath":"volumePerTime","displayName":"Volume Per Time"},{"itemValueDefinition":{"perUnit":"year","uid":"177D4642A07A","name":"Number of deliveries","fromData":false,"path":"deliveries","fromProfile":true,"valueDefinition":{"uid":"45433E48B39F","environment":{"uid":"5F5887BCF726"},"created":"2007-07-27 08:30:44.0","description":"","name":"amount","valueType":"DECIMAL","modified":"2007-07-27 08:30:44.0"},"drillDown":false},"perUnit":"year","uid":"FA09F1FF043D","startDate":"2010-07-16T10:44:00-04:00","unit":"","name":"Number of deliveries","value":"","path":"deliveries","displayPath":"deliveries","displayName":"Number of deliveries"}],"amounts":{"amount":[{"perUnit":"year","unit":"kg","default":"true","value":31.7772,"type":"CO2"},{"type":"CH4","unit":"kg","value":4.2}],"note":[{"type" : "comment","value" : "This is a text node"},{"type" : "comment","value" : "This is another text node"}]},"created":"2010-07-16T14:44:43Z","name":"gas","endDate":"","dataItem":{"uid":"66056991EE23","Label":"gas"},"modified":"2010-07-16T14:45:03Z"}],"profile":{"uid":"F8C2D1052BF7"}}'))
@cat = AMEE::Profile::Category.get(connection, "/profiles/F8C2D1052BF7/home/energy/quantity")
@cat.total_amount.should be_close(31.7772, 1e-4)
@cat.total_amount_unit.should == 'kg/year'
item = @cat.items[0]
item[:amounts].size.should be(2)
item[:amounts][0][:type].should == "CO2"
item[:amounts][0][:value].should be_close(31.7772, 1e-4)
item[:amounts][0][:unit].should == 'kg'
item[:amounts][0][:per_unit].should == 'year'
item[:amounts][0][:default].should be_true
item[:amounts][1][:type].should == "CH4"
item[:amounts][1][:value].should be_close(4.2, 1e-2)
item[:amounts][1][:unit].should == 'kg'
item[:amounts][1][:per_unit].should be_nil
item[:notes].size.should be(2)
item[:notes][0][:type].should == 'comment'
item[:notes][0][:value].should == 'This is a text node'
item[:notes][1][:type].should == 'comment'
item[:notes][1][:value].should == 'This is another text node'
end
#
# it "should fail gracefully with incorrect data" do
# connection = flexmock "connection"
# connection.should_receive(:get).with("/profiles/E54C5525AA3E", {}).and_return('{}'))
# lambda{AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E")}.should raise_error(AMEE::BadData)
# end
# it "parses recursive GET requests" do
# connection = flexmock "connection"
# connection.should_receive(:version).and_return(2.0)
# connection.should_receive(:get).with("/profiles/BE22C1732952/transport/car", {:recurse => true}).and_return('{"totalAmountPerMonth":"0","dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"profileDate":"200901","path":"/transport/car","profile":{"uid":"BE22C1732952"},"children":{"pager":{},"dataCategories":[{"dataCategory":{"modified":"2008-04-21 16:42:10.0","created":"2008-04-21 16:42:10.0","itemDefinition":{"uid":"C6BC60C55678"},"dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"uid":"883ADD27228F","environment":{"uid":"5F5887BCF726"},"path":"bands","name":"Bands"},"path":"/transport/car/bands"},{"totalAmountPerMonth":0.265,"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"},"path":"/transport/car/generic","children":{"dataCategories":[{"dataCategory":{"modified":"2008-09-23 12:18:03.0","created":"2008-09-23 12:18:03.0","itemDefinition":{"uid":"E6D0BB09578A"},"dataCategory":{"uid":"87E55DA88017","path":"generic","name":"Generic"},"uid":"417DD367E9AA","environment":{"uid":"5F5887BCF726"},"path":"electric","name":"Electric"},"path":"/transport/car/generic/electric"}],"profileItems":{"rows":[{"created":"2009-01-05 13:58:52.0","ecoDriving":"false","tyresUnderinflated":"false","dataItemLabel":"diesel, large","kmPerLitre":"0","distanceKmPerMonth":"1","end":"false","uid":"8450D6D97D2D","modified":"2009-01-05 13:59:05.0","airconFull":"false","dataItemUid":"4F6CBCEE95F7","validFrom":"20090101","amountPerMonth":"0.265","kmPerLitreOwn":"0","country":"","label":"ProfileItem","occupants":"-1","airconTypical":"true","path":"8450D6D97D2D","name":"","regularlyServiced":"true"}],"label":"ProfileItems"}}},{"dataCategory":{"modified":"2007-07-27 09:30:44.0","created":"2007-07-27 09:30:44.0","itemDefinition":{"uid":"07EBA32512DF"},"dataCategory":{"uid":"1D95119FB149","path":"car","name":"Car"},"uid":"95E76249584D","environment":{"uid":"5F5887BCF726"},"path":"specific","name":"Specific"},"path":"/transport/car/specific"}],"profileItems":{}}}'))
# cat = AMEE::Profile::Category.get(connection, "/profiles/BE22C1732952/transport/car", :recurse => true)
# cat.items.size.should == 0
# cat.children.size.should == 3
# cat.children[1][:name].should == "Generic"
# cat.children[1][:path].should == "generic"
# cat.children[1][:uid].should == "87E55DA88017"
# cat.children[1][:totalAmountPerMonth].should == 0.265
# cat.children[1][:children].size.should == 1
# cat.children[1][:children][0][:name].should == "Electric"
# cat.children[1][:children][0][:path].should == "electric"
# cat.children[1][:children][0][:uid].should == "417DD367E9AA"
# cat.children[1][:items].size.should == 1
# cat.children[1][:items][0][:amountPerMonth].should == 0.265
# cat.children[1][:items][0][:dataItemLabel].should == "diesel, large"
# cat.children[1][:items][0][:dataItemUid].should == "4F6CBCEE95F7"
# cat.children[1][:items][0][:values][:airconTypical].should == "true"
# cat.children[1][:items][0][:uid].should == "8450D6D97D2D"
# end
end
describe AMEE::Profile::Category, "with an authenticated connection" do
it "allows direct access to ProfileItems" do
end
it "should fail gracefully on other GET errors" do
connection = flexmock "connection"
connection.should_receive(:get).with("/profiles/E54C5525AA3E", {}).and_raise("unidentified error")
lambda{AMEE::Profile::Category.get(connection, "/profiles/E54C5525AA3E")}.should raise_error(AMEE::BadData)
end
end