test(persistence): add tests for stream chat persistence client
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -14,15 +14,40 @@ import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
/// A Helper class to construct new instances of [MoorChatDatabase] specifically
|
||||
/// for native platform applications
|
||||
class SharedDB {
|
||||
/// Returns a new instance of [VmDatabase] created using [userId]
|
||||
/// on a regular isolate.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.regular].
|
||||
static Future<VmDatabase> constructDatabase(
|
||||
/// Returns a new instance of [MoorChatDatabase].
|
||||
static MoorChatDatabase constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
}) async {
|
||||
ConnectionMode connectionMode = ConnectionMode.regular,
|
||||
}) {
|
||||
final dbName = 'db_$userId';
|
||||
if (connectionMode == ConnectionMode.background) {
|
||||
return MoorChatDatabase.connect(
|
||||
userId,
|
||||
DatabaseConnection.delayed(Future(() async {
|
||||
final isolate = await _createMoorIsolate(
|
||||
dbName,
|
||||
logStatements: logStatements,
|
||||
);
|
||||
return isolate.connect();
|
||||
})),
|
||||
);
|
||||
}
|
||||
return MoorChatDatabase(
|
||||
userId,
|
||||
LazyDatabase(
|
||||
() async => _constructDatabase(
|
||||
dbName,
|
||||
logStatements: logStatements,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<VmDatabase> _constructDatabase(
|
||||
String dbName, {
|
||||
bool logStatements = false,
|
||||
}) async {
|
||||
if (Platform.isIOS || Platform.isAndroid) {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final path = join(dir.path, '$dbName.sqlite');
|
||||
@@ -66,27 +91,6 @@ class SharedDB {
|
||||
|
||||
return await receivePort.first as MoorIsolate;
|
||||
}
|
||||
|
||||
/// Returns a new instance of [MoorChatDatabase] using the factory constructor
|
||||
/// [MoorChatDatabase.connect] created on a background isolate.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.background].
|
||||
static MoorChatDatabase constructMoorChatDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
}) {
|
||||
final dbName = 'db_$userId';
|
||||
return MoorChatDatabase.connect(
|
||||
userId,
|
||||
DatabaseConnection.delayed(Future(() async {
|
||||
final isolate = await _createMoorIsolate(
|
||||
dbName,
|
||||
logStatements: logStatements,
|
||||
);
|
||||
return isolate.connect();
|
||||
})),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _IsolateStartRequest {
|
||||
|
||||
@@ -1,28 +1,16 @@
|
||||
import 'package:moor/backends.dart';
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
|
||||
|
||||
/// A Helper class to construct new instances of [MoorChatDatabase]
|
||||
class SharedDB {
|
||||
/// Returns a new instance of database.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.regular].
|
||||
static Future<DelegatedDatabase> constructDatabase(
|
||||
/// Returns a new instance of [MoorChatDatabase].
|
||||
static MoorChatDatabase constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
ConnectionMode connectionMode = ConnectionMode.regular,
|
||||
}) {
|
||||
throw UnsupportedError(
|
||||
'No implementation of the constructDatabase api provided');
|
||||
}
|
||||
|
||||
/// Return a new instance of moor chat database.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.background].
|
||||
static MoorChatDatabase constructMoorChatDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
}) {
|
||||
throw UnsupportedError(
|
||||
'No implementation of the constructMoorChatDatabase api provided');
|
||||
'No implementation of the constructDatabase api provided',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,26 +6,14 @@ import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
/// A Helper class to construct new instances of [MoorChatDatabase] specifically
|
||||
/// for Web applications
|
||||
class SharedDB {
|
||||
/// Returns a new instance of [WebDatabase] created using [userId].
|
||||
///
|
||||
/// Generally used with [ConnectionMode.regular].
|
||||
static Future<WebDatabase> constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
}) async {
|
||||
final dbName = 'db_$userId';
|
||||
return WebDatabase(dbName, logStatements: logStatements);
|
||||
}
|
||||
|
||||
/// Returns a new instance of [MoorChatDatabase] creating using the
|
||||
/// default constructor.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.background].
|
||||
static MoorChatDatabase constructMoorChatDatabase(
|
||||
/// Returns a new instance of [MoorChatDatabase].
|
||||
static MoorChatDatabase constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
ConnectionMode connectionMode = ConnectionMode.regular, // Ignored on web
|
||||
}) {
|
||||
final dbName = 'db_$userId';
|
||||
return MoorChatDatabase(dbName, logStatements: logStatements);
|
||||
final queryExecutor = WebDatabase(dbName, logStatements: logStatements);
|
||||
return MoorChatDatabase(userId, queryExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user