Skip to content

Commit

Permalink
Merge pull request #52 from UBC-MDS/fixes-sid
Browse files Browse the repository at this point in the history
fix:refactored tests to a common format with fixtures
  • Loading branch information
MarekB19 authored Feb 4, 2025
2 parents aa8a847 + b1bf4d5 commit 78e8845
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 265 deletions.
116 changes: 54 additions & 62 deletions tests/bubblesort_test.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
# tests/bubble_sort_test.py
import pytest
from pysorting import (bubble_sort,
InvalidElementTypeError,
NonUniformTypeError,
InvalidAscendingTypeError)


def test_sorted_list(test_data_sorted):
"""Test if a pre-sorted list remains unchanged."""
expected = [1, 2, 3, 4, 5, 6, 7, 8]
actual = bubble_sort(test_data_sorted)

assert isinstance(actual, list)
assert actual == expected


def test_reverse_sorted_list(test_data1):
"""Test if a reverse sorting works properly."""
expected = [8, 7, 6, 5, 4, 3, 2, 1]
actual = bubble_sort(test_data1, ascending=False)
from pysorting import (
bubble_sort,
InvalidElementTypeError,
NonUniformTypeError,
InvalidAscendingTypeError,
)

# Define test cases as fixtures
@pytest.fixture(params=[
[], # empty array
[1], # single-element array
[1, 2, 3, 4, 5, 6, 7, 8], # already sorted array
[8, 7, 6, 5, 4, 3, 2, 1], # reverse-sorted array
[5, 2, 8, 3, 1], # unsorted array
])
def test_data(request):
return request.param

@pytest.fixture
def test_data_invalid_element():
return [1, "a", 3]

@pytest.fixture
def test_data_non_uniform():
return [1, 2, "3"]

@pytest.fixture
def test_data_error():
return "not a list"

def test_bubble_sort(test_data):
"""Test bubble sort with various inputs"""
expected = sorted(test_data)
actual = bubble_sort(test_data)

assert isinstance(actual, list)
assert actual == expected


def test_unsorted_list(test_data1):
"""Test if an unsorted list is sorted correctly."""
expected = [1, 2, 3, 4, 5, 6, 7, 8]
actual = bubble_sort(test_data1)
def test_bubble_sort_reverse(test_data):
"""Test bubble sort with reverse sorting"""
expected = sorted(test_data, reverse=True)
actual = bubble_sort(test_data, ascending=False)

assert isinstance(actual, list)
assert actual == expected


def test_single_element_list(test_data_single_element):
"""Test if a single-element list is handled correctly."""
expected = [5]
actual = bubble_sort(test_data_single_element, ascending=False)

assert isinstance(actual, list)
assert len(actual) == 1
assert actual == expected


def test_empty_list(test_data_empty):
"""Test if an empty list is handled correctly."""

actual = bubble_sort(test_data_empty)

assert isinstance(actual, list)
assert len(actual) == 0


def test_type_error(test_data_error2):
"""Test if a TypeError is raised for non-list inputs."""
with pytest.raises(TypeError):
bubble_sort(test_data_error2)


def test_invalid_type_error(test_invalid_error):
"""Test if a TypeError is raised for non-list inputs."""
with pytest.raises(InvalidElementTypeError):
bubble_sort(test_invalid_error)


def test_non_uniform_error(test_nonuniform_error):
"""Test if a TypeError is raised for non-list inputs."""
def test_bubble_sort_invalid_element(test_data_invalid_element):
"""Test bubble sort with invalid element type"""
with pytest.raises(NonUniformTypeError):
bubble_sort(test_nonuniform_error)
bubble_sort(test_data_invalid_element)

def test_bubble_sort_non_uniform(test_data_non_uniform):
"""Test bubble sort with non-uniform element types"""
with pytest.raises(NonUniformTypeError):
bubble_sort(test_data_non_uniform)

def test_invalid_ascending_type():
"""Test if InvalidAscendingTypeError is raised when ascending parameter is not a boolean."""
def test_bubble_sort_invalid_ascending_type():
"""Test bubble sort with invalid ascending parameter type"""
with pytest.raises(InvalidAscendingTypeError):
bubble_sort([1, 2, 3], ascending="not_a_boolean")
bubble_sort([1, 2, 3], ascending="not_a_boolean")

