From 1b2017b3aee6d5f829c6eb6e330f9f25cbb3443a Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 2 Dec 2021 13:56:39 +0100 Subject: [PATCH] Working on backend tests --- backend/main.go | 11 ++--- backend/main_test.go | 58 ++++++++++++++++++++---- backend/task/task.go | 2 +- backend/youtube/youtube-dl/youtube-dl.go | 3 +- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/backend/main.go b/backend/main.go index cda31a8..9477cea 100644 --- a/backend/main.go +++ b/backend/main.go @@ -43,16 +43,11 @@ func createTask(c *gin.Context) { } } -func status(c *gin.Context, downloader youtube.Downloader) error { - id, err := downloader.ExtractVideoID() - if err != nil { - return err - } +func getTask(c *gin.Context, id string) error { task, ok := tasks[id] if ok { c.JSON(http.StatusOK, task) } - return nil } @@ -97,8 +92,8 @@ func main() { r.POST("/task", createTask) - r.GET("/status", func(c *gin.Context) { - if err := status(c, youtube_dl.NewYoutubeDlDownloader(c.Query("url"), "yt-dlp")); err != nil { + r.GET("/task/:id", func(c *gin.Context) { + if err := getTask(c, c.Param("id")); err != nil { panic(err) } }) diff --git a/backend/main_test.go b/backend/main_test.go index 62c2900..5b762e0 100644 --- a/backend/main_test.go +++ b/backend/main_test.go @@ -8,6 +8,8 @@ import ( "strings" "testing" + "git.andreafazzi.eu/andrea/youtube-dl-service/task" + "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" "github.com/gin-gonic/gin" "github.com/remogatto/prettytest" ) @@ -27,14 +29,54 @@ func TestRunner(t *testing.T) { // Test the creation of a new task. A new task is created with a POST // request to the endpoint. func (t *testSuite) TestCreateTask() { + video, err := postTask("https://www.youtube.com/watch?v=AVIBLFl28vo") + if err != nil { + panic(err) + } + t.Equal("AVIBLFl28vo", video.ID) +} + +// Test the response of a GET /tasks/:id requests. +func (t *testSuite) TestGetTask() { + video, err := postTask("https://www.youtube.com/watch?v=AVIBLFl28vo") + if err != nil { + panic(err) + } + + // Create a new request + req, err := http.NewRequest("GET", "/task/"+video.ID, nil) + if err != nil { + panic(err) + } + + // Create an http recorder + rr := httptest.NewRecorder() + + // Set the context and call the handler + c, _ := gin.CreateTestContext(rr) + c.Request = req + + getTask(c, video.ID) + + // Read the response and assert it. + task := new(task.Task) + + if err := json.Unmarshal(rr.Body.Bytes(), &task); err != nil { + panic(err) + } + + t.Equal("AVIBLFl28vo", task.ID) +} + +func postTask(ytUrl string) (*youtube.Video, error) { // Set form values form := url.Values{} - form.Add("url", "https://www.youtube.com/watch?v=AVIBLFl28vo") + form.Add("url", ytUrl) // Create a new request req, err := http.NewRequest("POST", "/task", strings.NewReader(form.Encode())) if err != nil { - panic(err) + return nil, err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") @@ -49,13 +91,11 @@ func (t *testSuite) TestCreateTask() { createTask(c) // Read the response and assert it. - var data struct { - Id string + video := new(youtube.Video) + + if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil { + return nil, err } - if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil { - panic(err) - } - - t.Equal("AVIBLFl28vo", data.Id) + return video, nil } diff --git a/backend/task/task.go b/backend/task/task.go index 2f97490..958465c 100644 --- a/backend/task/task.go +++ b/backend/task/task.go @@ -9,7 +9,7 @@ const ( type Tasks map[string]*Task type Task struct { - Id string + ID string Status int Filename string } diff --git a/backend/youtube/youtube-dl/youtube-dl.go b/backend/youtube/youtube-dl/youtube-dl.go index 23ecce6..c18bb2a 100644 --- a/backend/youtube/youtube-dl/youtube-dl.go +++ b/backend/youtube/youtube-dl/youtube-dl.go @@ -28,6 +28,7 @@ func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) { func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) { tasks[video.ID] = &task.Task{ + ID: video.ID, Status: task.StatusDownloading, } @@ -51,7 +52,7 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas log.Printf("Download of video ID %s COMPLETED.", video.ID) tasks[video.ID] = &task.Task{ - Id: video.ID, + ID: video.ID, Status: task.StatusCompleted, Filename: video.Filename, }