test(persistence): Add tests for channel dao

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-03-15 14:17:01 +05:30
parent 8c0da62f79
commit 4cbbc059d3
6 changed files with 160 additions and 25 deletions
@@ -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:stream_chat/stream_chat.dart';
import '../converter/converter.dart';
import '../dao/dao.dart';
import '../entity/entity.dart';
import 'shared/shared_db.dart';
import 'package:stream_chat_persistence/src/converter/converter.dart';
import 'package:stream_chat_persistence/src/dao/dao.dart';
import 'package:stream_chat_persistence/src/entity/entity.dart';
import 'package:stream_chat_persistence/src/db/shared/shared_db.dart';
part 'moor_chat_database.g.dart';
LazyDatabase _openConnection(
String userId, {
bool logStatements = false,
bool persistOnDisk = true,
}) =>
LazyDatabase(() async => SharedDB.constructDatabase(
userId,
logStatements: logStatements,
persistOnDisk: persistOnDisk,
));
/// A chat database implemented using moor
@@ -47,11 +46,9 @@ class MoorChatDatabase extends _$MoorChatDatabase {
MoorChatDatabase(
this._userId, {
logStatements = false,
bool persistOnDisk = true,
}) : super(_openConnection(
_userId,
logStatements: logStatements,
persistOnDisk: persistOnDisk,
));
/// Instantiate a new database instance
@@ -60,6 +57,10 @@ class MoorChatDatabase extends _$MoorChatDatabase {
DatabaseConnection connection,
) : super.connect(connection);
/// Custom constructor used only for testing
@visibleForTesting
MoorChatDatabase.testable(this._userId) : super(VmDatabase.memory());
final String _userId;
/// User id to which the database is connected
@@ -21,20 +21,17 @@ class SharedDB {
static Future<VmDatabase> constructDatabase(
String userId, {
bool logStatements = false,
bool persistOnDisk = true,
}) async {
final dbName = 'db_$userId';
if (persistOnDisk) {
if (Platform.isIOS || Platform.isAndroid) {
final dir = await getApplicationDocumentsDirectory();
final path = join(dir.path, '$dbName.sqlite');
final file = File(path);
return VmDatabase(file, logStatements: logStatements);
}
if (Platform.isMacOS || Platform.isLinux) {
final file = File('$dbName.sqlite');
return VmDatabase(file, logStatements: logStatements);
}
if (Platform.isIOS || Platform.isAndroid) {
final dir = await getApplicationDocumentsDirectory();
final path = join(dir.path, '$dbName.sqlite');
final file = File(path);
return VmDatabase(file, logStatements: logStatements);
}
if (Platform.isMacOS || Platform.isLinux) {
final file = File('$dbName.sqlite');
return VmDatabase(file, logStatements: logStatements);
}
return VmDatabase.memory(logStatements: logStatements);
}
@@ -10,7 +10,6 @@ class SharedDB {
static Future<DelegatedDatabase> constructDatabase(
String userId, {
bool logStatements = false,
bool persistOnDisk = true,
}) {
throw UnsupportedError(
'No implementation of the constructDatabase api provided');
@@ -12,7 +12,6 @@ class SharedDB {
static Future<WebDatabase> constructDatabase(
String userId, {
bool logStatements = false,
bool persistOnDisk = true, // ignored on web
}) async {
final dbName = 'db_$userId';
return WebDatabase(dbName, logStatements: logStatements);
@@ -46,14 +46,14 @@ extension ChannelModelX on ChannelModel {
id: id,
type: type,
cid: cid,
config: config.toJson(),
config: config?.toJson(),
frozen: frozen,
lastMessageAt: lastMessageAt,
createdAt: createdAt,
updatedAt: updatedAt,
deletedAt: deletedAt,
memberCount: memberCount,
createdById: createdBy.id,
createdById: createdBy?.id,
extraData: extraData,
);
}