1<?php 2require_once('strataquerytest.inc.php'); 3 4/** 5 * Tests queries - operators. 6 * 7 * @group plugin_strata 8 * @group plugins 9 */ 10class query_operators_test extends Strata_Query_UnitTestCase { 11 12 function setup() { 13 parent::setup(); 14 } 15 16 function testEquals() { 17 $query = array ( 18 'type' => 'select', 19 'grouping'=>array(), 20 'group' => array ( 21 'type' => 'filter', 22 'lhs' => array ( 23 'type' => 'triple', 24 'subject' => array ( 25 'type' => 'variable', 26 'text' => 'p' 27 ), 28 'predicate' => array ( 29 'type' => 'literal', 30 'text' => 'tax rate' 31 ), 32 'object' => array ( 33 'type' => 'variable', 34 'text' => 'tax' 35 ) 36 ), 37 'rhs' => array (array ( 38 'type' => 'operator', 39 'lhs' => array ( 40 'type' => 'variable', 41 'text' => 'tax' 42 ), 43 'operator' => '=', 44 'rhs' => array ( 45 'type' => 'literal', 46 'text' => '2%' 47 ) 48 ) 49 )), 50 'projection' => array ( 51 'p' 52 ), 53 'ordering' => array ( 54 array ( 55 'variable' => 'p', 56 'direction' => 'asc' 57 ) 58 ) 59 ); 60 61 $expected = array ( 62 array ( 63 'p' => array('person:carol') 64 ) 65 ); 66 67 $this->assertQueryResult($query, $expected); 68 } 69 70 function testNotEquals() { 71 $query = array ( 72 'type' => 'select', 73 'grouping'=>array(), 74 'group' => array ( 75 'type' => 'filter', 76 'lhs' => array ( 77 'type' => 'triple', 78 'subject' => array ( 79 'type' => 'variable', 80 'text' => 'p' 81 ), 82 'predicate' => array ( 83 'type' => 'literal', 84 'text' => 'tax rate' 85 ), 86 'object' => array ( 87 'type' => 'variable', 88 'text' => 'tax' 89 ) 90 ), 91 'rhs' => array (array ( 92 'type' => 'operator', 93 'lhs' => array ( 94 'type' => 'variable', 95 'text' => 'tax' 96 ), 97 'operator' => '!=', 98 'rhs' => array ( 99 'type' => 'literal', 100 'text' => '2%' 101 ) 102 ) 103 )), 104 'projection' => array ( 105 'p' 106 ), 107 'ordering' => array ( 108 array ( 109 'variable' => 'p', 110 'direction' => 'asc' 111 ) 112 ) 113 ); 114 115 $expected = array ( 116 array ( 117 'p' => array('person:alice') 118 ), 119 array ( 120 'p' => array('person:bob') 121 ) 122 ); 123 124 $this->assertQueryResult($query, $expected); 125 } 126 127 function testLike() { 128 $query = array ( 129 'type' => 'select', 130 'grouping'=>array(), 131 'group' => array ( 132 'type' => 'filter', 133 'lhs' => array ( 134 'type' => 'triple', 135 'subject' => array ( 136 'type' => 'variable', 137 'text' => 'p' 138 ), 139 'predicate' => array ( 140 'type' => 'literal', 141 'text' => 'tax rate' 142 ), 143 'object' => array ( 144 'type' => 'variable', 145 'text' => 'tax' 146 ) 147 ), 148 'rhs' => array (array ( 149 'type' => 'operator', 150 'lhs' => array ( 151 'type' => 'variable', 152 'text' => 'tax' 153 ), 154 'operator' => '~', 155 'rhs' => array ( 156 'type' => 'literal', 157 'text' => '2%' 158 ) 159 ) 160 )), 161 'projection' => array ( 162 'p' 163 ), 164 'ordering' => array ( 165 array ( 166 'variable' => 'p', 167 'direction' => 'asc' 168 ) 169 ) 170 ); 171 172 $expected = array ( 173 array ( 174 'p' => array('person:carol') 175 ) 176 ); 177 178 $this->assertQueryResult($query, $expected); 179 } 180 181 function testNotLike() { 182 $query = array ( 183 'type' => 'select', 184 'grouping'=>array(), 185 'group' => array ( 186 'type' => 'filter', 187 'lhs' => array ( 188 'type' => 'triple', 189 'subject' => array ( 190 'type' => 'variable', 191 'text' => 'p' 192 ), 193 'predicate' => array ( 194 'type' => 'literal', 195 'text' => 'tax rate' 196 ), 197 'object' => array ( 198 'type' => 'variable', 199 'text' => 'tax' 200 ) 201 ), 202 'rhs' => array (array ( 203 'type' => 'operator', 204 'lhs' => array ( 205 'type' => 'variable', 206 'text' => 'tax' 207 ), 208 'operator' => '!~', 209 'rhs' => array ( 210 'type' => 'literal', 211 'text' => '2%' 212 ) 213 ) 214 )), 215 'projection' => array ( 216 'p' 217 ), 218 'ordering' => array ( 219 array ( 220 'variable' => 'p', 221 'direction' => 'asc' 222 ) 223 ) 224 ); 225 226 $expected = array ( 227 array ( 228 'p' => array('person:alice') 229 ), 230 array ( 231 'p' => array('person:bob') 232 ) 233 ); 234 235 $this->assertQueryResult($query, $expected); 236 } 237 238 function testBeginsWith() { 239 $query = array ( 240 'type' => 'select', 241 'grouping'=>array(), 242 'group' => array ( 243 'type' => 'filter', 244 'lhs' => array ( 245 'type' => 'triple', 246 'subject' => array ( 247 'type' => 'variable', 248 'text' => 'p' 249 ), 250 'predicate' => array ( 251 'type' => 'literal', 252 'text' => 'tax rate' 253 ), 254 'object' => array ( 255 'type' => 'variable', 256 'text' => 'tax' 257 ) 258 ), 259 'rhs' => array (array ( 260 'type' => 'operator', 261 'lhs' => array ( 262 'type' => 'variable', 263 'text' => 'tax' 264 ), 265 'operator' => '^~', 266 'rhs' => array ( 267 'type' => 'literal', 268 'text' => '2%' 269 ) 270 ) 271 )), 272 'projection' => array ( 273 'p' 274 ), 275 'ordering' => array ( 276 array ( 277 'variable' => 'p', 278 'direction' => 'asc' 279 ) 280 ) 281 ); 282 283 $expected = array ( 284 array ( 285 'p' => array('person:carol') 286 ) 287 ); 288 289 $this->assertQueryResult($query, $expected); 290 } 291 292 function testEndsWith() { 293 $query = array ( 294 'type' => 'select', 295 'grouping'=>array(), 296 'group' => array ( 297 'type' => 'filter', 298 'lhs' => array ( 299 'type' => 'triple', 300 'subject' => array ( 301 'type' => 'variable', 302 'text' => 'p' 303 ), 304 'predicate' => array ( 305 'type' => 'literal', 306 'text' => 'tax rate' 307 ), 308 'object' => array ( 309 'type' => 'variable', 310 'text' => 'tax' 311 ) 312 ), 313 'rhs' => array (array ( 314 'type' => 'operator', 315 'lhs' => array ( 316 'type' => 'variable', 317 'text' => 'tax' 318 ), 319 'operator' => '$~', 320 'rhs' => array ( 321 'type' => 'literal', 322 'text' => '2%' 323 ) 324 ) 325 )), 326 'projection' => array ( 327 'p' 328 ), 329 'ordering' => array ( 330 array ( 331 'variable' => 'p', 332 'direction' => 'asc' 333 ) 334 ) 335 ); 336 337 $expected = array ( 338 array ( 339 'p' => array('person:carol') 340 ) 341 ); 342 343 $this->assertQueryResult($query, $expected); 344 } 345 346} 347 348