Safety checks
[ajatus.git] / testsuite / tests / jqcouchv2.js
blobbfff3908a3d6b306e11faf6da936f650f2e1270f
1 module("jqCouchv2");
3 test("config", function() {
4 expect(2);
5 ok( $.jqCouch, "$.jqCouch" );
6 ok( $.ajatus.preferences.client.application_database != '', "Application database defined" );
7 });
9 test("DB Connection", function() {
10 var dbc = $.jqCouch.connection('db');
11 // console.log(db_connection);
13 ok( dbc.restart(), "Restart CouchDB" );
15 ok( dbc.exists($.ajatus.preferences.client.application_database), "App DB Exists" );
17 ok( dbc.info($.ajatus.preferences.client.application_database).db_name == $.ajatus.preferences.client.application_database, "App DB Info" );
19 ok( dbc.create('jqcouchv2_test_db').ok, "Create test DB" );
21 ok( dbc.all().length > 0, "More than 0 Databases exists" );
23 ok( dbc.del('jqcouchv2_test_db').ok, "Delete test DB" );
24 });
26 test("Doc Connection", function() {
27 var dbc = $.jqCouch.connection('db');
28 ok( dbc.create('jqcouchv2_test_db').ok, "Create test DB" );
30 var dc = $.jqCouch.connection('doc');
32 ok( dc.get($.ajatus.preferences.client.application_database+'/preferences')._id !== false, "Get App preferences doc" );
34 ok( dc.get($.ajatus.preferences.client.application_database+'/non_existant_doc')._id === false, "Get non existant doc" );
35 ok( dc.last_error.response.error == 'not_found', "Last error response.error: not_found");
36 ok( dc.last_error.response.reason == 'missing', "Last error response.reason: missing");
38 var map_fun = function(data) {
39 if (typeof data.value.metadata != 'undefined') {
40 return {metadata: data.value.metadata};
42 return {metadata: false};
44 ok( dc.get($.ajatus.preferences.client.application_database+'/preferences', {}, map_fun).metadata !== false, "Query with mapping" );
46 var doc = {_id:"0",a:1,b:1};
47 ok( dc.save('jqcouchv2_test_db', doc)._id !== false, "Create doc" );
48 ok( typeof doc._rev != 'undefined', "New doc _rev is defined" );
50 ok( dc.del('jqcouchv2_test_db', doc).ok, "Delete doc 0" );
51 ok( doc._deleted, "doc._deleted" );
53 var num_docs_to_create = 700;
54 var docs = genDocs(num_docs_to_create);
55 ok( dc.bulk_save('jqcouchv2_test_db', docs).ok, "Bulk create "+num_docs_to_create+" docs" );
57 ok( dc.all('jqcouchv2_test_db').total_rows > 0, "Get all docs" );
59 ok( dbc.del('jqcouchv2_test_db').ok, "Delete test DB" );
60 });
62 test("View Connection", function() {
63 var dbc = $.jqCouch.connection('db');
64 ok( dbc.create('jqcouchv2_test_db').ok, "Create test DB" );
66 var vc = $.jqCouch.connection('view');
67 ok( vc.exists($.ajatus.preferences.client.content_database, 'event') !== false, "View event exists" );
68 ok( vc.exists($.ajatus.preferences.client.content_database, 'event/all') !== false, "View event/all exists" );
69 ok( vc.exists($.ajatus.preferences.client.content_database, 'event/doesnt') === false, "View event/doesnt exists" );
70 ok( vc.exists($.ajatus.preferences.client.content_database, 'doesnt/exist') === false, "View doesnt/exist exists" );
72 var tmp_map = function(doc) {
73 if (doc.value._type == 'note') {
74 map(doc.value.metadata.created.val, doc);
77 ok( vc.temp($.ajatus.preferences.client.content_database, tmp_map).total_rows, "Run simple temp view" );
79 var tmp_map = function(doc) {
80 if (doc.value._type == 'note') {
81 map(doc.value.metadata.created.val, doc);
84 var map_fun = function(data) {
85 var tmp_data = {
86 offset: 0,
87 rows: [],
88 total_rows: 0
90 var tmp_rows = [];
91 $.each(data.rows, function(i,d){
92 if (i%2==0) {
93 tmp_rows.push(d);
95 });
97 if (tmp_rows.length > 0) {
98 tmp_data.rows = tmp_rows;
99 tmp_data.total_rows = tmp_rows.length;
102 return tmp_data;
104 ok( vc.temp($.ajatus.preferences.client.content_database, tmp_map, {}, map_fun).total_rows, "Run simple temp view with mapping function" );
106 var new_view = {
107 _id: 'test',
108 views: {
109 id_rev: function(doc) {map(doc._id, doc._rev);},
110 rev_id: function(doc) {map(doc._rev, doc._id);}
113 ok( vc.save('jqcouchv2_test_db', new_view).ok, "Save view");
114 ok( typeof new_view._rev != 'undefined', "New views revision is defined");
116 var num_docs_to_create = 400;
117 var docs = genDocs(num_docs_to_create);
118 ok( $.jqCouch.connection('doc').bulk_save('jqcouchv2_test_db', docs).ok, "Bulk create "+num_docs_to_create+" docs" );
120 ok( vc.get('jqcouchv2_test_db', 'test/id_rev').total_rows, "Get view test/id_rev");
122 ok( vc.update_config('cache', true), "Enable cache");
123 ok( vc.get('jqcouchv2_test_db', 'test/rev_id').total_rows, "Get view test/rev_id");
124 ok( vc.get('jqcouchv2_test_db', 'test/rev_id').rows, "Get view test/rev_id from cache");
125 ok( vc.purge_cache(), "Purge cache");
126 ok( vc.get('jqcouchv2_test_db', 'test/rev_id').rows, "Get view test/rev_id from empty cache");
128 ok( dbc.del('jqcouchv2_test_db').ok, "Delete test DB" );
133 function genDocs(n, templateDoc) {
134 var templateDocSrc = templateDoc ? templateDoc.toSource() : "{}"
135 var docs = []
136 for (var i=0; i<n; i++) {
137 var newDoc = eval("(" + templateDocSrc + ")");
138 newDoc._id = (i).toString();
139 newDoc.integer = i;
140 newDoc.string = (i).toString();
141 docs.push(newDoc);
143 return docs;