mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-31 03:27:40 +01:00
Tests and refactoring for FTS.
This commit is contained in:
parent
f989976d30
commit
abf79ca447
@ -29,6 +29,12 @@
|
||||
const char* Database::kDatabaseFilename = "clementine.db";
|
||||
const int Database::kSchemaVersion = 13;
|
||||
|
||||
Database::Token::Token(const QString& token, int start, int end)
|
||||
: token(token),
|
||||
start_offset(start),
|
||||
end_offset(end) {
|
||||
}
|
||||
|
||||
struct sqlite3_tokenizer_module {
|
||||
int iVersion;
|
||||
int (*xCreate)(
|
||||
@ -136,12 +142,8 @@ int Database::FTSOpen(
|
||||
if (!data[i].isLetterOrNumber()) {
|
||||
// Token finished.
|
||||
if (token.length() != 0) {
|
||||
Token t;
|
||||
t.token = token;
|
||||
t.start_offset = start_offset;
|
||||
t.end_offset = offset - 1;
|
||||
tokens << Token(token, start_offset, offset - 1);
|
||||
start_offset = offset;
|
||||
tokens << t;
|
||||
token.clear();
|
||||
} else {
|
||||
++start_offset;
|
||||
@ -156,12 +158,7 @@ int Database::FTSOpen(
|
||||
|
||||
if (i == str.length() - 1) {
|
||||
if (token.length() != 0) {
|
||||
Token t;
|
||||
t.token = token;
|
||||
t.start_offset = start_offset;
|
||||
t.end_offset = offset;
|
||||
start_offset = offset;
|
||||
tokens << t;
|
||||
tokens << Token(token, start_offset, offset);
|
||||
token.clear();
|
||||
}
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ class Database : public QObject {
|
||||
FRIEND_TEST(DatabaseTest, LikeDecomposes);
|
||||
FRIEND_TEST(DatabaseTest, FTSOpenParsesSimpleInput);
|
||||
FRIEND_TEST(DatabaseTest, FTSOpenParsesUTF8Input);
|
||||
FRIEND_TEST(DatabaseTest, FTSOpenParsesMultipleTokens);
|
||||
|
||||
// Do static initialisation like loading sqlite functions.
|
||||
static void StaticInit();
|
||||
@ -123,6 +124,7 @@ class Database : public QObject {
|
||||
int* end_offset,
|
||||
int* position);
|
||||
struct Token {
|
||||
Token(const QString& token, int start, int end);
|
||||
QString token;
|
||||
int start_offset;
|
||||
int end_offset;
|
||||
|
@ -131,3 +131,52 @@ TEST_F(DatabaseTest, LikeDecomposes) {
|
||||
EXPECT_TRUE(database_->Like("%tiesto%", "DJ Tiësto"));
|
||||
EXPECT_FALSE(database_->Like("%tisto%", "DJ Tiësto"));
|
||||
}
|
||||
|
||||
TEST_F(DatabaseTest, FTSOpenParsesSimpleInput) {
|
||||
sqlite3_tokenizer_cursor* cursor = NULL;
|
||||
Database::FTSOpen(NULL, "foo", 3, &cursor);
|
||||
ASSERT_TRUE(cursor);
|
||||
Database::UnicodeTokenizerCursor* real_cursor = reinterpret_cast<Database::UnicodeTokenizerCursor*>(cursor);
|
||||
QList<Database::Token> tokens = real_cursor->tokens;
|
||||
ASSERT_EQ(1, tokens.length());
|
||||
EXPECT_EQ(0, real_cursor->position);
|
||||
EXPECT_TRUE(real_cursor->current_utf8.isEmpty());
|
||||
|
||||
EXPECT_EQ("foo", tokens[0].token);
|
||||
EXPECT_EQ(0, tokens[0].start_offset);
|
||||
EXPECT_EQ(3, tokens[0].end_offset);
|
||||
}
|
||||
|
||||
TEST_F(DatabaseTest, FTSOpenParsesUTF8Input) {
|
||||
sqlite3_tokenizer_cursor* cursor = NULL;
|
||||
Database::FTSOpen(NULL, "Röyksopp", 9, &cursor);
|
||||
ASSERT_TRUE(cursor);
|
||||
Database::UnicodeTokenizerCursor* real_cursor = reinterpret_cast<Database::UnicodeTokenizerCursor*>(cursor);
|
||||
QList<Database::Token> tokens = real_cursor->tokens;
|
||||
ASSERT_EQ(1, tokens.length());
|
||||
EXPECT_EQ(0, real_cursor->position);
|
||||
EXPECT_TRUE(real_cursor->current_utf8.isEmpty());
|
||||
|
||||
EXPECT_EQ("royksopp", tokens[0].token);
|
||||
EXPECT_EQ(0, tokens[0].start_offset);
|
||||
EXPECT_EQ(9, tokens[0].end_offset);
|
||||
}
|
||||
|
||||
TEST_F(DatabaseTest, FTSOpenParsesMultipleTokens) {
|
||||
sqlite3_tokenizer_cursor* cursor = NULL;
|
||||
Database::FTSOpen(NULL, "Röyksopp foo", 13, &cursor);
|
||||
ASSERT_TRUE(cursor);
|
||||
Database::UnicodeTokenizerCursor* real_cursor = reinterpret_cast<Database::UnicodeTokenizerCursor*>(cursor);
|
||||
QList<Database::Token> tokens = real_cursor->tokens;
|
||||
ASSERT_EQ(2, tokens.length());
|
||||
EXPECT_EQ(0, real_cursor->position);
|
||||
EXPECT_TRUE(real_cursor->current_utf8.isEmpty());
|
||||
|
||||
EXPECT_EQ("royksopp", tokens[0].token);
|
||||
EXPECT_EQ(0, tokens[0].start_offset);
|
||||
EXPECT_EQ(9, tokens[0].end_offset);
|
||||
|
||||
EXPECT_EQ("foo", tokens[1].token);
|
||||
EXPECT_EQ(10, tokens[1].start_offset);
|
||||
EXPECT_EQ(13, tokens[1].end_offset);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user