test_rollinglimit.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_rollinglimit");
61  dbq.addQuery("drop table test_rollinglimit");
62  dbq.addQuery("create table test_rollinglimit (size integer, name text)");
63  dbq.execute();
64  dbq.resetQuery();
65 
66  insert(dbq, "1", "bart");
67  insert(dbq, "2", "homer");
68  insert(dbq, "3", "lisa");
69  insert(dbq, "4", "maggie");
70  insert(dbq, "5", "marge");
71 
72  // A rolling limit of 5 should finish at the third row
73  dbq.resetQuery();
74  dbq.rollingLimit = 5;
75  dbq.rollingLimitColumnIndex = 0;
76  dbq.addQuery("select size, name from test_rollinglimit order by name");
77  dbq.execute();
78 
79  var r = dbq.getResultObject();
80  var rows = r.getRowCount();
81  assertEqual(rows, 1);
82  assertEqual(r.getRowCell(0, 0), "3");
83  assertEqual(r.getRowCell(0, 1), "lisa");
84  assertEqual(dbq.rollingLimitResult, "3");
85 
86  // A rolling limit of 100 should not return any rows
87  dbq.resetQuery();
88  dbq.rollingLimit = 100;
89  dbq.rollingLimitColumnIndex = 0;
90  dbq.addQuery("select size, name from test_rollinglimit order by name");
91  dbq.execute();
92 
93  var r = dbq.getResultObject();
94  var rows = r.getRowCount();
95  assertEqual(rows, 0);
96 
97  // Rolling limit of 7 descending, should stop at maggie
98  dbq.resetQuery();
99  dbq.rollingLimit = 7;
100  dbq.rollingLimitColumnIndex = 0;
101  dbq.addQuery("select size, name from test_rollinglimit order by name desc");
102  dbq.execute();
103 
104  var r = dbq.getResultObject();
105  var rows = r.getRowCount();
106  assertEqual(rows, 1);
107  assertEqual(r.getRowCell(0, 0), "4");
108  assertEqual(r.getRowCell(0, 1), "maggie");
109  assertEqual(dbq.rollingLimitResult, "2");
110 }
111 
112 function insert(dbq, size, name) {
113  dbq.addQuery("insert into test_rollinglimit values ('" + size + "', '" + name + "')");
114  dbq.execute();
115  dbq.resetQuery();
116 }
117 
function runTest()
Test file.
const Cc
function assertEqual(aExpected, aActual, aMessage)
function insert(dbq, size, name)
var uri
Definition: FeedWriter.js:1135
const Ci
var ios
Definition: head_feeds.js:5