Repository: olingo-odata4-js Updated Branches: refs/heads/master 199d1fcb9 -> f9aaa8149
[OLINGO-316] Add first test for odata-json-tests, start with grunt test-node Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/commit/f9aaa814 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/tree/f9aaa814 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/diff/f9aaa814 Branch: refs/heads/master Commit: f9aaa8149dcffb39ab22966e5266af1d7167f479 Parents: 199d1fc Author: Sven Kobler <[email protected]> Authored: Fri Jun 6 14:00:52 2014 +0200 Committer: Sven Kobler <[email protected]> Committed: Fri Jun 6 14:00:52 2014 +0200 ---------------------------------------------------------------------- datajs/Gruntfile.js | 85 ++++++++++++++---------------- datajs/package.json | 14 +---- datajs/src/lib/odata.js | 1 + datajs/src/lib/odata/json.js | 6 +-- datajs/tests/common/djstest.js | 46 ++++++++++------ datajs/tests/common/mockHttpClient.js | 22 +++++--- datajs/tests/node-test-setup.js | 12 +++++ datajs/tests/odata-json-tests.js | 21 ++++---- datajs/tests/odata-qunit-tests.htm | 2 +- 9 files changed, 117 insertions(+), 92 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/Gruntfile.js ---------------------------------------------------------------------- diff --git a/datajs/Gruntfile.js b/datajs/Gruntfile.js index 6fdabdc..1c6d369 100644 --- a/datajs/Gruntfile.js +++ b/datajs/Gruntfile.js @@ -5,7 +5,9 @@ module.exports = function(grunt) { banner: grunt.file.read('src/banner.txt'), filename : '<%= pkg.name %>-<%= pkg.version %>', + browserify: { + // start with index.js and follow all required source in order pack them together datajs: { files: { 'build/<%= filename %>.js': ['src/index.js'], @@ -24,44 +26,13 @@ module.exports = function(grunt) { sourceMapName : 'build/<%= filename %>.map', sourceMapIncludeSources :true, }, + // uglify and compress the packed sources build: { src: 'build/<%= filename %>.js', dest: 'build/<%= filename %>.min.js' } }, - copy: { - saveOrig : { - files: [ - // includes files within path - {expand: false, flatten: true,src: './build/<%= filename %>.js', dest: 'demo/jscripts/<%= filename %>.bu_js'}, - ] - }, - toDemo: { - files: [ - // includes files within path - {expand: false, flatten: true,src: './build/<%= filename %>.js', dest: 'demo/jscripts/<%= filename %>.js'}, - {expand: false, flatten: true,src: './build/<%= filename %>.min.js', dest: 'demo/jscripts/<%= filename %>.min.js'}, - {expand: false, flatten: true,src: './build/<%= filename %>.map', dest: 'demo/jscripts/<%= filename %>.map'}, - {expand: false, flatten: true,src: './build/<%= filename %>.split_map', dest: 'demo/jscripts/<%= filename %>.split_map'}, - ] - } - }, connect: { - proxies: [{ - context: "/tests/endpoints/", // When the url contains this... - host: "localhost", - changeOrigin: true, - https: false, - port: 46541, - rejectUnauthorized: false, - }/*,{ - context: "/tests/common/", // When the url contains this... - host: "localhost", - changeOrigin: true, - https: false, - port: 46541, - rejectUnauthorized: false, - }*/], demo: { options: { port: 4001 , @@ -77,7 +48,8 @@ module.exports = function(grunt) { }, }, }, - test: { + // start a node webserver with proxy to host the qunit-test html files + 'test-browser': { options: { port: 4002 , hostname: "localhost", @@ -92,30 +64,55 @@ module.exports = function(grunt) { ]; }, }, + // proxy all request going to /tests/endpoints/ to the .net data services + proxies: [{ + context: "/tests/endpoints/", // When the url contains this... + host: "localhost", + changeOrigin: true, + https: false, + port: 46541, + rejectUnauthorized: false, + }], }, }, - open: { - demo: { - path: "http://<%= connect.demo.options.hostname %>:<%= connect.demo.options.port %>/demo.html", - options: { - delay : 500, + 'node-qunit': { + //used to run some background qunit test on node + 'default-tests': { + setup: { + log: { + summary: true, + assertions: true, + errors: true, + globalSummary: true, + coverage: false, + globalCoverage: false, + testing: true + }, + coverage: false, + deps: null, + namespace: null + }, + deps: '', + code: './tests/node-test-setup.js', + tests: ['./tests/odata-json-tests.js'], + done: function(err, res){ + !err && publishResults("node", res, this.async()); } - } + }, }, }); // These plugins provide necessary tasks. grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks("grunt-connect-proxy"); grunt.loadNpmTasks("grunt-contrib-connect"); - + grunt.loadNpmTasks('grunt-node-qunit'); // Default task. - grunt.registerTask('build', ['browserify:datajs', 'copy:toDemo', "uglify:build"]); - grunt.registerTask('run', ['configureProxies', 'connect:demo']); - grunt.registerTask('test', ['configureProxies', 'connect:test']); + grunt.registerTask('build', ['browserify:datajs', 'uglify:build']); + grunt.registerTask('test-browser', ['configureProxies:test-browser', 'connect:test-browser']); + grunt.registerTask('test-node', ['node-qunit:default-tests']); }; http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/package.json ---------------------------------------------------------------------- diff --git a/datajs/package.json b/datajs/package.json index 97017cf..a45f2e8 100644 --- a/datajs/package.json +++ b/datajs/package.json @@ -14,22 +14,14 @@ "contributors": [ { "name": "Bing Li", - "email": "[email protected]" + "email": "[email protected]" }, { "name": "Sven Kobler-Morris", "email": "[email protected]" } ], - "testling": { - "harness": "mocha-tdd", - "files": "test/sampletest_mocca.js", - "browsers": [ - "ie/8" - ] - }, "scripts": { - "test": "mocha --ui tdd" }, "devDependencies": { "browserify": "^4.1.5", @@ -37,9 +29,7 @@ "grunt-browserify": "^2.1.0", "grunt-connect-proxy": "^0.1.10", "grunt-contrib-connect": "^0.7.1", - "grunt-contrib-copy": "^0.5.0", "grunt-contrib-uglify": "^0.4.0", - "grunt-mocha": "^0.4.10", - "grunt-open": "^0.2.3" + "grunt-node-qunit": "^2.0.2" } } http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/src/lib/odata.js ---------------------------------------------------------------------- diff --git a/datajs/src/lib/odata.js b/datajs/src/lib/odata.js index 857c90c..7c44a19 100644 --- a/datajs/src/lib/odata.js +++ b/datajs/src/lib/odata.js @@ -22,6 +22,7 @@ exports.utils = odataUtils = require('./odata/utils.js'); exports.handler = odataHandler = require('./odata/handler.js'); exports.metadata = odataMetadata = require('./odata/metadata.js'); exports.net = odataNet = require('./odata/net.js'); +//exports.net = odataNet = require('./odata/net-nodejs.js');//testing only exports.gml = require('./odata/gml.js'); exports.jsonLight = require('./odata/json-light.js'); exports.json = odataJson = require('./odata/json.js'); http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/src/lib/odata/json.js ---------------------------------------------------------------------- diff --git a/datajs/src/lib/odata/json.js b/datajs/src/lib/odata/json.js index a4aeea7..5265c0e 100644 --- a/datajs/src/lib/odata/json.js +++ b/datajs/src/lib/odata/json.js @@ -243,7 +243,7 @@ var jsonParser = function (handler, text, context) { /// <returns>An object representation of the OData payload.</returns> var dataServiceVersion = context.dataServiceVersion; - var json = (typeof text === "string") ? window.JSON.parse(text) : text; + var json = (typeof text === "string") ? JSON.parse(text) : text; if ((maxVersion("4.0", dataServiceVersion) === dataServiceVersion)) { return json; @@ -265,7 +265,7 @@ var jsonToString = function (data) { Date.prototype.toJSON = function () { return formatDateTimeOffset(this); }; - result = window.JSON.stringify(data, jsonReplacer); + result = JSON.stringify(data, jsonReplacer); } finally { // Restore the original toJSON function Date.prototype.toJSON = dateToJSON; @@ -287,7 +287,7 @@ var jsonSerializer = function (handler, data, context) { context.dataServiceVersion = maxVersion(dataServiceVersion, "4.0"); var newdata = formatJsonLightRequestPayload(data); if (newdata) { - return window.JSON.stringify(newdata); + return JSON.stringify(newdata); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/tests/common/djstest.js ---------------------------------------------------------------------- diff --git a/datajs/tests/common/djstest.js b/datajs/tests/common/djstest.js index ab52567..554b064 100644 --- a/datajs/tests/common/djstest.js +++ b/datajs/tests/common/djstest.js @@ -17,10 +17,9 @@ * under the License. */ -(function (window, undefined) { +var init = function (window) { var djstest = {}; - window.djstest = djstest; djstest.indexedDB = window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.indexedDB; @@ -397,19 +396,32 @@ // Disable caching to ensure that every test-related AJAX request is actually being sent, // and set up a default error handler - $.ajaxSetup({ - cache: false, - error: function (jqXHR, textStatus, errorThrown) { - // Work around bug in IE-Mobile on Windows Phone 7 - if (jqXHR.status !== 1223) { - var err = { - status: jqXHR.status, - statusText: jqXHR.statusText, - responseText: jqXHR.responseText - }; - djstest.fail("AJAX request failed with: " + djstest.toString(err)); + if (window !== undefined) {//TODO improve + /*$.ajaxSetup({ + cache: false, + error: function (jqXHR, textStatus, errorThrown) { + // Work around bug in IE-Mobile on Windows Phone 7 + if (jqXHR.status !== 1223) { + var err = { + status: jqXHR.status, + statusText: jqXHR.statusText, + responseText: jqXHR.responseText + }; + djstest.fail("AJAX request failed with: " + djstest.toString(err)); + } + djstest.done(); } - djstest.done(); - } - }); -})(window); \ No newline at end of file + });*/ + } + + return djstest; +}; + +if (typeof window !== 'undefined') { + //in browser call init() directly window as context + window.djstest = init(window); +} else { + //expose function init to be called with a custom context + module.exports.init = init; +} + http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/tests/common/mockHttpClient.js ---------------------------------------------------------------------- diff --git a/datajs/tests/common/mockHttpClient.js b/datajs/tests/common/mockHttpClient.js index faea931..d1380f6 100644 --- a/datajs/tests/common/mockHttpClient.js +++ b/datajs/tests/common/mockHttpClient.js @@ -44,12 +44,9 @@ // MockHttpClient will throw an exception if it receives a request to a URI that is not mapped to either a request verifier or a response. // -(function (window, undefined) { - if (!window.MockHttpClient) { - window.MockHttpClient = {}; - } +var init = function (window, undefined) { - var httpClient = window.MockHttpClient; + var httpClient = {}; var responses = {}; var requestVerifiers = {}; @@ -123,4 +120,17 @@ this.async = value; return this; }; -})(this); + + return httpClient; +}; + + + +if (typeof window !== 'undefined') { + //in browser call init() directly window as context + window.MockHttpClient = init(window); +} else { + //expose function init to be called with a custom context + module.exports.init = init; +} + http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/tests/node-test-setup.js ---------------------------------------------------------------------- diff --git a/datajs/tests/node-test-setup.js b/datajs/tests/node-test-setup.js new file mode 100644 index 0000000..2a94b81 --- /dev/null +++ b/datajs/tests/node-test-setup.js @@ -0,0 +1,12 @@ +//Creates the global objects + +//tools +djstest = require("./common/djstest.js").init({}); +MockHttpClient = require("./common/MockHttpClient.js").init({}); + +//lib +datajs = require('./../src/lib/datajs.js'); +OData = require('./../src/lib/odata.js'); + + + http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/tests/odata-json-tests.js ---------------------------------------------------------------------- diff --git a/datajs/tests/odata-json-tests.js b/datajs/tests/odata-json-tests.js index b97bbd7..ebb7c6a 100644 --- a/datajs/tests/odata-json-tests.js +++ b/datajs/tests/odata-json-tests.js @@ -20,8 +20,8 @@ // odata-tests.js (function (window, undefined) { - - // DATAJS INTERNAL START + + djstest.addTest(function isArrayTest() { djstest.assert(datajs.utils.isArray([])); djstest.assert(datajs.utils.isArray([1, 2])); @@ -192,7 +192,7 @@ var i, len; for (i = 0, len = tests.length; i < len; i++) { - var data = window.JSON.stringify(tests[i].expected); + var data = JSON.stringify(tests[i].expected); var actual = OData.json.jsonParser(OData.json.jsonHandler, data, tests[i].context); djstest.assertAreEqualDeep(actual, tests[i].expected, "test " + i + "didn't return the expected data"); } @@ -834,7 +834,7 @@ for (i = 0, len = tests.length; i < len; i++) { var data = tests[i].data ? tests[i].data : tests[i].expected; var actual = OData.json.jsonSerializer(OData.json.jsonHandler, data, tests[i].context); - var expected = window.JSON.stringify(tests[i].expected); + var expected = JSON.stringify(tests[i].expected); djstest.assertAreEqualDeep(actual, expected, "test " + i + "didn't return the expected data"); } djstest.done(); @@ -843,7 +843,9 @@ djstest.addTest(function normalizeHeadersReadTest() { // Verifies that headers are normalized for reading. // See issue at http://datajs.codeplex.com/workitem/148 - window.MockHttpClient.clear().addResponse("/foo", { + MockHttpClient.clear(); + + MockHttpClient.clear().addResponse("/foo", { statusCode: 200, body: { "@odata.context": "http://foo", value: [] }, headers: { "unknown": "u", "Content-Encoding": "compress, gzip", "Content-Length": "8042", @@ -869,9 +871,11 @@ }); djstest.addTest(function normalizeHeadersWriteTest() { + // Verifies that headers are normalized for writing. // See issue at http://datajs.codeplex.com/workitem/148 - window.MockHttpClient.clear().addRequestVerifier("/foo", function (request) { + + MockHttpClient.clear().addRequestVerifier("/foo", function (request) { djstest.assertAreEqual(request.headers.Accept, "application/json", "Accept available"); djstest.assertAreEqual(request.headers["Content-Type"], "application/json", "json found"); djstest.assertAreEqual(request.headers["Content-Encoding"], "compress, gzip", "Content-Encoding available"); @@ -895,9 +899,8 @@ "odata-maxversion": "4.0", "prefer": "prefer" } }; - OData.request(request, function (data) { - }, undefined, undefined, MockHttpClient); + OData.request(request, function (data) { }, undefined, undefined, MockHttpClient); + }); - // DATAJS INTERNAL END })(this); http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/f9aaa814/datajs/tests/odata-qunit-tests.htm ---------------------------------------------------------------------- diff --git a/datajs/tests/odata-qunit-tests.htm b/datajs/tests/odata-qunit-tests.htm index f7b4676..5dd5fbe 100644 --- a/datajs/tests/odata-qunit-tests.htm +++ b/datajs/tests/odata-qunit-tests.htm @@ -41,7 +41,7 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL window.TestSynchronizer.init(QUnit); </script> - <script type="text/javascript" src="../build/datajs-2.0.0.js"></script> + <script type="text/javascript" src="../build/datajs-2.0.0.min.js"></script> <!--SK TODO enable <script type="text/javascript" src="../src/datajs.js"></script> <script type="text/javascript" src="../src/utils.js"></script> <script type="text/javascript" src="../src/xml.js"></script>
