Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ikrivosheev committed Oct 22, 2024
1 parent bd8fa36 commit 73ee225
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 58 deletions.
20 changes: 16 additions & 4 deletions src/commons.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow, fmt};

use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Formatter;

/// Exchange types. Most variants are for exchange types included with modern RabbitMQ distributions.
/// For custom types provided by 3rd party plugins, use the `Plugin(String)` variant.
Expand Down Expand Up @@ -129,7 +129,7 @@ pub enum BindingDestinationType {
}

impl fmt::Display for BindingDestinationType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BindingDestinationType::Queue => write!(f, "queue")?,
BindingDestinationType::Exchange => write!(f, "exchange")?,
Expand Down Expand Up @@ -189,7 +189,7 @@ pub enum PolicyTarget {
}

impl fmt::Display for PolicyTarget {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Into::<String>::into(self.clone()))?;

Ok(())
Expand Down Expand Up @@ -244,6 +244,12 @@ pub enum VirtualHostLimitTarget {
MaxQueues,
}

impl borrow::Borrow<str> for VirtualHostLimitTarget {
fn borrow(&self) -> &str {
self.as_ref()
}
}

impl AsRef<str> for VirtualHostLimitTarget {
fn as_ref(&self) -> &str {
match self {
Expand Down Expand Up @@ -286,6 +292,12 @@ pub enum UserLimitTarget {
MaxChannels,
}

impl borrow::Borrow<str> for UserLimitTarget {
fn borrow(&self) -> &str {
self.as_ref()
}
}

impl AsRef<str> for UserLimitTarget {
fn as_ref(&self) -> &str {
match self {
Expand Down
53 changes: 15 additions & 38 deletions src/responses.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Borrow;
use std::fmt;
use std::{fmt, ops};

use crate::commons::{BindingDestinationType, PolicyTarget};
use serde::{
Expand Down Expand Up @@ -55,6 +54,7 @@ impl fmt::Display for TagList {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct XArguments(pub Map<String, serde_json::Value>);

impl fmt::Display for XArguments {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let coll = &self.0;
Expand All @@ -69,6 +69,15 @@ impl fmt::Display for XArguments {
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(transparent)]
pub struct RuntimeParameterValue(pub Map<String, serde_json::Value>);

impl ops::Deref for RuntimeParameterValue {
type Target = Map<String, serde_json::Value>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl fmt::Display for RuntimeParameterValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let coll = &self.0;
Expand All @@ -80,26 +89,6 @@ impl fmt::Display for RuntimeParameterValue {
}
}

impl RuntimeParameterValue {
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<&serde_json::Value>
where
String: Borrow<Q>,
Q: ?Sized + Ord + Eq + core::hash::Hash,
{
self.0.get(key)
}

#[inline]
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
String: Borrow<Q>,
Q: ?Sized + Ord + Eq + core::hash::Hash,
{
self.0.contains_key(key)
}
}

#[derive(Debug, Deserialize, Clone)]
pub struct NodeList(Vec<String>);

Expand Down Expand Up @@ -146,23 +135,11 @@ pub struct VirtualHost {
#[derive(Debug, Deserialize, Clone)]
pub struct EnforcedLimits(pub Map<String, serde_json::Value>);

impl EnforcedLimits {
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<&serde_json::Value>
where
String: Borrow<Q>,
Q: ?Sized + Ord + Eq + core::hash::Hash,
{
self.0.get(key)
}
impl ops::Deref for EnforcedLimits {
type Target = Map<String, serde_json::Value>;

#[inline]
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
String: Borrow<Q>,
Q: ?Sized + Ord + Eq + core::hash::Hash,
{
self.0.contains_key(key)
fn deref(&self) -> &Self::Target {
&self.0
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/user_limit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ fn test_list_all_user_limits() {
let vec = result3.unwrap();
assert!(vec.iter().any(|it| it.username == params.name));

let key1 = UserLimitTarget::MaxConnections.to_string();
let key1 = UserLimitTarget::MaxConnections;
assert!(!vec
.iter()
.any(|it| it.username == params.name && it.limits.get(&key1).is_some()));
let key2 = UserLimitTarget::MaxChannels.to_string();
.any(|it| it.username == params.name && it.limits.get(key1.as_ref()).is_some()));
let key2 = UserLimitTarget::MaxChannels;
assert!(vec
.iter()
.any(|it| it.username == params.name && it.limits.get(&key2).is_some()));
.any(|it| it.username == params.name && it.limits.get(key2.as_ref()).is_some()));

rc.delete_user(params.name).unwrap();
}
Expand Down Expand Up @@ -72,14 +72,14 @@ fn test_list_user_limits() {
assert!(result3.is_ok());
let vec = result3.unwrap();

let key1 = UserLimitTarget::MaxChannels.to_string();
let key1 = UserLimitTarget::MaxChannels;
assert!(vec
.iter()
.any(|it| it.username == params.name && it.limits.get(&key1).is_some()));
let key2 = UserLimitTarget::MaxConnections.to_string();
.any(|it| it.username == params.name && it.limits.get(key1.as_ref()).is_some()));
let key2 = UserLimitTarget::MaxConnections;
assert!(!vec
.iter()
.any(|it| it.username == params.name && it.limits.get(&key2).is_some()));
.any(|it| it.username == params.name && it.limits.get(key2.as_ref()).is_some()));

rc.delete_user(params.name).unwrap();
}
16 changes: 8 additions & 8 deletions tests/virtual_host_limit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ fn test_list_all_vhost_limits() {
let vec = result3.unwrap();
assert!(vec.iter().any(|it| it.vhost == vh_params.name));

let key1 = VirtualHostLimitTarget::MaxConnections.to_string();
let key1 = VirtualHostLimitTarget::MaxConnections;
assert!(!vec
.iter()
.any(|it| it.vhost == vh_params.name && it.limits.get(&key1).is_some()));
let key2 = VirtualHostLimitTarget::MaxQueues.to_string();
.any(|it| it.vhost == vh_params.name && it.limits.get(key1.as_ref()).is_some()));
let key2 = VirtualHostLimitTarget::MaxQueues;
assert!(vec
.iter()
.any(|it| it.vhost == vh_params.name && it.limits.get(&key2).is_some()));
.any(|it| it.vhost == vh_params.name && it.limits.get(key2.as_ref()).is_some()));

rc.delete_vhost(vh_params.name).unwrap();
}
Expand All @@ -54,14 +54,14 @@ fn test_list_vhost_limits() {
assert!(result3.is_ok());
let vec = result3.unwrap();

let key1 = VirtualHostLimitTarget::MaxConnections.to_string();
let key1 = VirtualHostLimitTarget::MaxConnections;
assert!(vec
.iter()
.any(|it| it.vhost == vh_params.name && it.limits.get(&key1).is_some()));
let key2 = VirtualHostLimitTarget::MaxQueues.to_string();
.any(|it| it.vhost == vh_params.name && it.limits.get(key1.as_ref()).is_some()));
let key2 = VirtualHostLimitTarget::MaxQueues;
assert!(!vec
.iter()
.any(|it| it.vhost == vh_params.name && it.limits.get(&key2).is_some()));
.any(|it| it.vhost == vh_params.name && it.limits.get(key2.as_ref()).is_some()));

rc.delete_vhost(vh_params.name).unwrap();
}

0 comments on commit 73ee225

Please sign in to comment.