Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jul 4, 2021
1 parent 8fb44ac commit 1024af5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/turbo_flask/turbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ def push(self, stream, to=None):
to = self.clients.keys()
elif not hasattr(to, '__len__') or isinstance(to, str):
to = [to]
if hasattr(stream, '__len__') and not isinstance(stream, str):
stream = ''.join(stream)
for recipient in to:
for ws in self.clients[recipient]:
try:
ws.send(stream)
except ConnectionClosed:
except ConnectionClosed: # pragma: no cover
pass
56 changes: 56 additions & 0 deletions tests/test_turbo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from unittest import mock
import pytest
from flask import Flask, render_template_string
from werkzeug.exceptions import NotFound
Expand Down Expand Up @@ -148,3 +149,58 @@ def test_streams(self):
'<turbo-stream action="remove" target="bar">'
'<template></template></turbo-stream>'
)

def test_stream_response(self):
app = Flask(__name__)
turbo = turbo_flask.Turbo(app)

with app.test_request_context('/'):
r = turbo.stream([turbo.append('foo', 'bar'), turbo.remove('baz')])
assert r.get_data() == (
b'<turbo-stream action="append" target="bar">'
b'<template>foo</template>'
b'</turbo-stream>'
b'<turbo-stream action="remove" target="baz">'
b'<template></template>'
b'</turbo-stream>'
)

def test_push(self):
app = Flask(__name__)
turbo = turbo_flask.Turbo(app)
turbo.clients = {'123': [mock.MagicMock()], '456': [mock.MagicMock()]}

expected_stream = (
'<turbo-stream action="append" target="bar">'
'<template>foo</template>'
'</turbo-stream>'
'<turbo-stream action="remove" target="baz">'
'<template></template>'
'</turbo-stream>'
)
turbo.push([turbo.append('foo', 'bar'), turbo.remove('baz')])
turbo.clients['123'][0].send.assert_called_with(expected_stream)
turbo.clients['456'][0].send.assert_called_with(expected_stream)

def test_push_to(self):
app = Flask(__name__)
turbo = turbo_flask.Turbo(app)
turbo.clients = {'123': [mock.MagicMock()], '456': [mock.MagicMock()]}

expected_stream = (
'<turbo-stream action="append" target="bar">'
'<template>foo</template>'
'</turbo-stream>'
'<turbo-stream action="remove" target="baz">'
'<template></template>'
'</turbo-stream>'
)
turbo.push([turbo.append('foo', 'bar'), turbo.remove('baz')], to='456')
turbo.clients['123'][0].send.assert_not_called()
turbo.clients['456'][0].send.assert_called_with(expected_stream)
turbo.clients['123'][0].reset_mock()
turbo.clients['456'][0].reset_mock()
turbo.push([turbo.append('foo', 'bar'), turbo.remove('baz')],
to=['123'])
turbo.clients['123'][0].send.assert_called_with(expected_stream)
turbo.clients['456'][0].send.assert_not_called()

0 comments on commit 1024af5

Please sign in to comment.