1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Tests for text search clause generator

This commit is contained in:
J. King 2019-09-12 09:41:01 -04:00
parent fb6e2babb9
commit be5ad50f54
2 changed files with 24 additions and 2 deletions

View file

@ -219,10 +219,11 @@ class Database {
$values[] = $term; $values[] = $term;
} }
} }
$clause[] = "(".implode(" or ", $spec).")"; $spec = sizeof($spec) > 1 ? "(".implode(" or ", $spec).")" : (string) array_pop($spec);
$clause[] = $spec;
} }
$glue = $matchAny ? "or" : "and"; $glue = $matchAny ? "or" : "and";
$clause = $clause ? "(".implode(" $glue ", $clause).")" : ""; $clause = sizeof($clause) > 1 ? "(".implode(" $glue ", $clause).")" : (string) array_pop($clause);
return [$clause, $types, $values]; return [$clause, $types, $values];
} }

View file

@ -55,4 +55,25 @@ class TestDatabase extends \JKingWeb\Arsse\Test\AbstractTest {
["$params", $ints, $ints, "bool"], ["$params", $ints, $ints, "bool"],
]; ];
} }
/** @dataProvider provideSearchClauses */
public function testGenerateSearchClause(string $clause, array $values, array $inV, array $inC, bool $inAny) {
// this is not an exhaustive test; integration tests already cover the ins and outs of the functionality
$types = array_fill(0, sizeof($values), "str");
$exp = [$clause, $types, $values];
$this->assertSame($exp, self::$db->generateSearch($inV, $inC, $inAny));
}
public function provideSearchClauses() {
$terms = array_fill(0, Database::LIMIT_SET_SIZE + 1, "a");
$clause = array_fill(0, Database::LIMIT_SET_SIZE + 1, "test like '%a%' escape '^'");
$longString = str_repeat("0", Database::LIMIT_SET_STRING_LENGTH + 1);
return [
["test like ? escape '^'", ["%a%"], ["a"], ["test"], true],
["(col1 like ? escape '^' or col2 like ? escape '^')", ["%a%", "%a%"], ["a"], ["col1", "col2"], true],
["(".implode(" or ", $clause).")", [], $terms, ["test"], true],
["(".implode(" and ", $clause).")", [], $terms, ["test"], false],
["(".implode(" or ", $clause)." or test like ? escape '^')", ["%$longString%"], array_merge($terms, [$longString]), ["test"], true],
];
}
} }