From 3472044a01eb6e9473115bd0af0ff4b0c1ef0f3b Mon Sep 17 00:00:00 2001 From: Patton Date: Wed, 6 Dec 2023 11:33:49 -0500 Subject: [PATCH] fixed minor issues --- README.md | 11 ++++- backend/Controller/UserController.php | 4 +- backend/Model/UserModel.php | 4 +- backend/tests/StackTest.php | 47 +++++++++---------- backend/vendor/bin/.phpunit.result.cache | 2 +- ...esister.test.js => incorrectValue.test.js} | 0 src/{login.test.js => link.test.js} | 2 + src/render.test.js | 2 +- ...ongdisplay.test.js => searchInput.test.js} | 1 + 9 files changed, 41 insertions(+), 32 deletions(-) rename src/{Resister.test.js => incorrectValue.test.js} (100%) rename src/{login.test.js => link.test.js} (93%) rename src/{Songdisplay.test.js => searchInput.test.js} (94%) diff --git a/README.md b/README.md index 903d6ec..b5be8a6 100644 --- a/README.md +++ b/README.md @@ -99,10 +99,19 @@ before running the test, please make sure you have node and npm installed. In your command line, enter the following command to build the environment and then run the test: ```bash +cd .. # if you're still in the backend directory npm ci -npm test +npm test # if the test doesn't run please press `a` to manually run all the tests ``` +### The tests + +- The rendering test is in `render.test.js` +- The Link test is in `link.test.js` +- The input test is in `searchInput.test.js` & `incorrectValue.test.js` +- The incorrect value test is in `incorrectValue.test.js` +- The search functionality test is in `searchInput.test.js` (new functionality) + ## Generative AI We used ChatGPT to generate some part of the test. In particular, the fetchMock function in `Songdisplay.test.js` test is generated by ChatGPT. At the time we tried to write the test that examine the search function, we need to mock the case that the frontend fetches the data from the backend. However, we don't know how to mock the fetch function. Therefore, we asked gave our initial code to ChatGPT and asked `"can you help me imporve this test by mocking the backend response and fetch some song info?"`. Then ChatGPT generated the code diff --git a/backend/Controller/UserController.php b/backend/Controller/UserController.php index 26d707a..5d6fd94 100644 --- a/backend/Controller/UserController.php +++ b/backend/Controller/UserController.php @@ -20,14 +20,14 @@ public function login() $userModel = new UserModel($db); if ($userModel->verifyUserCredentials($username, $password)) { - $this->jsonResponse(['success' => true, 'message' => 'Login successful'], 200); + $this->jsonResponse(['success' => true, 'message' => 'Login successful'], 201); } else { $this->jsonResponse(['success' => false, 'message' => 'wrong username or password'], 401); } $db->closeConnection(); } - + public function register() { $input = file_get_contents('php://input'); diff --git a/backend/Model/UserModel.php b/backend/Model/UserModel.php index 014933f..08bf4f8 100644 --- a/backend/Model/UserModel.php +++ b/backend/Model/UserModel.php @@ -86,8 +86,6 @@ public function getSongInfo($song_id) $query->close(); } - - public function updateInfo($song_id, $artist, $song_name, $song_rating) { $query = $this->db->getConnection()->prepare("UPDATE ratings SET artist = ?, song = ?, rating = ? WHERE id = ?"); @@ -136,7 +134,7 @@ public function checkDuplicate($song_name, $user_name, $song_artist) public function createSong($song_name, $user_name, $song_artist, $song_rating) { - $query = $this->db->getConnection()->prepare("INSERT INTO ratings (`username`, `artist`, `song`, `rating`) VALUES (?, ?, ?, ?, ?)"); + $query = $this->db->getConnection()->prepare("INSERT INTO ratings (`username`, `artist`, `song`, `rating`) VALUES (?, ?, ?, ?)"); $query->bind_param('sssi', $user_name, $song_artist, $song_name, $song_rating); $query->execute(); $query->close(); diff --git a/backend/tests/StackTest.php b/backend/tests/StackTest.php index 2cb243d..2f1325d 100644 --- a/backend/tests/StackTest.php +++ b/backend/tests/StackTest.php @@ -1,5 +1,4 @@ client = new GuzzleHttp\Client(["base_uri" => "http://localhost/JS_BackUp/backend/index.php"]); } // register - public function testRegister(): void - { + public function testRegister(): void{ $data = [ 'json' => [ 'username' => 'test_admin', 'password' => '1234567890aaa', ] - ]; + ]; $response = $this->client->request('POST', '?action=register', $data); $this->assertEquals(201, $response->getStatusCode()); } // login: - public function testLogin(): void - { + public function testLogin(): void{ $data = [ 'json' => [ 'username' => 'test_admin', 'password' => '1234567890aaa', ] - ]; + ]; $response = $this->client->request('POST', '?action=login', $data); - $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals(201, $response->getStatusCode()); } // fail login - public function testFailLogin(): void - { + public function testFailLogin(): void{ $data = [ 'json' => [ 'username' => 'test_admin', @@ -55,10 +50,10 @@ public function testFailLogin(): void } // create song - public function testCreateSong(): void - { + public function testCreateSong(): void{ $data = [ 'json' => [ + 'id' => '1', 'username' => 'test_admin', 'song_artist' => 'test_admin', 'song_name' => 'admin_song', @@ -70,38 +65,42 @@ public function testCreateSong(): void } // get ratings - public function testGetRatings(): void - { + public function testGetRatings(): void{ $response = $this->client->request('GET', '?action=getRatings'); $this->assertEquals(200, $response->getStatusCode()); } // update song - public function testUpdateSong(): void - { + public function testUpdateSong(): void{ $data = [ 'json' => [ - 'id' => '53', + 'id' => '27', 'username' => 'test_admin', 'song_artist' => 'test_admin', 'song_name' => 'admin_song', 'song_rating' => '4', ] - ]; + ]; $response = $this->client->request('POST', '?action=updateSong', $data); $this->assertEquals(200, $response->getStatusCode()); } // delete song - public function testDeleteSong(): void - { + public function testDeleteSong(): void{ $data = [ 'json' => [ - 'id' => '53', + 'id' => '27', 'username' => 'test_admin', ] - ]; + ]; $response = $this->client->request('POST', '?action=deleteSong', $data); $this->assertEquals(200, $response->getStatusCode()); } + } + + +?> + + + diff --git a/backend/vendor/bin/.phpunit.result.cache b/backend/vendor/bin/.phpunit.result.cache index 321bd0c..a8b9b0b 100644 --- a/backend/vendor/bin/.phpunit.result.cache +++ b/backend/vendor/bin/.phpunit.result.cache @@ -1 +1 @@ -{"version":1,"defects":{"StackTest::testPost_NewSong":8,"StackTest::testRegister":8,"StackTest::testLogin":8,"StackTest::testCreateSong":8,"StackTest::testUpdateSong":8,"StackTest::testDeleteSong":8,"StackTest::testFailLogin":7,"StackTest::testGetRatings":8},"times":{"StackTest::testPushAndPop":0.005,"StackTest::testPost_NewSong":0.203,"StackTest::testRegister":0.064,"StackTest::testLogin":0.013,"StackTest::testCreateSong":0.008,"StackTest::testGetRatings":0.007,"StackTest::testUpdateSong":0.007,"StackTest::testDeleteSong":0.014,"StackTest::testFailLogin":0.016}} \ No newline at end of file +{"version":1,"defects":{"StackTest::testPost_NewSong":8,"StackTest::testRegister":8,"StackTest::testLogin":8,"StackTest::testCreateSong":8,"StackTest::testUpdateSong":8,"StackTest::testDeleteSong":8,"StackTest::testFailLogin":7,"StackTest::testGetRatings":8},"times":{"StackTest::testPushAndPop":0.005,"StackTest::testPost_NewSong":0.203,"StackTest::testRegister":0.145,"StackTest::testLogin":0.112,"StackTest::testCreateSong":0.006,"StackTest::testGetRatings":0.005,"StackTest::testUpdateSong":0.007,"StackTest::testDeleteSong":0.006,"StackTest::testFailLogin":0.112}} \ No newline at end of file diff --git a/src/Resister.test.js b/src/incorrectValue.test.js similarity index 100% rename from src/Resister.test.js rename to src/incorrectValue.test.js diff --git a/src/login.test.js b/src/link.test.js similarity index 93% rename from src/login.test.js rename to src/link.test.js index 477a2f8..48a259a 100644 --- a/src/login.test.js +++ b/src/link.test.js @@ -4,6 +4,8 @@ import { BrowserRouter } from "react-router-dom"; import "@testing-library/jest-dom"; import Login from "./login"; +// This files is testing the link functionality + // Define the test test("clicking on the Register link navigates to the Register page", () => { // Render the Login component within a BrowserRouter diff --git a/src/render.test.js b/src/render.test.js index a75fd5d..e8fe4d2 100644 --- a/src/render.test.js +++ b/src/render.test.js @@ -14,7 +14,7 @@ import "@testing-library/jest-dom"; import fetchMock from "jest-fetch-mock"; import { act } from "react-dom/test-utils"; -// Rendering test for the register page +// Rendering test for the login page test("Register Login", () => { render( diff --git a/src/Songdisplay.test.js b/src/searchInput.test.js similarity index 94% rename from src/Songdisplay.test.js rename to src/searchInput.test.js index b5e66da..92c8f1a 100644 --- a/src/Songdisplay.test.js +++ b/src/searchInput.test.js @@ -1,3 +1,4 @@ +// This file is testing the search functionality and input functionality import React from "react"; import { render, screen, act } from "@testing-library/react"; import userEvent from "@testing-library/user-event";