- Goal Test async function with test database
- Problem I have async functions for Django channels and when I create data in
setUp
the data are not reflected in the project!!!
- My tests: note: these tests inside a
APITestCase
class
class WebsocketTests(APITestCase):
async def test_connect(self):
userx = await sync_to_async(User.objects.create)(username='newusername',email='[email protected]')
users_number = await sync_to_async(User.objects.count)()
print(users_number) #<=== this returns 1 just fine
communicator = WebsocketCommunicator(application, 'alerts/')
connected, subprotocol = await communicator.connect()
assert connected
await communicator.disconnect()
@database_sync_to_async
def get_user(querys):
print(User.objects.count()) #<======= this returns 0 while it should return 1
try:
token = parse_qs(querys.decode("utf8"))['token'][0]
token_data = UntypedToken(token)
user_id = token_data["user_id"]
except:
return 'token is invalid'
try:
return User.objects.get(id=user_id)
except User.DoesNotExist:
return 'AnonymousUser'
class QueryAuthMiddleware:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
scope['user'] = await get_user(scope["query_string"])
return await self.app(scope, receive, send)
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": QueryAuthMiddleware(
URLRouter(
websocket_urlpatterns
)
),
})
#settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'OPTIONS': {
'timeout': 20, # in seconds
# see also
# https://docs.python.org/3.7/library/sqlite3.html#sqlite3.connect
},
'TEST': {
'NAME': os.path.join(BASE_DIR, "db_test.sqlite3"),
},
}
}
- I tride
- replace
APITestCase
with django TestCase
- switch to postgres
- use
from asgiref.sync import sync_to_async
setUp
method async too. Also no need to get frustated, async topics receive lesser answers because well there are lesser experts in those topics. – Shouse