test_like.js (5601B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // This file tests our LIKE implementation since we override it for unicode 6 7 function setup() { 8 getOpenedDatabase().createTable("t1", "x TEXT"); 9 10 var stmt = createStatement("INSERT INTO t1 (x) VALUES ('a')"); 11 stmt.execute(); 12 stmt.finalize(); 13 14 stmt = createStatement("INSERT INTO t1 (x) VALUES ('ab')"); 15 stmt.execute(); 16 stmt.finalize(); 17 18 stmt = createStatement("INSERT INTO t1 (x) VALUES ('abc')"); 19 stmt.execute(); 20 stmt.finalize(); 21 22 stmt = createStatement("INSERT INTO t1 (x) VALUES ('abcd')"); 23 stmt.execute(); 24 stmt.finalize(); 25 26 stmt = createStatement("INSERT INTO t1 (x) VALUES ('acd')"); 27 stmt.execute(); 28 stmt.finalize(); 29 30 stmt = createStatement("INSERT INTO t1 (x) VALUES ('abd')"); 31 stmt.execute(); 32 stmt.finalize(); 33 34 stmt = createStatement("INSERT INTO t1 (x) VALUES ('bc')"); 35 stmt.execute(); 36 stmt.finalize(); 37 38 stmt = createStatement("INSERT INTO t1 (x) VALUES ('bcd')"); 39 stmt.execute(); 40 stmt.finalize(); 41 42 stmt = createStatement("INSERT INTO t1 (x) VALUES ('xyz')"); 43 stmt.execute(); 44 stmt.finalize(); 45 46 stmt = createStatement("INSERT INTO t1 (x) VALUES ('ABC')"); 47 stmt.execute(); 48 stmt.finalize(); 49 50 stmt = createStatement("INSERT INTO t1 (x) VALUES ('CDE')"); 51 stmt.execute(); 52 stmt.finalize(); 53 54 stmt = createStatement("INSERT INTO t1 (x) VALUES ('ABC abc xyz')"); 55 stmt.execute(); 56 stmt.finalize(); 57 } 58 59 function test_count() { 60 var stmt = createStatement("SELECT count(*) FROM t1;"); 61 Assert.ok(stmt.executeStep()); 62 Assert.equal(stmt.getInt32(0), 12); 63 stmt.reset(); 64 stmt.finalize(); 65 } 66 67 function test_like_1() { 68 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 69 stmt.bindByIndex(0, "abc"); 70 var solutions = ["abc", "ABC"]; 71 Assert.ok(stmt.executeStep()); 72 Assert.ok(solutions.includes(stmt.getString(0))); 73 Assert.ok(stmt.executeStep()); 74 Assert.ok(solutions.includes(stmt.getString(0))); 75 Assert.ok(!stmt.executeStep()); 76 stmt.reset(); 77 stmt.finalize(); 78 } 79 80 function test_like_2() { 81 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 82 stmt.bindByIndex(0, "ABC"); 83 var solutions = ["abc", "ABC"]; 84 Assert.ok(stmt.executeStep()); 85 Assert.ok(solutions.includes(stmt.getString(0))); 86 Assert.ok(stmt.executeStep()); 87 Assert.ok(solutions.includes(stmt.getString(0))); 88 Assert.ok(!stmt.executeStep()); 89 stmt.reset(); 90 stmt.finalize(); 91 } 92 93 function test_like_3() { 94 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 95 stmt.bindByIndex(0, "aBc"); 96 var solutions = ["abc", "ABC"]; 97 Assert.ok(stmt.executeStep()); 98 Assert.ok(solutions.includes(stmt.getString(0))); 99 Assert.ok(stmt.executeStep()); 100 Assert.ok(solutions.includes(stmt.getString(0))); 101 Assert.ok(!stmt.executeStep()); 102 stmt.reset(); 103 stmt.finalize(); 104 } 105 106 function test_like_4() { 107 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 108 stmt.bindByIndex(0, "abc%"); 109 var solutions = ["abc", "abcd", "ABC", "ABC abc xyz"]; 110 Assert.ok(stmt.executeStep()); 111 Assert.ok(solutions.includes(stmt.getString(0))); 112 Assert.ok(stmt.executeStep()); 113 Assert.ok(solutions.includes(stmt.getString(0))); 114 Assert.ok(stmt.executeStep()); 115 Assert.ok(solutions.includes(stmt.getString(0))); 116 Assert.ok(stmt.executeStep()); 117 Assert.ok(solutions.includes(stmt.getString(0))); 118 Assert.ok(!stmt.executeStep()); 119 stmt.reset(); 120 stmt.finalize(); 121 } 122 123 function test_like_5() { 124 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 125 stmt.bindByIndex(0, "a_c"); 126 var solutions = ["abc", "ABC"]; 127 Assert.ok(stmt.executeStep()); 128 Assert.ok(solutions.includes(stmt.getString(0))); 129 Assert.ok(stmt.executeStep()); 130 Assert.ok(solutions.includes(stmt.getString(0))); 131 Assert.ok(!stmt.executeStep()); 132 stmt.reset(); 133 stmt.finalize(); 134 } 135 136 function test_like_6() { 137 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 138 stmt.bindByIndex(0, "ab%d"); 139 var solutions = ["abcd", "abd"]; 140 Assert.ok(stmt.executeStep()); 141 Assert.ok(solutions.includes(stmt.getString(0))); 142 Assert.ok(stmt.executeStep()); 143 Assert.ok(solutions.includes(stmt.getString(0))); 144 Assert.ok(!stmt.executeStep()); 145 stmt.reset(); 146 stmt.finalize(); 147 } 148 149 function test_like_7() { 150 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 151 stmt.bindByIndex(0, "a_c%"); 152 var solutions = ["abc", "abcd", "ABC", "ABC abc xyz"]; 153 Assert.ok(stmt.executeStep()); 154 Assert.ok(solutions.includes(stmt.getString(0))); 155 Assert.ok(stmt.executeStep()); 156 Assert.ok(solutions.includes(stmt.getString(0))); 157 Assert.ok(stmt.executeStep()); 158 Assert.ok(solutions.includes(stmt.getString(0))); 159 Assert.ok(stmt.executeStep()); 160 Assert.ok(solutions.includes(stmt.getString(0))); 161 Assert.ok(!stmt.executeStep()); 162 stmt.reset(); 163 stmt.finalize(); 164 } 165 166 function test_like_8() { 167 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?;"); 168 stmt.bindByIndex(0, "%bcd"); 169 var solutions = ["abcd", "bcd"]; 170 Assert.ok(stmt.executeStep()); 171 Assert.ok(solutions.includes(stmt.getString(0))); 172 Assert.ok(stmt.executeStep()); 173 Assert.ok(solutions.includes(stmt.getString(0))); 174 Assert.ok(!stmt.executeStep()); 175 stmt.reset(); 176 stmt.finalize(); 177 } 178 179 var tests = [ 180 test_count, 181 test_like_1, 182 test_like_2, 183 test_like_3, 184 test_like_4, 185 test_like_5, 186 test_like_6, 187 test_like_7, 188 test_like_8, 189 ]; 190 191 function run_test() { 192 setup(); 193 194 for (var i = 0; i < tests.length; i++) { 195 tests[i](); 196 } 197 198 cleanup(); 199 }