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 18, 2023
1 parent 7595e75 commit a7bcce6
Show file tree
Hide file tree
Showing 43 changed files with 1,179 additions and 806 deletions.

This file was deleted.

12 changes: 8 additions & 4 deletions tests/analytics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use chrono::{DateTime, Duration, Utc};
use common::{database::*, environment::{with_test_environment, TestEnvironment}, api_v3::ApiV3};
use common::{
api_v3::ApiV3,
database::*,
environment::{with_test_environment, TestEnvironment},
};
use itertools::Itertools;
use labrinth::models::ids::base62_impl::parse_base62;
use rust_decimal::{prelude::ToPrimitive, Decimal};
Expand All @@ -10,7 +14,7 @@ mod common;

#[actix_rt::test]
pub async fn analytics_revenue() {
with_test_environment(None, |test_env : TestEnvironment<ApiV3>| async move {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;

let alpha_project_id = test_env
Expand Down Expand Up @@ -120,8 +124,8 @@ pub async fn analytics_revenue() {
for k in sorted_keys {
assert_eq!(k % day, 0);
}

}).await;
})
.await;
}

fn to_f64_rounded_up(d: Decimal) -> f64 {
Expand Down
18 changes: 13 additions & 5 deletions tests/common/api_common/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ use std::collections::HashMap;

use actix_web::dev::ServiceResponse;
use async_trait::async_trait;
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::{VersionType, ProjectId}}};
use labrinth::{
models::{
projects::{ProjectId, VersionType},
teams::{OrganizationPermissions, ProjectPermissions},
},
search::SearchResults,
};

use crate::common::{api_v2::ApiV2, api_v3::ApiV3, dummy_data::TestFile};

use super::{Api, ApiProject, ApiTags, ApiTeams, ApiVersion, models::{CommonProject, CommonImageData, CommonVersion}};
use super::{
models::{CommonImageData, CommonProject, CommonVersion},
Api, ApiProject, ApiTags, ApiTeams, ApiVersion,
};

#[derive(Clone)]
pub enum GenericApi {
Expand All @@ -22,7 +31,7 @@ macro_rules! delegate_api_variant {
[$method_name:ident, $ret:ty, $($param_name:ident: $param_type:ty),*]
),* $(,)?
}

) => {
$(#[$meta])*
impl $impl_name for $struct_name {
Expand Down Expand Up @@ -53,7 +62,6 @@ impl Api for GenericApi {
Self::V3(api) => api.reset_search_index().await,
}
}

}

delegate_api_variant!(
Expand Down Expand Up @@ -127,4 +135,4 @@ delegate_api_variant!(
[get_project_versions_deserialized_common, Vec<CommonVersion>, project_id_slug: &str, game_versions: Option<Vec<String>>, loaders: Option<Vec<String>>,featured: Option<bool>,version_type: Option<VersionType>,limit: Option<usize>,offset: Option<usize>,pat: &str],
[edit_version_ordering, ServiceResponse, version_id: &str,ordering: Option<i32>,pat: &str],
}
);
);
153 changes: 130 additions & 23 deletions tests/common/api_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ use std::collections::HashMap;
use actix_web::dev::ServiceResponse;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use labrinth::{search::SearchResults, models::{teams::{ProjectPermissions, OrganizationPermissions}, projects::{VersionType, ProjectId}}, LabrinthConfig};
use labrinth::{
models::{
projects::{ProjectId, VersionType},
teams::{OrganizationPermissions, ProjectPermissions},
},
search::SearchResults,
LabrinthConfig,
};
use rust_decimal::Decimal;

use self::models::{CommonProject, CommonImageData, CommonLoaderData, CommonCategoryData, CommonTeamMember, CommonNotification, CommonVersion};
use self::models::{
CommonCategoryData, CommonImageData, CommonLoaderData, CommonNotification, CommonProject,
CommonTeamMember, CommonVersion,
};

use super::dummy_data::TestFile;

