test(persistence): Add tests for channel dao
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -1,23 +1,22 @@
|
|||||||
import 'package:moor/isolate.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:moor/ffi.dart';
|
||||||
import 'package:moor/moor.dart';
|
import 'package:moor/moor.dart';
|
||||||
import 'package:stream_chat/stream_chat.dart';
|
import 'package:stream_chat/stream_chat.dart';
|
||||||
|
|
||||||
import '../converter/converter.dart';
|
import 'package:stream_chat_persistence/src/converter/converter.dart';
|
||||||
import '../dao/dao.dart';
|
import 'package:stream_chat_persistence/src/dao/dao.dart';
|
||||||
import '../entity/entity.dart';
|
import 'package:stream_chat_persistence/src/entity/entity.dart';
|
||||||
import 'shared/shared_db.dart';
|
import 'package:stream_chat_persistence/src/db/shared/shared_db.dart';
|
||||||
|
|
||||||
part 'moor_chat_database.g.dart';
|
part 'moor_chat_database.g.dart';
|
||||||
|
|
||||||
LazyDatabase _openConnection(
|
LazyDatabase _openConnection(
|
||||||
String userId, {
|
String userId, {
|
||||||
bool logStatements = false,
|
bool logStatements = false,
|
||||||
bool persistOnDisk = true,
|
|
||||||
}) =>
|
}) =>
|
||||||
LazyDatabase(() async => SharedDB.constructDatabase(
|
LazyDatabase(() async => SharedDB.constructDatabase(
|
||||||
userId,
|
userId,
|
||||||
logStatements: logStatements,
|
logStatements: logStatements,
|
||||||
persistOnDisk: persistOnDisk,
|
|
||||||
));
|
));
|
||||||
|
|
||||||
/// A chat database implemented using moor
|
/// A chat database implemented using moor
|
||||||
@@ -47,11 +46,9 @@ class MoorChatDatabase extends _$MoorChatDatabase {
|
|||||||
MoorChatDatabase(
|
MoorChatDatabase(
|
||||||
this._userId, {
|
this._userId, {
|
||||||
logStatements = false,
|
logStatements = false,
|
||||||
bool persistOnDisk = true,
|
|
||||||
}) : super(_openConnection(
|
}) : super(_openConnection(
|
||||||
_userId,
|
_userId,
|
||||||
logStatements: logStatements,
|
logStatements: logStatements,
|
||||||
persistOnDisk: persistOnDisk,
|
|
||||||
));
|
));
|
||||||
|
|
||||||
/// Instantiate a new database instance
|
/// Instantiate a new database instance
|
||||||
@@ -60,6 +57,10 @@ class MoorChatDatabase extends _$MoorChatDatabase {
|
|||||||
DatabaseConnection connection,
|
DatabaseConnection connection,
|
||||||
) : super.connect(connection);
|
) : super.connect(connection);
|
||||||
|
|
||||||
|
/// Custom constructor used only for testing
|
||||||
|
@visibleForTesting
|
||||||
|
MoorChatDatabase.testable(this._userId) : super(VmDatabase.memory());
|
||||||
|
|
||||||
final String _userId;
|
final String _userId;
|
||||||
|
|
||||||
/// User id to which the database is connected
|
/// User id to which the database is connected
|
||||||
|
|||||||
@@ -21,20 +21,17 @@ class SharedDB {
|
|||||||
static Future<VmDatabase> constructDatabase(
|
static Future<VmDatabase> constructDatabase(
|
||||||
String userId, {
|
String userId, {
|
||||||
bool logStatements = false,
|
bool logStatements = false,
|
||||||
bool persistOnDisk = true,
|
|
||||||
}) async {
|
}) async {
|
||||||
final dbName = 'db_$userId';
|
final dbName = 'db_$userId';
|
||||||
if (persistOnDisk) {
|
if (Platform.isIOS || Platform.isAndroid) {
|
||||||
if (Platform.isIOS || Platform.isAndroid) {
|
final dir = await getApplicationDocumentsDirectory();
|
||||||
final dir = await getApplicationDocumentsDirectory();
|
final path = join(dir.path, '$dbName.sqlite');
|
||||||
final path = join(dir.path, '$dbName.sqlite');
|
final file = File(path);
|
||||||
final file = File(path);
|
return VmDatabase(file, logStatements: logStatements);
|
||||||
return VmDatabase(file, logStatements: logStatements);
|
}
|
||||||
}
|
if (Platform.isMacOS || Platform.isLinux) {
|
||||||
if (Platform.isMacOS || Platform.isLinux) {
|
final file = File('$dbName.sqlite');
|
||||||
final file = File('$dbName.sqlite');
|
return VmDatabase(file, logStatements: logStatements);
|
||||||
return VmDatabase(file, logStatements: logStatements);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return VmDatabase.memory(logStatements: logStatements);
|
return VmDatabase.memory(logStatements: logStatements);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ class SharedDB {
|
|||||||
static Future<DelegatedDatabase> constructDatabase(
|
static Future<DelegatedDatabase> constructDatabase(
|
||||||
String userId, {
|
String userId, {
|
||||||
bool logStatements = false,
|
bool logStatements = false,
|
||||||
bool persistOnDisk = true,
|
|
||||||
}) {
|
}) {
|
||||||
throw UnsupportedError(
|
throw UnsupportedError(
|
||||||
'No implementation of the constructDatabase api provided');
|
'No implementation of the constructDatabase api provided');
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ class SharedDB {
|
|||||||
static Future<WebDatabase> constructDatabase(
|
static Future<WebDatabase> constructDatabase(
|
||||||
String userId, {
|
String userId, {
|
||||||
bool logStatements = false,
|
bool logStatements = false,
|
||||||
bool persistOnDisk = true, // ignored on web
|
|
||||||
}) async {
|
}) async {
|
||||||
final dbName = 'db_$userId';
|
final dbName = 'db_$userId';
|
||||||
return WebDatabase(dbName, logStatements: logStatements);
|
return WebDatabase(dbName, logStatements: logStatements);
|
||||||
|
|||||||
@@ -46,14 +46,14 @@ extension ChannelModelX on ChannelModel {
|
|||||||
id: id,
|
id: id,
|
||||||
type: type,
|
type: type,
|
||||||
cid: cid,
|
cid: cid,
|
||||||
config: config.toJson(),
|
config: config?.toJson(),
|
||||||
frozen: frozen,
|
frozen: frozen,
|
||||||
lastMessageAt: lastMessageAt,
|
lastMessageAt: lastMessageAt,
|
||||||
createdAt: createdAt,
|
createdAt: createdAt,
|
||||||
updatedAt: updatedAt,
|
updatedAt: updatedAt,
|
||||||
deletedAt: deletedAt,
|
deletedAt: deletedAt,
|
||||||
memberCount: memberCount,
|
memberCount: memberCount,
|
||||||
createdById: createdBy.id,
|
createdById: createdBy?.id,
|
||||||
extraData: extraData,
|
extraData: extraData,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,139 @@
|
|||||||
|
import 'package:stream_chat/stream_chat.dart';
|
||||||
|
import 'package:stream_chat_persistence/src/dao/channel_dao.dart';
|
||||||
|
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
ChannelDao channelDao;
|
||||||
|
MoorChatDatabase database;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
database = MoorChatDatabase.testable('testUserId');
|
||||||
|
channelDao = database.channelDao;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getChannelByCid', () async {
|
||||||
|
const id = 'testId';
|
||||||
|
const cid = 'testCid';
|
||||||
|
const type = 'testType';
|
||||||
|
|
||||||
|
// Should be null initially
|
||||||
|
final channel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(channel, isNull);
|
||||||
|
|
||||||
|
// Saving a dummy channel
|
||||||
|
final dummyChannel = ChannelModel(
|
||||||
|
id: id,
|
||||||
|
type: type,
|
||||||
|
cid: cid,
|
||||||
|
config: ChannelConfig(),
|
||||||
|
);
|
||||||
|
await channelDao.updateChannels([dummyChannel]);
|
||||||
|
|
||||||
|
// Should match the dummy channel
|
||||||
|
final updatedChannel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(updatedChannel.id, id);
|
||||||
|
expect(updatedChannel.cid, cid);
|
||||||
|
expect(updatedChannel.type, type);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deleteChannelByCids', () async {
|
||||||
|
const id = 'testId';
|
||||||
|
const cid = 'testCid';
|
||||||
|
const type = 'testType';
|
||||||
|
|
||||||
|
// Saving a dummy channel
|
||||||
|
final dummyChannel = ChannelModel(
|
||||||
|
id: id,
|
||||||
|
type: type,
|
||||||
|
cid: cid,
|
||||||
|
config: ChannelConfig(),
|
||||||
|
);
|
||||||
|
await channelDao.updateChannels([dummyChannel]);
|
||||||
|
|
||||||
|
// Should match the dummy channel
|
||||||
|
final updatedChannel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(updatedChannel.id, id);
|
||||||
|
expect(updatedChannel.cid, cid);
|
||||||
|
expect(updatedChannel.type, type);
|
||||||
|
|
||||||
|
// Deleting the dummyChannel using cid
|
||||||
|
await channelDao.deleteChannelByCids([cid]);
|
||||||
|
|
||||||
|
// Fetched channel Should be null
|
||||||
|
final channel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(channel, isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cids', () async {
|
||||||
|
// Should be empty initially
|
||||||
|
final cids = await channelDao.cids;
|
||||||
|
expect(cids, []);
|
||||||
|
|
||||||
|
const id = 'testId';
|
||||||
|
const cid = 'testCid';
|
||||||
|
const type = 'testType';
|
||||||
|
|
||||||
|
// Saving a dummy channel
|
||||||
|
final dummyChannel = ChannelModel(
|
||||||
|
id: id,
|
||||||
|
type: type,
|
||||||
|
cid: cid,
|
||||||
|
config: ChannelConfig(),
|
||||||
|
);
|
||||||
|
await channelDao.updateChannels([dummyChannel]);
|
||||||
|
|
||||||
|
// Should return the cid of the dummy channel
|
||||||
|
final updatedCids = await channelDao.cids;
|
||||||
|
expect(updatedCids, [cid]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// /// Updates all the channels using the new [channelList] data
|
||||||
|
// Future<void> updateChannels(List<ChannelModel> channelList) => batch(
|
||||||
|
// (it) => it.insertAll(
|
||||||
|
// channels,
|
||||||
|
// channelList.map((c) => c.toEntity()).toList(),
|
||||||
|
// mode: InsertMode.insertOrReplace,
|
||||||
|
// ),
|
||||||
|
// );
|
||||||
|
|
||||||
|
test('updateChannels', () async {
|
||||||
|
const id = 'testId';
|
||||||
|
const cid = 'testCid';
|
||||||
|
const type = 'testType';
|
||||||
|
|
||||||
|
// Should be null initially
|
||||||
|
final channel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(channel, isNull);
|
||||||
|
|
||||||
|
// Saving a dummy channel
|
||||||
|
final dummyChannel = ChannelModel(
|
||||||
|
id: id,
|
||||||
|
type: type,
|
||||||
|
cid: cid,
|
||||||
|
config: ChannelConfig(),
|
||||||
|
);
|
||||||
|
await channelDao.updateChannels([dummyChannel]);
|
||||||
|
|
||||||
|
// Should match the dummy channel
|
||||||
|
final updatedChannel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(updatedChannel.id, id);
|
||||||
|
expect(updatedChannel.cid, cid);
|
||||||
|
expect(updatedChannel.type, type);
|
||||||
|
|
||||||
|
// Updating the previously saved channel
|
||||||
|
const newType = 'newTestType';
|
||||||
|
final newChannel = dummyChannel.copyWith(type: newType);
|
||||||
|
await channelDao.updateChannels([newChannel]);
|
||||||
|
|
||||||
|
// Should match the new channel
|
||||||
|
final newUpdatedChannel = await channelDao.getChannelByCid(cid);
|
||||||
|
expect(newUpdatedChannel.id, id);
|
||||||
|
expect(newUpdatedChannel.cid, cid);
|
||||||
|
expect(newUpdatedChannel.type, newType);
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await database.disconnect();
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user