ci: Test all databases in unit tests

This commit is contained in:
Krille 2023-12-15 12:26:08 +01:00 committed by Krille
parent 26c1629e7e
commit d8ec1601a3
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
1 changed files with 456 additions and 446 deletions

View File

@ -27,465 +27,475 @@ import 'package:matrix/matrix.dart';
import 'fake_database.dart'; import 'fake_database.dart';
void main() { void main() {
group('Matrix SDK Database Test', () { final databaseBuilders = {
late DatabaseApi database; 'Matrix SDK Database': getMatrixSdkDatabase,
late int toDeviceQueueIndex; 'Hive Database': getHiveDatabase,
test('Setup', () async { 'Hive Collections Database': getHiveCollectionsDatabase,
database = await getMatrixSdkDatabase(null); };
});
test('transaction', () async { for (final databaseBuilder in databaseBuilders.entries) {
var counter = 0; group('Test ${databaseBuilder.key}', () {
await database.transaction(() async { late DatabaseApi database;
expect(counter++, 0); late int toDeviceQueueIndex;
test('Setup', () async {
database = await databaseBuilder.value(null);
});
test('transaction', () async {
var counter = 0;
await database.transaction(() async { await database.transaction(() async {
expect(counter++, 1); expect(counter++, 0);
await Future.delayed(Duration(milliseconds: 50)); await database.transaction(() async {
expect(counter++, 2); expect(counter++, 1);
await Future.delayed(Duration(milliseconds: 50));
expect(counter++, 2);
});
expect(counter++, 3);
}); });
expect(counter++, 3);
}); });
}); test('insertIntoToDeviceQueue', () async {
test('insertIntoToDeviceQueue', () async { toDeviceQueueIndex = await database.insertIntoToDeviceQueue(
toDeviceQueueIndex = await database.insertIntoToDeviceQueue( 'm.test',
'm.test', 'txnId',
'txnId', '{"foo":"bar"}',
'{"foo":"bar"}', );
);
});
test('getToDeviceEventQueue', () async {
final toDeviceQueue = await database.getToDeviceEventQueue();
expect(toDeviceQueue.first.type, 'm.test');
});
test('deleteFromToDeviceQueue', () async {
await database.deleteFromToDeviceQueue(toDeviceQueueIndex);
final toDeviceQueue = await database.getToDeviceEventQueue();
expect(toDeviceQueue.isEmpty, true);
});
test('storeFile', () async {
await database.storeFile(
Uri.parse('mxc://test'), Uint8List.fromList([0]), 0);
final file = await database.getFile(Uri.parse('mxc://test'));
expect(file != null, database.supportsFileStoring);
});
test('getFile', () async {
await database.getFile(Uri.parse('mxc://test'));
});
test('deleteOldFiles', () async {
await database.deleteOldFiles(1);
final file = await database.getFile(Uri.parse('mxc://test'));
expect(file == null, true);
});
test('storeRoomUpdate', () async {
final roomUpdate = JoinedRoomUpdate.fromJson({
'highlight_count': 0,
'notification_count': 0,
'limited_timeline': false,
'membership': Membership.join,
}); });
final client = Client('testclient'); test('getToDeviceEventQueue', () async {
await database.storeRoomUpdate('!testroom', roomUpdate, client); final toDeviceQueue = await database.getToDeviceEventQueue();
final rooms = await database.getRoomList(client); expect(toDeviceQueue.first.type, 'm.test');
expect(rooms.single.id, '!testroom'); });
}); test('deleteFromToDeviceQueue', () async {
test('getRoomList', () async { await database.deleteFromToDeviceQueue(toDeviceQueueIndex);
final room = final toDeviceQueue = await database.getToDeviceEventQueue();
await database.getSingleRoom(Client('testclient'), '!testroom'); expect(toDeviceQueue.isEmpty, true);
expect(room?.id, '!testroom'); });
}); test('storeFile', () async {
test('getRoomList', () async { await database.storeFile(
final list = await database.getRoomList(Client('testclient')); Uri.parse('mxc://test'), Uint8List.fromList([0]), 0);
expect(list.single.id, '!testroom'); final file = await database.getFile(Uri.parse('mxc://test'));
}); expect(file != null, database.supportsFileStoring);
test('setRoomPrevBatch', () async { });
final client = Client('testclient'); test('getFile', () async {
await database.setRoomPrevBatch('1234', '!testroom', client); await database.getFile(Uri.parse('mxc://test'));
final rooms = await database.getRoomList(client); });
expect(rooms.single.prev_batch, '1234'); test('deleteOldFiles', () async {
}); await database.deleteOldFiles(1);
test('forgetRoom', () async { final file = await database.getFile(Uri.parse('mxc://test'));
await database.forgetRoom('!testroom'); expect(file == null, true);
final rooms = await database.getRoomList(Client('testclient')); });
expect(rooms.isEmpty, true); test('storeRoomUpdate', () async {
}); final roomUpdate = JoinedRoomUpdate.fromJson({
test('getClient', () async { 'highlight_count': 0,
await database.getClient('name'); 'notification_count': 0,
}); 'limited_timeline': false,
test('insertClient', () async { 'membership': Membership.join,
await database.insertClient( });
'name', final client = Client('testclient');
'homeserverUrl', await database.storeRoomUpdate('!testroom', roomUpdate, client);
'token', final rooms = await database.getRoomList(client);
'userId', expect(rooms.single.id, '!testroom');
'deviceId', });
'deviceName', test('getRoomList', () async {
'prevBatch', final room =
'olmAccount', await database.getSingleRoom(Client('testclient'), '!testroom');
); expect(room?.id, '!testroom');
});
test('getRoomList', () async {
final list = await database.getRoomList(Client('testclient'));
expect(list.single.id, '!testroom');
});
test('setRoomPrevBatch', () async {
final client = Client('testclient');
await database.setRoomPrevBatch('1234', '!testroom', client);
final rooms = await database.getRoomList(client);
expect(rooms.single.prev_batch, '1234');
});
test('forgetRoom', () async {
await database.forgetRoom('!testroom');
final rooms = await database.getRoomList(Client('testclient'));
expect(rooms.isEmpty, true);
});
test('getClient', () async {
await database.getClient('name');
});
test('insertClient', () async {
await database.insertClient(
'name',
'homeserverUrl',
'token',
'userId',
'deviceId',
'deviceName',
'prevBatch',
'olmAccount',
);
final client = await database.getClient('name'); final client = await database.getClient('name');
expect(client?['token'], 'token'); expect(client?['token'], 'token');
}); });
test('updateClient', () async { test('updateClient', () async {
await database.updateClient( await database.updateClient(
'homeserverUrl', 'homeserverUrl',
'token_different', 'token_different',
'userId', 'userId',
'deviceId', 'deviceId',
'deviceName', 'deviceName',
'prevBatch', 'prevBatch',
'olmAccount', 'olmAccount',
); );
final client = await database.getClient('name'); final client = await database.getClient('name');
expect(client?['token'], 'token_different'); expect(client?['token'], 'token_different');
}); });
test('updateClientKeys', () async { test('updateClientKeys', () async {
await database.updateClientKeys( await database.updateClientKeys(
'olmAccount2', 'olmAccount2',
); );
final client = await database.getClient('name'); final client = await database.getClient('name');
expect(client?['olm_account'], 'olmAccount2'); expect(client?['olm_account'], 'olmAccount2');
}); });
test('storeSyncFilterId', () async { test('storeSyncFilterId', () async {
await database.storeSyncFilterId( await database.storeSyncFilterId(
'1234', '1234',
); );
final client = await database.getClient('name'); final client = await database.getClient('name');
expect(client?['sync_filter_id'], '1234'); expect(client?['sync_filter_id'], '1234');
}); });
test('getAccountData', () async { test('getAccountData', () async {
await database.getAccountData(); await database.getAccountData();
}); });
test('storeAccountData', () async { test('storeAccountData', () async {
await database.storeAccountData('m.test', '{"foo":"bar"}'); await database.storeAccountData('m.test', '{"foo":"bar"}');
final events = await database.getAccountData(); final events = await database.getAccountData();
expect(events.values.single.type, 'm.test'); expect(events.values.single.type, 'm.test');
await database.storeAccountData('m.abc+de', '{"foo":"bar"}'); await database.storeAccountData('m.abc+de', '{"foo":"bar"}');
final events2 = await database.getAccountData(); final events2 = await database.getAccountData();
expect(events2.values.any((element) => element.type == 'm.abc+de'), true); expect(
}); events2.values.any((element) => element.type == 'm.abc+de'), true);
test('storeEventUpdate', () async { });
await database.storeEventUpdate( test('storeEventUpdate', () async {
EventUpdate( await database.storeEventUpdate(
roomID: '!testroom:example.com', EventUpdate(
type: EventUpdateType.timeline, roomID: '!testroom:example.com',
content: { type: EventUpdateType.timeline,
'type': EventTypes.Message, content: {
'content': { 'type': EventTypes.Message,
'body': '* edit 3', 'content': {
'msgtype': 'm.text', 'body': '* edit 3',
'm.new_content': {
'body': 'edit 3',
'msgtype': 'm.text', 'msgtype': 'm.text',
'm.new_content': {
'body': 'edit 3',
'msgtype': 'm.text',
},
'm.relates_to': {
'event_id': '\$source',
'rel_type': RelationshipTypes.edit,
},
}, },
'm.relates_to': { 'event_id': '\$event:example.com',
'event_id': '\$source', 'sender': '@bob:example.org',
'rel_type': RelationshipTypes.edit,
},
}, },
'event_id': '\$event:example.com', ),
'sender': '@bob:example.org', Client('testclient'));
}, });
), test('getEventById', () async {
Client('testclient')); final event = await database.getEventById('\$event:example.com',
}); Room(id: '!testroom:example.com', client: Client('testclient')));
test('getEventById', () async { expect(event?.type, EventTypes.Message);
final event = await database.getEventById('\$event:example.com', });
Room(id: '!testroom:example.com', client: Client('testclient'))); test('getEventList', () async {
expect(event?.type, EventTypes.Message); final events = await database.getEventList(
}); Room(id: '!testroom:example.com', client: Client('testclient')),
test('getEventList', () async { );
final events = await database.getEventList( expect(events.single.type, EventTypes.Message);
Room(id: '!testroom:example.com', client: Client('testclient')), });
); test('getUser', () async {
expect(events.single.type, EventTypes.Message); final user = await database.getUser('@bob:example.org',
}); Room(id: '!testroom:example.com', client: Client('testclient')));
test('getUser', () async { expect(user, null);
final user = await database.getUser('@bob:example.org', });
Room(id: '!testroom:example.com', client: Client('testclient'))); test('getUsers', () async {
expect(user, null); final users = await database.getUsers(
}); Room(id: '!testroom:example.com', client: Client('testclient')));
test('getUsers', () async { expect(users.isEmpty, true);
final users = await database.getUsers( });
Room(id: '!testroom:example.com', client: Client('testclient'))); test('removeEvent', () async {
expect(users.isEmpty, true); await database.removeEvent(
}); '\$event:example.com', '!testroom:example.com');
test('removeEvent', () async { final event = await database.getEventById('\$event:example.com',
await database.removeEvent( Room(id: '!testroom:example.com', client: Client('testclient')));
'\$event:example.com', '!testroom:example.com'); expect(event, null);
final event = await database.getEventById('\$event:example.com', });
Room(id: '!testroom:example.com', client: Client('testclient'))); test('getAllInboundGroupSessions', () async {
expect(event, null); final result = await database.getAllInboundGroupSessions();
}); expect(result.isEmpty, true);
test('getAllInboundGroupSessions', () async { });
final result = await database.getAllInboundGroupSessions(); test('getInboundGroupSession', () async {
expect(result.isEmpty, true); await database.getInboundGroupSession(
}); '!testroom:example.com', 'sessionId');
test('getInboundGroupSession', () async { });
await database.getInboundGroupSession( test('getInboundGroupSessionsToUpload', () async {
'!testroom:example.com', 'sessionId'); await database.getInboundGroupSessionsToUpload();
}); });
test('getInboundGroupSessionsToUpload', () async { test('storeInboundGroupSession', () async {
await database.getInboundGroupSessionsToUpload(); await database.storeInboundGroupSession(
}); '!testroom:example.com',
test('storeInboundGroupSession', () async { 'sessionId',
await database.storeInboundGroupSession( 'pickle',
'!testroom:example.com', '{"foo":"bar"}',
'sessionId', '{}',
'pickle', '{}',
'{"foo":"bar"}', 'senderKey',
'{}', '{}',
'{}', );
'senderKey', final session = await database.getInboundGroupSession(
'{}', '!testroom:example.com',
); 'sessionId',
final session = await database.getInboundGroupSession( );
'!testroom:example.com', expect(jsonDecode(session!.content)['foo'], 'bar');
'sessionId', });
); test('markInboundGroupSessionAsUploaded', () async {
expect(jsonDecode(session!.content)['foo'], 'bar'); await database.markInboundGroupSessionAsUploaded(
}); '!testroom:example.com', 'sessionId');
test('markInboundGroupSessionAsUploaded', () async { });
await database.markInboundGroupSessionAsUploaded( test('markInboundGroupSessionsAsNeedingUpload', () async {
'!testroom:example.com', 'sessionId'); await database.markInboundGroupSessionsAsNeedingUpload();
}); });
test('markInboundGroupSessionsAsNeedingUpload', () async { test('updateInboundGroupSessionAllowedAtIndex', () async {
await database.markInboundGroupSessionsAsNeedingUpload(); await database.updateInboundGroupSessionAllowedAtIndex(
}); '{}',
test('updateInboundGroupSessionAllowedAtIndex', () async { '!testroom:example.com',
await database.updateInboundGroupSessionAllowedAtIndex( 'sessionId',
'{}', );
'!testroom:example.com', });
'sessionId', test('updateInboundGroupSessionIndexes', () async {
); await database.updateInboundGroupSessionIndexes(
}); '{}',
test('updateInboundGroupSessionIndexes', () async { '!testroom:example.com',
await database.updateInboundGroupSessionIndexes( 'sessionId',
'{}', );
'!testroom:example.com', });
'sessionId', test('getSSSSCache', () async {
); final cache = await database.getSSSSCache('type');
}); expect(cache, null);
test('getSSSSCache', () async { });
final cache = await database.getSSSSCache('type'); test('storeSSSSCache', () async {
expect(cache, null); await database.storeSSSSCache('type', 'keyId', 'ciphertext', '{}');
}); final cache = (await database.getSSSSCache('type'))!;
test('storeSSSSCache', () async { expect(cache.type, 'type');
await database.storeSSSSCache('type', 'keyId', 'ciphertext', '{}'); expect(cache.keyId, 'keyId');
final cache = (await database.getSSSSCache('type'))!; expect(cache.ciphertext, 'ciphertext');
expect(cache.type, 'type'); expect(cache.content, '{}');
expect(cache.keyId, 'keyId'); });
expect(cache.ciphertext, 'ciphertext'); test('getOlmSessions', () async {
expect(cache.content, '{}'); final olm = await database.getOlmSessions(
}); 'identityKey',
test('getOlmSessions', () async { 'userId',
final olm = await database.getOlmSessions( );
'identityKey', expect(olm.isEmpty, true);
'userId', });
); test('getAllOlmSessions', () async {
expect(olm.isEmpty, true); var sessions = await database.getAllOlmSessions();
}); expect(sessions.isEmpty, true);
test('getAllOlmSessions', () async { await database.storeOlmSession(
var sessions = await database.getAllOlmSessions(); 'identityKey',
expect(sessions.isEmpty, true); 'sessionId',
await database.storeOlmSession( 'pickle',
'identityKey', 0,
'sessionId', );
'pickle', await database.storeOlmSession(
0, 'identityKey',
); 'sessionId2',
await database.storeOlmSession( 'pickle',
'identityKey', 0,
'sessionId2', );
'pickle', sessions = await database.getAllOlmSessions();
0, expect(
); sessions,
sessions = await database.getAllOlmSessions(); {
expect( 'identityKey': {
sessions, 'sessionId': {
{ 'identity_key': 'identityKey',
'identityKey': { 'pickle': 'pickle',
'sessionId': { 'session_id': 'sessionId',
'identity_key': 'identityKey', 'last_received': 0
'pickle': 'pickle', },
'session_id': 'sessionId', 'sessionId2': {
'last_received': 0 'identity_key': 'identityKey',
}, 'pickle': 'pickle',
'sessionId2': { 'session_id': 'sessionId2',
'identity_key': 'identityKey', 'last_received': 0
'pickle': 'pickle', }
'session_id': 'sessionId2',
'last_received': 0
} }
} },
}, );
); });
}); test('getOlmSessionsForDevices', () async {
test('getOlmSessionsForDevices', () async { final olm = await database.getOlmSessionsForDevices(
final olm = await database.getOlmSessionsForDevices( ['identityKeys'],
['identityKeys'], 'userId',
'userId', );
); expect(olm.isEmpty, true);
expect(olm.isEmpty, true); });
}); test('storeOlmSession', () async {
test('storeOlmSession', () async { if (!(await olmEnabled())) return;
if (!(await olmEnabled())) return; await database.storeOlmSession(
await database.storeOlmSession( 'identityKey',
'identityKey', 'sessionId',
'sessionId', 'pickle',
'pickle', 0,
0, );
); final olm = await database.getOlmSessions(
final olm = await database.getOlmSessions( 'identityKey',
'identityKey', 'userId',
'userId', );
); expect(olm.isNotEmpty, true);
expect(olm.isNotEmpty, true); });
}); test('getOutboundGroupSession', () async {
test('getOutboundGroupSession', () async { final session = await database.getOutboundGroupSession(
final session = await database.getOutboundGroupSession( '!testroom:example.com',
'!testroom:example.com', '@alice:example.com',
'@alice:example.com', );
); expect(session, null);
expect(session, null); });
}); test('storeOutboundGroupSession', () async {
test('storeOutboundGroupSession', () async { if (!(await olmEnabled())) return;
if (!(await olmEnabled())) return; await database.storeOutboundGroupSession(
await database.storeOutboundGroupSession( '!testroom:example.com',
'!testroom:example.com', 'pickle',
'pickle', '{}',
'{}', 0,
0, );
); final session = await database.getOutboundGroupSession(
final session = await database.getOutboundGroupSession( '!testroom:example.com',
'!testroom:example.com', '@alice:example.com',
'@alice:example.com', );
); expect(session?.devices.isEmpty, true);
expect(session?.devices.isEmpty, true); });
}); test('getLastSentMessageUserDeviceKey', () async {
test('getLastSentMessageUserDeviceKey', () async { final list = await database.getLastSentMessageUserDeviceKey(
final list = await database.getLastSentMessageUserDeviceKey( 'userId',
'userId', 'deviceId',
'deviceId', );
); expect(list.isEmpty, true);
expect(list.isEmpty, true); });
}); test('getUnimportantRoomEventStatesForRoom', () async {
test('getUnimportantRoomEventStatesForRoom', () async { final events = await database.getUnimportantRoomEventStatesForRoom(
final events = await database.getUnimportantRoomEventStatesForRoom( ['events'],
['events'], Room(id: '!mep', client: Client('testclient')),
Room(id: '!mep', client: Client('testclient')), );
); expect(events.isEmpty, true);
expect(events.isEmpty, true); });
}); test('getUserDeviceKeys', () async {
test('getUserDeviceKeys', () async { await database.getUserDeviceKeys(Client('testclient'));
await database.getUserDeviceKeys(Client('testclient')); });
}); test('storeUserCrossSigningKey', () async {
test('storeUserCrossSigningKey', () async { await database.storeUserCrossSigningKey(
await database.storeUserCrossSigningKey( '@alice:example.com',
'@alice:example.com', 'publicKey',
'publicKey', '{}',
'{}', false,
false, false,
false, );
); });
}); test('setVerifiedUserCrossSigningKey', () async {
test('setVerifiedUserCrossSigningKey', () async { await database.setVerifiedUserCrossSigningKey(
await database.setVerifiedUserCrossSigningKey( true,
true, '@alice:example.com',
'@alice:example.com', 'publicKey',
'publicKey', );
); });
}); test('setBlockedUserCrossSigningKey', () async {
test('setBlockedUserCrossSigningKey', () async { await database.setBlockedUserCrossSigningKey(
await database.setBlockedUserCrossSigningKey( true,
true, '@alice:example.com',
'@alice:example.com', 'publicKey',
'publicKey', );
); });
}); test('removeUserCrossSigningKey', () async {
test('removeUserCrossSigningKey', () async { await database.removeUserCrossSigningKey(
await database.removeUserCrossSigningKey( '@alice:example.com',
'@alice:example.com', 'publicKey',
'publicKey', );
); });
}); test('storeUserDeviceKeysInfo', () async {
test('storeUserDeviceKeysInfo', () async { await database.storeUserDeviceKeysInfo(
await database.storeUserDeviceKeysInfo( '@alice:example.com',
'@alice:example.com', true,
true, );
); });
}); test('storeUserDeviceKey', () async {
test('storeUserDeviceKey', () async { await database.storeUserDeviceKey(
await database.storeUserDeviceKey( '@alice:example.com',
'@alice:example.com', 'deviceId',
'deviceId', '{}',
'{}', false,
false, false,
false, 0,
0, );
); });
}); test('setVerifiedUserDeviceKey', () async {
test('setVerifiedUserDeviceKey', () async { await database.setVerifiedUserDeviceKey(
await database.setVerifiedUserDeviceKey( true,
true, '@alice:example.com',
'@alice:example.com', 'deviceId',
'deviceId', );
); });
}); test('setBlockedUserDeviceKey', () async {
test('setBlockedUserDeviceKey', () async { await database.setBlockedUserDeviceKey(
await database.setBlockedUserDeviceKey( true,
true, '@alice:example.com',
'@alice:example.com', 'deviceId',
'deviceId', );
); });
}); test('getStorePresences', () async {
test('getStorePresences', () async { const userId = '@alice:example.com';
const userId = '@alice:example.com'; final presence = CachedPresence(
final presence = CachedPresence( PresenceType.online,
PresenceType.online, 100,
100, 'test message',
'test message', true,
true, '@alice:example.com',
'@alice:example.com', );
); await database.storePresence(
await database.storePresence( userId,
userId, presence,
presence, );
); final storedPresence = await database.getPresence(userId);
final storedPresence = await database.getPresence(userId); expect(
expect( presence.toJson(),
presence.toJson(), storedPresence?.toJson(),
storedPresence?.toJson(), );
); });
});
// Clearing up from here // Clearing up from here
test('clearSSSSCache', () async { test('clearSSSSCache', () async {
await database.clearSSSSCache(); await database.clearSSSSCache();
}); });
test('clearCache', () async { test('clearCache', () async {
await database.clearCache(); await database.clearCache();
}); });
test('clear', () async { test('clear', () async {
await database.clear(); await database.clear();
}); });
test('Close', () async { test('Close', () async {
await database.close(); await database.close();
}); });
test('Delete', () async { test('Delete', () async {
final database = await getMatrixSdkDatabase(null); final database = await getMatrixSdkDatabase(null);
await database.storeAccountData( await database.storeAccountData(
'm.test.data', 'm.test.data',
jsonEncode({'foo': 'bar'}), jsonEncode({'foo': 'bar'}),
); );
await database.delete(); await database.delete();
// Check if previously stored data is gone: // Check if previously stored data is gone:
final reopenedDatabase = await getMatrixSdkDatabase(null); final reopenedDatabase = await getMatrixSdkDatabase(null);
final dump = await reopenedDatabase.getAccountData(); final dump = await reopenedDatabase.getAccountData();
expect(dump.isEmpty, true); expect(dump.isEmpty, true);
});
}); });
}); }
} }
Future<bool> olmEnabled() async { Future<bool> olmEnabled() async {