pub mod generic;
pub mod models;
#[async_trait(?Send)]
pub trait ApiBuildable : Api {
pub trait ApiBuildable: Api {
async fn build(labrinth_config: LabrinthConfig) -> Self;
}

Expand All @@ -27,7 +37,7 @@ pub trait Api: ApiProject + ApiTags + ApiTeams + ApiVersion {
pub trait ApiProject {
async fn add_public_project(
&self,
slug : &str,
slug: &str,
version_jar: Option<TestFile>,
modify_json: Option<json_patch::Patch>,
pat: &str,
Expand All @@ -36,13 +46,51 @@ pub trait ApiProject {
async fn get_project(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonProject;
async fn get_user_projects(&self, user_id_or_username: &str, pat: &str) -> ServiceResponse;
async fn get_user_projects_deserialized_common(&self, user_id_or_username: &str, pat: &str) -> Vec<CommonProject>;
async fn edit_project(&self,id_or_slug: &str,patch: serde_json::Value,pat: &str) -> ServiceResponse;
async fn edit_project_bulk(&self,ids_or_slugs: &[&str],patch: serde_json::Value,pat: &str,) -> ServiceResponse;
async fn edit_project_icon(&self,id_or_slug: &str,icon: Option<CommonImageData>,pat: &str,) -> ServiceResponse;
async fn search_deserialized_common(&self,query: Option<&str>,facets: Option<serde_json::Value>,pat: &str) -> SearchResults;
async fn get_analytics_revenue(&self, id_or_slugs: Vec<&str>, start_date: Option<DateTime<Utc>>, end_date: Option<DateTime<Utc>>, resolution_minutes: Option<u32>,pat: &str) -> ServiceResponse;
async fn get_analytics_revenue_deserialized(&self, id_or_slugs: Vec<&str>, start_date: Option<DateTime<Utc>>, end_date: Option<DateTime<Utc>>, resolution_minutes: Option<u32>,pat: &str) -> HashMap<String, HashMap<i64, Decimal>>;
async fn get_user_projects_deserialized_common(
&self,
user_id_or_username: &str,
pat: &str,
) -> Vec<CommonProject>;
async fn edit_project(
&self,
id_or_slug: &str,
patch: serde_json::Value,
pat: &str,
) -> ServiceResponse;
async fn edit_project_bulk(
&self,
ids_or_slugs: &[&str],
patch: serde_json::Value,
pat: &str,
) -> ServiceResponse;
async fn edit_project_icon(
&self,
id_or_slug: &str,
icon: Option<CommonImageData>,
pat: &str,
) -> ServiceResponse;
async fn search_deserialized_common(
&self,
query: Option<&str>,
facets: Option<serde_json::Value>,
pat: &str,
) -> SearchResults;
async fn get_analytics_revenue(
&self,
id_or_slugs: Vec<&str>,
start_date: Option<DateTime<Utc>>,
end_date: Option<DateTime<Utc>>,
resolution_minutes: Option<u32>,
pat: &str,
) -> ServiceResponse;
async fn get_analytics_revenue_deserialized(
&self,
id_or_slugs: Vec<&str>,
start_date: Option<DateTime<Utc>>,
end_date: Option<DateTime<Utc>>,
resolution_minutes: Option<u32>,
pat: &str,
) -> HashMap<String, HashMap<i64, Decimal>>;
}

#[async_trait(?Send)]
Expand All @@ -56,19 +104,53 @@ pub trait ApiTags {
#[async_trait(?Send)]
pub trait ApiTeams {
async fn get_team_members(&self, team_id: &str, pat: &str) -> ServiceResponse;
async fn get_team_members_deserialized_common(&self, team_id: &str, pat: &str) -> Vec<CommonTeamMember>;
async fn get_team_members_deserialized_common(
&self,
team_id: &str,
pat: &str,
) -> Vec<CommonTeamMember>;
async fn get_project_members(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_project_members_deserialized_common(&self, id_or_slug: &str, pat: &str) -> Vec<CommonTeamMember>;
async fn get_project_members_deserialized_common(
&self,
id_or_slug: &str,
pat: &str,
) -> Vec<CommonTeamMember>;
async fn get_organization_members(&self, id_or_title: &str, pat: &str) -> ServiceResponse;
async fn get_organization_members_deserialized_common(&self, id_or_title: &str, pat: &str) -> Vec<CommonTeamMember>;
async fn get_organization_members_deserialized_common(
&self,
id_or_title: &str,
pat: &str,
) -> Vec<CommonTeamMember>;
async fn join_team(&self, team_id: &str, pat: &str) -> ServiceResponse;
async fn remove_from_team(&self, team_id: &str, user_id: &str, pat: &str) -> ServiceResponse;
async fn edit_team_member(&self, team_id: &str, user_id: &str, patch: serde_json::Value, pat: &str) -> ServiceResponse;
async fn transfer_team_ownership(&self, team_id: &str, user_id: &str, pat: &str) -> ServiceResponse;
async fn edit_team_member(
&self,
team_id: &str,
user_id: &str,
patch: serde_json::Value,
pat: &str,
) -> ServiceResponse;
async fn transfer_team_ownership(
&self,
team_id: &str,
user_id: &str,
pat: &str,
) -> ServiceResponse;
async fn get_user_notifications(&self, user_id: &str, pat: &str) -> ServiceResponse;
async fn get_user_notifications_deserialized_common(&self, user_id: &str, pat: &str) -> Vec<CommonNotification>;
async fn get_user_notifications_deserialized_common(
&self,
user_id: &str,
pat: &str,
) -> Vec<CommonNotification>;
async fn mark_notification_read(&self, notification_id: &str, pat: &str) -> ServiceResponse;
async fn add_user_to_team(&self,team_id: &str,user_id: &str,project_permissions: Option<ProjectPermissions>,organization_permissions: Option<OrganizationPermissions>,pat: &str) -> ServiceResponse;
async fn add_user_to_team(
&self,
team_id: &str,
user_id: &str,
project_permissions: Option<ProjectPermissions>,
organization_permissions: Option<OrganizationPermissions>,
pat: &str,
) -> ServiceResponse;
async fn delete_notification(&self, notification_id: &str, pat: &str) -> ServiceResponse;
}

Expand All @@ -95,10 +177,29 @@ pub trait ApiVersion {
async fn get_version(&self, id_or_slug: &str, pat: &str) -> ServiceResponse;
async fn get_version_deserialized_common(&self, id_or_slug: &str, pat: &str) -> CommonVersion;
async fn get_versions(&self, ids_or_slugs: Vec<String>, pat: &str) -> ServiceResponse;
async fn get_versions_deserialized_common(&self, ids_or_slugs: Vec<String>, pat: &str) -> Vec<CommonVersion>;
async fn edit_version(&self, id_or_slug: &str, patch: serde_json::Value, pat: &str) -> ServiceResponse;
async fn get_version_from_hash(&self, id_or_slug: &str, hash: &str, pat: &str) -> ServiceResponse;
async fn get_version_from_hash_deserialized_common(&self, id_or_slug: &str, hash: &str, pat: &str) -> CommonVersion;
async fn get_versions_deserialized_common(
&self,
ids_or_slugs: Vec<String>,
pat: &str,
) -> Vec<CommonVersion>;
async fn edit_version(
&self,
id_or_slug: &str,
patch: serde_json::Value,
pat: &str,
) -> ServiceResponse;
async fn get_version_from_hash(
&self,
id_or_slug: &str,
hash: &str,
pat: &str,
) -> ServiceResponse;
async fn get_version_from_hash_deserialized_common(
&self,
id_or_slug: &str,
hash: &str,
pat: &str,
) -> CommonVersion;
async fn get_versions_from_hashes(
&self,
hashes: &[&str],
Expand Down Expand Up @@ -147,6 +248,7 @@ pub trait ApiVersion {
version_types: Option<Vec<String>>,
pat: &str,
) -> HashMap<String, CommonVersion>;
#[allow(clippy::too_many_arguments)]
async fn get_project_versions(
&self,
project_id_slug: &str,
Expand All @@ -158,6 +260,7 @@ pub trait ApiVersion {
offset: Option<usize>,
pat: &str,
) -> ServiceResponse;
#[allow(clippy::too_many_arguments)]
async fn get_project_versions_deserialized_common(
&self,
slug: &str,
Expand All @@ -170,5 +273,9 @@ pub trait ApiVersion {
pat: &str,
) -> Vec<CommonVersion>;
async fn edit_version_ordering(
&self,version_id: &str,ordering: Option<i32>,pat: &str,) -> ServiceResponse;
&self,
version_id: &str,
ordering: Option<i32>,
pat: &str,
) -> ServiceResponse;
}
20 changes: 14 additions & 6 deletions tests/common/api_common/models.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use chrono::{Utc, DateTime};
use labrinth::models::{projects::{ProjectId, ProjectStatus, ModeratorMessage, License, VersionId, GalleryItem, MonetizationStatus, DonationLink, VersionStatus, VersionType, VersionFile, Dependency}, teams::{TeamId, ProjectPermissions, OrganizationPermissions}, organizations::OrganizationId, threads::ThreadId, users::{UserId, User}, notifications::{NotificationId, NotificationBody, NotificationAction}};
use chrono::{DateTime, Utc};
use labrinth::models::{
notifications::{NotificationAction, NotificationBody, NotificationId},
organizations::OrganizationId,
projects::{
Dependency, DonationLink, GalleryItem, License, ModeratorMessage, MonetizationStatus,
ProjectId, ProjectStatus, VersionFile, VersionId, VersionStatus, VersionType,
},
teams::{OrganizationPermissions, ProjectPermissions, TeamId},
threads::ThreadId,
users::{User, UserId},
};
use rust_decimal::Decimal;
use serde::Deserialize;

// Fields shared by every version of the API.
// No struct in here should have ANY field that
// No struct in here should have ANY field that
// is not present in *every* version of the API.

// These are used for common tests- tests that can be used on both V2 AND v3 of the API and have the same results.
Expand Down Expand Up @@ -97,7 +107,6 @@ pub struct CommonCategoryData {
pub header: String,
}


/// A member of a team
#[derive(Deserialize)]
pub struct CommonTeamMember {
Expand All @@ -114,7 +123,6 @@ pub struct CommonTeamMember {
pub ordering: i64,
}


#[derive(Deserialize)]
pub struct CommonNotification {
pub id: NotificationId,
Expand All @@ -130,4 +138,4 @@ pub struct CommonNotification {
pub text: String,
pub link: String,
pub actions: Vec<NotificationAction>,
}
}
5 changes: 4 additions & 1 deletion tests/common/api_v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(dead_code)]

use super::{api_common::{Api, ApiBuildable}, environment::LocalService};
use super::{
api_common::{Api, ApiBuildable},
environment::LocalService,
};
use actix_web::{dev::ServiceResponse, test, App};
use async_trait::async_trait;
use labrinth::LabrinthConfig;
Expand Down
Loading

0 comments on commit a7bcce6

Please sign in to comment.