Add unit tests for custom LIKE() function.

This commit is contained in:
John Maguire 2010-03-22 16:03:24 +00:00
parent fac97046f9
commit 2f350c1ae6
3 changed files with 35 additions and 5 deletions

View File

@ -63,6 +63,12 @@ bool LibraryBackend::StaticInit() {
return false;
}
bool LibraryBackend::Like(const char* needle, const char* haystack) {
QString a = QString::fromUtf8(needle).section('%', 1, 1);
QString b = QString::fromUtf8(haystack);
return b.contains(a, Qt::CaseInsensitive);
}
// Custom LIKE(X, Y) function for sqlite3 that supports case insensitive unicode matching.
void LibraryBackend::SqliteLike(sqlite3_context* context, int argc, sqlite3_value** argv) {
Q_ASSERT(argc == 2 || argc == 3);
@ -74,11 +80,9 @@ void LibraryBackend::SqliteLike(sqlite3_context* context, int argc, sqlite3_valu
break;
}
case SQLITE_TEXT: {
const uchar* data_a = _sqlite3_value_text(argv[0]);
const uchar* data_b = _sqlite3_value_text(argv[1]);
QString a = QString::fromUtf8(reinterpret_cast<const char*>(data_a)).section('%', 1, 1);
QString b = QString::fromUtf8(reinterpret_cast<const char*>(data_b));
_sqlite3_result_int64(context, b.contains(a, Qt::CaseInsensitive) ? 1 : 0);
const char* data_a = reinterpret_cast<const char*>(_sqlite3_value_text(argv[0]));
const char* data_b = reinterpret_cast<const char*>(_sqlite3_value_text(argv[1]));
_sqlite3_result_int64(context, Like(data_a, data_b) ? 1 : 0);
break;
}
}

View File

@ -13,6 +13,8 @@
#include <sqlite3.h>
#include "gtest/gtest_prod.h"
class LibraryBackend : public QObject {
Q_OBJECT
@ -113,9 +115,15 @@ class LibraryBackend : public QObject {
QString injected_database_name_;
FRIEND_TEST(LibraryBackendTest, LikeWorksWithAllAscii);
FRIEND_TEST(LibraryBackendTest, LikeWorksWithUnicode);
FRIEND_TEST(LibraryBackendTest, LikeAsciiCaseInsensitive);
FRIEND_TEST(LibraryBackendTest, LikeUnicodeCaseInsensitive);
// Do static initialisation like loading sqlite functions.
static bool StaticInit();
// Custom LIKE() function for sqlite.
static bool Like(const char* needle, const char* haystack);
static void SqliteLike(sqlite3_context* context, int argc, sqlite3_value** argv);
typedef void (*Sqlite3CreateFunc) (
sqlite3*, const char*, int, int, void*,

View File

@ -140,6 +140,24 @@ TEST_F(LibraryBackendTest, AddInvalidSong) {
TEST_F(LibraryBackendTest, GetAlbumArtNonExistent) {
}
TEST_F(LibraryBackendTest, LikeWorksWithAllAscii) {
EXPECT_TRUE(LibraryBackend::Like("%ar%", "bar"));
EXPECT_FALSE(LibraryBackend::Like("%ar%", "foo"));
}
TEST_F(LibraryBackendTest, LikeWorksWithUnicode) {
EXPECT_TRUE(LibraryBackend::Like("%Снег%", "Снег"));
EXPECT_FALSE(LibraryBackend::Like("%Снег%", "foo"));
}
TEST_F(LibraryBackendTest, LikeAsciiCaseInsensitive) {
EXPECT_TRUE(LibraryBackend::Like("%ar%", "BAR"));
EXPECT_FALSE(LibraryBackend::Like("%ar%", "FOO"));
}
TEST_F(LibraryBackendTest, LikeUnicodeCaseInsensitive) {
EXPECT_TRUE(LibraryBackend::Like("%снег%", "Снег"));
}
// Test adding a single song to the database, then getting various information
// back about it.