Add Geary.ContactStore.search method, impementation and tests

This commit is contained in:
Michael Gratton 2019-06-12 07:43:02 +10:00 committed by Michael James Gratton
parent 66a664f98d
commit 71cb7fcdfe
7 changed files with 239 additions and 48 deletions

View file

@ -16,26 +16,6 @@ public class Geary.MockAccount : Account, MockObject {
}
public class MockContactStore : GLib.Object, ContactStore {
internal MockContactStore() {
}
public async Contact? get_by_rfc822(Geary.RFC822.MailboxAddress address,
GLib.Cancellable? cancellable)
throws GLib.Error {
throw new EngineError.UNSUPPORTED("Mock method");
}
public async void update_contacts(Gee.Collection<Contact> contacts,
GLib.Cancellable? cancellable)
throws GLib.Error {
throw new EngineError.UNSUPPORTED("Mock method");
}
}
public class MockClientService : ClientService {

View file

@ -14,7 +14,26 @@ internal class Geary.ContactStoreMock : ContactStore, MockObject, GLib.Object {
public async Contact? get_by_rfc822(Geary.RFC822.MailboxAddress address,
GLib.Cancellable? cancellable)
throws GLib.Error {
return object_call<Contact?>("get_by_rfc822", { address }, null);
return object_call<Contact?>(
"get_by_rfc822", { address, cancellable }, null
);
}
public async Gee.Collection<Contact> search(string query,
uint min_importance,
uint limit,
GLib.Cancellable? cancellable)
throws GLib.Error {
return object_call<Gee.Collection<Contact>>(
"search",
{
box_arg(query),
uint_arg(min_importance),
uint_arg(limit),
cancellable
},
Gee.Collection.empty<Contact>()
);
}
public async void update_contacts(Gee.Collection<Contact> updated,

View file

@ -17,6 +17,9 @@ class Geary.ContactStoreImplTest : TestCase {
public ContactStoreImplTest() {
base("Geary.ContactStoreImplTest");
add_test("get_by_rfc822", get_by_rfc822);
add_test("search_no_match", search_no_match);
add_test("search_email_match", search_email_match);
add_test("search_name_match", search_name_match);
add_test("update_new_contact", update_new_contact);
add_test("update_existing_contact", update_existing_contact);
}
@ -51,7 +54,7 @@ INSERT INTO ContactTable (
) VALUES (
1,
'test@example.com',
'Test',
'Test Name',
'Test@example.com',
50
);
@ -80,7 +83,7 @@ INSERT INTO ContactTable (
assert_non_null(existing, "Existing contact");
assert_string("Test@example.com", existing.email, "Existing email");
assert_string("test@example.com", existing.normalized_email, "Existing normalized_email");
assert_string("Test", existing.real_name, "Existing real_name");
assert_string("Test Name", existing.real_name, "Existing real_name");
assert_int(50, existing.highest_importance, "Existing highest_importance");
assert_false(existing.flags.always_load_remote_images(), "Existing flags");
@ -93,6 +96,62 @@ INSERT INTO ContactTable (
assert_null(missing, "Missing contact");
}
public void search_no_match() throws GLib.Error {
test_article.search.begin(
"blarg",
0,
10,
null,
(obj, ret) => { async_complete(ret); }
);
Gee.Collection<Contact> results = test_article.search.end(
async_result()
);
assert_int(0, results.size);
}
public void search_email_match() throws GLib.Error {
test_article.search.begin(
"example.com",
0,
10,
null,
(obj, ret) => { async_complete(ret); }
);
Gee.Collection<Contact> results = test_article.search.end(
async_result()
);
assert_int(1, results.size, "results.size");
Contact search_hit = Collection.get_first(results);
assert_string("Test@example.com", search_hit.email, "Existing email");
assert_string("test@example.com", search_hit.normalized_email, "Existing normalized_email");
assert_string("Test Name", search_hit.real_name, "Existing real_name");
assert_int(50, search_hit.highest_importance, "Existing highest_importance");
assert_false(search_hit.flags.always_load_remote_images(), "Existing flags");
}
public void search_name_match() throws GLib.Error {
test_article.search.begin(
"Test Name",
0,
10,
null,
(obj, ret) => { async_complete(ret); }
);
Gee.Collection<Contact> results = test_article.search.end(
async_result()
);
assert_int(1, results.size, "results.size");
Contact search_hit = Collection.get_first(results);
assert_string("Test@example.com", search_hit.email, "Existing email");
assert_string("test@example.com", search_hit.normalized_email, "Existing normalized_email");
assert_string("Test Name", search_hit.real_name, "Existing real_name");
assert_int(50, search_hit.highest_importance, "Existing highest_importance");
assert_false(search_hit.flags.always_load_remote_images(), "Existing flags");
}
public void update_new_contact() throws GLib.Error {
Contact not_persisted = new Contact(
"New@example.com",

View file

@ -16,6 +16,8 @@ class Geary.ImapDB.DatabaseTest : TestCase {
base("Geary.ImapDb.DatabaseTest");
add_test("open_new", open_new);
add_test("upgrade_0_6", upgrade_0_6);
add_test("utf8_case_insensitive_collation",
utf8_case_insensitive_collation);
}
public override void set_up() throws GLib.Error {
@ -123,6 +125,46 @@ class Geary.ImapDB.DatabaseTest : TestCase {
db.close();
}
public void utf8_case_insensitive_collation() throws GLib.Error {
Database db = new Database(
this.tmp_dir.get_child("test.db"),
GLib.File.new_for_path(_SOURCE_ROOT_DIR).get_child("sql"),
this.tmp_dir.get_child("attachments"),
new Geary.SimpleProgressMonitor(Geary.ProgressType.DB_UPGRADE),
new Geary.SimpleProgressMonitor(Geary.ProgressType.DB_VACUUM)
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_FILE, null,
(obj, ret) => { async_complete(ret); }
);
db.open.end(async_result());
db.exec("""
CREATE TABLE Test (id INTEGER PRIMARY KEY, test_str TEXT);
INSERT INTO Test (test_str) VALUES ('a');
INSERT INTO Test (test_str) VALUES ('B');
INSERT INTO Test (test_str) VALUES ('BB');
INSERT INTO Test (test_str) VALUES ('🤯');
""");
string[] expected = { "🤯", "BB", "B", "a" };
Db.Result result = db.query(
"SELECT test_str FROM Test ORDER BY test_str COLLATE UTF8ICASE DESC"
);
int i = 0;
while (!result.finished) {
assert_true(i < expected.length, "Too many rows");
assert_string(expected[i], result.string_at(0));
i++;
result.next();
}
assert_true(i == expected.length, "Not enough rows");
// Need to close it again to stop the GC process running
db.close();
}
private void unpack_archive(GLib.File archive, GLib.File dest)
throws Error {