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 7 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;
17 changes: 17 additions & 0 deletions backend/src/models/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use async_graphql::SimpleObject;

#[derive(SimpleObject, Debug, PartialEq, Eq)]
pub struct Channel {
pub id: String,
pub name: String,
// User型を実装したら書き換える
pub owner: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

ownerみたいな別のモデルを参照するやつは多分ComplexObject使うことになるのでいらないはず

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ComplexObject、詳しく見てないんでわからないんですけど、自分のフィールドを見て新しいフィールドを擬似的に作成してるものじゃないですか?使い方違うような気がする???

impl MyObj {
    async fn c(&self) -> i32 {
        self.a + self.b
    }```

Copy link
Contributor

Choose a reason for hiding this comment

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

ComplexObjectの認識は合ってると思う。

Copy link
Contributor

Choose a reason for hiding this comment

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

色々考えてどっちでもいい感ありそうだなってなっている。

  • ComplexObjectを使わない
    • 常にDBのjoinを使って対象となるownerの情報も取ることになる
    • 実装はシンプルだけどチャンネルの情報だけ欲しい場合には勿体無い
  • ComplexObjectを使う
    • ownerの情報が必要な時だけ計算される
    • おそらくself.idをもとにチャンネルテーブルに再検索をかけるので、必要となった時の計算は増える

Copy link
Contributor

Choose a reason for hiding this comment

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

俺の認識としてはComplexObjectはここのトレードオフのための仕組みだと思ってる。間違っていたらすまん。

Copy link
Contributor

Choose a reason for hiding this comment

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

この程度のものに関してはComplexObjectは使わないで、本当に重い処理(例えば検索付きのフィールドとか)に関してComplexObjectで切り分けるとかになるのかな?

pub description: Option<String>,
pub created_at: String,
pub archived: bool,
pub private: bool,
// User型を実装したら書き換える
pub users: Vec<String>,
// message型を実装したら書き換える
pub messages: Vec<String>,
}
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;
60 changes: 60 additions & 0 deletions backend/src/usecase/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::context::Context;
use crate::generate::entities::channel::Model;
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")
}

#[cfg(test)]
mod test {
use super::get_all_channel;
use crate::context::Context;
use crate::generate::entities::channel;
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: "0".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(),
owner: "0".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()]
}],
)
}
}