test(persistence): add getThreadMessagesByParentId along with pagination test in message, pinned message dao.
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -99,9 +99,26 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (options?.lessThan != null && msgList.isNotEmpty) {
|
||||
final lessThanIndex = msgList.indexWhere((m) => m.id == options.lessThan);
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
if (options?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (options?.limit != null) {
|
||||
return msgList.take(options.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
@@ -124,24 +141,26 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
|
||||
@@ -99,9 +99,26 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere((m) => m.id == options.lessThan);
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
if (options?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (options?.limit != null) {
|
||||
return msgList.take(options.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
@@ -124,24 +141,26 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ void main() {
|
||||
Future<List<Message>> _prepareTestData(
|
||||
String cid, {
|
||||
bool threads = false,
|
||||
bool mapAllThreadToFirstMessage = false,
|
||||
int count = 3,
|
||||
}) async {
|
||||
final messages = List.generate(
|
||||
@@ -43,7 +44,8 @@ void main() {
|
||||
id: 'testThreadMessageId$cid$index',
|
||||
type: 'testType',
|
||||
user: User(id: 'testUserId$index'),
|
||||
parentId: messages[index].id,
|
||||
parentId:
|
||||
mapAllThreadToFirstMessage ? messages[0].id : messages[index].id,
|
||||
createdAt: DateTime.now(),
|
||||
shadowed: math.Random().nextBool(),
|
||||
showInChannel: math.Random().nextBool(),
|
||||
@@ -184,6 +186,40 @@ void main() {
|
||||
expect(threadMessages.first.parentId, parentId);
|
||||
});
|
||||
|
||||
test('getThreadMessagesByParentId along with pagination', () async {
|
||||
const cid = 'testCid';
|
||||
const parentId = 'testMessageId${cid}0';
|
||||
const options = PaginationParams(
|
||||
limit: 15,
|
||||
lessThan: 'testThreadMessageId${cid}25',
|
||||
greaterThanOrEqual: 'testThreadMessageId${cid}5',
|
||||
);
|
||||
|
||||
// Messages should be empty initially
|
||||
final messages = await messageDao.getThreadMessagesByParentId(
|
||||
parentId,
|
||||
options: options,
|
||||
);
|
||||
expect(messages, isEmpty);
|
||||
|
||||
// Preparing test data
|
||||
final insertedMessages = await _prepareTestData(
|
||||
cid,
|
||||
threads: true,
|
||||
mapAllThreadToFirstMessage: true,
|
||||
count: 30,
|
||||
);
|
||||
expect(insertedMessages, isNotEmpty);
|
||||
|
||||
// Should fetch all the thread messages of parentId and apply the pagination
|
||||
final threadMessages = await messageDao.getThreadMessagesByParentId(
|
||||
parentId,
|
||||
options: options,
|
||||
);
|
||||
expect(threadMessages.length, 15);
|
||||
expect(threadMessages.first.parentId, parentId);
|
||||
});
|
||||
|
||||
test('getMessagesByCid', () async {
|
||||
const cid = 'testCid';
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ void main() {
|
||||
Future<List<Message>> _prepareTestData(
|
||||
String cid, {
|
||||
bool threads = false,
|
||||
bool mapAllThreadToFirstMessage = false,
|
||||
int count = 3,
|
||||
}) async {
|
||||
final messages = List.generate(
|
||||
@@ -43,7 +44,8 @@ void main() {
|
||||
id: 'testThreadMessageId$cid$index',
|
||||
type: 'testType',
|
||||
user: User(id: 'testUserId$index'),
|
||||
parentId: messages[index].id,
|
||||
parentId:
|
||||
mapAllThreadToFirstMessage ? messages[0].id : messages[index].id,
|
||||
createdAt: DateTime.now(),
|
||||
shadowed: math.Random().nextBool(),
|
||||
showInChannel: math.Random().nextBool(),
|
||||
@@ -185,6 +187,40 @@ void main() {
|
||||
expect(threadMessages.first.parentId, parentId);
|
||||
});
|
||||
|
||||
test('getThreadMessagesByParentId along with pagination', () async {
|
||||
const cid = 'testCid';
|
||||
const parentId = 'testMessageId${cid}0';
|
||||
const options = PaginationParams(
|
||||
limit: 15,
|
||||
lessThan: 'testThreadMessageId${cid}25',
|
||||
greaterThanOrEqual: 'testThreadMessageId${cid}5',
|
||||
);
|
||||
|
||||
// Messages should be empty initially
|
||||
final messages = await pinnedMessageDao.getThreadMessagesByParentId(
|
||||
parentId,
|
||||
options: options,
|
||||
);
|
||||
expect(messages, isEmpty);
|
||||
|
||||
// Preparing test data
|
||||
final insertedMessages = await _prepareTestData(
|
||||
cid,
|
||||
threads: true,
|
||||
mapAllThreadToFirstMessage: true,
|
||||
count: 30,
|
||||
);
|
||||
expect(insertedMessages, isNotEmpty);
|
||||
|
||||
// Should fetch all the thread messages of parentId and apply the pagination
|
||||
final threadMessages = await pinnedMessageDao.getThreadMessagesByParentId(
|
||||
parentId,
|
||||
options: options,
|
||||
);
|
||||
expect(threadMessages.length, 15);
|
||||
expect(threadMessages.first.parentId, parentId);
|
||||
});
|
||||
|
||||
test('getMessagesByCid', () async {
|
||||
const cid = 'testCid';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user