Skip to content

Commit

Permalink
fixed minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PattonYin committed Dec 6, 2023
1 parent 56a8e7e commit 3472044
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 32 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions backend/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 1 addition & 3 deletions backend/Model/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?");
Expand Down Expand Up @@ -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();
Expand Down
47 changes: 23 additions & 24 deletions backend/tests/StackTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

use PHPUnit\Framework\TestCase;


Expand All @@ -8,41 +7,37 @@ class StackTest extends TestCase
// For testing the API
protected $client;

protected function setUp(): void
{
protected function setUp() : void{
parent::setUp();
$this->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',
Expand All @@ -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',
Expand All @@ -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());
}

}


?>



2 changes: 1 addition & 1 deletion backend/vendor/bin/.phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
File renamed without changes.
2 changes: 2 additions & 0 deletions src/login.test.js → src/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<BrowserRouter>
Expand Down
1 change: 1 addition & 0 deletions src/Songdisplay.test.js → src/searchInput.test.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 3472044

Please sign in to comment.