Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fmt, clippy, prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Nov 25, 2023
1 parent 0aeb7e1 commit 5827955
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 101 deletions.

This file was deleted.

15 changes: 7 additions & 8 deletions tests/analytics.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use actix_web::test;
use chrono::{DateTime, Duration, Utc};
use common::permissions::PermissionsTest;
use common::permissions::PermissionsTestContext;
use common::{
api_v3::ApiV3,
database::*,
environment::{with_test_environment, TestEnvironment},
};
use common::permissions::PermissionsTest;
use common::permissions::PermissionsTestContext;
use itertools::Itertools;
use labrinth::models::ids::base62_impl::parse_base62;
use labrinth::models::teams::ProjectPermissions;
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn analytics_revenue() {
.get_analytics_revenue_deserialized(
vec![&alpha_project_id],
false,
None,
None,
None,
None,
USER_USER_PAT,
Expand Down Expand Up @@ -108,7 +108,7 @@ pub async fn analytics_revenue() {
.get_analytics_revenue_deserialized(
vec![&alpha_project_id],
false,
Some(Utc::now() - Duration::days(801)),
Some(Utc::now() - Duration::days(801)),
None,
None,
USER_USER_PAT,
Expand Down Expand Up @@ -144,8 +144,7 @@ fn to_f64_vec_rounded_up(d: Vec<Decimal>) -> Vec<f64> {

#[actix_rt::test]
pub async fn permissions_analytics_revenue() {
with_test_environment(None, |test_env : TestEnvironment<ApiV3>| async move {

with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let alpha_project_id = test_env
.dummy
.as_ref()
Expand Down Expand Up @@ -229,6 +228,6 @@ pub async fn permissions_analytics_revenue() {

// Cleanup test db
test_env.cleanup().await;
}).await;

})
.await;
}
8 changes: 4 additions & 4 deletions tests/common/api_common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::collections::HashMap;

use self::models::{
CommonCategoryData, CommonImageData, CommonLoaderData, CommonNotification, CommonProject,
CommonTeamMember, CommonVersion,
};
use actix_web::dev::ServiceResponse;
use async_trait::async_trait;
use labrinth::{
Expand All @@ -10,10 +14,6 @@ use labrinth::{
search::SearchResults,
LabrinthConfig,
};
use self::models::{
CommonCategoryData, CommonImageData, CommonLoaderData, CommonNotification, CommonProject,
CommonTeamMember, CommonVersion,
};

use super::dummy_data::TestFile;

Expand Down
2 changes: 1 addition & 1 deletion tests/common/api_v3/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,4 @@ impl ApiV3 {
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
}
}
5 changes: 3 additions & 2 deletions tests/common/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::{
// - returns a 200-299 if the scope is present
// - returns failure and success JSON bodies for requests that are 200 (for performing non-simple follow-up tests on)
// This uses a builder format, so you can chain methods to set the parameters to non-defaults (most will probably be not need to be set).
type JsonCheck = Box<dyn Fn(&serde_json::Value) + Send>;
pub struct PermissionsTest<'a, A: Api> {
test_env: &'a TestEnvironment<A>,
// Permissions expected to fail on this test. By default, this is all permissions except the success permissions.
Expand Down Expand Up @@ -54,8 +55,8 @@ pub struct PermissionsTest<'a, A: Api> {
// Closures that check the JSON body of the response for failure and success cases.
// These are used to perform more complex tests than just checking the status code.
// (eg: checking that the response contains the correct data)
failure_json_check: Option<Box<dyn Fn(&serde_json::Value) + Send>>,
success_json_check: Option<Box<dyn Fn(&serde_json::Value) + Send>>,
failure_json_check: Option<JsonCheck>,
success_json_check: Option<JsonCheck>,
}

pub struct PermissionsTestContext<'a> {
Expand Down
74 changes: 37 additions & 37 deletions tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@ async fn test_get_project() {
let versions = body["versions"].as_array().unwrap();
assert_eq!(versions[0], json!(alpha_version_id));

// Confirm that the request was cached
let mut redis_pool = test_env.db.redis_pool.connect().await.unwrap();
assert_eq!(
redis_pool
.get(PROJECTS_SLUGS_NAMESPACE, alpha_project_slug)
// Confirm that the request was cached
let mut redis_pool = test_env.db.redis_pool.connect().await.unwrap();
assert_eq!(
redis_pool
.get(PROJECTS_SLUGS_NAMESPACE, alpha_project_slug)
.await
.unwrap()
.and_then(|x| x.parse::<i64>().ok()),
Some(parse_base62(alpha_project_id).unwrap() as i64)
);

let cached_project = redis_pool
.get(
PROJECTS_NAMESPACE,
&parse_base62(alpha_project_id).unwrap().to_string(),
)
.await
.unwrap()
.and_then(|x| x.parse::<i64>().ok()),
Some(parse_base62(alpha_project_id).unwrap() as i64)
);

let cached_project = redis_pool
.get(
PROJECTS_NAMESPACE,
&parse_base62(alpha_project_id).unwrap().to_string(),
)
.await
.unwrap()
.unwrap();
let cached_project: serde_json::Value = serde_json::from_str(&cached_project).unwrap();
assert_eq!(cached_project["inner"]["slug"], json!(alpha_project_slug));
.unwrap();
let cached_project: serde_json::Value = serde_json::from_str(&cached_project).unwrap();
assert_eq!(cached_project["inner"]["slug"], json!(alpha_project_slug));

// Make the request again, this time it should be cached
let req = test::TestRequest::get()
Expand Down Expand Up @@ -259,24 +259,24 @@ async fn test_add_remove_project() {
let resp = test_env.api.remove_project("demo", USER_USER_PAT).await;
assert_eq!(resp.status(), 204);

// Confirm that the project is gone from the cache
let mut redis_pool = test_env.db.redis_pool.connect().await.unwrap();
assert_eq!(
redis_pool
.get(PROJECTS_SLUGS_NAMESPACE, "demo")
.await
.unwrap()
.and_then(|x| x.parse::<i64>().ok()),
None
);
assert_eq!(
redis_pool
.get(PROJECTS_SLUGS_NAMESPACE, &id)
.await
.unwrap()
.and_then(|x| x.parse::<i64>().ok()),
None
);
// Confirm that the project is gone from the cache
let mut redis_pool = test_env.db.redis_pool.connect().await.unwrap();
assert_eq!(
redis_pool
.get(PROJECTS_SLUGS_NAMESPACE, "demo")
.await
.unwrap()
.and_then(|x| x.parse::<i64>().ok()),
None
);
assert_eq!(
redis_pool
.get(PROJECTS_SLUGS_NAMESPACE, &id)
.await
.unwrap()
.and_then(|x| x.parse::<i64>().ok()),
None
);

// Old slug no longer works
let resp = api.get_project("demo", USER_USER_PAT).await;
Expand Down
36 changes: 18 additions & 18 deletions tests/v2/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,24 @@ async fn test_add_remove_project() {
let resp = test_env.api.remove_project("demo", USER_USER_PAT).await;
assert_eq!(resp.status(), 204);

// Confirm that the project is gone from the cache
let mut redis_conn = test_env.db.redis_pool.connect().await.unwrap();
assert_eq!(
redis_conn
.get(PROJECTS_SLUGS_NAMESPACE, "demo")
.await
.unwrap()
.map(|x| x.parse::<i64>().unwrap()),
None
);
assert_eq!(
redis_conn
.get(PROJECTS_SLUGS_NAMESPACE, &id)
.await
.unwrap()
.map(|x| x.parse::<i64>().unwrap()),
None
);
// Confirm that the project is gone from the cache
let mut redis_conn = test_env.db.redis_pool.connect().await.unwrap();
assert_eq!(
redis_conn
.get(PROJECTS_SLUGS_NAMESPACE, "demo")
.await
.unwrap()
.map(|x| x.parse::<i64>().unwrap()),
None
);
assert_eq!(
redis_conn
.get(PROJECTS_SLUGS_NAMESPACE, &id)
.await
.unwrap()
.map(|x| x.parse::<i64>().unwrap()),
None
);

// Old slug no longer works
let resp = api.get_project("demo", USER_USER_PAT).await;
Expand Down
28 changes: 14 additions & 14 deletions tests/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ async fn test_get_version() {
assert_eq!(&version.project_id.to_string(), alpha_project_id);
assert_eq!(&version.id.to_string(), alpha_version_id);

let mut redis_conn = test_env.db.redis_pool.connect().await.unwrap();
let cached_project = redis_conn
.get(
VERSIONS_NAMESPACE,
&parse_base62(alpha_version_id).unwrap().to_string(),
)
.await
.unwrap()
.unwrap();
let cached_project: serde_json::Value = serde_json::from_str(&cached_project).unwrap();
assert_eq!(
cached_project["inner"]["project_id"],
json!(parse_base62(alpha_project_id).unwrap())
);
let mut redis_conn = test_env.db.redis_pool.connect().await.unwrap();
let cached_project = redis_conn
.get(
VERSIONS_NAMESPACE,
&parse_base62(alpha_version_id).unwrap().to_string(),
)
.await
.unwrap()
.unwrap();
let cached_project: serde_json::Value = serde_json::from_str(&cached_project).unwrap();
assert_eq!(
cached_project["inner"]["project_id"],
json!(parse_base62(alpha_project_id).unwrap())
);

// Request should fail on non-existent version
let resp = api.get_version("false", USER_USER_PAT).await;
Expand Down

0 comments on commit 5827955

Please sign in to comment.