/**
 * @author alex
 
Copyright 2009 Palm, Inc.  All rights reserved.

*/
var NotesDbTests = Class.create({
	
    initialize: function(tickleFunction){
        this.tickleFunction = tickleFunction;
    },
    
    testNoteIsNew: function(recordResults){
        var note = new Note();
        Mojo.require(note.isNew());
        return Mojo.Test.passed;
    },
	
	getDatabase: function() {
		var db = new NotesDb();
		db.name = "Notes-test";
		return db;
	},
	
    emptyDatabase: function(initCallback){
        var db = this.getDatabase();
        db.transaction(function(tx){
			Mojo.log("empty");
            db.empty(tx);
        });
        return db;
    },
    testEmpty: function(recordResults){
        var db = this.emptyDatabase();
        db.load(undefined, 0, 1, function(offset, array, totalCount){
			Mojo.log("totalCount: " + totalCount);
            Mojo.requireEqual(0, totalCount);
            recordResults(Mojo.Test.passed);
        });
    },
    testSaveNote: function(recordResults){
        var db = this.emptyDatabase();
        var note = new Note();
        Mojo.requireEqual(undefined, note.id);
        note.text = "1";
        db.saveNote(note);
        db.load(undefined, 0, 1, function(offset, array, totalCount){
            // Make sure it is set in the original object
            var id = note.id;
            Mojo.requireEqual(id, 1);
            
            // And read in the result!
            var readNote = array[0];
            Mojo.requireEqual(id, readNote.id);
            Mojo.requireEqual(1, totalCount);
            Mojo.requireEqual("1", readNote.text);
            
            // Save again
            note.text = "2";
            db.saveNote(note);
            db.load(undefined, 0, 1, function(offset, array, totalCount){
                var readNote = array[0];
                Mojo.requireEqual(id, readNote.id);
                Mojo.requireEqual(1, totalCount);
                Mojo.requireEqual("2", readNote.text);
                
                // Save again setting null
                note.text = null;
                db.saveNote(note);
                db.load(undefined, 0, 1, function(offset, array, totalCount){
                    var readNote = array[0];
                    Mojo.requireEqual(id, readNote.id);
                    Mojo.requireEqual(1, totalCount);
                    Mojo.requireFalse(readNote.text);
                    
                    // Save again all fields!
                    note.text = '1';
                    note.modifiedTimestamp = 2;
                    note.position = 3;
					note.color = '4';
                    
                    db.saveNote(note);
                    db.loadNoteById(id, function(tx, readNote){
						Mojo.Log.logProperties(readNote);
                        Mojo.requireEqual(id, readNote.id);
						Mojo.requireEqual('1', readNote.text);
                        Mojo.requireEqual(2, readNote.modifiedTimestamp);
                        Mojo.requireEqual(3, readNote.position);
						Mojo.requireEqual('4', readNote.color);
						// Done!
						recordResults(Mojo.Test.passed);
                    });
                    
                });
            });
            
            
        });
    }
});

