Skip to content

Instantly share code, notes, and snippets.

@mtibeica
Created June 28, 2012 18:22
Show Gist options
  • Save mtibeica/3013051 to your computer and use it in GitHub Desktop.
Save mtibeica/3013051 to your computer and use it in GitHub Desktop.
node-xapian testing framework
var xapian = require('../xapian-binding');
var xapiantesting = require('./testing-xapian');
var tests=
[
{
name: 'initialize doc',
fatal: true,
action: function(objects, sync, fn) { objects.doc = new xapian.Document(); fn(null); }
},
{
name: 'set_data invalid arguments',
obj: 'doc',
method: 'set_data',
parameters: [],
result: function(err, result, fn) { fn(err == 'arguments are (string, [function])'); }
},
{
name: 'set_data hello world',
obj: 'doc',
method: 'set_data',
parameters: ["hello world"],
result: function(err, result, fn) { fn(err == null); }
},
{
name: 'get_data',
obj: 'doc',
method: 'get_data',
parameters: [],
result: function(err, result, fn) { fn(err == null && result == 'hello world'); }
},
{
name: 'add_value fudge',
obj: 'doc',
method: 'add_value',
parameters: [1, "fudge"],
result: function(err, result, fn) { fn(err == null); }
},
{
name: 'add_value chocolate',
obj: 'doc',
method: 'add_value',
parameters: [2, "chocolate"],
result: function(err, result, fn) { fn(err == null); }
},
{
name: 'get_value',
obj: 'doc',
method: 'get_value',
parameters: [1],
result: function(err, result, fn) { fn(err == null && result === 'fudge'); }
},
{
name: 'get_docid',
obj: 'doc',
method: 'get_docid',
parameters: [],
result: function(err, result, fn) { fn(err == null && result === 0); }
},
/*
my $it = $doc->values_begin();
ok( $it ne $doc->values_end() );
ok( "$it" eq "fudge" );
ok( $it->get_value() eq "fudge" );
ok( $it->get_valueno() == 1 );
++$it;
ok( $it ne $doc->values_end() );
ok( "$it" eq "chocolate" );
ok( $it->get_value() eq "chocolate" );
ok( $it->get_valueno() == 2 );
++$it;
ok( $it eq $doc->values_end() );
*/
{
name: 'remove_value',
obj: 'doc',
method: 'remove_value',
parameters: [1],
result: function(err, result, fn) { fn(err == null); }
},
{
name: 'initialize empty database',
fatal: true,
action: function(objects, sync, fn) {
if (sync) {
objects.db = new xapian.WritableDatabase('tmptestdb', xapian.DB_CREATE_OR_OVERWRITE);
fn(null);
} else {
new xapian.WritableDatabase('tmptestdb', xapian.DB_CREATE_OR_OVERWRITE, function(err, result) {
if (err) fn(err);
else {
objects.db = result;
fn(null);
}
});
}
}
},
{
name: 'add_document',
fatal: false,
action: function(objects, sync, fn) { objects.db.add_document(objects.doc); fn(null); }
},
]
xapiantesting.runTests('Document basics', tests, true);
/*
ok( $doc->get_value(1) eq "" );
ok( $doc->get_value(2) eq "chocolate" );
$doc->clear_values();
ok( $doc->get_value(2) eq "" );
my $database = Search::Xapian::WritableDatabase->new();
# in <= 0.8.3.0 this added with wdfinc 1
$doc->add_posting( "hello", 1, 100 );
# in <= 0.8.3.0 this added with wdfinc 0
$doc->add_posting( "hello", 2 );
$database->add_document($doc);
ok( $database->get_doclength(1) == 101 );
$doc = Search::Xapian::Document->new();
# in <= 0.8.3.0 this added with wdfinc 1 (happens to work as it should)
$doc->add_posting( "goodbye", 1, 1 );
# in <= 0.8.3.0 this added with wdfinc 1 (happens to work as it should)
$doc->add_posting( "goodbye", 2, 1 );
# in <= 0.8.3.0 this removed with wdfinc 0
$doc->remove_posting( "goodbye", 2 );
$database->add_document($doc);
ok( $database->get_doclength(2) == 1 );
$doc = Search::Xapian::Document->new();
# in <= 0.8.3.0 this added with wdfinc 1
$doc->add_term( "a", 100 );
# in <= 0.8.3.0 this added with wdfinc 0
$doc->add_term( "a" );
$database->add_document($doc);
ok( $database->get_doclength(3) == 101 );
ok( $it = $doc->termlist_begin());
ok( $it ne $doc->termlist_end());
ok( "$it" eq "a" );
ok( $it->get_termname() eq "a" );
++$it;
ok( $it eq $doc->termlist_end());
$doc->add_boolean_term( "b" );
$database->add_document($doc);
ok( $database->get_doclength(4) == 101 );
$doc->remove_term( "a" );
$database->add_document($doc);
ok( $database->get_doclength(5) == 0 );
*/
exports.runTests = function (name, tests, sync, _done) {
console.log(name);
var objects = {};
runTest(0);
function runTest(indTst)
{
if (indTst == tests.length) {
if (_done) _done();
return;
}
if ('action' in tests[indTst]) {
try {
tests[indTst].action(objects, sync, function(err) {
if (err) {
if (tests[indTst].fatal) {
console.log(' fatal ' + tests[indTst].name + ' - ' + err);
if (_done) _done();
} else {
console.log(' fail ' + tests[indTst].name);
runTest(indTst + 1);
}
} else {
console.log(' ok ' + tests[indTst].name);
runTest(indTst + 1);
}
});
} catch (ex) {
if (tests[indTst].fatal) {
console.log(' fatal ' + tests[indTst].name + ' - ' + ex.message);
if (_done) _done();
} else {
console.log(' fail ' + tests[indTst].name);
runTest(indTst + 1);
}
}
}
else {
if (sync) {
var aResult, aErr = null;
try {
aResult = objects[tests[indTst].obj][tests[indTst].method].apply(objects[tests[indTst].obj], tests[indTst].parameters);
} catch (ex) {
aErr = ex.message;
}
tests[indTst].result(aErr, aResult, function(result) {
console.log((result ? ' ok ' : ' fail ') + tests[indTst].name);
runTest(indTst + 1);
});
} else {
var aAlteredParameters = [];
for (var i=0; i < tests[indTst].parameters.length; i++ ) aAlteredParameters.push(tests[indTst].parameters[i]);
aAlteredParameters.push(function(err, result){
tests[indTst].result(err, result, function(result) {
console.log((result ? ' ok ' : ' fail ') + tests[indTst].name);
runTest(indTst + 1);
});
});
try {
objects[tests[indTst].obj][tests[indTst].method].apply(objects[tests[indTst].obj], aAlteredParameters);
} catch (ex) {
tests[indTst].result(ex.message, null, function(result) {
console.log((result ? ' ok ' : ' fail ') + tests[indTst].name);
runTest(indTst + 1);
});
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment