Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

チャンネル取得機能のテストの実装 #61

Merged
merged 14 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ sea-orm = { version = "0.12", features = [
"sqlx-postgres",
"runtime-tokio-native-tls",
"macros",
"mock"
] }
1 change: 1 addition & 0 deletions backend/src/models.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod channel;
pub mod test;
11 changes: 11 additions & 0 deletions backend/src/models/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use async_graphql::SimpleObject;

#[derive(SimpleObject, Debug, PartialEq, Eq)]
pub struct Channel {
pub id: String,
pub name: String,
pub description: Option<String>,
pub created_at: String,
pub archived: bool,
pub private: bool,
}
1 change: 1 addition & 0 deletions backend/src/usecase.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod channel;
pub mod test;
290 changes: 290 additions & 0 deletions backend/src/usecase/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
use crate::context::Context;
use crate::models::channel::Channel;

pub async fn get_all_channel(_ctx: &Context) -> Result<Vec<Channel>, ()> {
todo!("generate::entities::channel::Model to models::channel::Channel")
}

// User型を返すように要修正
pub async fn get_channel_owner_by_channel_id(
ctx: &Context,
channel_id: &str,
) -> Result<String, ()> {
todo!()
}

// User型を返すように要修正
pub async fn get_channel_users_by_channel_id(
ctx: &Context,
channel_id: &str,
) -> Result<Option<Vec<String>>, ()> {
todo!()
}

// Message型を返すように要修正
pub async fn get_messages_by_channel_id(
ctx: &Context,
channel_id: &str,
) -> Result<Option<Vec<String>>, ()> {
todo!()
}

#[cfg(test)]
mod test {
use super::{
get_all_channel, get_channel_owner_by_channel_id, get_channel_users_by_channel_id,
get_messages_by_channel_id,
};
use crate::context::Context;
use crate::generate::entities::channel;
use crate::generate::entities::channel_user;
use crate::generate::entities::message;
use crate::generate::entities::user;
use crate::models::channel::Channel;
use sea_orm::prelude::*;
use sea_orm::MockDatabase;

#[tokio::test]
async fn すべてのチャンネルを取得する() {
// Arrange
let db: DatabaseConnection = MockDatabase::new(sea_orm::DatabaseBackend::Postgres)
.append_query_results([vec![channel::Model {
id: "0".to_string(),
channel_name: "hoge".to_string(),
description: Some("huga".to_string()),
is_private: false,
created_user_id: "aaa".to_string(),
created_at: DateTime::parse_from_str("2024-08-08 00:00:00", "%Y-%m-%d %H:%M:%S")
.unwrap(),
updated_at: None,
archive_at: None,
deleted_at: None,
}]])
.into_connection();

let context = Context {
noharu36 marked this conversation as resolved.
Show resolved Hide resolved
env: "harukun".to_string(),
db,
};

// Action
let result = get_all_channel(&context).await.unwrap();

// Assert
assert_eq!(
result,
vec![Channel {
id: "0".to_string(),
name: "hoge".to_string(),
description: Some("huga".to_string()),
created_at: "2024-08-08 00:00:00".to_string(),
archived: false,
private: false,
//users: vec!["eraser".to_string(), "yuorei".to_string()],
//messages: vec!["welcome".to_string(), "zoo".to_string()]
}],
)
}

#[tokio::test]
async fn チャンネルオーナーを取得する() {
// Arrange
let db: DatabaseConnection = MockDatabase::new(sea_orm::DatabaseBackend::Postgres)
.append_query_results([vec![(
channel::Model {
id: "0".to_string(),
channel_name: "hoge".to_string(),
description: Some("huga".to_string()),
is_private: false,
created_user_id: "aaa".to_string(),
created_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
updated_at: None,
archive_at: None,
deleted_at: None,
},
user::Model {
id: "aaa".to_string(),
user_name: "haru".to_string(),
display_name: "haru".to_string(),
created_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
updated_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
},
)]])
.into_connection();

let context = Context {
env: "harukun".to_string(),
db,
};

// Action
let result = get_channel_owner_by_channel_id(&context, "aaa")
.await
.unwrap();
// Assert
assert_eq!(result, "aaa".to_string())
}

// append_query_resultsに3要素のタプルを渡すための拡張
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここを追加することでappend_query_resultsに3要素のタプルを渡すことができる。
書く場所がわからなかったのでとりあえずテストの中に書いてる

use sea_orm::{
EntityTrait, IdenStatic, IntoMockRow, Iterable, MockRow, ModelTrait, SelectA, SelectB,
};
use std::collections::BTreeMap;

struct IntoMockTripleCulumn<L, M, N>(L, M, N)
where
L: ModelTrait,
M: ModelTrait,
N: ModelTrait;

impl<L, M, N> IntoMockRow for IntoMockTripleCulumn<L, M, N>
where
L: ModelTrait,
M: ModelTrait,
N: ModelTrait,
{
fn into_mock_row(self) -> MockRow {
let mut mapped_join = BTreeMap::new();

for column in <<L as ModelTrait>::Entity as EntityTrait>::Column::iter() {
mapped_join.insert(
format!("{}{}", SelectA.as_str(), column.as_str()),
self.0.get(column),
);
}
for column in <<M as ModelTrait>::Entity as EntityTrait>::Column::iter() {
mapped_join.insert(
format!("{}{}", SelectB.as_str(), column.as_str()),
self.1.get(column),
);
}

for column in <<N as ModelTrait>::Entity as EntityTrait>::Column::iter() {
mapped_join.insert(format!("{}", column.as_str()), self.2.get(column));
}

mapped_join.into_mock_row()
}
}

#[tokio::test]
async fn チャンネルに参加しているユーザーの一覧を取得する() {
// Arrange
let db: DatabaseConnection = MockDatabase::new(sea_orm::DatabaseBackend::Postgres)
.append_query_results([vec![IntoMockTripleCulumn(
channel::Model {
id: "0".to_string(),
channel_name: "hoge".to_string(),
description: Some("huga".to_string()),
is_private: false,
created_user_id: "aaa".to_string(),
created_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
updated_at: None,
archive_at: None,
deleted_at: None,
},
channel_user::Model {
joined_at: DateTime::parse_from_str("2024-08-08 00:00:00", "%Y-%m-%d %H:%M:%S")
.unwrap(),
user_id: "aaa".to_string(),
channel_id: "0".to_string(),
},
user::Model {
id: "aaa".to_string(),
user_name: "haru".to_string(),
display_name: "haru".to_string(),
created_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
// Noneになる予定?
updated_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
},
)]])
.into_connection();

let context = Context {
env: "harukun".to_string(),
db,
};

// Action
let result = get_channel_users_by_channel_id(&context, "aaa")
.await
.unwrap()
.unwrap();
// Assert
assert_eq!(result[0], "aaa".to_string())
}

#[tokio::test]
async fn 全てのメッセージを取得する() {
// Arrange
let db: DatabaseConnection = MockDatabase::new(sea_orm::DatabaseBackend::Postgres)
.append_query_results([vec![(
channel::Model {
id: "0".to_string(),
channel_name: "hoge".to_string(),
description: Some("huga".to_string()),
is_private: false,
created_user_id: "aaa".to_string(),
created_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
updated_at: None,
archive_at: None,
deleted_at: None,
},
message::Model {
id: "aaa".to_string(),
user_id: "harukun".to_string(),
channel_id: "0".to_string(),
content: "test".to_string(),
created_at: DateTime::parse_from_str(
"2024-08-08 00:00:00",
"%Y-%m-%d %H:%M:%S",
)
.unwrap(),
updated_at: None,
deleted_at: None,
},
)]])
.into_connection();

let context = Context {
env: "harukun".to_string(),
db,
};

// Action
let result = get_messages_by_channel_id(&context, "aaa")
.await
.unwrap()
.unwrap();
// Assert
assert_eq!(result[0], "aaa".to_string())
}
}