Test literal SQLite LIKE search for iOS on a cloud Mac

Test literal SQLite LIKE search for iOS on a cloud Mac

Test literal SQLite LIKE search for iOS on a cloud Mac

A tester types 50%off into a search box, but the results include 50Xoff. Searching for an underscore in an order name matches any single character instead. The UI reports no error, and the database returns results successfully, yet the search contract has already changed. Start with an offline SQLite fixture on a cloud Mac, test input conversion separately from query behavior, and then run the same samples across every iOS regression test lane.

Separate literal search from pattern matching first

SQLite’s LIKE treats % as a sequence of zero or more characters and _ as any single character. Parameter binding keeps values separate from SQL syntax; it does not disable this interpretation. If the product promises to “find names containing this text,” wildcards in the input must be treated as ordinary characters.

Start by documenting three rules: match text anywhere in the name, do not search for an empty string, and preserve leading and trailing spaces rather than silently trimming them. If the product requires different behavior, change the contract and the tests instead of patching each entry point independently. Decide separately how to handle whitespace-only input; it is not the same as an empty string.

Parameter binding determines whether a value can become SQL syntax. Escaping determines whether a value is interpreted as a matching pattern. Neither step replaces the other.

This article uses a backslash as the escape character and explicitly declares ESCAPE in every query. Do not assume that a backslash behaves the same way without that declaration.

Build a minimal failing fixture on a cloud Mac

Run the complete script below in a terminal. The sample data lives entirely in an in-memory database, so it will not modify any project files. The CHECK constraint acts as an assertion: any unexpected result causes a failure. .bail on stops execution instead of letting the script continue after printing an error.

sqlite3 :memory: <<'SQL'
.bail on
CREATE TABLE names(value TEXT NOT NULL);
INSERT INTO names VALUES
('50%off'), ('50Xoff'), ('a_b'), ('axb'), ('a\b');

CREATE TEMP TABLE checks(
  label TEXT NOT NULL,
  ok INTEGER NOT NULL CHECK(ok = 1)
);

INSERT INTO checks
SELECT 'percent', count(*) IS 1
FROM names WHERE value LIKE '%50\%off%' ESCAPE '\';

INSERT INTO checks
SELECT 'underscore', count(*) IS 1
FROM names WHERE value LIKE '%a\_b%' ESCAPE '\';

INSERT INTO checks
SELECT 'backslash', count(*) IS 1
FROM names WHERE value LIKE '%a\\b%' ESCAPE '\';

INSERT INTO checks VALUES
('ascii-case', ('a' LIKE 'A') IS 1),
('non-ascii-case', ('æ' LIKE 'Æ') IS 0);

SELECT label, ok FROM checks ORDER BY label;
SQL

By default, SQLite does not treat a backslash as an escape prefix inside SQL string literals, so each single backslash shown here really is one character. Do not apply Swift string-literal rules directly to SQL files.

These assertions apply to SQLite’s default implementation, with no changes to LIKE behavior. If your team has loaded an extension or replaced a function, document the differences explicitly. Do not delete the case-sensitivity assertions just to make the script pass.

Put the conversion in one Swift function

The processing order must be fixed: escape backslashes first, then percent signs and underscores, and only then add the outer percent signs for a “contains” search. Changing that order can process newly inserted escape characters again and alter the pattern’s meaning.

import Foundation

func literalContainsPattern(_ input: String) -> String? {
    guard !input.isEmpty else { return nil }
    let escaped = input
        .replacingOccurrences(of: "\\", with: "\\\\")
        .replacingOccurrences(of: "%", with: "\\%")
        .replacingOccurrences(of: "_", with: "\\_")
    return "%" + escaped + "%"
}

precondition(literalContainsPattern("") == nil)
precondition(literalContainsPattern("50%off") == "%50\\%off%")
precondition(literalContainsPattern("a_b") == "%a\\_b%")
precondition(literalContainsPattern("a\\b") == "%a\\\\b%")
precondition(literalContainsPattern(" ") == "% %")

Save this as LikePattern.swift, then run swift LikePattern.swift. In an actual project, put the function and assertions in a test target. This command verifies the conversion in isolation; it does not replace database integration tests.

Use the fixed SQL statement below and bind the returned pattern to the first parameter. Do not concatenate it into the SQL string or let the data access layer escape it a second time.

SELECT value
FROM names
WHERE value LIKE ?1 ESCAPE '\'
ORDER BY value COLLATE BINARY;

Tests must compare the complete result list, not just the result count. For example, the input _ must return only a_b. Otherwise, an incorrect result set with the same number of rows could still pass.

Verify case handling and runtime differences separately

By default, LIKE is generally case-insensitive for ASCII letters, but it does not provide full Unicode case folding for non-ASCII characters. Adding COLLATE NOCASE to a column is not evidence that LIKE now supports case-insensitive matching in every language.

If the product requires case-insensitive search across languages, define explicit rules for generating search keys, and pin both the normalization method and the implementation version. Simply lowercasing both sides does not establish support for every language.

Record the following separately during Mac preflight checks and in the iOS test target:

SELECT sqlite_version();
PRAGMA compile_options;

Run the same data, parameters, and expected result lists on both platforms. A successful Mac command-line run only shows that the current library satisfies the fixture. The final verdict must come from the database connection the app actually uses. Run the fixture through the production query method as well, so you are not merely testing a separate copy of the SQL.

Turn failures into traceable regression records

Before merging, add four more input categories: consecutive percent signs, a trailing backslash, Chinese text mixed with wildcards, and text exceeding the product’s length limit. Reject overlong input explicitly at the entry point. Do not truncate it and continue querying, or the search will no longer reflect what the user submitted.

For each failure, record the original input, the converted pattern, and the expected and actual result lists. Use only synthetic samples in logs; do not retain real users’ search terms. Give each parallel test its own in-memory database so that data inserted by one test lane cannot affect another.

Finally, verify that an empty query really executes no SQL and that input length validation happens before pattern generation. Once these boundary cases are part of a fixed sample set, search acceptance testing no longer depends on someone “trying a few words.” It can identify exactly which step changed the matching semantics.

Frequently asked questions

Does binding a parameter make a percent sign literal?

No. Binding separates values from SQL syntax, but LIKE still interprets wildcards inside the value. Escape the escape character first, then % and _, and declare ESCAPE explicitly in the query.

Is a passing SQLite test on macOS enough?

No. The Mac command-line library may differ from the SQLite library linked by the iOS app. Run the same fixtures in the iOS test target and record sqlite_version() plus relevant compile options.

A dedicated Mac for every test lane

Rent a full physical Mac mini by the day, week, month, or quarter, with isolated test environments. Track setup progress in the console; service stops at the end of your term unless you renew. Submit a support ticket in the console if you need help.

Rent a Cloud Mac

Set up a Mac for your next test lane

Get an entire physical Mac mini to yourself and rent it for your test cycle. Track setup progress in the console; service stops at the end of your term unless you renew.