diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 1898104..98c778d 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -78,19 +78,3 @@ jobs: POETRY_PYPI_TOKEN: ${{ secrets.POETRY_PYPI_TOKEN }} run: | poetry publish --build --username $POETRY_PYPI_USERNAME --password $POETRY_PYPI_TOKEN - - - name: Save as Artifact - uses: actions/upload-artifact@v3 - with: - name: python-package-artifact - path: ./dist/ - - - name: Upload dist directory as Release Asset - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ github.event.release.upload_url }} - asset_path: ./dist/pyfilehandling-${{ github.event.release.name }}.tar.gz - asset_name: pyfilehandling-${{ github.event.release.name }}.tar.gz - asset_content_type: application/gzip diff --git a/poetry.lock b/poetry.lock index ef4e669..b2d92bf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,93 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. - -[[package]] -name = "colorama" -version = "0.4.6" -description = "Cross-platform colored terminal text." -optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" -files = [ - {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, - {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, -] - -[[package]] -name = "coverage" -version = "7.4.3" -description = "Code coverage measurement for Python" -optional = false -python-versions = ">=3.8" -files = [ - {file = "coverage-7.4.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:8580b827d4746d47294c0e0b92854c85a92c2227927433998f0d3320ae8a71b6"}, - {file = "coverage-7.4.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:718187eeb9849fc6cc23e0d9b092bc2348821c5e1a901c9f8975df0bc785bfd4"}, - {file = "coverage-7.4.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:767b35c3a246bcb55b8044fd3a43b8cd553dd1f9f2c1eeb87a302b1f8daa0524"}, - {file = "coverage-7.4.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ae7f19afe0cce50039e2c782bff379c7e347cba335429678450b8fe81c4ef96d"}, - {file = "coverage-7.4.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba3a8aaed13770e970b3df46980cb068d1c24af1a1968b7818b69af8c4347efb"}, - {file = "coverage-7.4.3-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:ee866acc0861caebb4f2ab79f0b94dbfbdbfadc19f82e6e9c93930f74e11d7a0"}, - {file = "coverage-7.4.3-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:506edb1dd49e13a2d4cac6a5173317b82a23c9d6e8df63efb4f0380de0fbccbc"}, - {file = "coverage-7.4.3-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:fd6545d97c98a192c5ac995d21c894b581f1fd14cf389be90724d21808b657e2"}, - {file = "coverage-7.4.3-cp310-cp310-win32.whl", hash = "sha256:f6a09b360d67e589236a44f0c39218a8efba2593b6abdccc300a8862cffc2f94"}, - {file = "coverage-7.4.3-cp310-cp310-win_amd64.whl", hash = "sha256:18d90523ce7553dd0b7e23cbb28865db23cddfd683a38fb224115f7826de78d0"}, - {file = "coverage-7.4.3-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:cbbe5e739d45a52f3200a771c6d2c7acf89eb2524890a4a3aa1a7fa0695d2a47"}, - {file = "coverage-7.4.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:489763b2d037b164846ebac0cbd368b8a4ca56385c4090807ff9fad817de4113"}, - {file = "coverage-7.4.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:451f433ad901b3bb00184d83fd83d135fb682d780b38af7944c9faeecb1e0bfe"}, - {file = "coverage-7.4.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fcc66e222cf4c719fe7722a403888b1f5e1682d1679bd780e2b26c18bb648cdc"}, - {file = "coverage-7.4.3-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b3ec74cfef2d985e145baae90d9b1b32f85e1741b04cd967aaf9cfa84c1334f3"}, - {file = "coverage-7.4.3-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:abbbd8093c5229c72d4c2926afaee0e6e3140de69d5dcd918b2921f2f0c8baba"}, - {file = "coverage-7.4.3-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:35eb581efdacf7b7422af677b92170da4ef34500467381e805944a3201df2079"}, - {file = "coverage-7.4.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:8249b1c7334be8f8c3abcaaa996e1e4927b0e5a23b65f5bf6cfe3180d8ca7840"}, - {file = "coverage-7.4.3-cp311-cp311-win32.whl", hash = "sha256:cf30900aa1ba595312ae41978b95e256e419d8a823af79ce670835409fc02ad3"}, - {file = "coverage-7.4.3-cp311-cp311-win_amd64.whl", hash = "sha256:18c7320695c949de11a351742ee001849912fd57e62a706d83dfc1581897fa2e"}, - {file = "coverage-7.4.3-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:b51bfc348925e92a9bd9b2e48dad13431b57011fd1038f08316e6bf1df107d10"}, - {file = "coverage-7.4.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:d6cdecaedea1ea9e033d8adf6a0ab11107b49571bbb9737175444cea6eb72328"}, - {file = "coverage-7.4.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3b2eccb883368f9e972e216c7b4c7c06cabda925b5f06dde0650281cb7666a30"}, - {file = "coverage-7.4.3-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6c00cdc8fa4e50e1cc1f941a7f2e3e0f26cb2a1233c9696f26963ff58445bac7"}, - {file = "coverage-7.4.3-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b9a4a8dd3dcf4cbd3165737358e4d7dfbd9d59902ad11e3b15eebb6393b0446e"}, - {file = "coverage-7.4.3-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:062b0a75d9261e2f9c6d071753f7eef0fc9caf3a2c82d36d76667ba7b6470003"}, - {file = "coverage-7.4.3-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:ebe7c9e67a2d15fa97b77ea6571ce5e1e1f6b0db71d1d5e96f8d2bf134303c1d"}, - {file = "coverage-7.4.3-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:c0a120238dd71c68484f02562f6d446d736adcc6ca0993712289b102705a9a3a"}, - {file = "coverage-7.4.3-cp312-cp312-win32.whl", hash = "sha256:37389611ba54fd6d278fde86eb2c013c8e50232e38f5c68235d09d0a3f8aa352"}, - {file = "coverage-7.4.3-cp312-cp312-win_amd64.whl", hash = "sha256:d25b937a5d9ffa857d41be042b4238dd61db888533b53bc76dc082cb5a15e914"}, - {file = "coverage-7.4.3-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:28ca2098939eabab044ad68850aac8f8db6bf0b29bc7f2887d05889b17346454"}, - {file = "coverage-7.4.3-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:280459f0a03cecbe8800786cdc23067a8fc64c0bd51dc614008d9c36e1659d7e"}, - {file = "coverage-7.4.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6c0cdedd3500e0511eac1517bf560149764b7d8e65cb800d8bf1c63ebf39edd2"}, - {file = "coverage-7.4.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:9a9babb9466fe1da12417a4aed923e90124a534736de6201794a3aea9d98484e"}, - {file = "coverage-7.4.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dec9de46a33cf2dd87a5254af095a409ea3bf952d85ad339751e7de6d962cde6"}, - {file = "coverage-7.4.3-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:16bae383a9cc5abab9bb05c10a3e5a52e0a788325dc9ba8499e821885928968c"}, - {file = "coverage-7.4.3-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:2c854ce44e1ee31bda4e318af1dbcfc929026d12c5ed030095ad98197eeeaed0"}, - {file = "coverage-7.4.3-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:ce8c50520f57ec57aa21a63ea4f325c7b657386b3f02ccaedeccf9ebe27686e1"}, - {file = "coverage-7.4.3-cp38-cp38-win32.whl", hash = "sha256:708a3369dcf055c00ddeeaa2b20f0dd1ce664eeabde6623e516c5228b753654f"}, - {file = "coverage-7.4.3-cp38-cp38-win_amd64.whl", hash = "sha256:1bf25fbca0c8d121a3e92a2a0555c7e5bc981aee5c3fdaf4bb7809f410f696b9"}, - {file = "coverage-7.4.3-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:3b253094dbe1b431d3a4ac2f053b6d7ede2664ac559705a704f621742e034f1f"}, - {file = "coverage-7.4.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:77fbfc5720cceac9c200054b9fab50cb2a7d79660609200ab83f5db96162d20c"}, - {file = "coverage-7.4.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6679060424faa9c11808598504c3ab472de4531c571ab2befa32f4971835788e"}, - {file = "coverage-7.4.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4af154d617c875b52651dd8dd17a31270c495082f3d55f6128e7629658d63765"}, - {file = "coverage-7.4.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8640f1fde5e1b8e3439fe482cdc2b0bb6c329f4bb161927c28d2e8879c6029ee"}, - {file = "coverage-7.4.3-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:69b9f6f66c0af29642e73a520b6fed25ff9fd69a25975ebe6acb297234eda501"}, - {file = "coverage-7.4.3-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:0842571634f39016a6c03e9d4aba502be652a6e4455fadb73cd3a3a49173e38f"}, - {file = "coverage-7.4.3-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:a78ed23b08e8ab524551f52953a8a05d61c3a760781762aac49f8de6eede8c45"}, - {file = "coverage-7.4.3-cp39-cp39-win32.whl", hash = "sha256:c0524de3ff096e15fcbfe8f056fdb4ea0bf497d584454f344d59fce069d3e6e9"}, - {file = "coverage-7.4.3-cp39-cp39-win_amd64.whl", hash = "sha256:0209a6369ccce576b43bb227dc8322d8ef9e323d089c6f3f26a597b09cb4d2aa"}, - {file = "coverage-7.4.3-pp38.pp39.pp310-none-any.whl", hash = "sha256:7cbde573904625509a3f37b6fecea974e363460b556a627c60dc2f47e2fffa51"}, - {file = "coverage-7.4.3.tar.gz", hash = "sha256:276f6077a5c61447a48d133ed13e759c09e62aff0dc84274a68dc18660104d52"}, -] - -[package.extras] -toml = ["tomli"] - -[[package]] -name = "exceptiongroup" -version = "1.1.3" -description = "Backport of PEP 654 (exception groups)" -optional = false -python-versions = ">=3.7" -files = [ - {file = "exceptiongroup-1.1.3-py3-none-any.whl", hash = "sha256:343280667a4585d195ca1cf9cef84a4e178c4b6cf2274caef9859782b567d5e3"}, - {file = "exceptiongroup-1.1.3.tar.gz", hash = "sha256:097acd85d473d75af5bb98e41b61ff7fe35efe6675e4f9370ec6ec5126d160e9"}, -] - -[package.extras] -test = ["pytest (>=6)"] +# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand. [[package]] name = "flake8" @@ -105,17 +16,6 @@ mccabe = ">=0.7.0,<0.8.0" pycodestyle = ">=2.11.0,<2.12.0" pyflakes = ">=3.2.0,<3.3.0" -[[package]] -name = "iniconfig" -version = "2.0.0" -description = "brain-dead simple config-ini parsing" -optional = false -python-versions = ">=3.7" -files = [ - {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, - {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, -] - [[package]] name = "mccabe" version = "0.7.0" @@ -127,32 +27,6 @@ files = [ {file = "mccabe-0.7.0.tar.gz", hash = "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325"}, ] -[[package]] -name = "packaging" -version = "23.2" -description = "Core utilities for Python packages" -optional = false -python-versions = ">=3.7" -files = [ - {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"}, - {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, -] - -[[package]] -name = "pluggy" -version = "1.4.0" -description = "plugin and hook calling mechanisms for python" -optional = false -python-versions = ">=3.8" -files = [ - {file = "pluggy-1.4.0-py3-none-any.whl", hash = "sha256:7db9f7b503d67d1c5b95f59773ebb58a8c1c288129a88665838012cfb07b8981"}, - {file = "pluggy-1.4.0.tar.gz", hash = "sha256:8c85c2876142a764e5b7548e7d9a0e0ddb46f5185161049a79b7e974454223be"}, -] - -[package.extras] -dev = ["pre-commit", "tox"] -testing = ["pytest", "pytest-benchmark"] - [[package]] name = "pycodestyle" version = "2.11.1" @@ -175,40 +49,7 @@ files = [ {file = "pyflakes-3.2.0.tar.gz", hash = "sha256:1c61603ff154621fb2a9172037d84dca3500def8c8b630657d1701f026f8af3f"}, ] -[[package]] -name = "pytest" -version = "8.1.1" -description = "pytest: simple powerful testing with Python" -optional = false -python-versions = ">=3.8" -files = [ - {file = "pytest-8.1.1-py3-none-any.whl", hash = "sha256:2a8386cfc11fa9d2c50ee7b2a57e7d898ef90470a7a34c4b949ff59662bb78b7"}, - {file = "pytest-8.1.1.tar.gz", hash = "sha256:ac978141a75948948817d360297b7aae0fcb9d6ff6bc9ec6d514b85d5a65c044"}, -] - -[package.dependencies] -colorama = {version = "*", markers = "sys_platform == \"win32\""} -exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} -iniconfig = "*" -packaging = "*" -pluggy = ">=1.4,<2.0" -tomli = {version = ">=1", markers = "python_version < \"3.11\""} - -[package.extras] -testing = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] - -[[package]] -name = "tomli" -version = "2.0.1" -description = "A lil' TOML parser" -optional = false -python-versions = ">=3.7" -files = [ - {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, - {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, -] - [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "d9e0f7c52c410f11fa585bfcce089e88ff3c66adee9577c65a839e4cdd910143" +content-hash = "1f2cf393aab6b2e49082242e427d68a860f00d3dc43853f1dc5fe30de6e319cf" diff --git a/pyfilehandling/jsonfile.py b/pyfilehandling/jsonfile.py index 9315ad0..8a3c694 100644 --- a/pyfilehandling/jsonfile.py +++ b/pyfilehandling/jsonfile.py @@ -24,7 +24,8 @@ def read(self) -> Dict: def write(self, data: Dict, indent: int = 4): if self.file.writable: json.dump(data, self.file, indent) - raise WriteNotPermitted(self.file.name) + else: + raise WriteNotPermitted(self.file.name) def read_key(self, key: str) -> Any: if self.data is None: diff --git a/pyfilehandling/jsonfile.pyi b/pyfilehandling/jsonfile.pyi new file mode 100644 index 0000000..c9c6fd5 --- /dev/null +++ b/pyfilehandling/jsonfile.pyi @@ -0,0 +1,77 @@ +from typing import Dict, Any + + +class jsonFile: + file: Any # Placeholder for file object, type inferred + + def __init__(self, filename: str, mode: str = "r") -> None: + """ + Initialize a jsonFile instance. + + Parameters: + - filename (str): The name of the JSON file. + - mode (str): The mode to open the file in (default is 'r' for read-only). + + Returns: + - None + """ + + def __enter__(self) -> Any: + """ + Enter the context manager and return the file object. + + Returns: + - Any: The file object. + """ + + def __exit__(self, exc_type, exc_value, exc_traceback) -> None: + """ + Exit the context manager and close the file. + + Parameters: + - exc_type: The exception type. + - exc_value: The exception value. + - exc_traceback: The traceback. + + Returns: + - None + """ + + def read(self) -> Dict: + """ + Read JSON data from the file and return it as a dictionary. + + Returns: + - Dict: The JSON data. + """ + + def write(self, data: Dict, indent: int = 4) -> None: + """ + Write JSON data to the file. + + Parameters: + - data (Dict): The data to write to the file. + - indent (int): The number of spaces to use for indentation (default is 4). + + Returns: + - None + """ + + def read_key(self, key: str) -> Any: + """ + Read a specific key from the JSON data. + + Parameters: + - key (str): The key to read from the JSON data. + + Returns: + - Any: The value associated with the key. + """ + + def close(self) -> None: + """ + Close the file. + + Returns: + - None + """ diff --git a/pyproject.toml b/pyproject.toml index 0796614..f708b8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pyfilehandling" -version = "3.2.1" +version = "3.2.2" description = "A Python package for file manipulation operations." authors = ["Jeel Dobariya "] license = "MIT" @@ -32,10 +32,6 @@ packages = [ [tool.poetry.dependencies] python = "^3.9" -[tool.poetry.group.test.dependencies] -pytest = ">=7.4.3,<9.0.0" -coverage = "^7.3.2" - [tool.poetry.group.lint.dependencies] flake8 = ">=6.1,<8.0" diff --git a/tests/conftest.py b/tests/conftest.py deleted file mode 100644 index 4d16572..0000000 --- a/tests/conftest.py +++ /dev/null @@ -1,8 +0,0 @@ -import sys -import os - -# Get the absolute path to the project directory -project_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) - -if project_dir not in sys.path: - sys.path.insert(0, project_dir) diff --git a/tests/mocking.py b/tests/mocking.py deleted file mode 100644 index 03b555b..0000000 --- a/tests/mocking.py +++ /dev/null @@ -1,43 +0,0 @@ -import pytest - -class MockFileHandle: - def __init__(self): - self.data = "" - self.__read_index = 0 - - def close(self): - return None - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, traceback): - pass - - def write(self, data): - self.data += data - - def writelines(self, lines): - self.data += "\n".join(lines) - - def read(self): - return self.data - - def readline(self): - lines = self.data.split("\n") - ind = 0 - for line in lines: - if ind == self.__read_index: - self.__read_index += 1 - return line + "\n" - else: - ind += 1 - return None - - def readlines(self): - lines = self.data.split("\n") - return [line + "\n" for line in lines if line] - -@pytest.fixture -def mock_file_handle(): - return MockFileHandle() diff --git a/tests/pyfilehandling/test_fileio.py b/tests/pyfilehandling/test_fileio.py deleted file mode 100644 index 10abc1e..0000000 --- a/tests/pyfilehandling/test_fileio.py +++ /dev/null @@ -1,141 +0,0 @@ -from pyfilehandling import fileio - -import pytest -from tests.mocking import mock_file_handle - -class TestWrite: - def test_write_append_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Hello, World!" - - fileio.write("abc.txt", data_to_write, "a") - - assert mock_file_handle.data == data_to_write - - def test_write_overwrite_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Hello, World!" - - fileio.write("abc.txt", data_to_write, "w") - - assert mock_file_handle.data == data_to_write - - def test_write_invalid_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Hello, World!" - - # Try to write with an invalid mode, expect a ValueError - with pytest.raises(ValueError, match="Invalid mode 'invalid_mode'. it should be 'w' or 'a'."): - fileio.write("abc.txt", data_to_write, mode="invalid_mode") - - -class TestRead: - # TODO: add testcases to check whether function raises `ValueError(f"Error reading from '{path}': {e}")` when path to file is broken. - # TODO: add testcases to check whether function return a empty string when file is not found. - def test_read_existing_file(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Hello, World!" - - with open("abc.txt", "w") as file: - file.write(data_to_write) - - content = fileio.read("abc.txt") - - assert content == data_to_write - - def test_read_empty_file(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - open("abc.txt", "w").close() - - content = fileio.read("abc.txt") - assert content == "" - -class TestReadline: - # TODO: add testcases to check whether function raises `ValueError(f"Error reading from '{path}': {e}")` when path to file is broken. - # TODO: add testcases to check whether function return a empty string when file is not found. - def test_readline_existing_file(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Line 1\nLine 2\nLine 3" - - with open("abc.txt", "w") as file: - file.write(data_to_write) - - lines = fileio.readline("abc.txt", 1) - - assert lines == "Line 2" - - def test_readline_invalid_lineNo_file(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - open("abc.txt", "w").close() - - with pytest.raises(IndexError): - fileio.readline("abc.txt", 5) - -class TestReadlines: - # TODO: add testcases to check whether function raises `ValueError(f"Error reading from '{path}': {e}")` when path to file is broken. - # TODO: add testcases to check whether function return a empty list when file is not found. - def test_readlines_existing_file(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Line 1\nLine 2\nLine 3" - - with open("abc.txt", "w") as file: - file.write(data_to_write) - - lines = fileio.readlines("abc.txt") - - assert lines == ["Line 1", "Line 2", "Line 3"] - - def test_readlines_empty_file(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - open("abc.txt", "w").close() - - lines = fileio.readlines("abc.txt") - assert lines == [] - -class TestWriteline: - def test_writeline_append_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Line 1" - - fileio.writeline("abc.txt", data_to_write, "a") - - assert mock_file_handle.data == data_to_write + "\n" - - def test_writeline_overwrite_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Line 1" - - fileio.writeline("abc.txt", data_to_write, "w") - - assert mock_file_handle.data == data_to_write + "\n" - - def test_writeline_invalid_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Hello, World!" - - with pytest.raises(ValueError, match="Invalid mode 'invalid_mode'. it should be 'w' or 'a'."): - fileio.writeline("abc.txt", data_to_write, mode="invalid_mode") - -class TestWritelines: - def test_writelines_append_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = ["Line 1", "Line 2", "Line 3"] - - fileio.writelines("abc.txt", data_to_write, "a") - - assert mock_file_handle.data == "Line 1\nLine 2\nLine 3\n" - - def test_writelines_overwrite_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = ["Line 1", "Line 2", "Line 3"] - - fileio.writelines("abc.txt", data_to_write, "w") - - assert mock_file_handle.data == "Line 1\nLine 2\nLine 3\n" - - def test_writelines_invalid_mode(self, monkeypatch, mock_file_handle): - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - data_to_write = "Hello, World!" - - with pytest.raises(ValueError, match="Invalid mode 'invalid_mode'. it should be 'w' or 'a'."): - fileio.writelines("abc.txt", data_to_write, mode="invalid_mode") diff --git a/tests/pyfilehandling/test_pyfilehandling.py b/tests/pyfilehandling/test_pyfilehandling.py deleted file mode 100644 index f9f23b9..0000000 --- a/tests/pyfilehandling/test_pyfilehandling.py +++ /dev/null @@ -1,7 +0,0 @@ -import pyfilehandling - -def test_read_is_present_at_package_level(): - assert pyfilehandling.read != None - -def test_write_is_present_at_package_level(): - assert pyfilehandling.write != None diff --git a/tests/test_mocking.py b/tests/test_mocking.py deleted file mode 100644 index 6a980c4..0000000 --- a/tests/test_mocking.py +++ /dev/null @@ -1,86 +0,0 @@ -import pytest -from tests.mocking import mock_file_handle - -def test_mock_file_handle_write(mock_file_handle): - mock_file_handle.write("Hello, World!") - assert mock_file_handle.data == "Hello, World!" - -def test_mock_file_handle_writelines(mock_file_handle): - lines = ["Line 1", "Line 2", "Line 3"] - mock_file_handle.writelines(lines) - assert mock_file_handle.data == "Line 1\nLine 2\nLine 3" - -def test_mock_file_handle_read(mock_file_handle): - mock_file_handle.data = "Testing Read Functionality" - assert mock_file_handle.read() == "Testing Read Functionality" - -def test_mock_file_handle_readline(mock_file_handle): - mock_file_handle.data = "Line 1\nLine 2\nLine 3" - assert mock_file_handle.readline() == "Line 1\n" - assert mock_file_handle.readline() == "Line 2\n" - assert mock_file_handle.readline() == "Line 3\n" - assert mock_file_handle.readline() is None - -def test_mock_file_handle_readlines(mock_file_handle): - mock_file_handle.data = "Line 1\nLine 2\nLine 3" - assert mock_file_handle.readlines() == ["Line 1\n", "Line 2\n", "Line 3\n"] - -def test_moked_file_handle_write_and_read(mock_file_handle): - # Use the mock_file_handle as a fixture in your test - mock_file_handle.write("Line 1\nLine 2\nLine 3") - assert mock_file_handle.read() == "Line 1\nLine 2\nLine 3" - assert mock_file_handle.readline() == "Line 1\n" - assert mock_file_handle.readline() == "Line 2\n" - assert mock_file_handle.readlines() == ["Line 1\n","Line 2\n","Line 3\n"] - assert mock_file_handle.readline() == "Line 3\n" - -# test code for `with .. as ..` block - -def test_mock_file_handle_write_withblock(mock_file_handle): - with mock_file_handle as file_handle: - file_handle.write("Hello, World!") - assert mock_file_handle.data == "Hello, World!" - -def test_mock_file_handle_writelines_withblock(mock_file_handle): - lines = ["Line 1", "Line 2", "Line 3"] - with mock_file_handle as file_handle: - file_handle.writelines(lines) - assert mock_file_handle.data == "Line 1\nLine 2\nLine 3" - -def test_mock_file_handle_read_withblock(mock_file_handle): - mock_file_handle.data = "Testing Read Functionality" - with mock_file_handle as file_handle: - assert file_handle.read() == "Testing Read Functionality" - -def test_mock_file_handle_readline_withblock(mock_file_handle): - mock_file_handle.data = "Line 1\nLine 2\nLine 3" - with mock_file_handle as file_handle: - assert file_handle.readline() == "Line 1\n" - assert file_handle.readline() == "Line 2\n" - assert file_handle.readline() == "Line 3\n" - assert file_handle.readline() is None - -def test_mock_file_handle_readlines_withblock(mock_file_handle): - mock_file_handle.data = "Line 1\nLine 2\nLine 3" - with mock_file_handle as file_handle: - assert file_handle.readlines() == ["Line 1\n", "Line 2\n", "Line 3\n"] - -# tested mocking - -def test_open_with_mock_file_handle(monkeypatch, mock_file_handle): - # Replace the built-in open function with the mock_file_handle - monkeypatch.setattr('builtins.open', lambda file, mode='r': mock_file_handle) - - # Now any code that calls open will get your mock_file_handle instead - with open('test_file.txt', 'w') as file: - file.write("Testing open with mock_file_handle") - - # Verify that the data was written to the mock_file_handle - assert mock_file_handle.data == "Testing open with mock_file_handle" - - # You can also use other file operations with the mock_file_handle - with open('test_file.txt', 'r') as file: - content = file.read() - - # Verify that the content matches what was written - assert content == "Testing open with mock_file_handle"