Skip to content

Commit

Permalink
Mock out get/put with awaitables in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Apr 7, 2022
1 parent 5871b09 commit a89ca9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 8 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, TypeVar
from unittest.mock import Mock

import attr
Expand Down Expand Up @@ -43,6 +43,13 @@ def membership(self) -> str:
return membership


T = TypeVar("T")


async def make_awaitable(value: T) -> T:
return value


def create_module(config_override: Dict[str, Any] = {}) -> InviteAutoAccepter:
# Create a mock based on the ModuleApi spec, but override some mocked functions
# because some capabilities are needed for running the tests.
Expand All @@ -51,9 +58,6 @@ def create_module(config_override: Dict[str, Any] = {}) -> InviteAutoAccepter:

# Python 3.6 doesn't support awaiting on a mock, so we make it return an awaitable
# value.
async def make_awaitable(result: Any) -> Any:
return result

module_api.update_room_membership.return_value = make_awaitable(None)
config = InviteAutoAccepter.parse_config(config_override)

Expand Down
20 changes: 15 additions & 5 deletions tests/test_accept_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import aiounittest

from synapse_auto_accept_invite import InviteAutoAccepter
from tests import MockEvent, create_module
from tests import MockEvent, create_module, make_awaitable


class InviteAutoAccepterTestCase(aiounittest.AsyncTestCase):
Expand Down Expand Up @@ -72,13 +72,16 @@ async def test_accept_invite_direct_message(self) -> None:
account_data_put: Mock = cast(
Mock, self.module._api.account_data_manager.put_global
)
account_data_put.return_value = make_awaitable(None)

account_data_get: Mock = cast(
Mock, self.module._api.account_data_manager.get_global
)

account_data_get.return_value = {
"@someone:random": ["!somewhere:random"],
}
account_data_get.return_value = make_awaitable(
{
"@someone:random": ["!somewhere:random"],
}
)

# Stop mypy from complaining that we give on_new_event a MockEvent rather than an
# EventBase.
Expand Down Expand Up @@ -175,6 +178,13 @@ async def test_accept_invite_direct_message_if_only_enabled_for_direct_messages(
config_override={"accept_invites_only_for_direct_messages": True},
)

# Patch out the account data get and put methods with dummy awaitables.
account_data_put: Mock = cast(Mock, module._api.account_data_manager.put_global)
account_data_put.return_value = make_awaitable(None)

account_data_get: Mock = cast(Mock, module._api.account_data_manager.get_global)
account_data_get.return_value = make_awaitable({})

invite = MockEvent(
sender=self.user_id,
state_key=self.invitee,
Expand Down

0 comments on commit a89ca9f

Please sign in to comment.