Skip to content

Commit 7c4eba2

Browse files
Add tests for ISO date string handling in OData queries and stores
- Implement tests to verify that ISO date strings are correctly serialized as DateTimeOffset and DateTime based on fieldTypes in OData queries. - Add tests to ensure ISO date strings remain as strings when fieldTypes indicate String. - Include tests for ODataStore and ODataContext to confirm that date properties are treated as strings when deserialization is disabled, and as Date objects when enabled.
1 parent 061adc3 commit 7c4eba2

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

packages/devextreme/testing/tests/DevExpress.data/odataQuery.tests.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,81 @@ QUnit.test('Values are converted according to \'fieldTypes\' property', function
930930
.always(done);
931931
});
932932

933+
QUnit.test('ISO date string serialized as date when fieldTypes indicate DateTimeOffset (v4)', function(assert) {
934+
assert.expect(1);
935+
936+
const done = assert.async();
937+
ajaxMock.setup({
938+
url: 'odata.org',
939+
callback: function(bag) { this.responseText = { value: [bag] }; }
940+
});
941+
942+
const iso = '1945-05-09T14:25:01.001Z';
943+
const expectedRe = /^date eq \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
944+
945+
QUERY('odata.org', { version: 4, fieldTypes: { date: 'DateTimeOffset' } })
946+
.filter(['date', '=', iso])
947+
.enumerate()
948+
.fail(function() { assert.ok(false, MUST_NOT_REACH_MESSAGE); })
949+
.done(function(r) { assert.ok(expectedRe.test(r[0].data['$filter'])); })
950+
.always(done);
951+
});
952+
953+
QUnit.test('ISO date string serialized as date when fieldTypes indicate DateTime (v2)', function(assert) {
954+
assert.expect(1);
955+
956+
const done = assert.async();
957+
ajaxMock.setup({
958+
url: 'odata.org',
959+
callback: function(bag) { this.responseText = { value: [bag] }; }
960+
});
961+
962+
const iso = '1945-05-09T14:25:01.001Z';
963+
const expectedRe = /^date eq datetime'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}'$/;
964+
965+
QUERY('odata.org', {
966+
version: 2,
967+
fieldTypes: { date: 'DateTime' }
968+
})
969+
.filter(['date', '=', iso])
970+
.enumerate()
971+
.fail(function() { assert.ok(false, MUST_NOT_REACH_MESSAGE); })
972+
.done(function(r) { assert.ok(expectedRe.test(r[0].data['$filter'])); })
973+
.always(done);
974+
});
975+
976+
QUnit.test('ISO date string remains string when fieldTypes indicate String (v2/v4)', function(assert) {
977+
assert.expect(2);
978+
979+
const done = assert.async();
980+
981+
const p1 = QUERY('odata.org', {
982+
version: 4,
983+
fieldTypes: { date: 'String' }
984+
})
985+
.filter(['date', '=', '1945-05-09T14:25:01.001Z'])
986+
.enumerate()
987+
.done(function(r) {
988+
assert.equal(r[0].data['$filter'], 'date eq \'1945-05-09T14:25:01.001Z\'');
989+
});
990+
991+
const p2 = QUERY('odata.org', {
992+
version: 2,
993+
fieldTypes: { date: 'String' }
994+
})
995+
.filter(['date', '=', '1945-05-09T14:25:01.001Z'])
996+
.enumerate()
997+
.done(function(r) {
998+
assert.equal(r[0].data['$filter'], 'date eq \'1945-05-09T14:25:01.001Z\'');
999+
});
1000+
1001+
$.when.apply($, [p1, p2])
1002+
.fail(function() {
1003+
assert.ok(false, MUST_NOT_REACH_MESSAGE);
1004+
})
1005+
.always(done);
1006+
});
1007+
9331008
QUnit.module('Server side capabilities', moduleConfig);
9341009
QUnit.test('can be done on server: any number of sort and filter before slice, first select, first slice', function(assert) {
9351010
assert.expect(2 * 5);

packages/devextreme/testing/tests/DevExpress.data/odataStore.tests.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,39 @@ QUnit.test('Dates, disableable, ODataStore', function(assert) {
17511751
.always(done);
17521752
});
17531753

1754+
QUnit.test('Dates, default disabled, ODataStore', function(assert) {
1755+
assert.expect(2);
1756+
1757+
const done = assert.async();
1758+
1759+
ajaxMock.setup({
1760+
url: 'odata.org',
1761+
responseText: { value: [{ dateProperty: '1945-05-09T14:25:12.1234567Z' }] }
1762+
});
1763+
1764+
ajaxMock.setup({
1765+
url: 'odata.org(1)',
1766+
responseText: { dateProperty: '1945-05-09T14:25:12.1234567Z' }
1767+
});
1768+
1769+
const store = new ODataStore({ version: 4, url: 'odata.org' });
1770+
const promises = [
1771+
store.load()
1772+
.done(function(r) {
1773+
assert.strictEqual(typeof r[0].dateProperty, 'string');
1774+
}),
1775+
1776+
store.byKey(1)
1777+
.done(function(r) {
1778+
assert.strictEqual(typeof r.dateProperty, 'string');
1779+
})
1780+
];
1781+
1782+
$.when.apply($, promises)
1783+
.fail(function() { assert.ok(false, MUST_NOT_REACH_MESSAGE); })
1784+
.always(done);
1785+
});
1786+
17541787
QUnit.test('Dates, disableable, ODataContext', function(assert) {
17551788
assert.expect(4);
17561789

@@ -1808,6 +1841,62 @@ QUnit.test('Dates, disableable, ODataContext', function(assert) {
18081841
.always(done);
18091842
});
18101843

1844+
QUnit.test('Dates, default disabled, ODataContext', function(assert) {
1845+
assert.expect(4);
1846+
1847+
const done = assert.async();
1848+
1849+
ajaxMock.setup({
1850+
url: 'odata.org/name',
1851+
responseText: { value: [{ dateProperty: '1945-05-09T14:25:12.1234567Z' }] }
1852+
});
1853+
1854+
ajaxMock.setup({
1855+
url: 'odata.org/function()',
1856+
responseText: { dateProperty: '1945-05-09T14:25:12.1234567Z' }
1857+
});
1858+
1859+
ajaxMock.setup({
1860+
url: 'odata.org/action',
1861+
responseText: { dateProperty: '1945-05-09T14:25:12.1234567Z' }
1862+
});
1863+
1864+
const ctx = new ODataContext({
1865+
version: 4,
1866+
url: 'odata.org',
1867+
entities: {
1868+
'X': { name: 'name' },
1869+
'Y': { name: 'name', deserializeDates: true }
1870+
}
1871+
});
1872+
1873+
const promises = [
1874+
ctx.get('function')
1875+
.done(function(r) {
1876+
assert.strictEqual(typeof r.dateProperty, 'string');
1877+
}),
1878+
1879+
ctx.invoke('action')
1880+
.done(function(r) {
1881+
assert.strictEqual(typeof r.dateProperty, 'string');
1882+
}),
1883+
1884+
ctx.X.load()
1885+
.done(function(r) {
1886+
assert.strictEqual(typeof r[0].dateProperty, 'string');
1887+
}),
1888+
1889+
ctx.Y.load()
1890+
.done(function(r) {
1891+
assert.ok(isDate(r[0].dateProperty));
1892+
})
1893+
];
1894+
1895+
$.when.apply($, promises)
1896+
.fail(function() { assert.ok(false, MUST_NOT_REACH_MESSAGE); })
1897+
.always(done);
1898+
});
1899+
18111900
QUnit.module('JSONP support', moduleConfig);
18121901
QUnit.test('load()', function(assert) {
18131902
const done = assert.async();

0 commit comments

Comments
 (0)