test(persistence): update tests after enabling foreign keys

This commit is contained in:
Dmitry Zhifarsky
2021-08-08 21:42:44 +03:00
parent 8adc38f8e4
commit 68311a6c6c
6 changed files with 278 additions and 98 deletions
@@ -61,12 +61,73 @@ void main() {
expect(updatedChannel.cid, cid);
expect(updatedChannel.type, type);
//Saving a dummy user
const userId = 'userId';
final dummyUser = User(id: userId);
await database.userDao.updateUsers([dummyUser]);
// Saving a dummy member
final dummyMember = Member(userId: userId, user: dummyUser);
await database.memberDao.updateMembers(cid, [dummyMember]);
// Should match the dummy member
final updatedMembers = await database.memberDao.getMembersByCid(cid);
expect(updatedMembers.length, 1);
expect(updatedMembers.first.userId, userId);
// Saving a dummy message
const messageId = 'messageId';
final dummyMessage = Message(id: messageId, user: dummyUser);
await database.messageDao.updateMessages(cid, [dummyMessage]);
// Should match the dummy message
final updatedMessages = await database.messageDao.getMessagesByCid(cid);
expect(updatedMessages.length, 1);
expect(updatedMessages.first.id, messageId);
// Saving a dummy read
final dummyRead = Read(lastRead: DateTime.now(), user: dummyUser);
await database.readDao.updateReads(cid, [dummyRead]);
// Should match the dummy read
final updatedReads = await database.readDao.getReadsByCid(cid);
expect(updatedReads.length, 1);
expect(updatedReads.first.user, dummyUser);
// Saving a dummy reaction
final dummyReaction =
Reaction(type: 'type', messageId: messageId, userId: userId);
await database.reactionDao.updateReactions([dummyReaction]);
// Should match the dummy reaction
final updatedReactions =
await database.reactionDao.getReactionsByUserId(messageId, userId);
expect(updatedReactions.length, 1);
expect(updatedReactions.first.messageId, messageId);
// Deleting the dummyChannel using cid
await channelDao.deleteChannelByCids([cid]);
// Fetched channel Should be null
final channel = await channelDao.getChannelByCid(cid);
expect(channel, isNull);
// Fetched members for passed cid should be empty
final members = await database.memberDao.getMembersByCid(cid);
expect(members, isEmpty);
// Fetched messages for passed cid should be empty
final messages = await database.messageDao.getMessagesByCid(cid);
expect(messages, isEmpty);
// Fetched reads for passed cid should be empty
final reads = await database.readDao.getReadsByCid(cid);
expect(reads, isEmpty);
// Fetched readtions for passed message id and user id should be empty
final reactions =
await database.reactionDao.getReactionsByUserId(messageId, userId);
expect(reactions, isEmpty);
});
test('cids', () async {
@@ -18,6 +18,7 @@ void main() {
});
Future<List<Member>> _prepareTestData(String cid) async {
final channels = [ChannelModel(cid: cid)];
final users = List.generate(3, (index) => User(id: 'testUserId$index'));
final memberList = List.generate(
3,
@@ -34,12 +35,13 @@ void main() {
),
);
await database.userDao.updateUsers(users);
await database.channelDao.updateChannels(channels);
await memberDao.updateMembers(cid, memberList);
return memberList;
}
test('getMembersByCid', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Should be empty initially
final members = await memberDao.getMembersByCid(cid);
@@ -70,7 +72,7 @@ void main() {
});
test('updateMembers', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final memberList = await _prepareTestData(cid);
@@ -132,7 +134,7 @@ void main() {
});
test('deleteMemberByCids', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final members = await _prepareTestData(cid);
@@ -23,6 +23,7 @@ void main() {
bool mapAllThreadToFirstMessage = false,
int count = 3,
}) async {
final channels = [ChannelModel(cid: cid)];
final users = List.generate(count, (index) => User(id: 'testUserId$index'));
final messages = List.generate(
count,
@@ -98,13 +99,20 @@ void main() {
if (quoted) ...quotedMessages,
if (threads) ...threadMessages
];
final reaction = Reaction(
type: 'type',
messageId: allMessages.first.id,
user: users.first,
);
await database.userDao.updateUsers(users);
await database.channelDao.updateChannels(channels);
await messageDao.updateMessages(cid, allMessages);
await database.reactionDao.updateReactions([reaction]);
return allMessages;
}
test('deleteMessageByIds', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final insertedMessages = await _prepareTestData(cid);
@@ -113,66 +121,112 @@ void main() {
final messages = await messageDao.getMessagesByCid(cid);
expect(messages.length, insertedMessages.length);
final firstMessageId = messages.first.id;
// Fetched reactions list should have one reaction for given message id
final reactions = await database.reactionDao.getReactions(firstMessageId);
expect(reactions.length, 1);
// Deleting 2 messages from DB
await messageDao.deleteMessageByIds(
['testMessageId${cid}0', 'testMessageId${cid}1'],
[firstMessageId, 'testMessageId${cid}1'],
);
// New fetched messages length should 2 less than the
// previous fetched messages
final newMessages = await messageDao.getMessagesByCid(cid);
expect(newMessages.length, messages.length - 2);
// Reaction for the first message should be deleted too
final newReactions =
await database.reactionDao.getReactions(firstMessageId);
expect(newReactions, isEmpty);
});
group('deleteMessageByCids', () {
const cid1 = 'testCid1';
const cid2 = 'testCid2';
const cid1 = 'test:Cid1';
const cid2 = 'test:Cid2';
test('should delete all the messages of first channel', () async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
test(
'should delete all the messages and reactions of first channel',
() async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
// Fetched message list should match the test message list length
final cid1Messages = await messageDao.getMessagesByCid(cid1);
final cid2Messages = await messageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Fetched message list should match the test message list length
final cid1Messages = await messageDao.getMessagesByCid(cid1);
final cid2Messages = await messageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Deleting all the messages of cid1
await messageDao.deleteMessageByCids([cid1]);
// Fetched reactions list should have one reaction for given message id
final cid1firstMessageId = cid1Messages.first.id;
final cid1Reactions =
await database.reactionDao.getReactions(cid1firstMessageId);
expect(cid1Reactions.length, 1);
// Fetched messages length of only cid1 should be empty
final cid1FetchedMessages = await messageDao.getMessagesByCid(cid1);
final cid2FetchedMessages = await messageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isNotEmpty);
});
// Deleting all the messages of cid1
await messageDao.deleteMessageByCids([cid1]);
test('should delete all the messages of both channel', () async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
// Fetched messages length of only cid1 should be empty
final cid1FetchedMessages = await messageDao.getMessagesByCid(cid1);
final cid2FetchedMessages = await messageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isNotEmpty);
// Fetched message list should match the test message list length
final cid1Messages = await messageDao.getMessagesByCid(cid1);
final cid2Messages = await messageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Reaction for the first message should be deleted too
final cid1FetchedReactions =
await database.reactionDao.getReactions(cid1firstMessageId);
expect(cid1FetchedReactions, isEmpty);
},
);
// Deleting all the messages of cid1
await messageDao.deleteMessageByCids([cid1, cid2]);
test(
'should delete all the messages and reactions of both channel',
() async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
// Fetched messages length of both cid1 and cid2 should be empty
final cid1FetchedMessages = await messageDao.getMessagesByCid(cid1);
final cid2FetchedMessages = await messageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isEmpty);
});
// Fetched message list should match the test message list length
final cid1Messages = await messageDao.getMessagesByCid(cid1);
final cid2Messages = await messageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Fetched reactions list should have one reaction for given message id
final cid1FirstMessageId = cid1Messages.first.id;
final cid1Reactions =
await database.reactionDao.getReactions(cid1FirstMessageId);
expect(cid1Reactions.length, 1);
final cid2FirstMessageId = cid2Messages.first.id;
final cid2Reactions =
await database.reactionDao.getReactions(cid2FirstMessageId);
expect(cid2Reactions.length, 1);
// Deleting all the messages of cid1
await messageDao.deleteMessageByCids([cid1, cid2]);
// Fetched messages length of both cid1 and cid2 should be empty
final cid1FetchedMessages = await messageDao.getMessagesByCid(cid1);
final cid2FetchedMessages = await messageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isEmpty);
// Reaction for the first message should be deleted too
final cid1FetchedReactions =
await database.reactionDao.getReactions(cid1FirstMessageId);
expect(cid1FetchedReactions, isEmpty);
final cid2FetchedReactions =
await database.reactionDao.getReactions(cid2FirstMessageId);
expect(cid2FetchedReactions, isEmpty);
},
);
});
test('getMessageById', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const id = 'testMessageId${cid}0';
// Should be null initially
@@ -190,7 +244,7 @@ void main() {
});
test('getThreadMessages', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Messages should be empty initially
final messages = await messageDao.getThreadMessages(cid);
@@ -209,7 +263,7 @@ void main() {
});
test('getThreadMessagesByParentId', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const parentId = 'testMessageId${cid}0';
// Messages should be empty initially
@@ -228,7 +282,7 @@ void main() {
});
test('getThreadMessagesByParentId along with pagination', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const parentId = 'testMessageId${cid}0';
const options = PaginationParams(
limit: 15,
@@ -262,7 +316,7 @@ void main() {
});
test('getMessagesByCid', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Should be empty initially
final messages = await messageDao.getMessagesByCid(cid);
@@ -283,7 +337,7 @@ void main() {
});
test('getMessagesByCid along with quotedMessage', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Should be empty initially
final messages = await messageDao.getMessagesByCid(cid);
@@ -301,7 +355,7 @@ void main() {
});
test('getMessagesByCid along with pagination', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const limit = 15;
const lessThan = 'testMessageId${cid}25';
const greaterThanOrEqual = 'testMessageId${cid}5';
@@ -333,7 +387,7 @@ void main() {
});
test('updateMessages', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final insertedMessages = await _prepareTestData(cid);
@@ -23,6 +23,7 @@ void main() {
bool mapAllThreadToFirstMessage = false,
int count = 3,
}) async {
final channels = [ChannelModel(cid: cid)];
final users = List.generate(count, (index) => User(id: 'testUserId$index'));
final messages = List.generate(
count,
@@ -83,13 +84,20 @@ void main() {
if (quoted) ...quotedMessages,
if (threads) ...threadMessages
];
final reaction = Reaction(
type: 'type',
messageId: allMessages.first.id,
user: users.first,
);
await database.userDao.updateUsers(users);
await database.channelDao.updateChannels(channels);
await pinnedMessageDao.updateMessages(cid, allMessages);
await database.reactionDao.updateReactions([reaction]);
return allMessages;
}
test('deleteMessageByIds', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final insertedMessages = await _prepareTestData(cid);
@@ -98,66 +106,116 @@ void main() {
final messages = await pinnedMessageDao.getMessagesByCid(cid);
expect(messages.length, insertedMessages.length);
final firstMessageId = messages.first.id;
// Fetched reactions list should have one reaction for given message id
final reactions = await database.reactionDao.getReactions(firstMessageId);
expect(reactions.length, 1);
// Deleting 2 messages from DB
await pinnedMessageDao.deleteMessageByIds(
['testMessageId${cid}0', 'testMessageId${cid}1'],
[firstMessageId, 'testMessageId${cid}1'],
);
// New fetched messages length should 2 less than the
// previous fetched messages
final newMessages = await pinnedMessageDao.getMessagesByCid(cid);
expect(newMessages.length, messages.length - 2);
// Reaction for the first message should be deleted too
final newReactions =
await database.reactionDao.getReactions(firstMessageId);
expect(newReactions, isEmpty);
});
group('deleteMessageByCids', () {
const cid1 = 'testCid1';
const cid2 = 'testCid2';
const cid1 = 'test:Cid1';
const cid2 = 'test:Cid2';
test('should delete all the messages of first channel', () async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
test(
'should delete all the messages and reactions of first channel',
() async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
// Fetched message list should match the test message list length
final cid1Messages = await pinnedMessageDao.getMessagesByCid(cid1);
final cid2Messages = await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Fetched message list should match the test message list length
final cid1Messages = await pinnedMessageDao.getMessagesByCid(cid1);
final cid2Messages = await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Deleting all the messages of cid1
await pinnedMessageDao.deleteMessageByCids([cid1]);
// Fetched reactions list should have one reaction for given message id
final cid1firstMessageId = cid1Messages.first.id;
final cid1Reactions =
await database.reactionDao.getReactions(cid1firstMessageId);
expect(cid1Reactions.length, 1);
// Fetched messages length of only cid1 should be empty
final cid1FetchedMessages = await pinnedMessageDao.getMessagesByCid(cid1);
final cid2FetchedMessages = await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isNotEmpty);
});
// Deleting all the messages of cid1
await pinnedMessageDao.deleteMessageByCids([cid1]);
test('should delete all the messages of both channel', () async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
// Fetched messages length of only cid1 should be empty
final cid1FetchedMessages =
await pinnedMessageDao.getMessagesByCid(cid1);
final cid2FetchedMessages =
await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isNotEmpty);
// Fetched message list should match the test message list length
final cid1Messages = await pinnedMessageDao.getMessagesByCid(cid1);
final cid2Messages = await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Reaction for the first message should be deleted too
final cid1FetchedReactions =
await database.reactionDao.getReactions(cid1firstMessageId);
expect(cid1FetchedReactions, isEmpty);
},
);
// Deleting all the messages of cid1
await pinnedMessageDao.deleteMessageByCids([cid1, cid2]);
test(
'should delete all the messages and reactions of both channel',
() async {
// Preparing test data
final cid1InsertedMessages = await _prepareTestData(cid1);
final cid2InsertedMessages = await _prepareTestData(cid2);
// Fetched messages length of both cid1 and cid2 should be empty
final cid1FetchedMessages = await pinnedMessageDao.getMessagesByCid(cid1);
final cid2FetchedMessages = await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isEmpty);
});
// Fetched message list should match the test message list length
final cid1Messages = await pinnedMessageDao.getMessagesByCid(cid1);
final cid2Messages = await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1Messages.length, cid1InsertedMessages.length);
expect(cid2Messages.length, cid2InsertedMessages.length);
// Fetched reactions list should have one reaction for given message id
final cid1FirstMessageId = cid1Messages.first.id;
final cid1Reactions =
await database.reactionDao.getReactions(cid1FirstMessageId);
expect(cid1Reactions.length, 1);
final cid2FirstMessageId = cid2Messages.first.id;
final cid2Reactions =
await database.reactionDao.getReactions(cid2FirstMessageId);
expect(cid2Reactions.length, 1);
// Deleting all the messages of cid1
await pinnedMessageDao.deleteMessageByCids([cid1, cid2]);
// Fetched messages length of both cid1 and cid2 should be empty
final cid1FetchedMessages =
await pinnedMessageDao.getMessagesByCid(cid1);
final cid2FetchedMessages =
await pinnedMessageDao.getMessagesByCid(cid2);
expect(cid1FetchedMessages, isEmpty);
expect(cid2FetchedMessages, isEmpty);
// Reaction for the first message should be deleted too
final cid1FetchedReactions =
await database.reactionDao.getReactions(cid1FirstMessageId);
expect(cid1FetchedReactions, isEmpty);
final cid2FetchedReactions =
await database.reactionDao.getReactions(cid2FirstMessageId);
expect(cid2FetchedReactions, isEmpty);
},
);
});
test('getMessageById', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const id = 'testMessageId${cid}0';
// Should be null initially
@@ -175,7 +233,7 @@ void main() {
});
test('getThreadMessages', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Messages should be empty initially
final messages = await pinnedMessageDao.getThreadMessages(cid);
@@ -194,7 +252,7 @@ void main() {
});
test('getThreadMessagesByParentId', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const parentId = 'testMessageId${cid}0';
// Messages should be empty initially
@@ -214,7 +272,7 @@ void main() {
});
test('getThreadMessagesByParentId along with pagination', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const parentId = 'testMessageId${cid}0';
const options = PaginationParams(
limit: 15,
@@ -248,7 +306,7 @@ void main() {
});
test('getMessagesByCid', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Should be empty initially
final messages = await pinnedMessageDao.getMessagesByCid(cid);
@@ -269,7 +327,7 @@ void main() {
});
test('getMessagesByCid along with quotedMessage', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Should be empty initially
final messages = await pinnedMessageDao.getMessagesByCid(cid);
@@ -287,7 +345,7 @@ void main() {
});
test('getMessagesByCid along with pagination', () async {
const cid = 'testCid';
const cid = 'test:Cid';
const limit = 15;
const lessThan = 'testMessageId${cid}25';
const greaterThanOrEqual = 'testMessageId${cid}5';
@@ -319,7 +377,7 @@ void main() {
});
test('updateMessages', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final insertedMessages = await _prepareTestData(cid);
@@ -21,6 +21,8 @@ void main() {
String? userId,
int count = 3,
}) async {
const cid = 'test:Cid';
final channels = [ChannelModel(cid: cid)];
final users = List.generate(count, (index) => User(id: 'testUserId$index'));
final message = Message(
id: messageId,
@@ -50,7 +52,8 @@ void main() {
);
await database.userDao.updateUsers(users);
await database.messageDao.updateMessages('testCid', [message]);
await database.channelDao.updateChannels(channels);
await database.messageDao.updateMessages(cid, [message]);
await reactionDao.updateReactions(reactions);
return reactions;
@@ -16,6 +16,7 @@ void main() {
});
Future<List<Read>> _prepareReadData(String cid, {int count = 3}) async {
final channels = [ChannelModel(cid: cid)];
final users = List.generate(count, (index) => User(id: 'testUserId$index'));
final reads = List.generate(
count,
@@ -27,12 +28,13 @@ void main() {
);
await database.userDao.updateUsers(users);
await database.channelDao.updateChannels(channels);
await readDao.updateReads(cid, reads);
return reads;
}
test('getReadsByCid', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Should be empty initially
final reads = await readDao.getReadsByCid(cid);
@@ -55,7 +57,7 @@ void main() {
});
test('updateReads', () async {
const cid = 'testCid';
const cid = 'test:Cid';
// Preparing test data
final insertedReads = await _prepareReadData(cid);