forked from stephencelis/SQLite.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFTS4Tests.swift
190 lines (164 loc) · 6.9 KB
/
FTS4Tests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import XCTest
import SQLite
class FTS4Tests: XCTestCase {
func test_create_onVirtualTable_withFTS4_compilesCreateVirtualTableExpression() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4()",
virtualTable.create(.FTS4())
)
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(\"string\")",
virtualTable.create(.FTS4(string))
)
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=simple)",
virtualTable.create(.FTS4(tokenize: .Simple))
)
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(\"string\", tokenize=porter)",
virtualTable.create(.FTS4([string], tokenize: .Porter))
)
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=unicode61 \"remove_diacritics=0\")",
virtualTable.create(.FTS4(tokenize: .Unicode61(removeDiacritics: false)))
)
XCTAssertEqual(
"""
CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=unicode61 \"remove_diacritics=1\"
\"tokenchars=.\" \"separators=X\")
""".replacingOccurrences(of: "\n", with: ""),
virtualTable.create(.FTS4(tokenize: .Unicode61(removeDiacritics: true,
tokenchars: ["."], separators: ["X"])))
)
}
func test_match_onVirtualTableAsExpression_compilesMatchExpression() {
assertSQL("(\"virtual_table\" MATCH 'string')", virtualTable.match("string") as SQLite.Expression<Bool>)
assertSQL("(\"virtual_table\" MATCH \"string\")", virtualTable.match(string) as SQLite.Expression<Bool>)
assertSQL("(\"virtual_table\" MATCH \"stringOptional\")", virtualTable.match(stringOptional) as SQLite.Expression<Bool?>)
}
func test_match_onVirtualTableAsQueryType_compilesMatchExpression() {
assertSQL("SELECT * FROM \"virtual_table\" WHERE (\"virtual_table\" MATCH 'string')",
virtualTable.match("string") as QueryType)
assertSQL("SELECT * FROM \"virtual_table\" WHERE (\"virtual_table\" MATCH \"string\")",
virtualTable.match(string) as QueryType)
assertSQL("SELECT * FROM \"virtual_table\" WHERE (\"virtual_table\" MATCH \"stringOptional\")",
virtualTable.match(stringOptional) as QueryType)
}
}
class FTS4ConfigTests: XCTestCase {
var config: FTS4Config!
override func setUp() {
super.setUp()
config = FTS4Config()
}
func test_empty_config() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4()",
sql(config))
}
func test_config_column() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(\"string\")",
sql(config.column(string)))
}
func test_config_columns() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(\"string\", \"int\")",
sql(config.columns([string, int])))
}
func test_config_unindexed_column() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(\"string\", notindexed=\"string\")",
sql(config.column(string, [.unindexed])))
}
func test_external_content_view() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(content=\"view\")",
sql(config.externalContent(_view )))
}
func test_external_content_virtual_table() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(content=\"virtual_table\")",
sql(config.externalContent(virtualTable)))
}
func test_tokenizer_simple() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=simple)",
sql(config.tokenizer(.Simple)))
}
func test_tokenizer_porter() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=porter)",
sql(config.tokenizer(.Porter)))
}
func test_tokenizer_unicode61() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=unicode61)",
sql(config.tokenizer(.Unicode61())))
}
func test_tokenizer_unicode61_with_options() {
XCTAssertEqual(
"""
CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(tokenize=unicode61 \"remove_diacritics=1\"
\"tokenchars=.\" \"separators=X\")
""".replacingOccurrences(of: "\n", with: ""),
sql(config.tokenizer(.Unicode61(removeDiacritics: true, tokenchars: ["."], separators: ["X"]))))
}
func test_content_less() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(content=\"\")",
sql(config.contentless()))
}
func test_config_matchinfo() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(matchinfo=\"fts3\")",
sql(config.matchInfo(.fts3)))
}
func test_config_order_asc() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(order=\"asc\")",
sql(config.order(.asc)))
}
func test_config_order_desc() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(order=\"desc\")",
sql(config.order(.desc)))
}
func test_config_compress() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(compress=\"compress_foo\")",
sql(config.compress("compress_foo")))
}
func test_config_uncompress() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(uncompress=\"uncompress_foo\")",
sql(config.uncompress("uncompress_foo")))
}
func test_config_languageId() {
XCTAssertEqual(
"CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(languageid=\"lid\")",
sql(config.languageId("lid")))
}
func test_config_all() {
XCTAssertEqual(
"""
CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(\"int\", \"string\", \"date\",
tokenize=porter, prefix=\"2,4\", content=\"table\", notindexed=\"string\", notindexed=\"date\",
languageid=\"lid\", matchinfo=\"fts3\", order=\"desc\")
""".replacingOccurrences(of: "\n", with: ""),
sql(config
.tokenizer(.Porter)
.column(int)
.column(string, [.unindexed])
.column(date, [.unindexed])
.externalContent(table)
.matchInfo(.fts3)
.languageId("lid")
.order(.desc)
.prefix([2, 4]))
)
}
func sql(_ config: FTS4Config) -> String {
virtualTable.create(.FTS4(config))
}
}