Skip to content

Commit

Permalink
Merge pull request #30 from VatsalJagani/more-test-cases
Browse files Browse the repository at this point in the history
More-test-cases
  • Loading branch information
VatsalJagani authored Mar 8, 2024
2 parents 684d015 + d2e3107 commit e01bff8
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/py_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ jobs:
run: pytest tests --junitxml=junit/test-results.xml --cov=src --cov-config=tests/.coveragerc --cov-report=xml

- name: Adding GitHub action step summary
uses: VatsalJagani/pytest-cov-action@v0.3
uses: VatsalJagani/pytest-cov-action@v0.5
with:
pytest_results_file: "junit/test-results.xml"
pytest_cov_file: "coverage.xml"
pytest_cov_failure_threshold: 40
if: ${{ always() }}

- name: Linting with Ruff
run: ruff --output-format=github .
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/git_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def get_file_hash(file_path):

def get_folder_hash(folder_path):
hash_md5 = hashlib.md5()
if not os.path.isdir(folder_path):
raise Exception("Incorrect folder_path provided.")
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_git_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os, sys

# path to be added -> /<this-repo>/tests/src
sys.path.insert(0, os.path.join(
os.path.dirname(os.path.dirname(__file__)),
'src'
)
)

import pytest
from helpers.git_manager import get_file_hash, get_folder_hash, get_multi_files_hash


# Use a fixture to create temporary files with known content for testing
@pytest.fixture
def sample_file(tmp_path):
file_path = tmp_path / "test_file.txt"
file_path.write_text("This is a test file")
return file_path


def test_get_file_hash_nonexistent_file():
file_path = "path/to/nonexistent_file"
with pytest.raises(FileNotFoundError):
get_file_hash(file_path)

def test_get_file_hash_with_tmp_file(sample_file):
expected_hash = "0b26e313ed4a7ca6904b0e9369e5b957" # MD5 hash of "This is a test file"
assert get_file_hash(sample_file) == expected_hash

def test_get_folder_hash_empty_folder():
folder_path = "empty_folder"
os.makedirs(folder_path)
expected_hash = "d41d8cd98f00b204e9800998ecf8427e" # MD5 hash of an empty string
assert get_folder_hash(folder_path) == expected_hash
os.rmdir(folder_path)

def test_get_folder_hash_with_files(sample_file):
folder_path = sample_file.parent
expected_hash = "894c0cee919eb49f7a36060c7a607fb7" # Hash of the single file in the folder
assert get_folder_hash(folder_path) == expected_hash

def test_get_folder_hash_nonexistent_folder():
folder_path = "path/to/nonexistent_folder"
with pytest.raises(Exception, match="Incorrect folder_path provided."):
abc = get_folder_hash(folder_path)
print(f"abc={abc}")

def test_get_multi_files_hash_empty_list():
file_paths = []
expected_hash = "d41d8cd98f00b204e9800998ecf8427e" # MD5 hash of an empty string
assert get_multi_files_hash(file_paths) == expected_hash

def test_get_multi_files_hash_multiple_files(sample_file):
file_paths = [sample_file, sample_file] # Use the same file twice for simplicity
expected_hash = "91405dbee0a9c47e7bc657bda7cb29a9" # Hypothetical hash of the combined hashes
assert get_multi_files_hash(file_paths) == expected_hash
37 changes: 37 additions & 0 deletions tests/test_github_action_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os, sys

# path to be added -> /<this-repo>/tests/src
sys.path.insert(0, os.path.join(
os.path.dirname(os.path.dirname(__file__)),
'src'
)
)

from helpers.github_action_utils import str_to_boolean_default_false, str_to_boolean_default_true


def test_str_to_boolean_default_true_positive_cases():
assert str_to_boolean_default_true("true") is True
assert str_to_boolean_default_true("T") is True
assert str_to_boolean_default_true("1") is True
assert str_to_boolean_default_true("YES") is True

def test_str_to_boolean_default_true_negative_cases():
assert str_to_boolean_default_true("false") is False
assert str_to_boolean_default_true("f") is False
assert str_to_boolean_default_true("0") is False
assert str_to_boolean_default_true("no") is False
assert str_to_boolean_default_true("") is True # Empty string defaults to True

