Entity utils tests

This commit is contained in:
Szymon Uglis 2021-03-10 00:21:42 +01:00
parent 0bc1e37e27
commit 7aeef08fb9
No known key found for this signature in database
GPG key ID: 112376C5BEE91FE2
7 changed files with 158 additions and 15 deletions

View file

@ -56,7 +56,7 @@ class Guild extends SnowflakeEntity {
late final Cacheable<Snowflake, TextChannel>? rulesChannel;
/// The guild owner's ID
late final Cacheable<Snowflake, User>? owner;
late final Cacheable<Snowflake, User> owner;
/// The guild's members.
late final Cache<Snowflake, Member> members;
@ -142,6 +142,13 @@ class Guild extends SnowflakeEntity {
this.splash = raw["splash"] as String?;
this.embedEnabled = raw["embed_enabled"] as bool?;
this.systemChannelFlags = raw["system_channel_flags"] as int;
this.premiumTier = PremiumTier.from(raw["premium_tier"] as int);
this.premiumSubscriptionCount = raw["premium_subscription_count"] as int?;
this.preferredLocale = raw["preferred_locale"] as String;
this.owner = _UserCacheable(client, Snowflake(raw["owner_id"]));
this.roles = _SnowflakeCache<Role>();
if (raw["roles"] != null) {
raw["roles"].forEach((o) {
@ -176,11 +183,6 @@ class Guild extends SnowflakeEntity {
this.afkChannel = _ChannelCacheable(client, Snowflake(raw["afk_channel_id"]));
}
this.systemChannelFlags = raw["system_channel_flags"] as int;
this.premiumTier = PremiumTier.from(raw["premium_tier"] as int);
this.premiumSubscriptionCount = raw["premium_subscription_count"] as int?;
this.preferredLocale = raw["preferred_locale"] as String;
this.members = _SnowflakeCache();
this.voiceStates = _SnowflakeCache();
@ -211,8 +213,6 @@ class Guild extends SnowflakeEntity {
// }
// });
this.owner = _UserCacheable(client, Snowflake(raw["owner_id"]));
if (raw["voice_states"] != null) {
raw["voice_states"].forEach((o) {
final state = VoiceState._new(client, o as Map<String, dynamic>);

View file

@ -82,7 +82,7 @@ class Member extends SnowflakeEntity {
/// Returns total permissions of user.
Future<Permissions> get effectivePermissions async {
final guildInstance = await guild.getOrDownload();
final owner = await guildInstance.owner?.getOrDownload();
final owner = await guildInstance.owner.getOrDownload();
if (this == owner) {
return Permissions.all();
}

View file

@ -2,7 +2,7 @@ part of nyxx;
/// Generic interface for caching entities.
/// Wraps [Map] interface and provides utilities for manipulating cache.
abstract class Cache<T, S> implements Disposable {
class Cache<T, S> implements Disposable {
late Map<T, S> _cache;
/// Returns values of cache
@ -66,9 +66,11 @@ abstract class Cache<T, S> implements Disposable {
Iterable<S> takeLast(int count) => values.toList().sublist(values.length - count);
/// Get first element
@override
S? get first => _cache.values.first;
/// Get last element
@override
S get last => _cache.values.last;
/// Get number of elements from cache

View file

@ -76,7 +76,7 @@ class MemberCachePolicy extends CachePolicy<Member> {
static final CachePolicy<Member> voice = MemberCachePolicy((member) => member.voiceState != null);
/// Cache only member which are owner of guild
static final CachePolicy<Member> owner = MemberCachePolicy((member) => member.guild.getFromCache()?.owner?.id == member.id);
static final CachePolicy<Member> owner = MemberCachePolicy((member) => member.guild.getFromCache()?.owner.id == member.id);
/// Default policy is [owner] or [voice]. So it caches guild owners and users in voice channels
static final CachePolicy<Member> def = owner.or(voice);

View file

@ -29,8 +29,8 @@ class EntityUtility {
/// Guild guild = EntityUtility.createGuild(bot, rawJson);
/// }
/// ```
static Guild createGuild(INyxx client, Map<String, dynamic> rawJson) =>
Guild._new(client, rawJson);
static Guild createGuild(INyxx client, Map<String, dynamic> rawJson, [bool guildCreate = false]) =>
Guild._new(client, rawJson, guildCreate);
/// Creates a Role object, can be used for other classes where you have correct rawJson data from the API.
/// ```dart

View file

@ -163,6 +163,12 @@ class PermissionsBuilder {
..viewGuildInsights = permissions.viewGuildInsights;
}
/// Calculates permission int
int calculatePermissionValue() {
final set = this._build();
return set.allow & ~set.deny;
}
_PermissionsSet _build() {
final permissionSet = _PermissionsSet();

View file

@ -28,12 +28,82 @@ final sampleMemberData = {
"deaf": false,
"mute": false,
"roles": [
"1234564"
"1234563"
"1234564",
"5434534"
],
"joined_at": DateTime.now().toIso8601String()
};
final sampleRoleData = {
"id": 456,
"name": "This is role",
"position": 1,
"hoist": false,
"managed": false,
"mentionable": false,
"permissions": (
PermissionsBuilder()
..sendMessages = true
..readMessageHistory = true
).calculatePermissionValue().toString(),
"color": DiscordColor.aquamarine.value,
};
final sampleTextChannel = {
"id": 1234,
"name": "This is text channel",
"position": 0,
"nsfw": true,
"topic": "This is topic",
"type": 0
};
final sampleVoiceChannel = {
"id": 4321,
"name": "This is voice channel",
"position": 1,
"type": 2
};
final sampleDMChannel = {
"id": 1234,
"type": 1,
"recipient": sampleUserRawData
};
final sampleEmoji = {
"id": 123,
"roles": []
};
final sampleGuildData = {
"id": 123,
"name": "This is guild name",
"region": "Europe",
"afk_timeout": 10,
"mfa_level": 1,
"verification_level": 1,
"default_message_notifications": 1,
"icon": null,
"discoverySplash": null,
"system_channel_flags": 1,
"premium_tier": 1,
"premium_subscription_count": 15,
"preferred_locale": "en_US",
"roles": [
sampleRoleData
],
"emojis": [
sampleEmoji
],
"owner_id": 321,
"channels": [
sampleTextChannel,
sampleVoiceChannel
],
"features": [ "FEATURE" ]
};
final client = NyxxRest("dum", 0);
void main() {
@ -186,6 +256,71 @@ void main() {
test("Create member object", () {
final member = EntityUtility.createGuildMember(client, 123.toSnowflake(), sampleMemberData);
expect(member.id, 123.toSnowflake());
expect(member.nickname, "This is nick");
expect(member.deaf, false);
expect(member.mute, false);
expect(member.roles, hasLength(2));
expect(member.joinedAt, isNotNull);
});
test("Create guild object", () {
final guild = EntityUtility.createGuild(client, sampleGuildData, true);
expect(guild.id, 123.toSnowflake());
expect(guild.name, "This is guild name");
expect(guild.region, "Europe");
expect(guild.afkTimeout, 10);
expect(guild.mfaLevel, 1);
expect(guild.verificationLevel, 1);
expect(guild.notificationLevel, 1);
expect(guild.iconURL(), isNull);
expect(guild.discoveryURL(), isNull);
expect(guild.systemChannelFlags, 1);
expect(guild.premiumTier, PremiumTier.tier1);
expect(guild.premiumSubscriptionCount, 15);
expect(guild.preferredLocale, "en_US");
expect(guild.roles.count, 1);
expect(guild.roles.first!.id, 456.toSnowflake());
expect(guild.emojis.count, 1);
expect(guild.emojis.first!.id, 123.toSnowflake());
expect(guild.channels.toList().length, 2);
expect(guild.channels.first.id, 1234.toSnowflake());
expect(guild.channels.last.id, 4321.toSnowflake());
expect(guild.owner.id, 321.toSnowflake());
});
test("Create Text channel", () {
final channel = EntityUtility.createTextGuildChannel(client, 123.toSnowflake(), sampleTextChannel);
expect(channel.id, 1234.toSnowflake());
expect(channel.name, "This is text channel");
expect(channel.position, 0);
expect(channel.isNsfw, true);
expect(channel.topic, "This is topic");
expect(channel.channelType, ChannelType.text);
});
test("Create Voice channel", () {
final channel = EntityUtility.createVoiceGuildChannel(client, 123.toSnowflake(), sampleVoiceChannel);
expect(channel.id, 4321.toSnowflake());
expect(channel.name, "This is voice channel");
expect(channel.position, 1);
expect(channel.channelType, ChannelType.voice);
});
test("Create Role", () {
final role = EntityUtility.createRole(client, 123.toSnowflake(), sampleRoleData);
expect(role.id, 456.toSnowflake());
expect(role.name, "This is role");
expect(role.position, 1);
expect(role.hoist, false);
expect(role.managed, false);
expect(role.mentionable, false);
expect(role.color, DiscordColor.aquamarine);
expect(role.permissions.raw, PermissionsConstants.sendMessages | PermissionsConstants.readMessageHistory);
});
});
}