/tests/test.html
https://github.com/podviaznikov/indexeddb-backbonejs-adapter · HTML · 658 lines · 635 code · 22 blank · 1 comment · 0 complexity · b08e1c42c2d4f5f887125f7351602ddc MD5 · raw file
- <!DOCTYPE html>
- <html>
- <head>
- <script src="../lib/jquery.js"></script>
- <link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
- <script type="text/javascript" src="../lib/qunit.js"></script>
- <!-- Dependencies -->
- <script src="../lib/underscore-min.js" type="text/javascript"></script>
- <script src="../lib/backbone.js" type="text/javascript"></script>
- <script src="../backbone-indexeddb.js" type="text/javascript"></script>
- <script>
- var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
- var database = {
- id: "movies-database",
- description: "The database for the Movies",
- migrations: [{
- version: "1.0",
- migrate: function (db, versionRequest, next) {
- var store = db.createObjectStore("movies");
- next();
- }
- }, {
- version: "1.1",
- migrate: function (db, versionRequest, next) {
- var store = versionRequest.transaction.objectStore("movies");
- store.createIndex("titleIndex", "title", {
- unique: false
- });
- store.createIndex("formatIndex", "format", {
- unique: false
- });
- next();
- }
- }]
- };
- var Movie = Backbone.Model.extend({
- database: database,
- storeName: "movies"
- });
- var Theater = Backbone.Collection.extend({
- database: database,
- storeName: "movies",
- model: Movie
- });
- function addAllMovies(movies, done) {
- if (!movies) {
- movies = [{
- title: "Hello",
- format: "blueray",
- id: "1"
- }, {
- title: "Bonjour",
- format: "dvd",
- id: "2"
- }, {
- title: "Halo",
- format: "blueray",
- id: "3"
- }, {
- title: "Nihao",
- format: "streaming",
- id: "4"
- }, {
- title: "Ciao",
- format: "dvd",
- id: "5"
- }];
- }
- var movie = movies.shift();
- if (movie) {
- var m = new Movie();
- m.save(movie, {
- success: function () {
- addAllMovies(movies, done);
- },
- error: function (o, error) {
- start();
- equals(true, false, error.error.target.webkitErrorMessage);
- }
- });
- } else {
- done();
- }
- }
- function deleteNext(movies, done) {
- if (movies.length === 0) {
- done();
- } else {
- movies[0].destroy({
- success: function () {
- deleteNext(movies, done);
- }
- });
- }
- }
- function nextTest() {
- var t = tests.shift();
- if (t) {
- console.log(t[0]);
- asyncTest(t[0], t[1]);
- }
- }
- var tests = [
- ["create model", function () {
- var movie = new Movie();
- movie.save({
- title: "The Matrix",
- format: "dvd"
- }, {
- success: function () {
- start();
- equals(true, true, "The movie should be saved successfully");
- nextTest();
- },
- error: function (o, error) {
- start();
- if (window.console && window.console.log) window.console.log(error);
- equals(true, false, error.error.target.webkitErrorMessage);
- nextTest();
- }
- });
- }],
- ["read model with id", function () {
- var movie = new Movie();
- movie.save({
- title: "Avatar",
- format: "blue-ray"
- }, {
- success: function () {
- // Ok, now we need to create a new movie object and retrieve it
- var saved = new Movie({
- id: movie.id
- });
- saved.fetch({
- success: function (object) {
- // success
- start();
- equals(saved.toJSON().title, movie.toJSON().title, "The movie should have the right title");
- equals(saved.toJSON().format, movie.toJSON().format, "The movie should have the right format");
- equals(object.toJSON().title, movie.toJSON().title, "The movie should have the right title");
- equals(object.toJSON().format, movie.toJSON().format, "The movie should have the right format");
- nextTest();
- },
- error: function (object, error) {
- start();
- equals(true, false, error);
- nextTest();
- // Failure
- }
- });
- },
- error: function (o, error) {
- start();
- equals(true, false, error.error.target.webkitErrorMessage);
- nextTest();
- }
- });
- }],
- ["read model with index", function () {
- function saveMovieAndReadWithIndex(movie) {
- movie.save({}, {
- success: function () {
- // Ok, now we need to create a new movie object and retrieve it
- var movie2 = new Movie({
- title: "Avatar",
- });
- movie2.fetch({
- success: function (object) {
- console.log("FOUND! SUCCESS");
- // success
- start();
- equals(movie2.toJSON().title, movie.toJSON().title, "The movie should have the right title");
- equals(movie2.toJSON().format, movie.toJSON().format, "The movie should have the right format");
- nextTest();
- },
- error: function (object, error) {
- start();
- equals(true, false, error);
- nextTest();
- }
- });
- },
- error: function (o, error) {
- start();
- equals(true, false, error.error.target.webkitErrorMessage);
- nextTest();
- }
- });
- }
- var movie = new Movie({
- title: "Avatar",
- });
- movie.fetch({
- success: function () {
- movie.destroy({
- success: function () {
- saveMovieAndReadWithIndex(movie);
- }
- });
- },
- error: function () {
- saveMovieAndReadWithIndex(movie);
- }
- });
- }],
- ["read model that do not exist with index", function () {
- var movie = new Movie({
- title: "Memento",
- });
- movie.fetch({
- success: function (object) {
- // success
- start();
- equals(true, false, error);
- nextTest();
- },
- error: function (object, error) {
- start();
- equals(true, true, error);
- nextTest();
- }
- });
- }],
- ["update model", function () {
- var movie = new Movie();
- movie.save({
- title: "Star Wars, Episode IV",
- format: "dvd"
- }, {
- success: function () {
- movie.save({
- title: "Star Wars, Episode V"
- }, {
- success: function () {
- movie.fetch({
- success: function (object) {
- // success
- start();
- equals("Star Wars, Episode V", movie.toJSON().title, "The movie should have the right title");
- equals("dvd", movie.toJSON().format, "The movie should have the right format");
- nextTest();
- },
- error: function (object, error) {
- start();
- equals(true, false, error);
- nextTest();
- }
- });
- },
- error: function () {
- start();
- equals(true, false, "Would not update");
- nextTest();
- }
- });
- },
- error: function (object, error) {
- start();
- equals(true, false, error.error.target.webkitErrorMessage);
- nextTest();
- }
- });
- }],
- ["delete model", function () {
- var movie = new Movie();
- movie.save({
- title: "Avatar",
- format: "blue-ray"
- }, {
- success: function (object) {
- // success
- movie.destroy({
- success: function (object) {
- // success
- equals(true, true, "It should delete the object");
- movie.fetch({
- success: function () {
- start();
- equals(true, false, "Object was not deleted");
- nextTest();
- },
- error: function (object, error) {
- start();
- equals(error, "Not Found", "Object was deleted");
- nextTest();
- }
- });
- },
- error: function (object, error) {
- // error
- start();
- equals(true, false, error);
- nextTest();
- }
- });
- },
- error: function (error) {
- // error
- start();
- equals(true, false, error.error.target.webkitErrorMessage);
- nextTest();
- }
- });
- }],
- ["read collection with no options", function () {
- var theater = new Theater();
- theater.fetch({
- success: function () {
- deleteNext(theater.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- theater.fetch({
- success: function () {
- start();
- equals(theater.models.length, 5, "Should have 5 elements");
- deepEqual(theater.pluck("title"), ["Hello", "Bonjour", "Halo", "Nihao", "Ciao"], "Should have [\"Hello\", \"Bonjour\", \"Halo\", \"Nihao\", \"Ciao\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection with limit", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- limit: 3,
- success: function () {
- start();
- equals(theater.models.length, 3, "Should have 3 elements");
- deepEqual(theater.pluck("title"), ["Hello", "Bonjour", "Halo"], "Should have [\"Hello\", \"Bonjour\", \"Halo\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection with offset", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- offset: 2,
- success: function () {
- start();
- equals(theater.models.length, 3, "Should have 3 elements");
- deepEqual(theater.pluck("title"), ["Halo", "Nihao", "Ciao"], "Should have [\"Halo\", \"Nihao\", \"Ciao\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection with offset and limit", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- offset: 1,
- limit: 2,
- success: function () {
- start();
- equals(theater.models.length, 2, "Should have 2 elements");
- deepEqual(theater.pluck("title"), ["Bonjour", "Halo"], "Should have [\"Bonjour\", \"Halo\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection with range", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- range: ["1.5", "4.5"],
- success: function () {
- start();
- equals(theater.models.length, 3, "Should have 3 elements");
- deepEqual(theater.pluck("title"), ["Bonjour", "Halo", "Nihao"], "Should have [\"Bonjour\", \"Halo\", \"Nihao\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection via condition on index with a single value", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- conditions: {
- format: "dvd"
- },
- success: function () {
- start();
- equals(theater.models.length, 2, "Should have 2 elements");
- deepEqual(theater.pluck("title"), ["Bonjour", "Ciao"], "Should have [\"Bonjour\", \"Ciao\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection via condition on index with a range", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- conditions: {
- format: ["a", "f"]
- },
- success: function () {
- start();
- equals(theater.models.length, 4, "Should have 4 elements");
- deepEqual(theater.pluck("title"), ["Hello", "Halo", "Bonjour", "Ciao"], "Should have [\"Hello\", \"Halo\", \"Bonjour\", \"Ciao\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection via condition on index with a range and a limit", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- limit: 2,
- conditions: {
- format: ["a", "f"]
- },
- success: function () {
- start();
- equals(theater.models.length, 2, "Should have 2 elements");
- deepEqual(theater.pluck("title"), ["Hello", "Halo"], "Should have [\"Hello\", \"Halo\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection via condition on index with a range, an offset and a limit", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- offset: 2,
- limit: 2,
- conditions: {
- format: ["a", "f"]
- },
- success: function () {
- start();
- equals(theater.models.length, 2, "Should have 2 elements");
- deepEqual(theater.pluck("title"), ["Bonjour", "Ciao"], "Should have [\"Bonjour\", \"Ciao\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["read collection via condition on index with a range reversed", function () {
- var theaterToClean = new Theater();
- theaterToClean.fetch({
- success: function () {
- deleteNext(theaterToClean.models, function () {
- addAllMovies(null, function () {
- // Now all movies are inserted. Which is good.
- var theater = new Theater();
- theater.fetch({
- conditions: {
- format: ["f", "a"]
- },
- success: function () {
- start();
- equals(theater.models.length, 4, "Should have 4 elements");
- deepEqual(theater.pluck("title"), ["Ciao", "Bonjour", "Halo", "Hello"], "Should have [\"Ciao\", \"Bonjour\", \"Halo\", \"Hello\"]");
- nextTest();
- }
- });
- });
- });
- }
- });
- }],
- ["support for the 'addIndividually' property", function () {
- var counter = 0,
- theater = new Theater();
- theater.fetch({
- addIndividually: true,
- success: function () {
- // console.log('success callback', arguments);
- },
- error: function () {
- // console.log('error callback', arguments);
- }
- });
- start();
- // we want 5 'add' events
- expect(5);
- theater.bind('add', function (model, collection) {
- counter += 1;
- equal(collection.length, counter)
- // this is a bit hokey, but will do the trick.
- if (collection.length === 5) nextTest();
- });
- }]
- ];
- // Let's go run the tests
-
- nextTest(tests);
-
-
- // First, let's see the content of the database... because I'm little worried about it!
- // var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
- // var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; // No prefix in moz
- //
- // var dbRequest = indexedDB.open(database.id, "");
- //
- // var db = null;
- // var cycles = 0;
- //
- // var all = [];
- //
- // dbRequest.onsuccess = function (e) {
- // db = e.target.result; // Attach the connection ot the queue.
- // var readTransaction = db.transaction(["movies"], IDBTransaction.READ_ONLY);
- // var store = readTransaction.objectStore("movies");
- //
- // var index = store.index("titleIndex");
- // var readCursor = index.openCursor();
- //
- // readCursor.onsuccess = function (e) {
- // var cursor = e.target.result;
- // if (!cursor) {
- // var done = _.after(all.length + 1, function(){
- // console.log("----- END OF DEBUG -----");
- // nextTest(tests);
- // })
- // done();
- // _.each(all, function (obj, index) {
- // console.log("Deleting : " + JSON.stringify(obj));
- //
- // var deleteTransaction = db.transaction(["movies"], IDBTransaction.READ_WRITE);
- // var store = deleteTransaction.objectStore("movies");
- //
- // var deleteRequest = store.delete(obj.id);
- // deleteRequest.onsuccess = function (event) {
- // console.log("OK. ONE LESS");
- // done();
- // };
- // deleteRequest.onerror = function (event) {
- // console.log("DANG")
- // };
- // });
- // }
- // else {
- // all.push(cursor.value);
- // cursor.continue();
- // }
- // };
- // }
- // dbRequest.onerror = function (e) {
- // console.error("Couldn't not connect to the database");
- // };
- //
- // dbRequest.onabort = function (e) {
- // console.error("Connection to the database aborted");
- // };
- //
- </script>
- </head>
-
- <body>
- <h1 id="qunit-header">
- Inbox QUnit test
- </h1>
- <h2 id="qunit-banner">
- </h2>
- <div id="qunit-testrunner-toolbar">
- </div>
- <h2 id="qunit-userAgent">
- </h2>
- <ol id="qunit-tests">
- </ol>
- <div id="log" style="">
- </div>
- </body>
- </html>