Skip to content

Commit

Permalink
API Tests(#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
KeoFoxy authored Aug 4, 2024
1 parent fc92040 commit 1f91ed1
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 184 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ jobs:
build-and-test:
runs-on: ubuntu-latest

# services:
# db:
# image: postgres:16-alpine
# env:
# POSTGRES_USER: admin
# POSTGRES_PASSWORD: admin
# POSTGRES_DB: foxgres
# options: >-
# --health-cmd="pg_isready -U admin"
# --health-interval=10s
# --health-timeout=5s
# --health-retries=5
# ports:
# - 5432:5432

env:
LOG_LEVEL: debug
DATABASE_HOST: localhost
Expand Down
6 changes: 4 additions & 2 deletions Sources/App/Controllers/AnimeController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ struct AnimeController: RouteCollection {

@Sendable
func delete(req: Request) async throws -> HTTPStatus {
guard let anime = try await Anime.find(req.parameters.get("get"), on: req.db) else {
guard let animeID = req.parameters.get("id", as: UUID.self) else {
throw Abort(.badRequest)
}
guard let anime = try await Anime.find(animeID, on: req.db) else {
throw Abort(.notFound)
}

try await anime.delete(on: req.db)
return .noContent
}
Expand Down
37 changes: 0 additions & 37 deletions Sources/App/Controllers/TodoController.swift

This file was deleted.

17 changes: 0 additions & 17 deletions Sources/App/DTOs/TodoDTO.swift

This file was deleted.

14 changes: 0 additions & 14 deletions Sources/App/Migrations/CreateTodo.swift

This file was deleted.

29 changes: 0 additions & 29 deletions Sources/App/Models/Todo.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public func configure(_ app: Application) async throws {
app.migrations.add(CreateAnimeSchema())
app.migrations.add(AddGenreAndImgUrlFields())
app.migrations.add(FillDatabase())
app.migrations.add(CreateTodo())

app.views.use(.leaf)
// Disable CORS
Expand Down
1 change: 0 additions & 1 deletion Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func routes(_ app: Application) throws {
"Hello, world!"
}

try app.register(collection: TodoController())
try app.register(collection: CharacterController())
try app.register(collection: AnimeController())
}
112 changes: 112 additions & 0 deletions Tests/AppTests/AnimeControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
@testable import App
import Vapor
import XCTVapor
import Fluent

final class AnimeControllerTests: XCTestCase {
var app: Application!

override func setUp() async throws {
self.app = try await Application.make(.testing)
try await configure(app)
try await app.autoMigrate()
}

override func tearDown() async throws {
try await app.autoRevert()
try await self.app.asyncShutdown()
self.app = nil
}

func testGetAllAnimes() async throws {
try await self.app.test(.GET, "animes", afterResponse: { res async in
XCTAssertEqual(res.status, .ok)
let responseAnimes = try? res.content.decode([AnimeDTO].self)
XCTAssertEqual(responseAnimes?.count, 18)
})
}

func testCreateAndGetAnimeById() async throws {
let anime = AnimeDTO(id: UUID(), titleEn: "Test Anime", titleJp: "テストアニメ", description: "A test anime", releaseDate: Date(), rating: 9.0, episodeCount: 24, type: .tv, characters: [])

try await self.app.test(.POST, "animes", beforeRequest: { req async in
try? req.content.encode(anime)
}, afterResponse: { res in
XCTAssertEqual(res.status, .ok)
})

try await self.app.test(.GET, "animes/\(anime.id!)", afterResponse: { res async in
XCTAssertEqual(res.status, .ok)

do {
let responseAnime = try res.content.decode(AnimeDTO.self)
XCTAssertEqual(responseAnime.id, anime.id)
XCTAssertEqual(responseAnime.titleEn, anime.titleEn)
XCTAssertEqual(responseAnime.titleJp, anime.titleJp)
XCTAssertEqual(responseAnime.description, anime.description)
XCTAssertEqual(responseAnime.rating, anime.rating)
XCTAssertEqual(responseAnime.episodeCount, anime.episodeCount)
XCTAssertEqual(responseAnime.type, anime.type)
} catch {
XCTFail("Failed to decode response: \(error)")
}
})
}


func testCreateAnime() async throws {
let newAnimeDTO = AnimeDTO(
id: nil,
titleEn: "Wotakoi: Love is Hard for Otaku",
titleJp: "ヲタクに恋は難しい",
description: "Description",
releaseDate: Date(),
rating: 9.0,
episodeCount: 11,
type: .tv,
characters: [],
genres: nil,
imageUrl: "https://cdn.myanimelist.net/images/anime/1864/93518.jpg"
)

try await self.app.test(.POST, "animes", beforeRequest: { req async in
try? req.content.encode(newAnimeDTO)
}, afterResponse: { res in
XCTAssertEqual(res.status, .ok)
let responseAnime = try res.content.decode(AnimeDTO.self)
XCTAssertEqual(responseAnime.titleEn, newAnimeDTO.titleEn)
XCTAssertEqual(responseAnime.rating, newAnimeDTO.rating)
})
}

func testUpdateAnime() async throws {
let anime = Anime(titleEn: "Anime", titleJp: "アニメ", releaseDate: Date(), rating: 8.0, episodeCount: 12, type: .tv, characters: [])
try await anime.create(on: app.db)

let updatedAnimeDTO = AnimeDTO(id: anime.id, titleEn: "Updated Anime", titleJp: "更新されたアニメ", description: "Updated Description", releaseDate: Date(), rating: 9.0, episodeCount: 24, type: .tv, characters: [], genres: nil, imageUrl: nil)

try await app.test(.PUT, "animes/\(anime.id!)", beforeRequest: { req async in
try? req.content.encode(updatedAnimeDTO)
}, afterResponse: { res in
XCTAssertEqual(res.status, .ok)
let responseAnime = try res.content.decode(AnimeDTO.self)
XCTAssertEqual(responseAnime.titleEn, updatedAnimeDTO.titleEn)
})
}

func testDeleteAnime() async throws {
let anime = Anime(id: UUID(), titleEn: "Anime", titleJp: "アニメ", releaseDate: Date(), rating: 8.0, episodeCount: 12, type: .tv, characters: [])
try await anime.create(on: app.db)

guard let savedAnime = try await Anime.find(anime.id, on: app.db) else {
XCTFail("Anime not saved in the database")
return
}

try await app.test(.DELETE, "animes/\(savedAnime.id!)", afterResponse: { res async in
XCTAssertEqual(res.status, .noContent)
let deletedAnime = try? await Anime.find(savedAnime.id, on: app.db)
XCTAssertNil(deletedAnime)
})
}
}
68 changes: 0 additions & 68 deletions Tests/AppTests/AppTests.swift

This file was deleted.

Loading

0 comments on commit 1f91ed1

Please sign in to comment.