add event tests

This commit is contained in:
Salvatore Giordano
2021-06-17 10:34:53 +02:00
parent 91907639ad
commit 907573dab1
6 changed files with 176 additions and 34 deletions
@@ -4,7 +4,7 @@ import 'package:stream_chat/src/core/models/command.dart';
part 'channel_config.g.dart';
/// The class that contains the information about the configuration of a channel
@JsonSerializable(createToJson: false)
@JsonSerializable()
class ChannelConfig {
/// Constructor used for json serialization
ChannelConfig({
@@ -87,4 +87,7 @@ class ChannelConfig {
/// True if urls appears as attachments
@JsonKey(defaultValue: false)
final bool urlEnrichment;
/// Serialize to json
Map<String, dynamic> toJson() => _$ChannelConfigToJson(this);
}
@@ -32,3 +32,22 @@ ChannelConfig _$ChannelConfigFromJson(Map<String, dynamic> json) {
urlEnrichment: json['url_enrichment'] as bool? ?? false,
);
}
Map<String, dynamic> _$ChannelConfigToJson(ChannelConfig instance) =>
<String, dynamic>{
'automod': instance.automod,
'commands': instance.commands.map((e) => e.toJson()).toList(),
'connect_events': instance.connectEvents,
'created_at': instance.createdAt.toIso8601String(),
'updated_at': instance.updatedAt.toIso8601String(),
'max_message_length': instance.maxMessageLength,
'message_retention': instance.messageRetention,
'mutes': instance.mutes,
'reactions': instance.reactions,
'read_events': instance.readEvents,
'replies': instance.replies,
'search': instance.search,
'typing_events': instance.typingEvents,
'uploads': instance.uploads,
'url_enrichment': instance.urlEnrichment,
};
@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/channel_model.dart';
import 'package:stream_chat/src/core/models/message.dart';
@@ -160,11 +162,14 @@ class Event {
channelType: channelType ?? this.channelType,
parentId: parentId ?? this.parentId,
extraData: extraData ?? this.extraData,
isLocal: isLocal,
);
}
/// The channel embedded in the event object
@JsonSerializable()
@JsonSerializable(
createToJson: false,
)
class EventChannel extends ChannelModel {
/// Constructor used for json serialization
EventChannel({
@@ -212,10 +217,4 @@ class EventChannel extends ChannelModel {
'members',
...ChannelModel.topLevelFields,
];
/// Serialize to json
@override
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$EventChannelToJson(this),
);
}
@@ -89,29 +89,3 @@ EventChannel _$EventChannelFromJson(Map<String, dynamic> json) {
extraData: json['extra_data'] as Map<String, dynamic>? ?? {},
);
}
Map<String, dynamic> _$EventChannelToJson(EventChannel instance) {
final val = <String, dynamic>{
'id': instance.id,
'type': instance.type,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('cid', readonly(instance.cid));
writeNotNull('config', readonly(instance.config));
writeNotNull('created_by', readonly(instance.createdBy));
val['frozen'] = instance.frozen;
writeNotNull('last_message_at', readonly(instance.lastMessageAt));
writeNotNull('created_at', readonly(instance.createdAt));
writeNotNull('updated_at', readonly(instance.updatedAt));
writeNotNull('deleted_at', readonly(instance.deletedAt));
writeNotNull('member_count', readonly(instance.memberCount));
val['extra_data'] = instance.extraData;
val['members'] = instance.members?.map((e) => e.toJson()).toList();
return val;
}
+99
View File
@@ -0,0 +1,99 @@
{
"id": "!members-v9ktpgmYysZA-MjgC-GMoeEawFHSelkOdTu6JGxFZWU",
"type": "messaging",
"cid": "messaging:!members-v9ktpgmYysZA-MjgC-GMoeEawFHSelkOdTu6JGxFZWU",
"last_message_at": "2020-12-02T04:22:16.755334Z",
"created_at": "2020-10-01T08:49:32.90162Z",
"updated_at": "2021-06-17T08:18:48.188996Z",
"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-17T08:12:05.062115Z",
"banned": false,
"online": true,
"image": "https://placehold.jp/150x150.png",
"invisible": false,
"name": "Proud darkness",
"unread_count": 0,
"username": "Rioland"
},
"frozen": false,
"disabled": false,
"members": [
{
"user_id": "super-band-9",
"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-17T08:12:05.062115Z",
"banned": false,
"online": true,
"image": "https://placehold.jp/150x150.png",
"invisible": false,
"name": "Proud darkness",
"unread_count": 0,
"username": "Rioland"
},
"role": "owner",
"created_at": "2020-10-01T08:49:32.905052Z",
"updated_at": "2020-10-01T08:49:32.905052Z",
"banned": false,
"shadow_banned": false
},
{
"user_id": "cc48de8e-b2db-48e7-bba5-c03cefd61430",
"user": {
"id": "cc48de8e-b2db-48e7-bba5-c03cefd61430",
"role": "user",
"created_at": "2020-07-22T14:59:38.026809Z",
"updated_at": "2020-07-22T14:59:38.31338Z",
"banned": false,
"online": false,
"image": "https://images-na.ssl-images-amazon.com/images/M/MV5BMjA3NjYzMzE1MV5BMl5BanBnXkFtZTgwNTA4NDY4OTE@._V1_UX172_CR0,0,172,256_AL_.jpg",
"name": "Ana De Armas"
},
"role": "member",
"created_at": "2020-10-01T08:49:32.905053Z",
"updated_at": "2020-10-01T08:49:32.905053Z",
"banned": false,
"shadow_banned": 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"
}
]
},
"name": "test"
}
@@ -54,5 +54,53 @@ void main() {
},
);
});
test('copyWith', () {
final event = Event.fromJson(jsonFixture('event.json'));
var newEvent = event.copyWith();
expect(newEvent.type, 'type');
expect(newEvent.cid, 'cid');
expect(newEvent.connectionId, 'connectionId');
expect(newEvent.createdAt, isA<DateTime>());
expect(newEvent.me, isA<OwnUser>());
expect(newEvent.user, isA<User>());
expect(newEvent.isLocal, false);
newEvent = event.copyWith(
type: 'test',
cid: 'test',
connectionId: 'test',
extraData: {},
user: User(id: 'test'),
channelId: 'test',
totalUnreadCount: 2,
channelType: 'testtype',
);
expect(newEvent.channelType, 'testtype');
expect(newEvent.totalUnreadCount, 2);
expect(newEvent.type, 'test');
expect(newEvent.channelId, 'test');
expect(newEvent.cid, 'test');
expect(newEvent.connectionId, 'test');
expect(newEvent.extraData, {});
expect(newEvent.user!.id, 'test');
});
group('eventChannel', () {
test('should parse json correctly', () {
final eventChannel =
EventChannel.fromJson(jsonFixture('event_channel.json'));
expect(eventChannel.type, 'messaging');
expect(eventChannel.cid,
'messaging:!members-v9ktpgmYysZA-MjgC-GMoeEawFHSelkOdTu6JGxFZWU');
expect(eventChannel.createdBy!.id, 'super-band-9');
expect(eventChannel.frozen, false);
expect(eventChannel.members!.length, 2);
expect(eventChannel.memberCount, 2);
expect(eventChannel.config, isA<ChannelConfig>());
expect(eventChannel.name, 'test');
});
});
});
}