test(persistence): add tests for user dao

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-03-16 21:11:07 +05:30
parent 66a577901c
commit e87a8a07a1
3 changed files with 107 additions and 0 deletions
@@ -100,4 +100,28 @@ class User {
/// Serialize to json
Map<String, dynamic> toJson() =>
Serialization.moveFromExtraDataToRoot(_$UserToJson(this), topLevelFields);
/// Creates a copy of [User] with specified attributes overridden.
User copyWith({
String id,
String role,
DateTime createdAt,
DateTime updatedAt,
DateTime lastActive,
bool online,
Map<String, dynamic> extraData,
bool banned,
List<String> teams,
}) =>
User(
id: id ?? this.id,
role: role ?? this.role,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
lastActive: lastActive ?? this.lastActive,
online: online ?? this.online,
extraData: extraData ?? this.extraData,
banned: banned ?? this.banned,
teams: teams ?? this.teams,
);
}
@@ -20,4 +20,10 @@ class UserDao extends DatabaseAccessor<MoorChatDatabase> with _$UserDaoMixin {
mode: InsertMode.insertOrReplace,
),
);
/// Returns the list of all the users stored in db
Future<List<User>> getUsers() =>
(select(users)..orderBy([(u) => OrderingTerm.desc(u.createdAt)]))
.map((it) => it.toUser())
.get();
}
@@ -0,0 +1,77 @@
import 'dart:math' as math;
import 'package:stream_chat_persistence/src/dao/dao.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'package:test/test.dart';
import 'package:stream_chat/stream_chat.dart';
void main() {
UserDao userDao;
MoorChatDatabase database;
setUp(() {
database = MoorChatDatabase.testable('testUserId');
userDao = database.userDao;
});
Future<List<User>> _prepareUserData({int count = 3}) async {
final users = List.generate(
count,
(index) => User(
id: 'testUserId$index',
role: 'testRole',
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
lastActive: DateTime.now(),
online: math.Random().nextBool(),
banned: math.Random().nextBool(),
),
);
await userDao.updateUsers(users);
return users;
}
test('updateUsers', () async {
// Preparing test data
final insertedUsers = await _prepareUserData();
// Modifying one of the user and also adding one new
final copyUser = insertedUsers.first.copyWith(online: false);
final newUser = User(
id: 'testUserId3',
role: 'testRole',
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
lastActive: DateTime.now(),
online: math.Random().nextBool(),
banned: math.Random().nextBool(),
);
await userDao.updateUsers([copyUser, newUser]);
// Fetched users length should be one more than inserted users.
// copyUser `online` modified field should be `false`.
// Fetched users should contain the newUser.
final fetchedUsers = await userDao.getUsers();
expect(fetchedUsers.length, insertedUsers.length + 1);
expect(fetchedUsers.firstWhere((it) => it.id == copyUser.id).online, false);
expect(fetchedUsers.contains(newUser), true);
});
test('getUsers', () async {
// Should be empty initially
final users = await userDao.getUsers();
expect(users, isEmpty);
// Preparing test data
final insertedUsers = await _prepareUserData();
expect(insertedUsers, isNotEmpty);
// Fetched user list should match inserted user list length
final fetchedUsers = await userDao.getUsers();
expect(fetchedUsers.length, insertedUsers.length);
});
tearDown(() async {
await database.disconnect();
});
}