def test_str_to_boolean_default_false_positive_cases():
assert str_to_boolean_default_false("true") is True
assert str_to_boolean_default_false("t") is True
assert str_to_boolean_default_false("1") is True
assert str_to_boolean_default_false("yes") is True

def test_str_to_boolean_default_false_negative_cases():
assert str_to_boolean_default_false("false") is False
assert str_to_boolean_default_false("f") is False
assert str_to_boolean_default_false("0") is False
assert str_to_boolean_default_false("no") is False
assert str_to_boolean_default_false("") is False # Empty string defaults to False
80 changes: 80 additions & 0 deletions tests/test_global_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

import os, sys

# path to be added -> /<this-repo>/tests/src
sys.path.insert(0, os.path.join(
os.path.dirname(os.path.dirname(__file__)),
'src'
)
)

from helpers.global_variables import GlobalVariables

def test_initiate_valid_args():
# Set up
app_dir_name = "my_app"
repodir_name = "custom_repo"
root_dir_path = "/path/to/project"

# Call the method
GlobalVariables.initiate(app_dir_name, repodir_name, root_dir_path)

# Assertions
assert GlobalVariables.ROOT_DIR_PATH == root_dir_path
assert GlobalVariables.ORIGINAL_REPO_DIR_NAME == repodir_name
assert GlobalVariables.ORIGINAL_REPO_DIR_PATH == os.path.join(root_dir_path, repodir_name)
assert GlobalVariables.APP_DIR_NAME == app_dir_name
assert GlobalVariables.ORIGINAL_APP_DIR_PATH == os.path.join(GlobalVariables.ORIGINAL_REPO_DIR_PATH, app_dir_name)

# Cleanup (optional, reset variables)
GlobalVariables.ROOT_DIR_PATH = None
GlobalVariables.ORIGINAL_REPO_DIR_NAME = None
GlobalVariables.ORIGINAL_REPO_DIR_PATH = None
GlobalVariables.APP_DIR_NAME = None
GlobalVariables.ORIGINAL_APP_DIR_PATH = None

def test_initiate_missing_args():
# Set up
app_dir_name = "my_app"

# Call the method
GlobalVariables.initiate(app_dir_name)

# Assertions (using os.getcwd() for current working directory)
assert GlobalVariables.ROOT_DIR_PATH == os.getcwd()
assert GlobalVariables.ORIGINAL_REPO_DIR_NAME == "repodir"
assert GlobalVariables.ORIGINAL_REPO_DIR_PATH == os.path.join(GlobalVariables.ROOT_DIR_PATH, "repodir")
assert GlobalVariables.APP_DIR_NAME == app_dir_name
assert GlobalVariables.ORIGINAL_APP_DIR_PATH == os.path.join(GlobalVariables.ORIGINAL_REPO_DIR_PATH, app_dir_name)

# Cleanup (optional, reset variables)
GlobalVariables.ROOT_DIR_PATH = None
GlobalVariables.ORIGINAL_REPO_DIR_NAME = None
GlobalVariables.ORIGINAL_REPO_DIR_PATH = None
GlobalVariables.APP_DIR_NAME = None
GlobalVariables.ORIGINAL_APP_DIR_PATH = None

def test_set_app_package_id():
package_id = "my_app_id"
GlobalVariables.set_app_package_id(package_id)
assert GlobalVariables.APP_PACKAGE_ID == package_id

def test_set_app_version():
version = "1.2.3"
GlobalVariables.set_app_version(version)
assert GlobalVariables.APP_VERSION == version
assert GlobalVariables.APP_VERSION_ENCODED == "1_2_3" # Encoded version

# Cleanup (optional, reset variables)
GlobalVariables.APP_VERSION = None
GlobalVariables.APP_VERSION_ENCODED = None

def test_set_app_build_number():
build_number = "456"
GlobalVariables.set_app_build_number(build_number)
assert GlobalVariables.APP_BUILD_NUMBER == build_number
assert GlobalVariables.APP_BUILD_NUMBER_ENCODED == "456" # Encoded build number

# Cleanup (optional, reset variables)
GlobalVariables.APP_BUILD_NUMBER = None
GlobalVariables.APP_BUILD_NUMBER_ENCODED = None

0 comments on commit e01bff8

Please sign in to comment.