def test_bubble_sort_error(test_data_error):
"""Test bubble sort with non-list input"""
with pytest.raises(TypeError):
bubble_sort(test_data_error)
97 changes: 54 additions & 43 deletions tests/insertionsort_test.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
# tests/insertion_sort_test.py
import pytest
# from pysorting import insertion_sort
from pysorting import (insertion_sort,
InvalidElementTypeError,
NonUniformTypeError,
InvalidAscendingTypeError)
from pysorting import (
insertion_sort,
InvalidElementTypeError,
NonUniformTypeError,
InvalidAscendingTypeError,
)

# Define test cases as fixtures
@pytest.fixture(params=[
[], # empty array
[1], # single-element array
[1, 2, 3, 4, 5, 6, 7, 8], # already sorted array
[8, 7, 6, 5, 4, 3, 2, 1], # reverse-sorted array
[5, 2, 8, 3, 1], # unsorted array
[1, 2, 3.0, 4, 5], # array with floats
])
def test_data(request):
return request.param

def test_sorted(test_data_sorted):
"""test that function returns a sorted list given an already sorted list"""
expected = [1, 2, 3, 4, 5, 6, 7, 8]
actual = insertion_sort(test_data_sorted)
assert actual == expected
@pytest.fixture
def test_data_invalid_element():
return [1, "a", 3]

def test_ascending_false(test_data1):
"""test that function is able to return ascending sorted list"""
expected = [8,7,6,5,4,3,2,1]
actual = insertion_sort(test_data1, ascending = False)
assert actual == expected
@pytest.fixture
def test_data_non_uniform():
return [1, 2, "3"]

def test_insertion_sort(test_data1):
"""test that function returns a sorted list given an unsorted list"""
expected = [1,2,3,4,5,6,7,8]
actual = insertion_sort(test_data1)
assert actual == expected
@pytest.fixture
def test_data_error():
return "not a list"

def test_empty(test_data_empty):
"""test that function can handle an empty list"""
actual = insertion_sort(test_data_empty)
assert isinstance(actual,list)
assert len(actual) == 0
def test_insertion_sort(test_data):
"""Test insertion sort with various inputs"""
expected = sorted(test_data)
actual = insertion_sort(test_data)

def test_string_error(test_data_error2):
"""test that function returns a typer error given a string is passed"""
with pytest.raises(TypeError):
insertion_sort(test_data_error2)
assert isinstance(actual, list)
assert actual == expected

def test_one_float(test_data_float):
"""test that function can handle floats as well as ints"""
expected = [1,2,3.0,4,5]
actual = insertion_sort(test_data_float)
def test_insertion_sort_reverse(test_data):
"""Test insertion sort with reverse sorting"""
expected = sorted(test_data, reverse=True)
actual = insertion_sort(test_data, ascending=False)

assert isinstance(actual, list)
assert actual == expected

def test_invalid_type_error(test_invalid_error):
"""Test if a TypeError is raised for non-list inputs."""
with pytest.raises(InvalidElementTypeError):
insertion_sort(test_invalid_error)
def test_insertion_sort_invalid_element(test_data_invalid_element):
"""Test insertion sort with invalid element type"""
with pytest.raises(NonUniformTypeError):
insertion_sort(test_data_invalid_element)

def test_non_uniform_error(test_nonuniform_error):
"""Test if a TypeError is raised for non-list inputs."""
def test_insertion_sort_non_uniform(test_data_non_uniform):
"""Test insertion sort with non-uniform element types"""
with pytest.raises(NonUniformTypeError):
insertion_sort(test_nonuniform_error)
insertion_sort(test_data_non_uniform)

def test_invalid_ascending_type():
"""Test if InvalidAscendingTypeError is raised when ascending parameter is not a boolean."""
def test_insertion_sort_invalid_ascending_type():
"""Test insertion sort with invalid ascending parameter type"""
with pytest.raises(InvalidAscendingTypeError):
insertion_sort([1, 2, 3], ascending="not_a_boolean")
insertion_sort([1, 2, 3], ascending="not_a_boolean")

def test_insertion_sort_error(test_data_error):
"""Test insertion sort with non-list input"""
with pytest.raises(TypeError):
insertion_sort(test_data_error)
116 changes: 54 additions & 62 deletions tests/quicksort_test.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,78 @@
# tests/quick_sort_test.py
import pytest
from pysorting import quick_sort, InvalidElementTypeError, NonUniformTypeError, InvalidAscendingTypeError


def test_sorted_list(test_data_sorted):
"""Test if a pre-sorted list remains unchanged."""
expected = [1, 2, 3, 4, 5, 6, 7, 8]
actual = quick_sort(test_data_sorted)

