|
| 1 | +import XCTest |
| 2 | +import SQLite3 |
| 3 | +@testable import SQLite |
| 4 | + |
| 5 | +class QueryPlanParserTests: XCTestCase { |
| 6 | + func testColumnsFromSingleTables() { |
| 7 | + let tables = ["conversations", "TABLE", "SCAN"] |
| 8 | + let queryPlan: Array<SQLiteRow> = self.queryPlan([(1, 0, 0, "SCAN TABLE conversations")]) |
| 9 | + let expected: Set<String> = ["conversations"] |
| 10 | + let actual = SQLite.QueryPlanParser.tables(in: queryPlan, matching: tables) |
| 11 | + XCTAssertEqual(expected, actual) |
| 12 | + } |
| 13 | + |
| 14 | + func testColumnsFromMultipleTables() { |
| 15 | + let tables = ["✌🏼 table", "first table", "sqlite_autoindex_✌🏼 table_1", "USING"] |
| 16 | + let queryPlan: Array<SQLiteRow> = self.queryPlan([ |
| 17 | + (1, 0, 0, "SEARCH TABLE ✌🏼 table USING INDEX sqlite_autoindex_✌🏼 table_1 (id column=?)"), |
| 18 | + (4, 0, 0, "SEARCH TABLE first table USING INDEX sqlite_autoindex_first table_1 (id column=?)") |
| 19 | + ]) |
| 20 | + let expected: Set<String> = ["first table", "✌🏼 table"] |
| 21 | + let actual = SQLite.QueryPlanParser.tables(in: queryPlan, matching: tables) |
| 22 | + XCTAssertEqual(expected, actual) |
| 23 | + } |
| 24 | + |
| 25 | + func testColumnsWithMergesJoinsAndJSON() { |
| 26 | + let tables = ["AS", "text_messages", "providers", "patients", "|||"] |
| 27 | + let queryPlan: Array<SQLiteRow> = self.queryPlan([ |
| 28 | + (1, 0, 0, "MERGE (UNION ALL)"), |
| 29 | + (3, 1, 0, "LEFT"), |
| 30 | + (10, 3, 0, "SEARCH TABLE text_messages USING INDEX text_messages_index"), |
| 31 | + (18, 3, 0, "SCAN TABLE patients"), |
| 32 | + (27, 3, 0, "SCAN TABLE json_each AS USING VIRTUAL TABLE INDEX 1:"), |
| 33 | + (52, 1, 0, "RIGHT"), |
| 34 | + (62, 52, 0, "SEARCH TABLE text_messages USING CONVERING INDEX sqlite_autoindex_1"), |
| 35 | + (66, 52, 0, "SCAN TABLE json_each AS USING VIRTUAL TABLE INDEX 1:"), |
| 36 | + ]) |
| 37 | + let expected: Set<String> = ["text_messages", "patients"] |
| 38 | + let actual = SQLite.QueryPlanParser.tables(in: queryPlan, matching: tables) |
| 39 | + XCTAssertEqual(expected, actual) |
| 40 | + } |
| 41 | + |
| 42 | + func testColumnsWithSimilarNames() { |
| 43 | + let tables = ["a", "ab", "abc", "abcd"] |
| 44 | + let queryPlan: Array<SQLiteRow> = self.queryPlan([ |
| 45 | + (1, 0, 0, "SCAN TABLE a"), |
| 46 | + (3, 1, 0, "SCAN TABLE abcd"), |
| 47 | + (10, 3, 0, "SEARCH TABLE ab USING INDEX ab_index"), |
| 48 | + ]) |
| 49 | + let expected: Set<String> = ["a", "ab", "abcd"] |
| 50 | + let actual = SQLite.QueryPlanParser.tables(in: queryPlan, matching: tables) |
| 51 | + XCTAssertEqual(expected, actual) |
| 52 | + } |
| 53 | + |
| 54 | + func testColumnsWithReservedWordsAndControlCharacters() { |
| 55 | + let tables = ["USING", "| |", "AS", "&&", "||", "USING AS"] |
| 56 | + let queryPlan: Array<SQLiteRow> = self.queryPlan([ |
| 57 | + (1, 0, 0, "SEARCH TABLE USING AS USING USING_AS_index"), |
| 58 | + (3, 1, 0, "SCAN TABLE &&"), |
| 59 | + (10, 3, 0, "SEARCH TABLE | | USING INDEX ab_index"), |
| 60 | + ]) |
| 61 | + let expected: Set<String> = ["USING AS", "&&", "| |"] |
| 62 | + let actual = SQLite.QueryPlanParser.tables(in: queryPlan, matching: tables) |
| 63 | + XCTAssertEqual(expected, actual) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension QueryPlanParserTests { |
| 68 | + private func queryPlan(_ rows: Array<(Int, Int, Int, String)>) -> Array<SQLiteRow> { |
| 69 | + return rows.map { queryPlan($0, $1, $2, $3) } |
| 70 | + } |
| 71 | + |
| 72 | + private func queryPlan(_ id: Int, _ parent: Int, _ notused: Int, |
| 73 | + _ detail: String) -> SQLiteRow { |
| 74 | + return ["id": .integer(Int64(id)), "parent": .integer(Int64(parent)), |
| 75 | + "notused": .integer(Int64(notused)), "detail": .text(detail)] |
| 76 | + } |
| 77 | +} |
0 commit comments