test_tree_collate.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
31 function runTest () {
32 
33  var dbq = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
34  .createInstance(Ci.sbIDatabaseQuery);
35 
36  var ios = Cc["@mozilla.org/network/io-service;1"]
37  .createInstance(Ci.nsIIOService);
38 
39  var dir = Cc["@mozilla.org/file/directory_service;1"]
40  .createInstance(Ci.nsIProperties);
41 
42  var testdir = dir.get("ProfD", Ci.nsIFile);
43 
44  var actualdir = testdir.clone();
45  actualdir.append("db_tests");
46 
47  if(!actualdir.exists())
48  {
49  try {
50  actualdir.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
51  } catch(e) {
52  //Some failures might be handled later. Some might be ignored.
53  throw e;
54  }
55  }
56 
57  var uri = ios.newFileURI(actualdir);
58  dbq.databaseLocation = uri;
59 
60  dbq.setDatabaseGUID("test_tree_collate");
61  dbq.addQuery("drop table test_collate");
62  dbq.addQuery("create table test_collate (id text, ordinal text unique collate tree)");
63  dbq.execute();
64  dbq.waitForCompletion();
65  dbq.resetQuery();
66 
67  // Test some junk data
68  insert(dbq, "1", "");
69  insert(dbq, "2", ".");
70  insert(dbq, "3", "..");
71  insert(dbq, "4", "......");
72  insert(dbq, "5", null);
73 
74  clear(dbq);
75 
76  var a = [];
77  insertAt(dbq, a, "a", 0);
78 
79  insertAt(dbq, a, "b", 1);
80 
81  insertAt(dbq, a, "c", 1);
82 
83  insertAt(dbq, a, "d", 0);
84 
85  insertAt(dbq, a, "e", 0);
86 
87  insertAt(dbq, a, "f", 2);
88 
89  insertAt(dbq, a, "g", 2);
90 
91  insertAt(dbq, a, "h", 2);
92 
93  return Components.results.NS_OK;
94 }
95 
96 function insert(dbq, id, ordinal) {
97  if (ordinal) {
98  dbq.addQuery("insert into test_collate values ('" + id + "', '" + ordinal + "')");
99  }
100  else {
101  dbq.addQuery("insert into test_collate values ('" + id + "', null)");
102  }
103  dbq.execute();
104  dbq.waitForCompletion();
105  dbq.resetQuery();
106 }
107 
108 function clear(dbq) {
109  dbq.addQuery("delete from test_collate");
110  dbq.execute();
111  dbq.waitForCompletion();
112  dbq.resetQuery();
113 }
114 
115 function assertTable(dbq, a) {
116  dbq.addQuery("select id from test_collate order by ordinal asc");
117  dbq.execute();
118  dbq.waitForCompletion();
119  var r = dbq.getResultObject();
120  var rows = r.getRowCount();
121  if (rows != a.length) {
122  fail("query returned " + rows + " rows, array length is " + a.length);
123  }
124 
125  for (var i = 0; i < rows; i++) {
126  assertEqual(a[i], r.getRowCell(i, 0));
127  }
128  dbq.resetQuery();
129 
130 }
131 
132 function insertAt(dbq, a, id, index) {
133  //log("insert at id = " + id + " index = " + index + "\n");
134  if (a.length == 0) {
135  insert(dbq, id, "0");
136  a[0] = id;
137  }
138  else {
139  if (index == a.length) {
140  //log("****** last\n");
141  insert(dbq, id, index);
142  a.push(id);
143  }
144  else {
145  var belowPath = getPathFromIndex(dbq, index);
146  if (index == 0) {
147  //log("****** first\n");
148  insert(dbq, id, belowPath + ".0");
149  }
150  else {
151  var abovePath = getPathFromIndex(dbq, index - 1);
152  //log("****** between " + abovePath + " " + belowPath + "\n");
153  var below = belowPath.split(".");
154  var above = abovePath.split(".");
155  if (below.length == above.length) {
156  insert(dbq, id, belowPath + ".0");
157  }
158  else {
159  if (below.length > above.length) {
160  below[below.length - 1] = parseInt(below[below.length - 1]) - 1;
161  insert(dbq, id, below.join("."));
162  }
163  else {
164  above[above.length - 1] = parseInt(above[above.length - 1]) + 1;
165  insert(dbq, id, above.join("."));
166  }
167  }
168  }
169  a.splice(index, 0, id);
170  }
171  }
172  //log(a + "\n");
173  assertTable(dbq, a);
174 
175 }
176 
177 function getPathFromIndex(dbq, index) {
178 
179  dbq.addQuery("select ordinal from test_collate order by ordinal asc limit 1 offset " + index);
180  dbq.execute();
181  dbq.waitForCompletion();
182  var r = dbq.getResultObject();
183  var path = r.getRowCell(0, 0);
184  dbq.resetQuery();
185  return path;
186 }
187 
function assertTable(dbq, a)
const Cc
function fail(aMessage)
menuItem id
Definition: FeedWriter.js:971
function clear(dbq)
function assertEqual(aExpected, aActual, aMessage)
function insertAt(dbq, a, id, index)
function getPathFromIndex(dbq, index)
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
function insert(dbq, id, ordinal)
const Ci
var ios
Definition: head_feeds.js:5
function runTest()
Test file.
_getSelectedPageStyle s i