feat(persistence): add pinned message reactions table
This commit is contained in:
@@ -92,7 +92,7 @@ void main() {
|
||||
await database.userDao.updateUsers(users);
|
||||
await database.channelDao.updateChannels(channels);
|
||||
await pinnedMessageDao.updateMessages(cid, allMessages);
|
||||
await database.reactionDao.updateReactions([reaction]);
|
||||
await database.pinnedMessageReactionDao.updateReactions([reaction]);
|
||||
return allMessages;
|
||||
}
|
||||
|
||||
@@ -109,7 +109,8 @@ void main() {
|
||||
final firstMessageId = messages.first.id;
|
||||
|
||||
// Fetched reactions list should have one reaction for given message id
|
||||
final reactions = await database.reactionDao.getReactions(firstMessageId);
|
||||
final reactions =
|
||||
await database.pinnedMessageReactionDao.getReactions(firstMessageId);
|
||||
expect(reactions.length, 1);
|
||||
|
||||
// Deleting 2 messages from DB
|
||||
@@ -124,7 +125,7 @@ void main() {
|
||||
|
||||
// Reaction for the first message should be deleted too
|
||||
final newReactions =
|
||||
await database.reactionDao.getReactions(firstMessageId);
|
||||
await database.pinnedMessageReactionDao.getReactions(firstMessageId);
|
||||
expect(newReactions, isEmpty);
|
||||
});
|
||||
|
||||
@@ -147,8 +148,8 @@ void main() {
|
||||
|
||||
// Fetched reactions list should have one reaction for given message id
|
||||
final cid1firstMessageId = cid1Messages.first.id;
|
||||
final cid1Reactions =
|
||||
await database.reactionDao.getReactions(cid1firstMessageId);
|
||||
final cid1Reactions = await database.pinnedMessageReactionDao
|
||||
.getReactions(cid1firstMessageId);
|
||||
expect(cid1Reactions.length, 1);
|
||||
|
||||
// Deleting all the messages of cid1
|
||||
@@ -163,8 +164,8 @@ void main() {
|
||||
expect(cid2FetchedMessages, isNotEmpty);
|
||||
|
||||
// Reaction for the first message should be deleted too
|
||||
final cid1FetchedReactions =
|
||||
await database.reactionDao.getReactions(cid1firstMessageId);
|
||||
final cid1FetchedReactions = await database.pinnedMessageReactionDao
|
||||
.getReactions(cid1firstMessageId);
|
||||
expect(cid1FetchedReactions, isEmpty);
|
||||
},
|
||||
);
|
||||
@@ -184,12 +185,12 @@ void main() {
|
||||
|
||||
// Fetched reactions list should have one reaction for given message id
|
||||
final cid1FirstMessageId = cid1Messages.first.id;
|
||||
final cid1Reactions =
|
||||
await database.reactionDao.getReactions(cid1FirstMessageId);
|
||||
final cid1Reactions = await database.pinnedMessageReactionDao
|
||||
.getReactions(cid1FirstMessageId);
|
||||
expect(cid1Reactions.length, 1);
|
||||
final cid2FirstMessageId = cid2Messages.first.id;
|
||||
final cid2Reactions =
|
||||
await database.reactionDao.getReactions(cid2FirstMessageId);
|
||||
final cid2Reactions = await database.pinnedMessageReactionDao
|
||||
.getReactions(cid2FirstMessageId);
|
||||
expect(cid2Reactions.length, 1);
|
||||
|
||||
// Deleting all the messages of cid1
|
||||
@@ -204,11 +205,11 @@ void main() {
|
||||
expect(cid2FetchedMessages, isEmpty);
|
||||
|
||||
// Reaction for the first message should be deleted too
|
||||
final cid1FetchedReactions =
|
||||
await database.reactionDao.getReactions(cid1FirstMessageId);
|
||||
final cid1FetchedReactions = await database.pinnedMessageReactionDao
|
||||
.getReactions(cid1FirstMessageId);
|
||||
expect(cid1FetchedReactions, isEmpty);
|
||||
final cid2FetchedReactions =
|
||||
await database.reactionDao.getReactions(cid2FirstMessageId);
|
||||
final cid2FetchedReactions = await database.pinnedMessageReactionDao
|
||||
.getReactions(cid2FirstMessageId);
|
||||
expect(cid2FetchedReactions, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
import 'package:stream_chat_persistence/src/dao/pinned_message_reaction_dao.dart';
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../../stream_chat_persistence_client_test.dart';
|
||||
|
||||
void main() {
|
||||
late PinnedMessageReactionDao pinnedMessageReactionDao;
|
||||
late MoorChatDatabase database;
|
||||
|
||||
setUp(() {
|
||||
database = testDatabaseProvider('testUserId');
|
||||
pinnedMessageReactionDao = database.pinnedMessageReactionDao;
|
||||
});
|
||||
|
||||
Future<List<Reaction>> _prepareReactionData(
|
||||
String messageId, {
|
||||
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,
|
||||
type: 'testType',
|
||||
user: users.first,
|
||||
createdAt: DateTime.now(),
|
||||
shadowed: math.Random().nextBool(),
|
||||
showInChannel: math.Random().nextBool(),
|
||||
replyCount: 3,
|
||||
updatedAt: DateTime.now(),
|
||||
extraData: const {'extra_test_field': 'extraTestData'},
|
||||
text: 'Dummy text',
|
||||
pinned: math.Random().nextBool(),
|
||||
pinnedAt: DateTime.now(),
|
||||
pinnedBy: users.first,
|
||||
);
|
||||
final reactions = List.generate(
|
||||
count,
|
||||
(index) => Reaction(
|
||||
type: 'testType$index',
|
||||
createdAt: DateTime.now(),
|
||||
userId: userId ?? users[index].id,
|
||||
messageId: message.id,
|
||||
score: count + 3,
|
||||
extraData: {'extra_test_field': 'extraTestData'},
|
||||
),
|
||||
);
|
||||
|
||||
await database.userDao.updateUsers(users);
|
||||
await database.channelDao.updateChannels(channels);
|
||||
await database.pinnedMessageDao.updateMessages(cid, [message]);
|
||||
await pinnedMessageReactionDao.updateReactions(reactions);
|
||||
|
||||
return reactions;
|
||||
}
|
||||
|
||||
test('getReactions', () async {
|
||||
const messageId = 'testMessageId';
|
||||
|
||||
// Should be empty initially
|
||||
final reactions = await pinnedMessageReactionDao.getReactions(messageId);
|
||||
expect(reactions, isEmpty);
|
||||
|
||||
// Adding sample reactions
|
||||
final insertedReactions = await _prepareReactionData(messageId);
|
||||
expect(insertedReactions, isNotEmpty);
|
||||
|
||||
// Fetched reaction length should match inserted reactions length.
|
||||
// Every reaction messageId should match the provided messageId.
|
||||
final fetchedReactions =
|
||||
await pinnedMessageReactionDao.getReactions(messageId);
|
||||
expect(fetchedReactions.length, insertedReactions.length);
|
||||
expect(fetchedReactions.every((it) => it.messageId == messageId), true);
|
||||
});
|
||||
|
||||
test('getReactionsByUserId', () async {
|
||||
const messageId = 'testMessageId';
|
||||
const userId = 'testUserId';
|
||||
|
||||
// Should be empty initially
|
||||
final reactions =
|
||||
await pinnedMessageReactionDao.getReactionsByUserId(messageId, userId);
|
||||
expect(reactions, isEmpty);
|
||||
|
||||
// Adding sample reactions
|
||||
final insertedReactions =
|
||||
await _prepareReactionData(messageId, userId: userId);
|
||||
expect(insertedReactions, isNotEmpty);
|
||||
|
||||
// Fetched reaction length should match inserted reactions length.
|
||||
// Every reaction messageId should match the provided messageId.
|
||||
// Every reaction userId should match the provided userId.
|
||||
final fetchedReactions =
|
||||
await pinnedMessageReactionDao.getReactionsByUserId(messageId, userId);
|
||||
expect(fetchedReactions.length, insertedReactions.length);
|
||||
expect(fetchedReactions.every((it) => it.messageId == messageId), true);
|
||||
expect(fetchedReactions.every((it) => it.userId == userId), true);
|
||||
});
|
||||
|
||||
test('updateReactions', () async {
|
||||
const messageId = 'testMessageId';
|
||||
|
||||
// Preparing test data
|
||||
final reactions = await _prepareReactionData(messageId);
|
||||
|
||||
// Modifying one of the reaction and also adding one new
|
||||
final copyReaction = reactions.first.copyWith(score: 33);
|
||||
final newReaction = Reaction(
|
||||
type: 'testType3',
|
||||
createdAt: DateTime.now(),
|
||||
userId: 'testUserId3',
|
||||
messageId: messageId,
|
||||
score: 30,
|
||||
extraData: {'extra_test_field': 'extraTestData'},
|
||||
);
|
||||
|
||||
await pinnedMessageReactionDao.updateReactions([copyReaction, newReaction]);
|
||||
|
||||
// Fetched reaction length should be one more than inserted reactions.
|
||||
// copyReaction `score` modified field should be 33.
|
||||
// Fetched reactions should contain the newReaction.
|
||||
final fetchedReactions =
|
||||
await pinnedMessageReactionDao.getReactions(messageId);
|
||||
expect(fetchedReactions.length, reactions.length + 1);
|
||||
expect(
|
||||
fetchedReactions
|
||||
.firstWhere((it) =>
|
||||
it.userId == copyReaction.userId && it.type == copyReaction.type)
|
||||
.score,
|
||||
33,
|
||||
);
|
||||
expect(
|
||||
fetchedReactions
|
||||
.where((it) =>
|
||||
it.userId == newReaction.userId && it.type == newReaction.type)
|
||||
.isNotEmpty,
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
group('deleteReactionsByMessageIds', () {
|
||||
const messageId1 = 'testMessageId1';
|
||||
const messageId2 = 'testMessageId2';
|
||||
test('should delete all the reactions of first message', () async {
|
||||
// Preparing test data
|
||||
final insertedReactions1 = await _prepareReactionData(messageId1);
|
||||
final insertedReactions2 = await _prepareReactionData(messageId2);
|
||||
|
||||
// Fetched reaction list length should match
|
||||
// the inserted reactions list length
|
||||
final reactions1 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId1);
|
||||
final reactions2 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId2);
|
||||
expect(reactions1.length, insertedReactions1.length);
|
||||
expect(reactions2.length, insertedReactions2.length);
|
||||
|
||||
// Deleting all the reactions of messageId1
|
||||
await pinnedMessageReactionDao.deleteReactionsByMessageIds([messageId1]);
|
||||
|
||||
// Fetched reactions length of only messageId1 should be empty
|
||||
final fetchedReactions1 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId1);
|
||||
final fetchedReactions2 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId2);
|
||||
expect(fetchedReactions1, isEmpty);
|
||||
expect(fetchedReactions2, isNotEmpty);
|
||||
});
|
||||
test('should delete all the messages of both message', () async {
|
||||
// Preparing test data
|
||||
final insertedReactions1 = await _prepareReactionData(messageId1);
|
||||
final insertedReactions2 = await _prepareReactionData(messageId2);
|
||||
|
||||
// Fetched reaction list length should match
|
||||
// the inserted reactions list length
|
||||
final reactions1 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId1);
|
||||
final reactions2 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId2);
|
||||
expect(reactions1.length, insertedReactions1.length);
|
||||
expect(reactions2.length, insertedReactions2.length);
|
||||
|
||||
// Deleting all the reactions of messageId1 and messageId2
|
||||
await pinnedMessageReactionDao
|
||||
.deleteReactionsByMessageIds([messageId1, messageId2]);
|
||||
|
||||
// Fetched reactions length of both messages should be empty
|
||||
final fetchedReactions1 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId1);
|
||||
final fetchedReactions2 =
|
||||
await pinnedMessageReactionDao.getReactions(messageId2);
|
||||
expect(fetchedReactions1, isEmpty);
|
||||
expect(fetchedReactions2, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await database.disconnect();
|
||||
});
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import 'package:test/test.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:stream_chat_persistence/src/mapper/pinned_message_reaction_mapper.dart';
|
||||
|
||||
import '../utils/date_matcher.dart';
|
||||
|
||||
void main() {
|
||||
test('toReaction should map the entity into Reaction', () {
|
||||
final user = User(id: 'testUserId');
|
||||
final message = Message(id: 'testMessageId');
|
||||
final entity = PinnedMessageReactionEntity(
|
||||
userId: user.id,
|
||||
messageId: message.id,
|
||||
type: 'haha',
|
||||
score: 33,
|
||||
createdAt: DateTime.now(),
|
||||
extraData: {'extra_test_data': 'extraData'},
|
||||
);
|
||||
|
||||
final reaction = entity.toReaction(user: user);
|
||||
expect(reaction, isA<Reaction>());
|
||||
expect(reaction.userId, entity.userId);
|
||||
expect(reaction.messageId, entity.messageId);
|
||||
expect(reaction.type, entity.type);
|
||||
expect(reaction.score, entity.score);
|
||||
expect(reaction.createdAt, isSameDateAs(entity.createdAt));
|
||||
expect(reaction.extraData, entity.extraData);
|
||||
});
|
||||
|
||||
test('toEntity should map reaction into PinnedMessageReactionEntity', () {
|
||||
final user = User(id: 'testUserId');
|
||||
final message = Message(id: 'testMessageId');
|
||||
final reaction = Reaction(
|
||||
userId: user.id,
|
||||
messageId: message.id,
|
||||
type: 'haha',
|
||||
score: 33,
|
||||
createdAt: DateTime.now(),
|
||||
extraData: {'extra_test_data': 'extraData'},
|
||||
);
|
||||
|
||||
final entity = reaction.toPinnedEntity();
|
||||
expect(entity, isA<PinnedMessageReactionEntity>());
|
||||
expect(entity.userId, reaction.userId);
|
||||
expect(entity.messageId, reaction.messageId);
|
||||
expect(entity.type, reaction.type);
|
||||
expect(entity.score, reaction.score);
|
||||
expect(entity.createdAt, isSameDateAs(reaction.createdAt));
|
||||
expect(entity.extraData, reaction.extraData);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user