chrisbillows / raindrop-todoist-syncer

Converts favourited Raindrops into tasks in Todoist.
2 stars 1 forks source link

Implement a full test ENV (and resolve the `.env` fiasco) #46

Closed chrisbillows closed 6 months ago

chrisbillows commented 6 months ago

Ref: Issue 31 comment

Acceptance Criteria

Pytest will be reinstated in the GitHub workflow... and pass!

Approach

1) Centralise from dotenv import load_dotenv commands.

Currently this is in the raindrop.py and todoist.py files. It should work if we move this to be in main.py only. (I think???).

So try that! This allow more certainty when adding:

2) Add if statement logic to loading the dotenv file.

if os.getenv("ENV") != "test":
    from dotenv import load_dotenv
    load_dotenv()

This will be in one place only main.py giving us that single source of importing the .env file.

For handling during the test environment we will:

3) Implement a test specific environment variables file .env.test.

This should be able to change very simple variables.

4) This test environment will trigger a setup_module such as the below. This should be automatically picked up by pytest (can it be in the conftest.py?).

from dotenv import load_dotenv
import os

def setup_module(module):
    """Setup for the entire module"""
    os.environ['ENV'] = 'test'  # Indicate test environment
       load_dotenv(dotenv_path='.env.test', override=True)

5) Go test by test and remove any of the piecemeal environment variables mocking that has been done.

6) Uncomment pytest in the GitHub workflow.

chrisbillows commented 6 months ago

So we now only get two fails on the GitHub workflow `

=========================== short test summary info ============================
FAILED tests/integration/test_raindrop_access_token_refresher_int.py::TestAccessTokenRefreshRunner::test_happy_path - FileNotFoundError: [Errno 2] No such file or directory: '.env'
FAILED tests/unit/test_todoist.py::TestInit::test_main_work_api_key - assert None is not None
 +  where None = <todoist.TodoistTaskCreatorDev object at 0x7f3d9a5b7eb0>.TODOIST_API_KEY
=================== 2 failed, 138 passed, 5 skipped in 9.60s ===================

The first is fair enough - it's getting muddled because there is no .env file for it to write over. Possibly we can just mock one with a temp directory for this test? (Although why does the unit test for that function pass?)

The todoist one I do not get!

Could consider deleting .env file locally and experimenting there for speed?