Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common functions in API unit test modules should be moved to utils #33

Open
NamesJ opened this issue Mar 7, 2021 · 1 comment
Open
Labels
good first issue Good for newcomers invalid This doesn't seem right

Comments

@NamesJ
Copy link
Owner

NamesJ commented Mar 7, 2021

Modules in the tests package for testing API resources in certain namespaces (e.g. test_transfer_api.py', 'test_offer_api.py') repeat similar code for functions which use a session client to send a request and return a response -- functions usually named something like create_()', get_<entity>_data(), etc.. For example in test_transfer_api.py, the function get_transfer_data().

Because resources are designed such that request data (if any) is expected to always be JSON, the implementation of these functions are (usually) virtually identical.

So I think they should be consolidated somewhere in utils sub-package of the tests package.

@NamesJ NamesJ added invalid This doesn't seem right good first issue Good for newcomers labels Mar 7, 2021
@NamesJ
Copy link
Owner Author

NamesJ commented Mar 8, 2021

Proposed changes:

Old way

# test_transfer_api.py
...


def get_transfer_data(self, access_token, id):
    return self.client.get(
        f'/api/transfer',
        headers={'Authorization': f'Bearer {access_token}'},
        content_type='application/json',
        data=json.dumps({ 'id': id })
    )


class TestTransferBlueprint(BaseTestCase):
    def test_transfer_get(self):
        ...


        transfer = Transfer(...)
        ...


        transfer_response = get_transfer_data(self, access_token, transfer.id)
        ...

New way

# tests/utils/common.py
...


from app import db
...


def get_entity_data(client, access_token, entity_name, payload):
    return self.client.get(
        f'/api/{entity_name}',
        headers={'Authorization': f'Bearer {access_token}'},
        content_type='application/json',
        data=json.dumps(payload)
    )
# tests/test_transfer_api.py
from tests.utils.common import get_entity_data
...


class TestTransferBlueprint(BaseTestCase):
    def test_transfer_get(self):
        ...


        transfer = Transfer(...)
        ...


        payload = { 'id': transfer.id }
        transfer_response = get_entity_data(self, access_token, 'transfer', payload)
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

1 participant