Skip to content

Commit

Permalink
Merge pull request #6 from atlasharry/5-python-code
Browse files Browse the repository at this point in the history
5 python code
  • Loading branch information
atlasharry authored Dec 6, 2023
2 parents faecffb + 54b2c81 commit 8fc67bc
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 122 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Python
__pycache__/
.pytest_cache/
58 changes: 58 additions & 0 deletions PythonCode/problem1.py
Original file line number Diff line number Diff line change
@@ -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"]))
37 changes: 37 additions & 0 deletions PythonCode/unit_test.py
Original file line number Diff line number Diff line change
@@ -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])
61 changes: 61 additions & 0 deletions PythonCode/unit_testing_sample_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# COMP 333 Software Engineering
# Wesleyan University
# Alex Kaplan ([email protected]) and Sebastian Zimmeck ([email protected])

"""
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]))
67 changes: 63 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
```
118 changes: 0 additions & 118 deletions unit_test.py

This file was deleted.

0 comments on commit 8fc67bc

Please sign in to comment.