odata-client/spec/service-spec.js

85 строки
2.3 KiB
JavaScript

'use strict';
const request = require('../request');
const Odata = require('../index');
describe('batch tests', function() {
var config;
var q;
beforeAll(function(done) {
request.getAsync('http://services.odata.org/V4/(S(readwrite))/OData/OData.svc/')
.then(function(response) {
var body = JSON.parse(response.body);
var r = /(.*)\$metadata/.exec(body['@odata.context']);
if(r) {
config = {
service: r[1],
format: 'json'
};
}
})
.finally(done);
});
beforeEach(function() {
q = Odata(config);
});
it('should retrieve price for item ID 6', function(done) {
q.resource('Products', 6).select('Price').get()
.then(function(response) {
var body = JSON.parse(response.body);
expect(body.Price).toEqual(18.8);
})
.catch(function(err) {
fail(err);
})
.finally(done);
});
it('should change price for item ID 6', function(done) {
q.resource('Products', 6).patch({'@odata.type': 'ODataDemo.Product', Price: 20.0}).
then(function(response) {
expect(response.statusCode).toEqual(204);
q = Odata(config);
return q.resource('Products', 6).get();
})
.then(function(response) {
var body = JSON.parse(response.body);
expect(body.Price).toEqual(20.0);
})
.catch(function(err) {
fail(err);
})
.finally(done);
});
it('should change price for items 4 and 5 using a batch', function(done) {
q.batch()
.resource('Products', 4).patch({'@odata.type': 'ODataDemo.Product', Price: 14.4}, {content_id: 1})
.resource('Products', 5).patch({'@odata.type': 'ODataDemo.Product', Price: 23.2}, {content_id: 2});
q.send()
.then(function(response) {
q = Odata(config);
return q.resource('Products', 4).select('Price').get()
})
.then(function(response) {
var body = JSON.parse(response.body);
expect(body.Price).toEqual(14.4);
q = Odata(config);
return q.resource('Products', 5).select('Price').get()
})
.then(function(response) {
var body = JSON.parse(response.body);
expect(body.Price).toEqual(23.2);
})
.catch(function(err) {
fail(err);
})
.finally(done);
});
});