add OwnUser test

This commit is contained in:
Salvatore Giordano
2021-06-17 09:31:53 +02:00
parent 51f8e030f5
commit 4e9699dbf4
4 changed files with 152 additions and 44 deletions
@@ -1,14 +1,14 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/device.dart';
import 'package:stream_chat/src/core/models/mute.dart';
import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/src/core/models/user.dart';
import 'package:stream_chat/src/core/util/serializer.dart';
part 'own_user.g.dart';
/// The class that defines the own user model
/// This object can be found in [Event]
@JsonSerializable()
@JsonSerializable(createToJson: false)
class OwnUser extends User {
/// Constructor used for json serialization
OwnUser({
@@ -53,28 +53,23 @@ class OwnUser extends User {
);
/// List of user devices
@JsonKey(
includeIfNull: false,
toJson: Serializer.readOnly,
defaultValue: <Device>[])
@JsonKey(includeIfNull: false, defaultValue: <Device>[])
final List<Device> devices;
/// List of users muted by the user
@JsonKey(
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: <Mute>[])
@JsonKey(includeIfNull: false, defaultValue: <Mute>[])
final List<Mute> mutes;
/// List of users muted by the user
@JsonKey(
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: <Mute>[])
@JsonKey(includeIfNull: false, defaultValue: <Mute>[])
final List<Mute> channelMutes;
/// Total unread messages by the user
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0)
@JsonKey(includeIfNull: false, defaultValue: 0)
final int totalUnreadCount;
/// Total unread channels by the user
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
@JsonKey(includeIfNull: false)
final int? unreadChannels;
/// Known top level fields.
@@ -87,10 +82,4 @@ class OwnUser extends User {
'channel_mutes',
...User.topLevelFields,
];
/// Serialize to json
@override
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$OwnUserToJson(this),
);
}
@@ -38,29 +38,3 @@ OwnUser _$OwnUserFromJson(Map<String, dynamic> json) {
banned: json['banned'] as bool? ?? false,
);
}
Map<String, dynamic> _$OwnUserToJson(OwnUser instance) {
final val = <String, dynamic>{
'id': instance.id,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('role', readonly(instance.role));
writeNotNull('created_at', readonly(instance.createdAt));
writeNotNull('updated_at', readonly(instance.updatedAt));
writeNotNull('last_active', readonly(instance.lastActive));
writeNotNull('online', readonly(instance.online));
writeNotNull('banned', readonly(instance.banned));
val['extra_data'] = instance.extraData;
writeNotNull('devices', readonly(instance.devices));
writeNotNull('mutes', readonly(instance.mutes));
writeNotNull('channel_mutes', readonly(instance.channelMutes));
writeNotNull('total_unread_count', readonly(instance.totalUnreadCount));
writeNotNull('unread_channels', readonly(instance.unreadChannels));
return val;
}
+100
View File
@@ -0,0 +1,100 @@
{
"id": "super-band-9",
"role": "user",
"created_at": "2020-03-03T16:48:28.853674Z",
"updated_at": "2021-05-26T03:22:20.296181Z",
"last_active": "2021-06-16T11:59:59.003453014Z",
"banned": false,
"online": true,
"devices": [
{
"push_provider": "firebase",
"id": "cRS8elU4Q-qqdCAvHR2kSa:APA91bFy7MEgPyXbnFWi3uoanr_x8Vbi42JcWOXlg8p3vyIL5FuW4bjpVfamqQjYCgwDGxPA0C4qavOadE-uiKeGQJp6Sp5D2KDW9Od_BlDqzwEPJnVG9gC1zbj7NKCfXRqbOA2Wh2mW",
"created_at": "2020-04-23T14:36:21.838196Z",
"user_id": "super-band-9"
}
],
"mutes": [],
"channel_mutes": [
{
"user": {
"id": "super-band-9",
"role": "user",
"created_at": "2020-03-03T16:48:28.853674Z",
"updated_at": "2021-05-26T03:22:20.296181Z",
"last_active": "2021-06-16T11:59:59.003453014Z",
"banned": false,
"online": true,
"image": "https://placehold.jp/150x150.png",
"invisible": false,
"name": "Proud darkness",
"unread_count": 0,
"username": "Rioland"
},
"channel": {
"id": "!members-Qsp7PpigdPkW0rJk0603y5GnTiF1iRfoDc4SAngMMmw",
"type": "messaging",
"cid": "messaging:!members-Qsp7PpigdPkW0rJk0603y5GnTiF1iRfoDc4SAngMMmw",
"last_message_at": "2020-12-02T06:56:18.003432Z",
"created_at": "2020-11-30T10:25:32.494601Z",
"updated_at": "2020-11-30T10:25:32.494601Z",
"created_by": {
"id": "super-band-9",
"role": "user",
"created_at": "2020-03-03T16:48:28.853674Z",
"updated_at": "2021-05-26T03:22:20.296181Z",
"last_active": "2021-06-16T11:59:59.003453014Z",
"banned": false,
"online": true,
"unread_count": 0,
"username": "Rioland",
"image": "https://placehold.jp/150x150.png",
"invisible": false,
"name": "Proud darkness"
},
"frozen": false,
"disabled": false,
"member_count": 2,
"config": {
"created_at": "2020-04-15T14:57:17.00966Z",
"updated_at": "2021-05-25T14:25:30.405621Z",
"name": "messaging",
"typing_events": true,
"read_events": true,
"connect_events": true,
"search": true,
"reactions": true,
"replies": true,
"mutes": true,
"uploads": true,
"url_enrichment": true,
"custom_events": false,
"push_notifications": true,
"message_retention": "infinite",
"max_message_length": 5000,
"automod": "disabled",
"automod_behavior": "flag",
"blocklist": "profanity_en_2020_v1",
"blocklist_behavior": "block",
"automod_thresholds": {},
"commands": [
{
"name": "giphy",
"description": "Post a random gif to the channel",
"args": "[text]",
"set": "fun_set"
}
]
}
},
"created_at": "2020-12-04T10:39:06.512021Z",
"updated_at": "2020-12-04T10:39:06.512021Z"
}
],
"total_unread_count": 0,
"unread_channels": 0,
"language": "",
"image": "https://placehold.jp/150x150.png",
"name": "Proud darkness",
"username": "Rioland"
}
@@ -0,0 +1,45 @@
import 'package:stream_chat/src/core/models/own_user.dart';
import 'package:stream_chat/src/core/models/user.dart';
import 'package:test/test.dart';
import '../../utils.dart';
void main() {
group('src/models/own_user', () {
test('should parse json correctly', () {
final ownUser = OwnUser.fromJson(jsonFixture('own_user.json'));
expect(ownUser.id, 'super-band-9');
expect(ownUser.role, 'user');
expect(ownUser.createdAt, DateTime.parse('2020-03-03T16:48:28.853674Z'));
expect(ownUser.updatedAt, DateTime.parse('2021-05-26T03:22:20.296181Z'));
expect(
ownUser.lastActive, DateTime.parse('2021-06-16T11:59:59.003453014Z'));
expect(ownUser.banned, false);
expect(ownUser.online, true);
expect(ownUser.devices.length, 1);
expect(ownUser.mutes.length, 0);
expect(ownUser.channelMutes.length, 1);
expect(ownUser.totalUnreadCount, 0);
expect(ownUser.unreadChannels, 0);
expect(ownUser.extraData['image'], 'https://placehold.jp/150x150.png');
expect(ownUser.extraData['name'], 'Proud darkness');
expect(ownUser.extraData['username'], 'Rioland');
});
test('should initialize a OwnUser from a User correctly', () {
final user = User.fromJson(jsonFixture('user.json'));
final ownUser = OwnUser.fromUser(user);
expect(ownUser.id, user.id);
expect(ownUser.id, user.id);
expect(ownUser.role, user.role);
expect(ownUser.createdAt, user.createdAt);
expect(ownUser.updatedAt, user.updatedAt);
expect(ownUser.lastActive, user.lastActive);
expect(ownUser.online, user.online);
expect(ownUser.banned, user.banned);
expect(ownUser.extraData, user.extraData);
});
});
}