test(persistence): add tests for connection event dao

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-03-16 15:51:41 +05:30
parent 083ca28283
commit edc8c4d181
5 changed files with 202 additions and 65 deletions
@@ -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');
}