assert isinstance(actual, list)
assert actual == expected


def test_reverse_sorted_list(test_data1):
"""Test if a reverse sorting works properly."""
expected = [8, 7, 6, 5, 4, 3, 2, 1]
actual = quick_sort(test_data1, ascending=False)

assert isinstance(actual, list)
assert actual == expected


def test_unsorted_list(test_data1):
"""Test if an unsorted list is sorted correctly."""
expected = [1, 2, 3, 4, 5, 6, 7, 8]
actual = quick_sort(test_data1)
# Define test cases as fixtures
@pytest.fixture(params=[
[], # empty array
[1], # single-element array
[1, 2, 3, 4, 5, 6, 7, 8], # already sorted array
[8, 7, 6, 5, 4, 3, 2, 1], # reverse-sorted array
[5, 2, 8, 3, 1], # unsorted array
[1, 2, 3.0, 4, 5], # array with floats
])
def test_data(request):
return request.param

@pytest.fixture
def test_data_invalid_element():
return [1, "a", 3]

@pytest.fixture
def test_data_non_uniform():
return [1, 2, "3"]

@pytest.fixture
def test_data_large_sorted():
return list(range(1, 101))

@pytest.fixture
def test_data_large_unsorted():
return list(range(1, 101))[::-1]

def test_quick_sort(test_data):
"""Test quick sort with various inputs"""
expected = sorted(test_data)
actual = quick_sort(test_data)

assert isinstance(actual, list)
assert actual == expected


def test_single_element_list(test_data_single_element):
"""Test if a single-element list is handled correctly."""
expected = [5]
actual = quick_sort(test_data_single_element)
def test_quick_sort_reverse(test_data):
"""Test quick sort with reverse sorting"""
expected = sorted(test_data, reverse=True)
actual = quick_sort(test_data, ascending=False)

assert isinstance(actual, list)
assert len(actual) == 1
assert actual == expected

def test_quick_sort_invalid_element(test_data_invalid_element):
"""Test quick sort with invalid element type"""
with pytest.raises(NonUniformTypeError):
quick_sort(test_data_invalid_element)

def test_empty_list(test_data_empty):
"""Test if an empty list is handled correctly."""
actual = quick_sort(test_data_empty)

assert isinstance(actual, list)
assert len(actual) == 0

def test_invalid_type_error(test_invalid_error):
"""Test if an InvalidElementTypeError is raised for invalid element types."""
with pytest.raises(InvalidElementTypeError):
quick_sort(test_invalid_error)

def test_non_uniform_error(test_nonuniform_error):
"""Test if a NonUniformTypeError is raised for lists with mixed types."""
def test_quick_sort_non_uniform(test_data_non_uniform):
"""Test quick sort with non-uniform element types"""
with pytest.raises(NonUniformTypeError):
quick_sort(test_nonuniform_error)
quick_sort(test_data_non_uniform)

def test_invalid_ascending_type():
"""Test if InvalidAscendingTypeError is raised when ascending parameter is not a boolean."""
def test_quick_sort_invalid_ascending_type():
"""Test quick sort with invalid ascending parameter type"""
with pytest.raises(InvalidAscendingTypeError):
quick_sort([1, 2, 3], ascending="not_a_boolean")

def test_float_list(test_data_float):
"""Test if a list with both integers and floats is sorted correctly."""
expected = [1, 2, 3.0, 4, 5]
actual = quick_sort(test_data_float)
def test_quick_sort_large_sorted(test_data_large_sorted):
"""Test quick sort with large sorted input"""
expected = test_data_large_sorted
actual = quick_sort(test_data_large_sorted)

assert isinstance(actual, list)
assert actual == expected

def test_large_sorted_list(large_sorted_list):
"""Test sorting of a large already sorted list."""
actual = quick_sort(large_sorted_list)

assert isinstance(actual, list)
assert actual == large_sorted_list


def test_large_unsorted_list(large_unsorted_list):
"""Test sorting of a large unsorted list."""
def test_quick_sort_large_unsorted(test_data_large_unsorted):
"""Test quick sort with large unsorted input"""
expected = list(range(1, 101))
actual = quick_sort(large_unsorted_list)
actual = quick_sort(test_data_large_unsorted)

assert isinstance(actual, list)
assert actual == expected
assert actual == expected
Loading

0 comments on commit 78e8845

Please sign in to comment.