Repository: h2non/pook

Issue:
Flush mocks when `pook.activate` used as a wrapper
### Description:
When using `pook.activate` as a decorator for test functions, existing mocks are not flushed before activating the mocks in each test. This can lead to test cases affecting each other, resulting in non-independent tests, which is a concern in test suites where each test should be isolated.

### Expected Behavior:
When `pook.activate` is used to wrap a test function, it should automatically flush all existing mocks both before activating the new mocks and after the test function completes. This would ensure that each test starts with a clean slate and works independently without being affected by mocks from previous test cases.

### Actual Behavior:
Currently, when `pook.activate` is used as a wrapper, it retains the mocks from previous tests, causing test cases to interfere with each other.

### Suggested Solution:
Flush mocks before activation and after the testing function

### Example:
```python
import pook

@pook.on
def test_case_1():
    pook.get('http://example.com', reply=200)
    # ... perform some assertions and not hit the endpoint

@pook.on
def test_case_2():
    pook.get('http://example.com', reply=404)
    
    # Sending a request to the same endpoint
    response = some_http_library.get('http://example.com')
    
    # Expecting a 404 response, but this may fail due to leftover mock from test_case_1
    assert response.status_code == 404
```

In this example, test_case_2 will fails if both test cases are run together but it will pass when run individually. This behavior makes debugging extremely difficult because the tests are not properly isolated, leading to inconsistent results.

Discussion:
@KyleJamesWalker: Note: You can also check `pook.done()` at the end of your test which will raise if you have pending mocks that have not been matched. 
@sarayourfriend: Thanks again @shift0965, I've released your fix in https://github.com/h2non/pook/releases/tag/v2.1.1 :tada: @shift0965: > Thanks again @shift0965, I've released your fix in https://github.com/h2non/pook/releases/tag/v2.1.1 🎉

Thanks!! Could you also help me release it on PyPI 🙏@sarayourfriend: Ah, apologies! I thought I had pushed it :facepalm: It's there now, thanks very much for the ping!

Solution patch:
diff --git a/src/pook/activate_async.py b/src/pook/activate_async.py
index 0a4cf72..77d739a 100644
--- a/src/pook/activate_async.py
+++ b/src/pook/activate_async.py
@@ -24,5 +24,6 @@ async def wrapper(*args, **kw):
                 fn(*args, **kw)
         finally:
             _engine.disable()
+            _engine.reset()
 
     return wrapper
diff --git a/src/pook/api.py b/src/pook/api.py
index 3147fa6..21708c5 100644
--- a/src/pook/api.py
+++ b/src/pook/api.py
@@ -138,6 +138,7 @@ def wrapper(*args, **kw):
             fn(*args, **kw)
         finally:
             _engine.disable()
+            _engine.reset()
 
     return wrapper
 


Test patch:
diff --git a/tests/unit/api_test.py b/tests/unit/api_test.py
index b872136..2f8d7b3 100644
--- a/tests/unit/api_test.py
+++ b/tests/unit/api_test.py
@@ -1,3 +1,4 @@
+import asyncio
 import pytest
 
 from pook import api
@@ -47,3 +48,28 @@ def test_mock_contructors(engine):
 
     assert len(engine.mocks) == 0
     assert engine.active is False
+
+
+def test_activate_as_decorator(engine):
+    @api.activate
+    def test_activate():
+        api.get("foo.com")
+        assert engine.active is True
+        assert engine.isdone() is False
+
+    test_activate()
+    assert engine.active is False
+    assert engine.isdone() is True
+
+
+async def test_activate_as_decorator_for_async(engine):
+    @api.activate
+    async def test_activate():
+        await asyncio.sleep(0)
+        api.get("foo.com")
+        assert engine.active is True
+        assert engine.isdone() is False
+
+    await test_activate()
+    assert engine.active is False
+    assert engine.isdone() is True