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)
|
.map(_messageFromJoinRow)
|
||||||
.get());
|
.get());
|
||||||
|
|
||||||
if (options?.lessThan != null && msgList.isNotEmpty) {
|
if (msgList.isNotEmpty) {
|
||||||
final lessThanIndex = msgList.indexWhere((m) => m.id == options.lessThan);
|
if (options?.lessThan != null) {
|
||||||
msgList.removeRange(lessThanIndex, msgList.length);
|
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;
|
return msgList;
|
||||||
}
|
}
|
||||||
@@ -124,24 +141,26 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
|||||||
.map(_messageFromJoinRow)
|
.map(_messageFromJoinRow)
|
||||||
.get());
|
.get());
|
||||||
|
|
||||||
if (messagePagination?.lessThan != null) {
|
if (msgList.isNotEmpty) {
|
||||||
final lessThanIndex = msgList.indexWhere(
|
if (messagePagination?.lessThan != null) {
|
||||||
(m) => m.id == messagePagination.lessThan,
|
final lessThanIndex = msgList.indexWhere(
|
||||||
);
|
(m) => m.id == messagePagination.lessThan,
|
||||||
if (lessThanIndex != -1) {
|
);
|
||||||
msgList.removeRange(lessThanIndex, msgList.length);
|
if (lessThanIndex != -1) {
|
||||||
|
msgList.removeRange(lessThanIndex, msgList.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (messagePagination?.greaterThanOrEqual != null) {
|
||||||
if (messagePagination?.greaterThanOrEqual != null) {
|
final greaterThanIndex = msgList.indexWhere(
|
||||||
final greaterThanIndex = msgList.indexWhere(
|
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
);
|
||||||
);
|
if (greaterThanIndex != -1) {
|
||||||
if (greaterThanIndex != -1) {
|
msgList.removeRange(0, greaterThanIndex);
|
||||||
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;
|
return msgList;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,9 +99,26 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
|||||||
.map(_messageFromJoinRow)
|
.map(_messageFromJoinRow)
|
||||||
.get());
|
.get());
|
||||||
|
|
||||||
if (options?.lessThan != null) {
|
if (msgList.isNotEmpty) {
|
||||||
final lessThanIndex = msgList.indexWhere((m) => m.id == options.lessThan);
|
if (options?.lessThan != null) {
|
||||||
msgList.removeRange(lessThanIndex, msgList.length);
|
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;
|
return msgList;
|
||||||
}
|
}
|
||||||
@@ -124,24 +141,26 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
|||||||
.map(_messageFromJoinRow)
|
.map(_messageFromJoinRow)
|
||||||
.get());
|
.get());
|
||||||
|
|
||||||
if (messagePagination?.lessThan != null) {
|
if (msgList.isNotEmpty) {
|
||||||
final lessThanIndex = msgList.indexWhere(
|
if (messagePagination?.lessThan != null) {
|
||||||
(m) => m.id == messagePagination.lessThan,
|
final lessThanIndex = msgList.indexWhere(
|
||||||
);
|
(m) => m.id == messagePagination.lessThan,
|
||||||
if (lessThanIndex != -1) {
|
);
|
||||||
msgList.removeRange(lessThanIndex, msgList.length);
|
if (lessThanIndex != -1) {
|
||||||
|
msgList.removeRange(lessThanIndex, msgList.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (messagePagination?.greaterThanOrEqual != null) {
|
||||||
if (messagePagination?.greaterThanOrEqual != null) {
|
final greaterThanIndex = msgList.indexWhere(
|
||||||
final greaterThanIndex = msgList.indexWhere(
|
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
);
|
||||||
);
|
if (greaterThanIndex != -1) {
|
||||||
if (greaterThanIndex != -1) {
|
msgList.removeRange(0, greaterThanIndex);
|
||||||
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;
|
return msgList;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ void main() {
|
|||||||
Future<List<Message>> _prepareTestData(
|
Future<List<Message>> _prepareTestData(
|
||||||
String cid, {
|
String cid, {
|
||||||
bool threads = false,
|
bool threads = false,
|
||||||
|
bool mapAllThreadToFirstMessage = false,
|
||||||
int count = 3,
|
int count = 3,
|
||||||
}) async {
|
}) async {
|
||||||
final messages = List.generate(
|
final messages = List.generate(
|
||||||
@@ -43,7 +44,8 @@ void main() {
|
|||||||
id: 'testThreadMessageId$cid$index',
|
id: 'testThreadMessageId$cid$index',
|
||||||
type: 'testType',
|
type: 'testType',
|
||||||
user: User(id: 'testUserId$index'),
|
user: User(id: 'testUserId$index'),
|
||||||
parentId: messages[index].id,
|
parentId:
|
||||||
|
mapAllThreadToFirstMessage ? messages[0].id : messages[index].id,
|
||||||
createdAt: DateTime.now(),
|
createdAt: DateTime.now(),
|
||||||
shadowed: math.Random().nextBool(),
|
shadowed: math.Random().nextBool(),
|
||||||
showInChannel: math.Random().nextBool(),
|
showInChannel: math.Random().nextBool(),
|
||||||
@@ -184,6 +186,40 @@ void main() {
|
|||||||
expect(threadMessages.first.parentId, parentId);
|
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 {
|
test('getMessagesByCid', () async {
|
||||||
const cid = 'testCid';
|
const cid = 'testCid';
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ void main() {
|
|||||||
Future<List<Message>> _prepareTestData(
|
Future<List<Message>> _prepareTestData(
|
||||||
String cid, {
|
String cid, {
|
||||||
bool threads = false,
|
bool threads = false,
|
||||||
|
bool mapAllThreadToFirstMessage = false,
|
||||||
int count = 3,
|
int count = 3,
|
||||||
}) async {
|
}) async {
|
||||||
final messages = List.generate(
|
final messages = List.generate(
|
||||||
@@ -43,7 +44,8 @@ void main() {
|
|||||||
id: 'testThreadMessageId$cid$index',
|
id: 'testThreadMessageId$cid$index',
|
||||||
type: 'testType',
|
type: 'testType',
|
||||||
user: User(id: 'testUserId$index'),
|
user: User(id: 'testUserId$index'),
|
||||||
parentId: messages[index].id,
|
parentId:
|
||||||
|
mapAllThreadToFirstMessage ? messages[0].id : messages[index].id,
|
||||||
createdAt: DateTime.now(),
|
createdAt: DateTime.now(),
|
||||||
shadowed: math.Random().nextBool(),
|
shadowed: math.Random().nextBool(),
|
||||||
showInChannel: math.Random().nextBool(),
|
showInChannel: math.Random().nextBool(),
|
||||||
@@ -185,6 +187,40 @@ void main() {
|
|||||||
expect(threadMessages.first.parentId, parentId);
|
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 {
|
test('getMessagesByCid', () async {
|
||||||
const cid = 'testCid';
|
const cid = 'testCid';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user