Skip to content

Commit

Permalink
prefer newer unittest.mock when available (#284)
Browse files Browse the repository at this point in the history
* prefer newer unittest.mock when available

* Update test/test_client.py

Signed-off-by: Asif Saif Uddin <[email protected]>

* flake8

---------

Signed-off-by: Asif Saif Uddin <[email protected]>
Co-authored-by: Asif Saif Uddin <[email protected]>
  • Loading branch information
a-detiste and auvipy authored Sep 17, 2024
1 parent 5c9fc11 commit 22f8bb7
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/sources/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ necessarily need a connected socket, in fact, you don't even need a socket at al
>>> def data_source():
>>> yield TextMessage(u'hello world')
>>> from mock import MagicMock
>>> from unittest.mock import MagicMock
>>> source = MagicMock(side_effect=data_source)
>>> ws = EchoWebSocket(sock=source)
>>> ws.send(u'hello there')
2 changes: 1 addition & 1 deletion example/basic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

cur_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
index_path = os.path.join(cur_dir, 'index.html')
index_page = file(index_path, 'r').read()
index_page = open(index_path, 'r').read()

class ChatWebSocketHandler(WebSocket):
def received_message(self, m):
Expand Down
4 changes: 3 additions & 1 deletion example/droid_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

__doc__ = """
WebSocket client that pushes Android sensor metrics to the
websocket server it is connected to.
Expand Down Expand Up @@ -64,7 +66,7 @@ def run(self):
continue

c = lambda rad: rad * 360.0 / math.pi
print c(azimuth), c(pitch), c(roll), x, y, z
print(c(azimuth), c(pitch), c(roll), x, y, z)

if self.client.terminated:
break
Expand Down
8 changes: 5 additions & 3 deletions example/droid_sensor_cherrypy_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

import os.path
import cherrypy

Expand All @@ -8,7 +10,7 @@
class BroadcastWebSocketHandler(WebSocket):
def received_message(self, m):
cherrypy.engine.publish('websocket-broadcast', str(m))

class Root(object):
@cherrypy.expose
def display(self):
Expand Down Expand Up @@ -61,15 +63,15 @@ def index(self):
</body>
</html>
"""

if __name__ == '__main__':
cherrypy.config.update({
'server.socket_host': '0.0.0.0',
'server.socket_port': 9000,
'tools.staticdir.root': os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
}
)
print os.path.abspath(os.path.join(__file__, 'static'))
print(os.path.abspath(os.path.join(__file__, 'static')))
WebSocketPlugin(cherrypy.engine).subscribe()
cherrypy.tools.websocket = WebSocketTool()

Expand Down
4 changes: 2 additions & 2 deletions example/websensors/templates/.cache/board.html.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def render_body(context,**pageargs):
# SOURCE LINE 1
__M_writer(u'<!DOCTYPE html>\n<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->\n<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->\n<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n <title>Mobile remote control</title>\n <meta name="description" content="Remote control your webapp with your mobile device">\n <meta name="viewport" content="width=device-width, initial-scale=1">\n <meta name="twitter:card" content="Sylvain Hellegouarch\'s twitter account">\n <meta name="twitter:site" content="@lawouach">\n <meta name="twitter:url" content="http://www.defuze.org">\n\n <link rel="stylesheet" href="/static/vendors/initializr/css/normalize.min.css">\n <link rel="stylesheet" href="/static/vendors/initializr/css/main.css">\n <link rel="stylesheet" href="/static/css/style.css">\n\n <script src="/static/vendors/initializr/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>\n </head>\n <body>\n <!--[if lt IE 7]>\n <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>\n <![endif]-->\n\n <div class="main-container" id="arena">\n\t <canvas id="tools" width="70" height="600"></canvas>\n\t <canvas id="board" width="150" height="600"></canvas>\n </div>\n\n <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>\n\t<script type="application/javascript" src="/static/vendors/jcanvas/jcanvas.min.js"></script>\n <script type="application/javascript" src="/static/vendors/initializr/js/plugins.js"></script>\n <script type="application/javascript" src="/static/js/drawingboard.js"></script>\n <script type="application/javascript">\n\t $(document).ready(function() {\n\t $.board({board: "')
# SOURCE LINE 38
__M_writer(unicode(boardid))
__M_writer(str(boardid))
__M_writer(u'", participant: "')
__M_writer(unicode(participantid))
__M_writer(str(participantid))
__M_writer(u'"});\n\t });\n </script>\n </body>\n</html>\n')
return ''
finally:
Expand Down
4 changes: 2 additions & 2 deletions example/websensors/templates/.cache/index.html.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def render_body(context,**pageargs):
# SOURCE LINE 1
__M_writer(u'<!DOCTYPE html>\n<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->\n<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->\n<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n <title>Shared drawing board</title>\n <meta name="description" content="Remote control your webapp with your mobile device">\n <meta name="viewport" content="width=device-width, initial-scale=1">\n <meta name="twitter:card" content="Sylvain Hellegouarch\'s twitter account">\n <meta name="twitter:site" content="@lawouach">\n <meta name="twitter:url" content="http://www.defuze.org">\n\n <link rel="stylesheet" href="/static/vendors/initializr/css/normalize.min.css">\n <link rel="stylesheet" href="/static/vendors/initializr/css/main.css">\n\n <script src="/static/vendors/initializr/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>\n </head>\n <body>\n <!--[if lt IE 7]>\n <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>\n <![endif]-->\n\n <div class="header-container">\n <header class="wrapper clearfix">\n <h1 class="title">shared drawing board</h1>\n </header>\n </div>\n\n <div class="main-container">\n <div class="main wrapper clearfix">\n\n <article>\n <section>\n\t <p>This demo will demonstrate a fun usage of WebSocket and Canvases.\n\t The idea is that you will share a drawing board with other\n\t users or devices and each time someone will draw something onto\n\t the canvas, all other participants will see the drawing in near real-time\n\t on their side too.\n\t </p>\n\t <p>Please use the link below to share a drawing board:</p>\n\t <p><a href="http://192.168.1.13:8080/board/')
# SOURCE LINE 44
__M_writer(unicode(boardid))
__M_writer(str(boardid))
__M_writer(u'">http://192.168.1.13:8080/board/')
__M_writer(unicode(boardid))
__M_writer(str(boardid))
__M_writer(u'</a></p>\n </section>\n </article>\n\n </div> <!-- #main -->\n </div> <!-- #main-container -->\n\n <div class="footer-container">\n <footer class="wrapper">\n <p>&copy; 2014 | <a href="http://www.defuze.org/">Sylvain Hellegouarch</a></p>\n </footer>\n </div>\n\n <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>\n <script type="application/javascript" src="/static/vendors/initializr/js/plugins.js"></script>\n </body>\n</html>\n')
return ''
finally:
Expand Down
5 changes: 4 additions & 1 deletion test/test_cherrypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import time
import unittest

from mock import MagicMock, call
try:
from unittest.mock import MagicMock, call
except ImportError:
from mock import MagicMock, call

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
Expand Down
6 changes: 4 additions & 2 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import socket
import time
import unittest

from mock import MagicMock, patch
try:
from unittest.mock import MagicMock, patch
except ImportError:
from mock import MagicMock, patch

from ws4py import WS_KEY
from ws4py.exc import HandshakeError
Expand Down
5 changes: 4 additions & 1 deletion test/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
except ImportError:
from itertools import zip_longest

from mock import MagicMock, call, patch
try:
from unittest.mock import MagicMock, call, patch
except ImportError:
from mock import MagicMock, call, patch

from ws4py.manager import WebSocketManager, SelectPoller,\
EPollPoller
Expand Down
9 changes: 7 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# -*- coding: utf-8 -*-
import unittest

try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock

from ws4py import format_addresses
from ws4py.websocket import WebSocket
from mock import MagicMock


class WSUtilities(unittest.TestCase):
def test_format_address(self):
Expand All @@ -14,7 +19,7 @@ def test_format_address(self):

log = format_addresses(ws)
self.assertEqual(log, "[Local => 127.0.0.1:52300 | Remote => 127.0.0.1:4800]")

if __name__ == '__main__':
suite = unittest.TestSuite()
loader = unittest.TestLoader()
Expand Down
5 changes: 4 additions & 1 deletion test/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import socket
import struct

from mock import MagicMock, call, patch
try:
from unittest.mock import MagicMock, call, patch
except ImportError:
from mock import MagicMock, call, patch

from ws4py.framing import Frame, \
OPCODE_CONTINUATION, OPCODE_TEXT, \
Expand Down
2 changes: 1 addition & 1 deletion ws4py/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def ord(c):
else:
py3k = False
from urlparse import urlsplit
range = xrange
range = xrange # noqa: F821
unicode = unicode
basestring = basestring
ord = ord
Expand Down
2 changes: 1 addition & 1 deletion ws4py/server/cherrypyserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,4 @@ def index(self):
cherrypy.log("Handler created: %s" % repr(cherrypy.request.ws_handler))

cherrypy.quickstart(Root(), '/', config={'/': {'tools.websocket.on': True,
'tools.websocket.handler_cls': EchoWebSocketHandler}})
'tools.websocket.handler_cls': EchoWebSocketHandler}}) # noqa: F821

0 comments on commit 22f8bb7

Please sign in to comment.