From 4e9699dbf49816104f9d4975671f5604cefa60f1 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 16 Jun 2021 16:06:55 +0200 Subject: [PATCH] add OwnUser test --- .../lib/src/core/models/own_user.dart | 25 ++--- .../lib/src/core/models/own_user.g.dart | 26 ----- .../stream_chat/test/fixtures/own_user.json | 100 ++++++++++++++++++ .../test/src/core/models/own_user_test.dart | 45 ++++++++ 4 files changed, 152 insertions(+), 44 deletions(-) create mode 100644 packages/stream_chat/test/fixtures/own_user.json create mode 100644 packages/stream_chat/test/src/core/models/own_user_test.dart diff --git a/packages/stream_chat/lib/src/core/models/own_user.dart b/packages/stream_chat/lib/src/core/models/own_user.dart index 22705b05..7ceffbfa 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.dart @@ -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: []) + @JsonKey(includeIfNull: false, defaultValue: []) final List devices; /// List of users muted by the user - @JsonKey( - includeIfNull: false, toJson: Serializer.readOnly, defaultValue: []) + @JsonKey(includeIfNull: false, defaultValue: []) final List mutes; /// List of users muted by the user - @JsonKey( - includeIfNull: false, toJson: Serializer.readOnly, defaultValue: []) + @JsonKey(includeIfNull: false, defaultValue: []) final List 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 toJson() => Serializer.moveFromExtraDataToRoot( - _$OwnUserToJson(this), - ); } diff --git a/packages/stream_chat/lib/src/core/models/own_user.g.dart b/packages/stream_chat/lib/src/core/models/own_user.g.dart index 760edc01..26e4786e 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.g.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.g.dart @@ -38,29 +38,3 @@ OwnUser _$OwnUserFromJson(Map json) { banned: json['banned'] as bool? ?? false, ); } - -Map _$OwnUserToJson(OwnUser instance) { - final val = { - '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; -} diff --git a/packages/stream_chat/test/fixtures/own_user.json b/packages/stream_chat/test/fixtures/own_user.json new file mode 100644 index 00000000..dc6aabd1 --- /dev/null +++ b/packages/stream_chat/test/fixtures/own_user.json @@ -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" +} diff --git a/packages/stream_chat/test/src/core/models/own_user_test.dart b/packages/stream_chat/test/src/core/models/own_user_test.dart new file mode 100644 index 00000000..edaf022e --- /dev/null +++ b/packages/stream_chat/test/src/core/models/own_user_test.dart @@ -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); + }); + }); +}