Permalink
Browse files

Basic search tests.

1 parent bbbcae5 commit df2006434b5124ed43945f3565cd4c1474a7921e @zefhemel zefhemel committed Aug 13, 2010
Showing with 128 additions and 54 deletions.
  1. +0 −3 CHANGELOG
  2. +28 −0 CHANGES
  3. +4 −4 persistence.search.js
  4. +2 −2 persistence.store.mysql.js
  5. +14 −11 persistence.store.sql.js
  6. +3 −3 persistence.store.websql.js
  7. +1 −1 test/test.persistence.js
  8. +23 −12 test/test.search.html
  9. +53 −18 test/test.search.js
View
@@ -1,3 +0,0 @@
-Changes:
-
-* persistence.db.log is now called persistence.debug
View
28 CHANGES
@@ -0,0 +1,28 @@
+Changes
+=======
+
+* Moved all the SQL stuff into persistence.store.sql.js, and WebSQL to
+ persistence.store.websql.js. So, to use a WebSQL browser database you need
+ to include 3 files in your HTML now:
+
+ <script src="persistence.js" type="application/javascript"></script>
+ <script src="persistence.store.sql.js" type="application/javascript"></script>
+ <script src="persistence.store.websql.js" type="application/javascript"></script>
+
+ Then, instead of using `persistence.connect` use:
+
+ persistence.store.websql.config(persistence, 'dbname', 'My db', 5 * 1024 * 1024);
+
+ For node.js and MySQL:
+
+ var persistence = require('./persistence').persistence;
+ var persistenceStore = require('./persistence.store.mysql');
+
+ persistenceStore.config(persistence, 'localhost', 'somedb', 'user', 'pw');
+ var session = persistenceStore.getSession();
+ ...
+ session.close();
+
+* persistence.db.log is now called persistence.debug
+
+v0.1.1: Last version with only one persistence.js file
View
@@ -91,7 +91,7 @@ persistence.search.config = function(persistence, dialect) {
}
var sql = '(';
if(word.search(/\*/) !== -1) {
- sql += "`" + indexTbl + "`.`word` LIKE '" + word.replace(/\*/g, '%');
+ sql += "`" + indexTbl + "`.`word` LIKE '" + word.replace(/\*/g, '%') + "'";
} else if(prefixByDefault) {
sql += "`" + indexTbl + "`.`word` LIKE '" + word + "%'";
} else {
@@ -159,9 +159,9 @@ persistence.search.config = function(persistence, dialect) {
for(var entityName in entityMeta) {
var meta = entityMeta[entityName];
if(meta.textIndex) {
- queries.push([dialect.createTable(entityName + + '_Index', [['entityId', 'VARCHAR(32)'], ['prop', 'VARCHAR(30)'], ['word', 'VARCHAR(100)'], ['occurences', 'INT']]), null]);
- queries.push([dialect.createIndex(entityName, ['prop', 'word']), null]);
- queries.push([dialect.createIndex(entityName, ['word']), null]);
+ queries.push([dialect.createTable(entityName + '_Index', [['entityId', 'VARCHAR(32)'], ['prop', 'VARCHAR(30)'], ['word', 'VARCHAR(100)'], ['occurrences', 'INT']]), null]);
+ queries.push([dialect.createIndex(entityName + '_Index', ['prop', 'word']), null]);
+ queries.push([dialect.createIndex(entityName + '_Index', ['word']), null]);
persistence.generatedTables[entityName + '_Index'] = true;
}
}
@@ -70,7 +70,7 @@ exports.config = function(persistence, hostname, db, username, password) {
};
return that;
}
- var mysqlDialect = {
+ exports.mysqlDialect = {
columnTypeToSql: function(type) {
switch(type) {
case 'JSON': return 'TEXT';
@@ -103,6 +103,6 @@ exports.config = function(persistence, hostname, db, username, password) {
}
};
- sql.config(persistence, mysqlDialect);
+ sql.config(persistence, exports.mysqlDialect);
};
View
@@ -17,9 +17,10 @@ if(!persistence.store) {
persistence.store.sql = {};
persistence.store.sql.config = function(persistence, dialect) {
- var generatedTables = {}; // set
var argspec = persistence.argspec;
+ persistence.generatedTables = {}; // set
+
/**
* Synchronize the data model with the database, creates table that had not
* been defined before
@@ -67,28 +68,28 @@ persistence.store.sql.config = function(persistence, dialect) {
for (var rel in meta.hasMany) {
if (meta.hasMany.hasOwnProperty(rel) && meta.hasMany[rel].manyToMany) {
tableName = meta.hasMany[rel].tableName;
- if (!generatedTables[tableName]) {
+ if (!persistence.generatedTables[tableName]) {
var otherMeta = meta.hasMany[rel].type.meta;
queries.push([dialect.createIndex(tableName, [meta.name + "_" + rel]), null]);
queries.push([dialect.createIndex(tableName, [otherMeta.name + "_" + meta.hasMany[rel].inverseProperty]), null]);
queries.push([dialect.createTable(tableName, [[meta.name + "_" + rel, "VARCHAR(32)"], [otherMeta.name + "_" + meta.hasMany[rel].inverseProperty, "VARCHAR(32)"]]), null]);
- generatedTables[tableName] = true;
+ persistence.generatedTables[tableName] = true;
}
}
}
colDefs.push(["id", "VARCHAR(32)", "PRIMARY KEY"]);
- generatedTables[meta.name] = true;
+ persistence.generatedTables[meta.name] = true;
queries.push([dialect.createTable(meta.name, colDefs), null]);
}
}
+ var fns = persistence.schemaSyncHooks;
+ for(var i = 0; i < fns.length; i++) {
+ fns[i](tx);
+ }
if(emulate) {
// Done
callback(tx);
} else {
- var fns = persistence.schemaSyncHooks;
- for(var i = 0; i < fns.length; i++) {
- fns[i](tx);
- }
executeQueriesSeq(tx, queries, function() {
callback(tx);
});
@@ -194,8 +195,8 @@ persistence.store.sql.config = function(persistence, dialect) {
// First emulate syncing the schema (to know which tables were created)
this.schemaSync(tx, function() {
var tableArray = [];
- for (var p in generatedTables) {
- if (generatedTables.hasOwnProperty(p)) {
+ for (var p in persistence.generatedTables) {
+ if (persistence.generatedTables.hasOwnProperty(p)) {
tableArray.push(p);
}
}
@@ -216,7 +217,7 @@ persistence.store.sql.config = function(persistence, dialect) {
}
session.clean();
- generatedTables = {};
+ persistence.generatedTables = {};
}, true);
};
@@ -396,6 +397,8 @@ persistence.store.sql.config = function(persistence, dialect) {
}
}
+ persistence.executeQueriesSeq = executeQueriesSeq;
+
/////////////////////////// QueryCollection patches to work in SQL environment
persistence.NullFilter.prototype.sql = function (alias, values) {
return "1=1";
@@ -17,7 +17,7 @@ if(!persistence.store) {
persistence.store.websql = {};
-persistence.store.websql.config = function(dbname, description, size) {
+persistence.store.websql.config = function(persistence, dbname, description, size) {
var conn = null;
/**
@@ -175,7 +175,7 @@ persistence.store.websql.config = function(dbname, description, size) {
///////////////////////// SQLite dialect
- var sqliteDialect = {
+ persistence.store.websql.sqliteDialect = {
columnTypeToSql: function(type) {
switch(type) {
case 'JSON': return 'TEXT';
@@ -209,7 +209,7 @@ persistence.store.websql.config = function(dbname, description, size) {
};
// Configure persistence for generic sql persistence, using sqliteDialect
- persistence.store.sql.config(persistence, sqliteDialect);
+ persistence.store.sql.config(persistence, persistence.store.websql.sqliteDialect);
// Make the connection
conn = persistence.db.connect(dbname, description, size);
View
@@ -1,6 +1,6 @@
$(document).ready(function(){
persistence.store.websql.config(persistence, 'persistencetest', 'My db', 5 * 1024 * 1024);
- persistence.db.log = true;
+ persistence.debug = true;
var Project = persistence.define('Project', {
name: "TEXT"
View
@@ -1,13 +1,24 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script src="http://code.google.com/apis/gears/gears_init.js" type="application/javascript"></script>
- <script src="../persistence.js" type="application/javascript"></script>
- <script src="../persistence.search.js" type="application/javascript"></script>
- <script src="test.search.js" type="application/javascript"></script>
- </head>
- <body>
- All output is sent to the console. In Firefox, use firebug for this, in Safari/Chrome you can use the developer tools.
- </body>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <script src="qunit/jquery.js"></script>
+ <link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen" />
+ <script type="text/javascript" src="qunit/qunit.js"></script>
+
+ <script src="http://code.google.com/apis/gears/gears_init.js"></script>
+ <script src="../persistence.js" type="application/javascript"></script>
+ <script src="../persistence.store.sql.js" type="application/javascript"></script>
+ <script src="../persistence.store.websql.js" type="application/javascript"></script>
+ <script src="../persistence.search.js" type="application/javascript"></script>
+ <script type="text/javascript" src='util.js'></script>
+ <script type="text/javascript" src='test.search.js'></script>
+</head>
+<body>
+ <h1 id="qunit-header">persistence.js search tests</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
</html>
+
View
@@ -1,20 +1,55 @@
-persistence.connect('searchtest', 'My search db', 5 * 1024 * 1024);
+$(document).ready(function(){
+ persistence.store.websql.config(persistence, 'searchtest', 'My db', 5 * 1024 * 1024);
+ persistence.search.config(persistence, persistence.store.websql.sqliteDialect);
+ persistence.debug = true;
+
+ var Note = persistence.define('Note', {
+ title: "TEXT",
+ text: "TEXT"
+ });
+
+ Note.textIndex('title');
+ Note.textIndex('text');
+
+ module("Setup");
+
+ asyncTest("setting up database", 1, function() {
+ persistence.reset(function() {
+ persistence.schemaSync(function(tx){
+ ok(tx.executeSql, 'schemaSync passed transaction as argument to callback');
+ start();
+ });
+ });
+ });
+
+ module("Search test");
+
+ asyncTest("Create some sample data", function() {
+ persistence.add(new Note({title: "My first note", text: "This is my first note. It has a rather high duplication quotient, or whatever."}));
+ persistence.add(new Note({title: "My second note", text: "This is my second note. Isn't it a cool note? Third, fourth."}));
+ persistence.add(new Note({title: "My third note", text: "Nothing here yet"}));
+ persistence.add(new Note({title: "Unrelated", text: "Under contruction."}));
+ persistence.flush(function() {
+ start();
+ });
+ });
+
+ asyncTest("Searching", function() {
+ Note.search("note").list(function(results) {
+ equals(results.length, 3, "returned correct number of results");
+ equals(results[0].title, "My second note", "Found most relevant result");
+ Note.search("title: third").list(function(results) {
+ equals(results.length, 1, "returned correct number of results");
+ equals(results[0].title, "My third note", "Searched in only title");
+ Note.search("thi*").list(function(results) {
+ equals(results.length, 3, "wildcard search");
+ Note.search("thi*").limit(1).list(function(results) {
+ equals(results.length, 1, "limit number of search results");
+ start();
+ });
+ });
+ });
+ });
+ });
-var Note = persistence.define('Note', {
- name: "TEXT",
- text: "TEXT"
-});
-Note.textIndex('name');
-Note.textIndex('text');
-
-persistence.schemaSync(function (tx) {
- var n = new Note();
- n.name = "Note 1";
- n.text = "This is my first note, it's quite cool!";
- persistence.add(n);
- n = new Note();
- n.name = "Note 2";
- n.text = "This another note, it's quite cool as well! I love these notes.";
- persistence.add(n);
- persistence.flush();
});

0 comments on commit df20064

Please sign in to comment.