From 7c06769be1e4496881344620770133a4d9bcc48f Mon Sep 17 00:00:00 2001 From: Patton Date: Wed, 6 Dec 2023 09:52:27 -0500 Subject: [PATCH 1/2] reorganized code; commented code; added .gitignore (issue #5) --- .gitignore | 4 + Python Code/problem1.py | 58 ++++++++++++ Python Code/unit_test.py | 37 ++++++++ Python Code/unit_testing_sample_code.py | 61 ++++++++++++ unit_test.py | 118 ------------------------ 5 files changed, 160 insertions(+), 118 deletions(-) create mode 100644 Python Code/problem1.py create mode 100644 Python Code/unit_test.py create mode 100644 Python Code/unit_testing_sample_code.py delete mode 100644 unit_test.py diff --git a/.gitignore b/.gitignore index 4d29575..47638e0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,7 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +# Python +__pycache__/ +.pytest_cache/ \ No newline at end of file diff --git a/Python Code/problem1.py b/Python Code/problem1.py new file mode 100644 index 0000000..62b1d3e --- /dev/null +++ b/Python Code/problem1.py @@ -0,0 +1,58 @@ +# import the unit_testing_sample_code module +import unit_testing_sample_code as utsc + +# Test for the srting_capitalizer function +def teststring(test_num, expected, actual): + if expected == actual: + print("Test " + str(test_num) + " passed!" + str(expected) + " matches " + str(actual) + ".") + else: + print("Test " + str(test_num) + " failed. Expected:" + str(expected) + ". Got: " + str(actual) + ".") + +# Test for the capitalize_list function +def teststrlist(test_num, expected_list, actual_list): + print("Test " + str(test_num) + ": ") + for i in range(0, len(expected_list)): + if expected_list[i] == actual_list[i]: + print("Part " + str(i) + " in test " + str(test_num) + " passed!" + str(expected_list[i]) + " matches " + str(actual_list[i]) + ".") + else: + print("Part " + str(i) + " in test " + str(test_num) + " failed. Expected:" + str(expected_list[i]) + ". Got: " + str(actual_list[i]) + ".") + +# Test for the integer_manipulator function +def testint(test_num, expected, actual): + if expected == actual: + print("Test " + str(test_num) + " passed!" + str(expected) + " matches " + str(actual) + ".") + else: + print("Test " + str(test_num) + " failed. Expected:" + str(expected) + ". Got: " + str(actual) + ".") + +# Test for the manipulate_list function +def testintlist(test_num, expected_list, actual_list): + print("Test " + str(test_num) + ": ") + for i in range(0, len(expected_list)): + if expected_list[i] == actual_list[i]: + print("Part " + str(i) + " in test " + str(test_num) + " passed!" + str(expected_list[i]) + " matches " + str(actual_list[i]) + ".") + else: + print("Part " + str(i) + " in test " + str(test_num) + " failed. Expected:" + str(expected_list[i]) + ". Got: " + str(actual_list[i]) + ".") + + +# Print out the tests +print("\nString Capitalizer Tests:") +# test_string is the function for testing the string capitalizer and takes +# three arguments: test number (“0”), expected output value (“TwO”), and +# the call to the string_capitalizer function with the argument “two”. +teststring("0", "TwO", utsc.string_capitalizer("two")) +teststring("1", "C", utsc.string_capitalizer("c")) +teststring("2", "FouR", utsc.string_capitalizer(4)) +teststring("3", "", utsc.string_capitalizer("")) + +print("\nList Capitalizer Tests:") +teststrlist("0", ["TwO","C","FouR",""], utsc.capitalize_list(["two","c",4,""])) + +print("\nInteger Manipulator Tests:") +testint("0", 66, utsc.integer_manipulator(10)) +testint("1", 2, utsc.integer_manipulator(2)) +testint("2", 6, utsc.integer_manipulator(3)) +testint("3", 0, utsc.integer_manipulator(0)) +testint("4", 1, utsc.integer_manipulator("three")) + +print("\nManipulate List Tests:") +testintlist("0", [66,2,6,0,1], utsc.manipulate_list([10,2,3,0,"three"])) \ No newline at end of file diff --git a/Python Code/unit_test.py b/Python Code/unit_test.py new file mode 100644 index 0000000..f6ccf19 --- /dev/null +++ b/Python Code/unit_test.py @@ -0,0 +1,37 @@ +# import the unit_testing_sample_code module +import unit_testing_sample_code as utsc +import pytest + +# Test for the srting_capitalizer functions +def test_string_1(): + assert "TwO" == utsc.string_capitalizer("two") + +def test_string_2(): + assert "C" == utsc.string_capitalizer("c") + +def test_string_3(): + assert "FouR" == utsc.string_capitalizer('four') + +def test_string_4(): + assert "" == utsc.string_capitalizer("") + +# Test for the capitalize_list function +def test_str_list_1(): + assert ["TwO","C","FouR",""] == utsc.capitalize_list(["two","c","4",""]) + +# Test for the integer_manipulator function +def test_int_1(): + assert 66 == utsc.integer_manipulator(10) + +def test_int_2(): + assert 2 == utsc.integer_manipulator(2) + +def test_int_3(): + assert 6 == utsc.integer_manipulator(3) + +def test_int_4(): + assert 0 == utsc.integer_manipulator(0) + +# Test for the manipulate_list function +def test_int_list_2(): + assert [66,2,6,0,0] == utsc.manipulate_list([10,2,3,0,1]) \ No newline at end of file diff --git a/Python Code/unit_testing_sample_code.py b/Python Code/unit_testing_sample_code.py new file mode 100644 index 0000000..43a9b6a --- /dev/null +++ b/Python Code/unit_testing_sample_code.py @@ -0,0 +1,61 @@ +# COMP 333 Software Engineering +# Wesleyan University +# Alex Kaplan (akaplan01@wesleyan.edu) and Sebastian Zimmeck (szimmeck@wesleyan.edu) + +""" +Capitalizes the first and last character of a string. +""" +def string_capitalizer(tobecapitalized): + if tobecapitalized == "": + return tobecapitalized + i = 0 + try: + tbclist = [*tobecapitalized] + while i < (len(tbclist)): + if i == 0 or i == (len(tbclist) - 1): + tbclist[i] = tbclist[i].upper() + i = i + 1 + tobecapitalized = ''.join(tbclist) + return tobecapitalized + except: + # If failure, return the input as is. + return tobecapitalized + +""" +Capitalizes the first and last character of a string in a list of strings. +""" +def capitalize_list(slist): + i = 0 + while i < len(slist): + current_string = slist[i] + slist[i] = string_capitalizer(current_string) + i = i + 1 + return slist + +""" +Squares, doubles, and then floor divides (by 3) an integer, in that order. +""" +def integer_manipulator(n): + try: + n = ((n*n)*2)//3 + return n + except: + # If failure, return the input as is. + return n + +""" +Squares, doubles, and then floor divides (by 3) an integer, in that order, in a +list of integers. +""" +def manipulate_list(intlist): + i = 0 + while i < len(intlist): + intlist[i] = integer_manipulator(intlist[i]) + i = i + 1 + return intlist + +# Sample function calls. Comment in to run. +# print(string_capitalizer("hello")) +# print(capitalize_list(["hello","good","bye"])) +# print(integer_manipulator(7)) +# print(manipulate_list([7,8])) \ No newline at end of file diff --git a/unit_test.py b/unit_test.py deleted file mode 100644 index 1f74eed..0000000 --- a/unit_test.py +++ /dev/null @@ -1,118 +0,0 @@ -# import the unit_testing_sample_code module -import unit_testing_sample_code as utsc -import pytest - -# def teststring(test_num, expected, actual): -# if expected == actual: -# print("Test " + str(test_num) + " passed!" + str(expected) + " matches " + str(actual) + ".") -# else: -# print("Test " + str(test_num) + " failed. Expected:" + str(expected) + ". Got: " + str(actual) + ".") - -# def teststrlist(test_num, expected_list, actual_list): -# print("Test " + str(test_num) + ": ") -# for i in range(0, len(expected_list)): -# if expected_list[i] == actual_list[i]: -# print("Part " + str(i) + " in test " + str(test_num) + " passed!" + str(expected_list[i]) + " matches " + str(actual_list[i]) + ".") -# else: -# print("Part " + str(i) + " in test " + str(test_num) + " failed. Expected:" + str(expected_list[i]) + ". Got: " + str(actual_list[i]) + ".") - -# def testint(test_num, expected, actual): -# if expected == actual: -# print("Test " + str(test_num) + " passed!" + str(expected) + " matches " + str(actual) + ".") -# else: -# print("Test " + str(test_num) + " failed. Expected:" + str(expected) + ". Got: " + str(actual) + ".") - -# def testintlist(test_num, expected_list, actual_list): -# print("Test " + str(test_num) + ": ") -# for i in range(0, len(expected_list)): -# if expected_list[i] == actual_list[i]: -# print("Part " + str(i) + " in test " + str(test_num) + " passed!" + str(expected_list[i]) + " matches " + str(actual_list[i]) + ".") -# else: -# print("Part " + str(i) + " in test " + str(test_num) + " failed. Expected:" + str(expected_list[i]) + ". Got: " + str(actual_list[i]) + ".") - -# print("\nString Capitalizer Tests:") -# # test_string is the function for testing the string capitalizer and takes -# # three arguments: test number (“0”), expected output value (“TwO”), and -# # the call to the string_capitalizer function with the argument “two”. -# teststring("0", "TwO", utsc.string_capitalizer("two")) -# teststring("1", "C", utsc.string_capitalizer("c")) -# teststring("2", "FouR", utsc.string_capitalizer(4)) -# teststring("3", "", utsc.string_capitalizer("")) - -# print("\nList Capitalizer Tests:") -# teststrlist("0", ["TwO","C","FouR",""], utsc.capitalize_list(["two","c",4,""])) - -# print("\nInteger Manipulator Tests:") -# testint("0", 66, utsc.integer_manipulator(10)) -# testint("1", 2, utsc.integer_manipulator(2)) -# testint("2", 6, utsc.integer_manipulator(3)) -# testint("3", 0, utsc.integer_manipulator(0)) -# testint("4", 1, utsc.integer_manipulator("three")) - -# print("\nManipulate List Tests:") -# testintlist("0", [66,2,6,0,1], utsc.manipulate_list([10,2,3,0,"three"])) - -def test_string_1(): - assert "TwO" == utsc.string_capitalizer("two") - -def test_string_2(): - assert "C" == utsc.string_capitalizer("c") - -def test_string_3(): - assert "FouR" == utsc.string_capitalizer('four') - -def test_string_4(): - assert "" == utsc.string_capitalizer("") - -def test_str_list_1(): - assert ["TwO","C","FouR",""] == utsc.capitalize_list(["two","c","4",""]) - -def test_int_1(): - assert 66 == utsc.integer_manipulator(10) - -def test_int_2(): - assert 2 == utsc.integer_manipulator(2) - -def test_int_3(): - assert 6 == utsc.integer_manipulator(3) - -def test_int_4(): - assert 0 == utsc.integer_manipulator(0) - -def test_int_list_2(): - assert [66,2,6,0,0] == utsc.manipulate_list([10,2,3,0,1]) - - - -# @pytest.mark.parametrize("input_string, expected_output", [ -# ("two", "TwO"), -# ("c", "C"), -# (4, "FouR"), # Assuming your function can handle numeric inputs -# ("", "") -# ]) -# def test_string_capitalizer(input_string, expected_output): -# assert utsc.string_capitalizer(input_string) == expected_output - -# # Test cases for list capitalizer -# def test_capitalize_list(): -# input_list = ["two", "c", 4, ""] -# expected_output = ["TwO", "C", "FouR", ""] -# assert utsc.capitalize_list(input_list) == expected_output - -# # Test cases for integer manipulator -# @pytest.mark.parametrize("input_int, expected_output", [ -# (10, 66), -# (2, 2), -# (3, 6), -# (0, 0), -# ("three", 1) # Assuming your function can handle string inputs -# ]) -# def test_integer_manipulator(input_int, expected_output): -# assert utsc.integer_manipulator(input_int) == expected_output - -# # Test cases for manipulate list -# def test_manipulate_list(): -# input_list = [10, 2, 3, 0, "three"] -# expected_output = [66, 2, 6, 0, 1] -# assert utsc.manipulate_list(input_list) == expected_output - From 54b2c81fcc9bfcf5e640137aed455a41aa3c09cc Mon Sep 17 00:00:00 2001 From: Patton Date: Wed, 6 Dec 2023 10:14:50 -0500 Subject: [PATCH 2/2] added comments and readme (issue #5) --- {Python Code => PythonCode}/problem1.py | 0 {Python Code => PythonCode}/unit_test.py | 0 .../unit_testing_sample_code.py | 0 README.md | 67 +++++++++++++++++-- 4 files changed, 63 insertions(+), 4 deletions(-) rename {Python Code => PythonCode}/problem1.py (100%) rename {Python Code => PythonCode}/unit_test.py (100%) rename {Python Code => PythonCode}/unit_testing_sample_code.py (100%) diff --git a/Python Code/problem1.py b/PythonCode/problem1.py similarity index 100% rename from Python Code/problem1.py rename to PythonCode/problem1.py diff --git a/Python Code/unit_test.py b/PythonCode/unit_test.py similarity index 100% rename from Python Code/unit_test.py rename to PythonCode/unit_test.py diff --git a/Python Code/unit_testing_sample_code.py b/PythonCode/unit_testing_sample_code.py similarity index 100% rename from Python Code/unit_testing_sample_code.py rename to PythonCode/unit_testing_sample_code.py diff --git a/README.md b/README.md index 50aac9f..eaed2c3 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,70 @@ In addition to the back up, we also have codes related to Unit Testing and Continuous Integration(Python pytest, Jest, PHPUnit, GitHub Actions, Generative AI) for COMP 333: Software Engineering 2023 Homework 5 +## Python test -## Python -```pip install pytest``` +### Problem 1 (unit test without pytest) +Please run the code at PythonCode/problem1.py (Through terminal) + +``` +python -u "PythonCode/problem1.py" +``` + +### Problem 2 (unit test with pytest) + +How to set up the environment: (Through terminal) + +``` +pip install pytest +``` + +Run the test: (Through terminal) + +``` +pytest +``` + +## Backend Test -## Backend ### Environment Config -### Hard code id; username; password + +Please setup the phpunit following testing-tutorial on class website. + +### How to run + +Navigate to the backend folder of backend + +``` +cd backend +./vendor/bin/phpunit tests/StackTest.php +``` + +### Hard coded id (song_id), username, password + +For testRegister, testLogin, testFailLogin: +the unsername and password are hard coded, make sure such user doesn't exist in the database before the test. + +``` +'username' => 'test_admin', +'password' => '1234567890aaa', +``` + +In the testUpdateSong and testDeleteSong tests, we use the song_id as a parameter to identify the specific song we want to update or delete. This song_id is automatically generated and incremented in our SQL database whenever a new song is added. + +Therefore, when writing or updating these tests, it's crucial to ensure that the song_id used in the test matches the song_id of the song you expect to be present in the database. This is because the song_id in the database will automatically increment each time a new song is added. + +In other words, if you're writing a test to update or delete a song that you've just inserted into the database for testing purposes, you need to know the song_id that was assigned to that song and use it in your test. If the song_id in the test doesn't match any song_id in the database, the test will fail because it can't find the song it's supposed to update or delete. + +``` +'id' => '53', +'username' => 'test_admin', +'song_artist' => 'test_admin', +'song_name' => 'admin_song', +'song_rating' => '4', +``` + +``` +'id' => '53', +'username' => 'test_admin', +```