From 8432902df19338c3aec226bbd66a4818a5aa4eba Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Tue, 23 Sep 2025 17:16:48 +0200 Subject: [PATCH] Second commit added few test, first api to add book --- api_test.go | 76 ++++++++++++++++++++++++++++++++++++ config_test/test.toml | 9 +++++ front/package-lock.json | 24 +++++++++++- front/package.json | 3 +- front/src/AddBook.vue | 41 +++++++++++++++++++ front/src/App.vue | 4 +- front/src/AppNavBar.vue | 11 ++++-- front/src/BookCard.vue | 5 ++- front/src/BooksBrowser.vue | 80 +++++++++++--------------------------- front/src/api.js | 29 ++++++++++++++ front/src/fetch.js | 13 ------- front/src/main.js | 16 +++++++- go.mod | 3 ++ internal/api/dto.go | 7 ++++ internal/api/mapper.go | 9 +++++ internal/api/routes.go | 27 +++++++++++++ internal/config/config.go | 9 ++--- internal/db/init.go | 24 +----------- main.go | 31 +++++++-------- 19 files changed, 298 insertions(+), 123 deletions(-) create mode 100644 api_test.go create mode 100644 config_test/test.toml create mode 100644 front/src/AddBook.vue create mode 100644 front/src/api.js delete mode 100644 front/src/fetch.js create mode 100644 internal/api/dto.go create mode 100644 internal/api/mapper.go create mode 100644 internal/api/routes.go diff --git a/api_test.go b/api_test.go new file mode 100644 index 0000000..a69878d --- /dev/null +++ b/api_test.go @@ -0,0 +1,76 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/gin-gonic/gin" + + "git.artlef.fr/PersonalLibraryManager/internal/config" + "github.com/stretchr/testify/assert" +) + +func testSetup() *gin.Engine { + c := config.LoadConfig("config_test/test.toml") + return setup(&c) +} + +func TestGetBooksHandler(t *testing.T) { + router := testSetup() + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/books", nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 200, w.Code) +} + +func TestPostBookHandler_Ok(t *testing.T) { + bookJson := + `{ + "title": "Le château", + "author": "Kafka", + "rating": 9 + }` + testPostBookHandler(t, bookJson, 200) +} + +func TestPostBookHandler_OkOnlyTitle(t *testing.T) { + bookJson := + `{ + "title": "Le château" + }` + testPostBookHandler(t, bookJson, 200) +} + +func TestPostBookHandler_noTitle(t *testing.T) { + bookJson := + `{ + "author": "Kafka", + "rating": 9 + }` + testPostBookHandler(t, bookJson, 400) +} + +func TestPostBookHandler_WrongRating(t *testing.T) { + bookJson := + `{ + "title": "Le château", + "author": "Kafka", + "rating": 15 + }` + testPostBookHandler(t, bookJson, 400) +} + +func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) { + router := testSetup() + w := httptest.NewRecorder() + + req, _ := http.NewRequest("POST", "/book", + strings.NewReader(string(bookJson))) + router.ServeHTTP(w, req) + + assert.Equal(t, expectedCode, w.Code) + +} diff --git a/config_test/test.toml b/config_test/test.toml new file mode 100644 index 0000000..e173fa3 --- /dev/null +++ b/config_test/test.toml @@ -0,0 +1,9 @@ + +# Path to sqlite database file. +database_file_path = "file::memory:?cache=shared" + +# The path to the sql file to load for demo data. +demo_data_path = "" + +# The port to listen on for the server. +port = "8080" diff --git a/front/package-lock.json b/front/package-lock.json index a1a5633..7bbfd75 100644 --- a/front/package-lock.json +++ b/front/package-lock.json @@ -8,7 +8,8 @@ "name": "personal-library-manager", "version": "0.0.0", "dependencies": { - "vue": "^3.5.18" + "vue": "^3.5.18", + "vue-router": "^4.5.1" }, "devDependencies": { "@eslint/js": "^9.31.0", @@ -1682,6 +1683,12 @@ "@vue/shared": "3.5.21" } }, + "node_modules/@vue/devtools-api": { + "version": "6.6.4", + "resolved": "https://registry.npmjs.org/@vue/devtools-api/-/devtools-api-6.6.4.tgz", + "integrity": "sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g==", + "license": "MIT" + }, "node_modules/@vue/devtools-core": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/@vue/devtools-core/-/devtools-core-8.0.1.tgz", @@ -3989,6 +3996,21 @@ "eslint": "^8.57.0 || ^9.0.0" } }, + "node_modules/vue-router": { + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/vue-router/-/vue-router-4.5.1.tgz", + "integrity": "sha512-ogAF3P97NPm8fJsE4by9dwSYtDwXIY1nFY9T6DyQnGHd1E2Da94w9JIolpe42LJGIl0DwOHBi8TcRPlPGwbTtw==", + "license": "MIT", + "dependencies": { + "@vue/devtools-api": "^6.6.4" + }, + "funding": { + "url": "https://github.com/sponsors/posva" + }, + "peerDependencies": { + "vue": "^3.2.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/front/package.json b/front/package.json index 7364789..efe8928 100644 --- a/front/package.json +++ b/front/package.json @@ -14,7 +14,8 @@ "format": "prettier --write src/" }, "dependencies": { - "vue": "^3.5.18" + "vue": "^3.5.18", + "vue-router": "^4.5.1" }, "devDependencies": { "@eslint/js": "^9.31.0", diff --git a/front/src/AddBook.vue b/front/src/AddBook.vue new file mode 100644 index 0000000..9609e92 --- /dev/null +++ b/front/src/AddBook.vue @@ -0,0 +1,41 @@ + + + + + diff --git a/front/src/App.vue b/front/src/App.vue index 681db68..685d1c8 100644 --- a/front/src/App.vue +++ b/front/src/App.vue @@ -1,6 +1,6 @@ diff --git a/front/src/AppNavBar.vue b/front/src/AppNavBar.vue index d52a51e..63e54fc 100644 --- a/front/src/AppNavBar.vue +++ b/front/src/AppNavBar.vue @@ -1,4 +1,6 @@ - +