From 9efd14ceacf0db8bc936762c8e89b51669c6776d Mon Sep 17 00:00:00 2001 From: Damon Davison Date: Tue, 1 Jan 2013 18:35:44 +0000 Subject: [PATCH] Adds tests for Order features - Created JSON fixtures for Order responses - Debugged camel case conversion error - Order.fields returns snake case fields --- lib/nacre/api/order_service_resource.rb | 2 +- .../api/order_service_resources/order.rb | 2 +- .../order_search_results_spec.rb | 33 +-- .../order_search_spec.rb | 2 +- .../api/order_service_resources/order_spec.rb | 20 +- spec/fixtures/json/order.json | 264 ++++++++++++++++++ spec/fixtures/json/order_search_result.json | 122 ++++++++ .../json/order_search_result_empty.json | 1 + 8 files changed, 414 insertions(+), 32 deletions(-) create mode 100644 spec/fixtures/json/order.json create mode 100644 spec/fixtures/json/order_search_result.json create mode 100644 spec/fixtures/json/order_search_result_empty.json diff --git a/lib/nacre/api/order_service_resource.rb b/lib/nacre/api/order_service_resource.rb index 949cb8a..420dda7 100644 --- a/lib/nacre/api/order_service_resource.rb +++ b/lib/nacre/api/order_service_resource.rb @@ -62,7 +62,7 @@ def self.find_many(id_set) def load_values(values) self.class.fields.each do |field| - self.public_send "#{field.to_s.underscore}=", values[field.to_s].to_openstruct + self.public_send "#{field.to_s}=", values[field.to_s.camelize(:lower)].to_openstruct end end diff --git a/lib/nacre/api/order_service_resources/order.rb b/lib/nacre/api/order_service_resources/order.rb index 0889c84..36efff1 100644 --- a/lib/nacre/api/order_service_resources/order.rb +++ b/lib/nacre/api/order_service_resources/order.rb @@ -32,7 +32,7 @@ class Order < OrderServiceResource ] def self.fields - FIELDS + FIELDS.map(&:to_s).map(&:underscore).map(&:to_sym) end fields.each do |attr| diff --git a/spec/api/order_service_resources/order_search_results_spec.rb b/spec/api/order_service_resources/order_search_results_spec.rb index 0b23d7e..91b435c 100644 --- a/spec/api/order_service_resources/order_search_results_spec.rb +++ b/spec/api/order_service_resources/order_search_results_spec.rb @@ -24,32 +24,27 @@ it 'results items should be an array of OpenStructs' do results.should be_a(Nacre::API::OrderSearchResults) - results.length.should == 7 - - results.items.first.orderId.should == 1000 - results.items.first.orderName.should == "Misc item without VAT" - results.items.first.SKU.should == "" - results.items.first.EAN.should be_nil - results.items.first.UPC.should be_nil - results.items.first.ISBN.should be_nil - results.items.first.stockTracked.should == false - results.items.first.salesChannelName.should == "Brightpearl" - results.items.first.createdOn.should == "2007-05-29T10:42:08.000Z" - results.items.first.updatedOn.should == "2007-09-08T14:42:45.000Z" - results.items.first.brightpearlCategoryCode.should == 1000 - results.items.first.orderGroupId.should == 0 + results.length.should == 3 + + results.items.first.orderId.should == 123456 + results.items.first.orderTypeId.should == 1 + results.items.first.contactId.should == 253 + results.items.first.orderStatusId.should == 4 + results.items.first.orderStockStatusId.should == 3 + results.items.first.createdOn.should == '2012-12-13T13:00:42.000Z' + results.items.first.createdById == '280' end it "should have the correct metadata" do - results.metadata.columns.length.should == 12 + results.metadata.columns.length.should == 7 results.metadata.firstResult.should == 1 - results.metadata.lastResult.should == 7 - results.metadata.resultsAvailable.should == 7 - results.metadata.resultsReturned.should == 7 + results.metadata.lastResult.should == 3 + results.metadata.resultsAvailable.should == 3 + results.metadata.resultsReturned.should == 3 end it "should return the id set of the search results" do - results.id_set.should =~ Array(1000..1006) + results.id_set.should =~ [123456,123457,123458] end end end diff --git a/spec/api/order_service_resources/order_search_spec.rb b/spec/api/order_service_resources/order_search_spec.rb index c9bfa03..e72796b 100644 --- a/spec/api/order_service_resources/order_search_spec.rb +++ b/spec/api/order_service_resources/order_search_spec.rb @@ -13,7 +13,7 @@ response = mock("response") response.stub(:body).and_return(response_json) connection.should_receive(:get). - with(search_url). + with(search_url + '?pageSize=200&firstResult=1'). any_number_of_times. and_return(response) end diff --git a/spec/api/order_service_resources/order_spec.rb b/spec/api/order_service_resources/order_spec.rb index 1736eb2..6196d42 100644 --- a/spec/api/order_service_resources/order_spec.rb +++ b/spec/api/order_service_resources/order_spec.rb @@ -52,7 +52,7 @@ response = mock("response") response.stub(:body).and_return(response_json) connection.should_receive(:get). - with("/order-service/order/1"). + with("/order-service/order/123456"). any_number_of_times. and_return(response) end @@ -61,7 +61,7 @@ let(:response_json) { { "response" => [] }.to_json } it 'should return nil' do - Nacre::API::Order.find(1008).should be_nil + Nacre::API::Order.find(123456).should be_nil end end @@ -70,11 +70,11 @@ let(:response_json) { IO.read("spec/fixtures/json/order.json") } it 'should return a valid Order' do - order = Nacre::API::Order.find(1008) + order = Nacre::API::Order.find(123456) order.should be_a(Nacre::API::Order) - order.id.should == 1008 - order.identity["sku"].should == "SKU0001" - #order.orderTypeId.should == 1 #TODO uncamelize this + order.id.should == 123456 + order.reference.should == 'order#001' + order.currency.accounting_currency_code.should == 'GBP' end end end @@ -101,7 +101,7 @@ response = mock("response") response.stub(:body).and_return(response_json) connection.should_receive(:get). - with("/order-service/order/1000,1001,1002,1003,1004,1005,1006"). + with("/order-service/order/123456,123457,123458"). and_return(response) orders = Nacre::API::Order.all @@ -110,9 +110,9 @@ order = orders.first order.should be_a(Nacre::API::Order) - order.id.should == 1008 - order.identity["sku"].should == "SKU0001" - #order.orderTypeId.should == 1 #TODO uncamelize this + order.id.should == 123456 + order.reference.should == 'order#001' + order.currency.accounting_currency_code.should == 'GBP' end end end diff --git a/spec/fixtures/json/order.json b/spec/fixtures/json/order.json new file mode 100644 index 0000000..5936e36 --- /dev/null +++ b/spec/fixtures/json/order.json @@ -0,0 +1,264 @@ +{ + "response": [ + { + "id": 123456, + "parentOrderId" : 123455, + "orderTypeCode": "PO", + "reference": "order#001", + "acknowledged": 1, + "orderStatus": { + "orderStatusId": 6, + "name": "Pending PO" + }, + "stockStatusCode": "NON", + "allocationStatusCode": "ANR", + "placedOn": "2011-06-29T11:12:24.000+01:00", + "createdOn": "2011-06-29T11:12:24.000+01:00", + "createdById": 200, + "priceListId": 1, + "priceModeCode": "EXC", + "delivery": { + "deliveryDate": "2011-07-08T00:00:00.000+01:00", + "shippingMethodId": 1 + }, + "invoices": [ + { + "invoiceReference": "", + "taxDate": "2011-06-29T00:00:00.000+01:00", + "dueDate": "2011-07-29T00:00:00.000+01:00" + } + ], + "currency": { + "accountingCurrencyCode": "GBP", + "orderCurrencyCode": "GBP", + "exchangeRate": "1.000000" + }, + "totalValue": { + "net": "50.00", + "taxAmount": "10.00" + }, + "assignment": { + "current": { + "staffOwnerContactId": 200, + "projectId": 1, + "channelId": 2, + "leadSourceId": 4, + "teamId": 2 + } + }, + "parties": { + "supplier": { + "contactId": 901, + "addressFullName": "Mr Drink", + "companyName": "Drink Supplies", + "addressLine1": "1 Main Road", + "addressLine2": "Out Of Town", + "addressLine3": "Bristol", + "addressLine4": "Avon", + "postalCode": "bs99 9sb", + "country": "United Kingdom", + "telephone": "01234 987654", + "mobileTelephone": "", + "email": "mr.drink@drinksupplies.com" + }, + "delivery": { + "addressFullName": "", + "companyName": "wacCorp", + "addressLine1": "Brightpearl HQ", + "addressLine2": "", + "addressLine3": "Bristol", + "addressLine4": "", + "postalCode": "BS2 9AG", + "country": "United Kingdom", + "telephone": "01234 567890", + "mobileTelephone": "", + "email": "will@waccorp.com" + }, + "billing": { + "contactId": 901, + "addressFullName": "", + "companyName": "wacCorp", + "addressLine1": "Brightpearl HQ", + "addressLine2": "", + "addressLine3": "Bristol", + "addressLine4": "", + "postalCode": "BS2 9AG", + "country": "United Kingdom", + "telephone": "01234 567890", + "mobileTelephone": "", + "email": "will@waccorp.com" + } + }, + "orderRows": { + "152": { + "orderRowSequence": "5", + "productId": 1011, + "productName": "Product C", + "productSku": "SKU1000", + "quantity": { + "magnitude": "2.00" + }, + "rowValue": { + "taxRate": "20.00", + "taxCode": "T20", + "rowNet": { + "currencyCode": "GBP", + "value": "0.0000" + }, + "rowTax": { + "currencyCode": "GBP", + "value": "0.00" + } + }, + "productOptions": { + "Size": "Medium" + }, + "nominalCode": "4000", + "composition": { + "bundleParent": false, + "bundleChild": true, + "parentOrderRowId": 149 + } + }, + "147": { + "orderRowSequence": "0", + "productId": 1008, + "productName": "Product B", + "productSku": "SKU0001", + "quantity": { + "magnitude": "1.00" + }, + "rowValue": { + "taxRate": "20.00", + "taxCode": "T20", + "rowNet": { + "currencyCode": "GBP", + "value": "83.3333" + }, + "rowTax": { + "currencyCode": "GBP", + "value": "16.67" + } + }, + "nominalCode": "4000", + "composition": { + "bundleParent": false, + "bundleChild": false, + "parentOrderRowId": 0 + } + }, + "148": { + "orderRowSequence": "1", + "productId": 1009, + "productName": "Product C", + "productSku": "SKU1000", + "quantity": { + "magnitude": "5.00" + }, + "rowValue": { + "taxRate": "20.00", + "taxCode": "T20", + "rowNet": { + "currencyCode": "GBP", + "value": "8.3333" + }, + "rowTax": { + "currencyCode": "GBP", + "value": "8.33" + } + }, + "productOptions": { + "Size": "Large" + }, + "nominalCode": "4000", + "composition": { + "bundleParent": false, + "bundleChild": false, + "parentOrderRowId": 0 + } + }, + "149": { + "orderRowSequence": "2", + "productId": 1014, + "productName": "Bundle", + "productSku": "", + "quantity": { + "magnitude": "1.00" + }, + "rowValue": { + "taxRate": "20.00", + "taxCode": "T20", + "rowNet": { + "currencyCode": "GBP", + "value": "12.5000" + }, + "rowTax": { + "currencyCode": "GBP", + "value": "2.50" + } + }, + "nominalCode": "4000", + "composition": { + "bundleParent": true, + "bundleChild": false, + "parentOrderRowId": 0 + } + }, + "150": { + "orderRowSequence": "3", + "productId": 1008, + "productName": "Product B", + "productSku": "SKU0001", + "quantity": { + "magnitude": "5.00" + }, + "rowValue": { + "taxRate": "20.00", + "taxCode": "T20", + "rowNet": { + "currencyCode": "GBP", + "value": "0.0000" + }, + "rowTax": { + "currencyCode": "GBP", + "value": "0.00" + } + }, + "nominalCode": "4000", + "composition": { + "bundleParent": false, + "bundleChild": true, + "parentOrderRowId": 149 + } + }, + "151": { + "orderRowSequence": "4", + "productId": 1010, + "productName": "Product D", + "productSku": "", + "quantity": { + "magnitude": "3.00" + }, + "rowValue": { + "taxRate": "20.00", + "taxCode": "T20", + "rowNet": { + "currencyCode": "GBP", + "value": "0.0000" + }, + "rowTax": { + "currencyCode": "GBP", + "value": "0.00" + } + }, + "nominalCode": "4000", + "composition": { + "bundleParent": false, + "bundleChild": true, + "parentOrderRowId": 149 + } + } + } + } + ] +} diff --git a/spec/fixtures/json/order_search_result.json b/spec/fixtures/json/order_search_result.json new file mode 100644 index 0000000..20a7e5c --- /dev/null +++ b/spec/fixtures/json/order_search_result.json @@ -0,0 +1,122 @@ +{ + "response":{ + "metaData":{ + "resultsAvailable":3, + "resultsReturned":3, + "firstResult":1, + "lastResult":3, + "columns":[ + { + "name":"orderId", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "required":false + }, + { + "name":"orderTypeId", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "referenceData":[ + "orderTypeNames" + ], + "required":false + }, + { + "name":"contactId", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "required":false + }, + { + "name":"orderStatusId", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "referenceData":[ + "orderStatusNames" + ], + "required":false + }, + { + "name":"orderStockStatusId", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "referenceData":[ + "orderStockStatusNames" + ], + "required":false + }, + { + "name":"createdOn", + "sortable":true, + "filterable":true, + "reportDataType":"PERIOD", + "required":false + }, + { + "name":"createdById", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "required":false + } + ], + "sorting":[ + { + "filterable":{ + "name":"orderId", + "sortable":true, + "filterable":true, + "reportDataType":"INTEGER", + "required":false + }, + "direction":"ASC" + } + ] + }, + "results":[ + [ + 123456, + 1, + 253, + 4, + 3, + "2012-12-13T13:00:42.000Z", + 280 + ], + [ + 123457, + 1, + 253, + 4, + 3, + "2012-12-14T13:00:42.000Z", + 280 + ], + [ + 123458, + 1, + 253, + 4, + 3, + "2012-12-14T14:00:42.000Z", + 280 + ] + ] + }, + "reference":{ + "orderTypeNames":{ + "1":"SALES_ORDER" + }, + "orderStatusNames":{ + "4":"Invoiced" + }, + "orderStockStatusNames":{ + "3":"All fulfilled" + } + } +} diff --git a/spec/fixtures/json/order_search_result_empty.json b/spec/fixtures/json/order_search_result_empty.json new file mode 100644 index 0000000..51efcbd --- /dev/null +++ b/spec/fixtures/json/order_search_result_empty.json @@ -0,0 +1 @@ +{"response":{"metaData":{"resultsAvailable":0,"resultsReturned":0,"columns":[{"name":"orderId","sortable":true,"filterable":true,"reportDataType":"INTEGER","required":false},{"name":"orderTypeId","sortable":true,"filterable":true,"reportDataType":"INTEGER","referenceData":["orderTypeNames"],"required":false},{"name":"contactId","sortable":true,"filterable":true,"reportDataType":"INTEGER","required":false},{"name":"orderStatusId","sortable":true,"filterable":true,"reportDataType":"INTEGER","referenceData":["orderStatusNames"],"required":false},{"name":"orderStockStatusId","sortable":true,"filterable":true,"reportDataType":"INTEGER","referenceData":["orderStockStatusNames"],"required":false},{"name":"createdOn","sortable":true,"filterable":true,"reportDataType":"PERIOD","required":false},{"name":"createdById","sortable":true,"filterable":true,"reportDataType":"INTEGER","required":false}],"sorting":[{"filterable":{"name":"orderId","sortable":true,"filterable":true,"reportDataType":"INTEGER","required":false},"direction":"ASC"}]},"results":[]},"reference":{}}