Skip to content

Conversation

@Taragolis
Copy link
Contributor

@Taragolis Taragolis commented Dec 1, 2022

related: #27970

Migrate Amazon provider's hooks and utils tests to pytest.

All changes are more or less straightforward:

  • Get rid of unittests.TestCase class and TestCase.assert* methods
  • Replace decorator parameterized.expand by pytest.mark.parametrize.
  • Convert setUp* and tearDown* methods to appropriate pytest alternative

See additional findings, info about significant changes and potential follow up in comments

@Taragolis Taragolis requested a review from eladkal as a code owner December 1, 2022 18:25
@boring-cyborg boring-cyborg bot added area:providers provider:amazon AWS/Amazon - related issues labels Dec 1, 2022
Comment on lines 112 to 114
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simple change actually speedup test execution from 30 sec to 2 seconds

before

============================ slowest 100 durations =============================
  30.03s call     tests/providers/amazon/aws/hooks/test_emr_containers.py::TestEmrContainerHook::test_query_status_polling_with_timeout

after

2.01s call     tests/providers/amazon/aws/hooks/test_emr_containers.py::TestEmrContainerHook::test_query_status_polling_with_timeout

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, but do we need to wait at all ? is poll_interval = 0 an option ?

Copy link
Contributor Author

@Taragolis Taragolis Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we do not need to wait at all but better to use freezegun or mock time.sleep however with poll_interval = 0 it would be run just 0.5 sec (localy).

But also it shows that there is no validation for this value. Usual 0 for any wait interval in general not a good idea,
because it could easily converted to something like

while True:
    pass  # 100% core 😢 usage

Or in this case just exceed the limits of AWS API

@Taragolis Taragolis added the changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) label Dec 1, 2022
@ferruzzi
Copy link
Contributor

ferruzzi commented Dec 1, 2022

That's awesome. I was literally looking at this earlier this week and have some local code where I started working on it. 👍 Will review ASAP

Copy link
Contributor

@ferruzzi ferruzzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

(non-binding approval)

Copy link
Contributor

@ferruzzi ferruzzi Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to remove the given/when/then comments in the very few places they still exist while we're at it? If you don't want to mess with that now, that's understandable too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now main target just migrate to pytests.
It is good point to change something from the past 😉
To be honest a lot of test need to rewrite later anyway

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, every time I try do add some new code I find a dozen older bits I'd love to redo in the process. Like why are we mocking get_conn in every test instead of making one fixture to do it? I'll take care of that one at some point. Can't fix it all at once 👍

Copy link
Contributor

@ferruzzi ferruzzi Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(No change requested/required, just chatter) I mentioned on slack that I had started on some of these; this was the rabbit hole that got me there... I started updating any hooks that still used get_conn() instead of the conn cached property, which led to updating the mocked values in the unit tests, which led to converting them all to pytest which... never got finished :P So again, thanks for taking this on!

Copy link
Contributor

@vincbeck vincbeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to see this clean up!

Copy link
Contributor

@vandonr-amz vandonr-amz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice.
I noticed that on some setup_methods you added the optional method parameter without using it, while you skipped it on others. I think it'd be best to omit it wherever possible (i.e. probably everywhere in this case)

Comment on lines 112 to 114
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, but do we need to wait at all ? is poll_interval = 0 an option ?

@Taragolis
Copy link
Contributor Author

Taragolis commented Dec 1, 2022

nice. I noticed that on some setup_methods you added the optional method parameter without using it, while you skipped it on others. I think it'd be best to omit it wherever possible (i.e. probably everywhere in this case)

That's funny but if setup_method or teardown_method decorated by @mock.patсh then method signature (self, method) should use

@Taragolis Taragolis closed this Dec 1, 2022
@Taragolis Taragolis reopened this Dec 1, 2022
@vandonr-amz
Copy link
Contributor

nice. I noticed that on some setup_methods you added the optional method parameter without using it, while you skipped it on others. I think it'd be best to omit it wherever possible (i.e. probably everywhere in this case)

That's funny but if setup_method or teardown_method decorated by @mock.patсh then method signature (self, method) should use

Ah yes I imagine because of the order of arguments, but I imagine in test_datasync it's not needed ?
Is it also needed for mocks that "generate" no arguments, like the @mock.patch.dict ?

and nit: I like using _ as name for the arguments that are unused, it's a clear way to mark them as "only here for the compiler to be happy"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original @vandonr-amz

Ah yes I imagine because of the order of arguments, but I imagine in test_datasync it's not needed ?

Actually this arg needed because we decorated entire class

test setup failed
args = (<tests.providers.amazon.aws.hooks.test_datasync.TestDataSyncHookMocked object at 0x109fdf4c0>, <bound method TestData...HookMocked.test_init of <tests.providers.amazon.aws.hooks.test_datasync.TestDataSyncHookMocked object at 0x109fdf4c0>>)
kwargs = {}

    def wrapper(*args, **kwargs):
        self.start(reset=reset)
        try:
>           result = func(*args, **kwargs)
E           TypeError: setup_method() takes 1 positional argument but 2 were given

Comment on lines 309 to 317
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it also needed for mocks that "generate" no arguments, like the @mock.patch.dict ?

And even with this case we need 2 arguments

args = (<tests.providers.amazon.aws.hooks.test_batch_client.TestBatchClientDelays object at 0x1071f0f70>, <bound method TestB...tDelays.test_init of <tests.providers.amazon.aws.hooks.test_batch_client.TestBatchClientDelays object at 0x1071f0f70>>)
kw = {}

    @wraps(f)
    def _inner(*args, **kw):
        self._patch_dict()
        try:
>           return f(*args, **kw)
E           TypeError: setup_method() takes 1 positional argument but 2 were given

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now main target just migrate to pytests.
It is good point to change something from the past 😉
To be honest a lot of test need to rewrite later anyway

Comment on lines -325 to +324
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original @vandonr-amz

and nit: I like using _ as name for the arguments that are unused, it's a clear way to mark them as "only here for the compiler to be happy"

This mostly as reminder what the actual argument here.

Copy link
Contributor

@vandonr-amz vandonr-amz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting how arguments work with mocks in setup...
Thank you for double-checking.

Copy link
Contributor

@ferruzzi ferruzzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation on the method fixture, that was somewhat confusing. I think the rest of my thoughts were addressed, non-binding approval pending the other folks are cool with their comments.

@Taragolis Taragolis force-pushed the amazon-provider-tests-hooks-convert-pytest branch from a2c1503 to c4201d9 Compare December 4, 2022 14:32
@potiuk potiuk merged commit f02a7e9 into apache:main Dec 5, 2022
@Taragolis Taragolis deleted the amazon-provider-tests-hooks-convert-pytest branch December 5, 2022 06:43
Taragolis pushed a commit that referenced this pull request Dec 7, 2022
…28145)

Related: #28039 #28139

Migrate Amazon provider's transfer tests to `pytest`.

All changes are more or less straightforward:

- Get rid of unittests.TestCase class and TestCase.assert* methods
- Convert setUp* and tearDown* methods to appropriate pytest alternative
- Replace decorator `parameterized.expand` by `pytest.mark.parametrize`.
- Renamed `@patch` to `@mock.patch` to conform to other tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants