Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpakishore committed Nov 22, 2023
1 parent 9a60784 commit f7de766
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install flit
flit install
flit install --deps production
- name: Test with pytest
run: |
pytest .
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ dependencies = [
"pandas==2.1.3",
"rich==13.7.0",
"typer[all]==0.9.0",
"pytest==7.4.3",
]

[project.optional-dependencies]
test = [
"pytest==7.4.3"
]

dev = [
"ipykernel",
Expand Down
2 changes: 1 addition & 1 deletion src/ak_sap/Database/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def apply(self):
except Exception as e:
log.critical(str(e) + f'Import Log: {ImportLog}')

def _array_to_pandas(headers: tuple[str], array: tuple) -> pd.DataFrame:
def _array_to_pandas(headers: tuple, array: tuple) -> pd.DataFrame:
"""Given the table headers as tuple and table data as a single tuple;
Returns table as a dataframe."""
num_fields = len(headers)
Expand Down
56 changes: 56 additions & 0 deletions src/tests/Database/test_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pandas as pd
import pytest
import unittest

from ak_sap.Database.tables import _array_to_pandas, flatten_dataframe

# Test case with a valid input
def test_array_to_pandas_valid():
headers = ('Name', 'Age', 'City')
data = ('John', 25, 'New York', 'Alice', 30, 'San Francisco')
expected_df = pd.DataFrame({
'Name': ['John', 'Alice'],
'Age': [25, 30],
'City': ['New York', 'San Francisco']
})
result_df = _array_to_pandas(headers, data)
pd.testing.assert_frame_equal(result_df, expected_df)

# Test case with an invalid input (array length is not divisible by header length)
def test_array_to_pandas_invalid_length():
headers = ('Name', 'Age', 'City')
data = ('John', 25, 'New York', 'Alice', 30) # Missing City for Alice
with pytest.raises(AssertionError, match=r'Array length \(\d+\) is not divisible by header length \(\d+\)'):
_array_to_pandas(headers, data)

class TestFlattenDataFrame(unittest.TestCase):

def test_flatten_empty_dataframe(self):
df = pd.DataFrame()
result = flatten_dataframe(df)
self.assertEqual(result, tuple(), "Flattening an empty dataframe should return an empty tuple")

def test_flatten_dataframe_with_values(self):
data = {'A': [1, 2, 3], 'B': ['a', 'b', 'c'], 'C': [4.5, 6.7, 8.9]}
df = pd.DataFrame(data)
result = flatten_dataframe(df)
expected = ('1', 'a', '4.5', '2', 'b', '6.7', '3', 'c', '8.9')
self.assertEqual(result, expected, "Flattening a dataframe with values should produce the correct tuple")

def test_flatten_dataframe_with_nan_values(self):
data = {'A': [1, 2, None], 'B': ['a', None, 'c'], 'C': [4.5, 6.7, 8.9]}
df = pd.DataFrame(data)
result = flatten_dataframe(df)
expected = ('1.0', 'a', '4.5', '2.0', None, '6.7', None, 'c', '8.9')
self.assertEqual(result, expected, "Flattening a dataframe with NaN values should produce the correct tuple")

def test_flatten_dataframe_with_empty_strings(self):
data = {'A': [1, 2, ''], 'B': ['a', '', 'c'], 'C': [4.5, 6.7, 8.9]}
df = pd.DataFrame(data)
result = flatten_dataframe(df)
expected = ('1', 'a', '4.5', '2', None, '6.7', None, 'c', '8.9')
self.assertEqual(result, expected, "Flattening a dataframe with empty strings should produce the correct tuple")


if __name__ == '__main__':
unittest.main()

0 comments on commit f7de766

Please sign in to comment.