test(persistence): add tests for connection event dao
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -121,6 +121,46 @@ class Event {
|
||||
_$EventToJson(this),
|
||||
topLevelFields,
|
||||
);
|
||||
|
||||
/// Creates a copy of [Event] with specified attributes overridden.
|
||||
Event copyWith({
|
||||
String type,
|
||||
String cid,
|
||||
String channelId,
|
||||
String channelType,
|
||||
String connectionId,
|
||||
DateTime createdAt,
|
||||
OwnUser me,
|
||||
User user,
|
||||
Message message,
|
||||
EventChannel channel,
|
||||
Member member,
|
||||
Reaction reaction,
|
||||
int totalUnreadCount,
|
||||
int unreadChannels,
|
||||
bool online,
|
||||
String parentId,
|
||||
Map<String, dynamic> extraData,
|
||||
}) =>
|
||||
Event(
|
||||
type: type ?? this.type,
|
||||
cid: cid ?? this.cid,
|
||||
connectionId: connectionId ?? this.connectionId,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
me: me ?? this.me,
|
||||
user: user ?? this.user,
|
||||
message: message ?? this.message,
|
||||
totalUnreadCount: totalUnreadCount ?? this.totalUnreadCount,
|
||||
unreadChannels: unreadChannels ?? this.unreadChannels,
|
||||
reaction: reaction ?? this.reaction,
|
||||
online: online ?? this.online,
|
||||
channel: channel ?? this.channel,
|
||||
member: member ?? this.member,
|
||||
channelId: channelId ?? this.channelId,
|
||||
channelType: channelType ?? this.channelType,
|
||||
parentId: parentId ?? this.parentId,
|
||||
extraData: extraData ?? this.extraData,
|
||||
);
|
||||
}
|
||||
|
||||
/// The channel embedded in the event object
|
||||
|
||||
@@ -22,8 +22,11 @@ dependencies:
|
||||
ref: develop
|
||||
path: packages/stream_chat
|
||||
|
||||
dependency_overrides:
|
||||
stream_chat:
|
||||
path: ../stream_chat
|
||||
|
||||
dev_dependencies:
|
||||
test: ^1.15.7
|
||||
build_runner: ^1.11.0
|
||||
moor_generator: ^3.4.1
|
||||
pedantic: ^1.9.2
|
||||
moor_generator: ^3.4.1
|
||||
@@ -5,6 +5,8 @@ import 'package:stream_chat_persistence/src/dao/channel_query_dao.dart';
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../utils/date_matcher.dart';
|
||||
|
||||
void main() {
|
||||
MoorChatDatabase database;
|
||||
ChannelQueryDao channelQueryDao;
|
||||
@@ -24,7 +26,7 @@ void main() {
|
||||
const cids = ['testCid1', 'testCid2', 'testCid3'];
|
||||
|
||||
final cachedCids = await channelQueryDao.getCachedChannelCids(filter);
|
||||
expect(cachedCids, []);
|
||||
expect(cachedCids, isEmpty);
|
||||
|
||||
// Updating channel queries
|
||||
await channelQueryDao.updateChannelQueries(filter, cids);
|
||||
@@ -43,7 +45,7 @@ void main() {
|
||||
const cids = ['testCid1', 'testCid2', 'testCid3'];
|
||||
|
||||
final cachedCids = await channelQueryDao.getCachedChannelCids(filter);
|
||||
expect(cachedCids, []);
|
||||
expect(cachedCids, isEmpty);
|
||||
|
||||
// Updating channel queries
|
||||
await channelQueryDao.updateChannelQueries(filter, cids);
|
||||
@@ -91,7 +93,7 @@ void main() {
|
||||
|
||||
test('should return empty list of channels', () async {
|
||||
final channels = await channelQueryDao.getChannels(filter: filter);
|
||||
expect(channels, []);
|
||||
expect(channels, isEmpty);
|
||||
});
|
||||
|
||||
test('should return all the inserted channels', () async {
|
||||
@@ -113,30 +115,14 @@ void main() {
|
||||
|
||||
// Should match createdAt date
|
||||
expect(
|
||||
updatedChannel.createdAt.hour,
|
||||
insertedChannel.createdAt.hour,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.createdAt.minute,
|
||||
insertedChannel.createdAt.minute,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.createdAt.second,
|
||||
insertedChannel.createdAt.second,
|
||||
updatedChannel.createdAt,
|
||||
isSameDateAs(insertedChannel.createdAt),
|
||||
);
|
||||
|
||||
// Should match lastMessageAt date
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.hour,
|
||||
insertedChannel.lastMessageAt.hour,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.minute,
|
||||
insertedChannel.lastMessageAt.minute,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.second,
|
||||
insertedChannel.lastMessageAt.second,
|
||||
updatedChannel.lastMessageAt,
|
||||
isSameDateAs(insertedChannel.lastMessageAt),
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -168,30 +154,14 @@ void main() {
|
||||
|
||||
// Should match createdAt date
|
||||
expect(
|
||||
updatedChannel.createdAt.hour,
|
||||
insertedChannel.createdAt.hour,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.createdAt.minute,
|
||||
insertedChannel.createdAt.minute,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.createdAt.second,
|
||||
insertedChannel.createdAt.second,
|
||||
updatedChannel.createdAt,
|
||||
isSameDateAs(insertedChannel.createdAt),
|
||||
);
|
||||
|
||||
// Should match lastMessageAt date
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.hour,
|
||||
insertedChannel.lastMessageAt.hour,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.minute,
|
||||
insertedChannel.lastMessageAt.minute,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.second,
|
||||
insertedChannel.lastMessageAt.second,
|
||||
updatedChannel.lastMessageAt,
|
||||
isSameDateAs(insertedChannel.lastMessageAt),
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -235,30 +205,14 @@ void main() {
|
||||
|
||||
// Should match createdAt date
|
||||
expect(
|
||||
updatedChannel.createdAt.hour,
|
||||
insertedChannel.createdAt.hour,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.createdAt.minute,
|
||||
insertedChannel.createdAt.minute,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.createdAt.second,
|
||||
insertedChannel.createdAt.second,
|
||||
updatedChannel.createdAt,
|
||||
isSameDateAs(insertedChannel.createdAt),
|
||||
);
|
||||
|
||||
// Should match lastMessageAt date
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.hour,
|
||||
insertedChannel.lastMessageAt.hour,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.minute,
|
||||
insertedChannel.lastMessageAt.minute,
|
||||
);
|
||||
expect(
|
||||
updatedChannel.lastMessageAt.second,
|
||||
insertedChannel.lastMessageAt.second,
|
||||
updatedChannel.lastMessageAt,
|
||||
isSameDateAs(insertedChannel.lastMessageAt),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
import 'package:stream_chat_persistence/src/dao/connection_event_dao.dart';
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../utils/date_matcher.dart';
|
||||
|
||||
void main() {
|
||||
ConnectionEventDao eventDao;
|
||||
MoorChatDatabase database;
|
||||
|
||||
setUp(() {
|
||||
database = MoorChatDatabase.testable('testUserId');
|
||||
eventDao = database.connectionEventDao;
|
||||
});
|
||||
|
||||
test('connectionEvent', () async {
|
||||
// Should be null initially
|
||||
final event = await eventDao.connectionEvent;
|
||||
expect(event, isNull);
|
||||
|
||||
// Adding a new event
|
||||
final newEvent = Event(
|
||||
createdAt: DateTime.now(),
|
||||
totalUnreadCount: 33,
|
||||
unreadChannels: 3,
|
||||
me: OwnUser(id: 'testUserId'),
|
||||
);
|
||||
await eventDao.updateConnectionEvent(newEvent);
|
||||
|
||||
// Should match the added event
|
||||
final updatedEvent = await eventDao.connectionEvent;
|
||||
expect(updatedEvent.me.id, newEvent.me.id);
|
||||
expect(updatedEvent.totalUnreadCount, newEvent.totalUnreadCount);
|
||||
expect(updatedEvent.unreadChannels, newEvent.unreadChannels);
|
||||
});
|
||||
|
||||
test('lastSyncAt', () async {
|
||||
// Should be null initially
|
||||
final lastSyncAt = await eventDao.lastSyncAt;
|
||||
expect(lastSyncAt, isNull);
|
||||
|
||||
// Adding an event for testing
|
||||
final event = Event(
|
||||
createdAt: DateTime.now(),
|
||||
totalUnreadCount: 33,
|
||||
unreadChannels: 3,
|
||||
me: OwnUser(id: 'testUserId'),
|
||||
);
|
||||
await eventDao.updateConnectionEvent(event);
|
||||
|
||||
// Updating it's last sync
|
||||
final now = DateTime.now();
|
||||
await eventDao.updateLastSyncAt(now);
|
||||
|
||||
// Should match the updated last sync
|
||||
final updatedLastSyncAt = await eventDao.lastSyncAt;
|
||||
expect(updatedLastSyncAt, isSameDateAs(now));
|
||||
});
|
||||
|
||||
test('updateConnectionEvent', () async {
|
||||
// Adding and event for testing
|
||||
final event = Event(
|
||||
createdAt: DateTime.now(),
|
||||
totalUnreadCount: 33,
|
||||
unreadChannels: 3,
|
||||
me: OwnUser(id: 'testUserId'),
|
||||
);
|
||||
await eventDao.updateConnectionEvent(event);
|
||||
|
||||
// Should match the previously added event
|
||||
final fetchedEvent = await eventDao.connectionEvent;
|
||||
expect(fetchedEvent.me.id, event.me.id);
|
||||
expect(fetchedEvent.totalUnreadCount, event.totalUnreadCount);
|
||||
expect(fetchedEvent.unreadChannels, event.unreadChannels);
|
||||
|
||||
// Updating the added event
|
||||
final newEvent = event.copyWith(unreadChannels: 4);
|
||||
await eventDao.updateConnectionEvent(newEvent);
|
||||
|
||||
// Should match the updated event
|
||||
final fetchedNewEvent = await eventDao.connectionEvent;
|
||||
expect(fetchedNewEvent.me.id, event.me.id);
|
||||
expect(fetchedNewEvent.totalUnreadCount, event.totalUnreadCount);
|
||||
expect(fetchedNewEvent.unreadChannels, newEvent.unreadChannels);
|
||||
});
|
||||
|
||||
test('updateLastSyncAt', () async {
|
||||
// Should be null initially
|
||||
final lastSyncAt = await eventDao.lastSyncAt;
|
||||
expect(lastSyncAt, isNull);
|
||||
|
||||
// Adding an event just for testing
|
||||
final event = Event(
|
||||
createdAt: DateTime.now(),
|
||||
totalUnreadCount: 33,
|
||||
unreadChannels: 3,
|
||||
me: OwnUser(id: 'testUserId'),
|
||||
);
|
||||
await eventDao.updateConnectionEvent(event);
|
||||
|
||||
// Updating it's last sync
|
||||
final now = DateTime.now();
|
||||
await eventDao.updateLastSyncAt(now);
|
||||
|
||||
// Should match the last sync
|
||||
final updatedLastSyncAt = await eventDao.lastSyncAt;
|
||||
expect(updatedLastSyncAt, isSameDateAs(now));
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await database.disconnect();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:test/test.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
Matcher isSameDateAs(DateTime targetDate) =>
|
||||
_IsSameDateAs(targetDate: targetDate);
|
||||
|
||||
class _IsSameDateAs extends Matcher {
|
||||
const _IsSameDateAs({
|
||||
@required this.targetDate,
|
||||
}) : assert(targetDate != null, '');
|
||||
|
||||
final DateTime targetDate;
|
||||
|
||||
@override
|
||||
bool matches(covariant DateTime date, Map matchState) =>
|
||||
date.year == targetDate.year &&
|
||||
date.month == targetDate.month &&
|
||||
date.day == targetDate.day &&
|
||||
date.hour == targetDate.hour &&
|
||||
date.minute == targetDate.minute &&
|
||||
date.second == targetDate.second;
|
||||
|
||||
@override
|
||||
Description describe(Description description) =>
|
||||
description.add('is same date as $targetDate');
|
||||
}
|
||||
Reference in New Issue
Block a user