Skip to content

Commit

Permalink
Fix tests, maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
mpasternak committed Jul 10, 2022
1 parent 1ddd94a commit 0407fa5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Expand Up @@ -79,6 +79,9 @@ simplejson = "3.17.5"
django-dbtemplates-iplweb = "4.1.0"
django-reversion = "4.0.0"
channels = "3.0.5"
# asgiref = "3.3.4"
# Bez asgiref 3.3.4 daphne wyrzuca "single thread executor, would deadlock"
# i efektywnie nie mozna testowac na lokalnej maszynie.
uvicorn = {version = "0.14.0", extras = ["standard"]}
channels-redis = "3.4.0"
django-flexible-reports = ">0.2.9"
Expand Down
40 changes: 30 additions & 10 deletions src/asgi_live_server.py
Expand Up @@ -2,26 +2,46 @@
import warnings

import pytest
from channels.testing import ChannelsLiveServerTestCase
from asgiref.sync import sync_to_async
from pytest_django.lazy_django import skip_if_no_django

from asgi_testing import DaphneThread
# https://github.com/django/channels/issues/1722#issuecomment-1032965993


class PytestChannelsLiveServerTestCase(ChannelsLiveServerTestCase):
ProtocolServerProcess = DaphneThread

@property
def url(self):
return self.live_server_url
def patch_sync_to_async(*args, **kwargs):
"""
Monkey Patch the sync_to_async decorator
---------------------------------------
ASGIRef made a change in their defaults that has caused major problems
for channels. The decorator below needs to be updated to use
thread_sensitive=False, thats why we are patching it on our side for now.
https://github.com/django/channels/blob/main/channels/http.py#L220
"""
kwargs["thread_sensitive"] = False
return sync_to_async(*args, **kwargs)


@pytest.fixture(scope="session")
def asgi_live_server(request):
"""Run a live Daphne server in the background during tests.
"""
"""Run a live Daphne server in the background during tests."""
skip_if_no_django()

import asgiref

# Monkey Patches
asgiref.sync.sync_to_async = patch_sync_to_async

from channels.testing import ChannelsLiveServerTestCase

from asgi_testing import DaphneThread

class PytestChannelsLiveServerTestCase(ChannelsLiveServerTestCase):
ProtocolServerProcess = DaphneThread

@property
def url(self):
return self.live_server_url

import django

addr = request.config.getvalue("liveserver") or os.getenv(
Expand Down
37 changes: 12 additions & 25 deletions src/bpp/tests/util.py
Expand Up @@ -265,6 +265,9 @@ def select_select2_autocomplete(
:param element_id: ID elementu (tekst)
:param value: tekst do wpisania
"""

old_value = browser.find_by_id(f"select2-{element_id}-container").text

# Znajdź interesujący nas select2-autocomplete
element = browser.find_by_id(element_id)[0]
sibling = element.find_by_xpath("following-sibling::span")
Expand All @@ -285,40 +288,24 @@ def select_select2_autocomplete(
# tries = 0
# while True:

old_active = element.parent.switch_to.active_element
while True:
sibling.click()
time.sleep(random.randint(100, 1000) / 1000)
new_active = element.parent.switch_to.active_element

if new_active != old_active:
break
sibling.click()

old_value = None
# W tym momencie pojawi sie popup. Input type z klasą .select2-search__field
# będzie jedyny na stronie:
WebDriverWait(browser, SHORT_WAIT_TIME).until(
lambda driver: driver.find_by_css(".select2-search__field")
)

while old_value is None:
old_value = browser.find_by_id(f"select2-{element_id}-container").text
time.sleep(0.3)
input_box = element.parent.find_by_css(".select2-search__field")[0]

# for letter in value:
new_active.send_keys(value)
time.sleep(1)
list(input_box.type(value, slowly=True))

wait_for(
lambda: "Trwa wyszukiwanie…"
not in browser.find_by_id(f"select2-{element_id}-results").value
)

if value_before_enter:
try:
wait_for(
lambda: value_before_enter
in browser.find_by_id(f"select2-{element_id}-results").value,
max_seconds=LONG_WAIT_TIME,
)
except TimeoutError as e:
raise e
new_active.send_keys(Keys.ENTER)
input_box.type(Keys.ENTER)
time.sleep(0.5)

if wait_for_new_value:
Expand Down

0 comments on commit 0407fa5

Please sign in to comment.