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 all 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
40 changes: 33 additions & 7 deletions .github/workflows/ci-backend.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
name: CI

on:
push:
paths:
- "backend/**"
workflow_dispatch:


jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -19,20 +16,49 @@ jobs:

- name: Checkout
uses: actions/checkout@v4

- name: Restore Cache
uses: Swatinem/rust-cache@v2
with:
workspaces: backend

- name: Build project
run: |
cargo build

lint:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable

- name: Checkout
uses: actions/checkout@v4
- name: Restore Cache
uses: Swatinem/rust-cache@v2
with:
workspaces: backend
- name: lint check
run: |
cargo clippy

- name: format check
run: |
cargo fmt --check
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable

- name: Checkout
uses: actions/checkout@v4
- name: Restore Cache
uses: Swatinem/rust-cache@v2
with:
workspaces: backend
- name: test
run: |
cargo test
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;
142 changes: 142 additions & 0 deletions backend/src/usecase/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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};
use crate::context::Context;
use crate::generate::entities::channel;
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,
}],
)
}

#[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())
}

#[tokio::test]
async fn チャンネルに参加しているユーザーの一覧を取得する() {
// SeaORMのMockの仕様上find_with_related()のシミュレートが出来ないためテストが不可能
}

#[tokio::test]
async fn 全てのメッセージを取得する() {
// SeaORMのMockの仕様上find_with_related()のシミュレートが出来ないためテストが不可能
}
}
Loading