Skip to content

Commit

Permalink
Use logging in tests (#117)
Browse files Browse the repository at this point in the history
 * Set up in base of tests
 * Convert `print_*` to proper log statements
 * Fix broken intents tests message
  • Loading branch information
declension committed Jan 2, 2019
1 parent c0ee1a4 commit be1f824
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
16 changes: 16 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
#
# Copyright 2018-2019 Nick Boultbee
# This file is part of squeeze-alexa.
#
# squeeze-alexa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# See LICENSE for full license

from logging import basicConfig, INFO

LOG_FORMAT = "%(asctime)s %(levelname)-7s [%(name)-20s] %(message)s"
basicConfig(level=INFO, format=LOG_FORMAT, datefmt="%H:%M:%S")
7 changes: 4 additions & 3 deletions tests/intents_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# (at your option) any later version.
#
# See LICENSE for full license

from logging import getLogger
from unittest import TestCase

import os
Expand All @@ -19,10 +19,11 @@
from squeezealexa.alexa.intents import Audio, Power
from squeezealexa.main import handler, SqueezeAlexa
from squeezealexa.squeezebox.server import Server
from squeezealexa.utils import print_d
from tests.transport.fake_transport import FakeTransport
from tests.utils import ROOT

log = getLogger(__name__)

INTENTS_V0_PATH = os.path.join(ROOT,
'metadata/intents/v0/intents.json')

Expand Down Expand Up @@ -54,7 +55,7 @@ def test_all_handler(self):
server = Server(transport=fake_output)
alexa = SqueezeAlexa(server=server)
for name, func in handler._handlers.items():
print_d(">>> Testing function() <<<", function=func.__name__)
log.info(">>> Testing %s <<<", func.__name__)
session = {'sessionId': None}
intent = {'requestId': 'abcd', 'slots': {}}
raw = func(alexa, intent, session, None)
Expand Down
12 changes: 6 additions & 6 deletions tests/transport/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
from os.path import join
from socketserver import TCPServer, BaseRequestHandler

from squeezealexa.utils import print_d
from tests.utils import TEST_DATA_DIR

from logging import getLogger
log = getLogger(__name__)


class CertFiles:
CERT_AND_KEY = join(TEST_DATA_DIR, 'cert-and-key.pem')
Expand All @@ -37,7 +39,7 @@ def handle(self):
except UnicodeDecodeError:
data = "(invalid)"
response = response_for(data)
print_d("> \"%s\"\n%s" % (data.strip(), response))
log.debug('"%s" -> "%s"', data.strip(), response.replace('\n', '\\n'))
self.request.sendall(response.encode('utf-8'))


Expand All @@ -59,16 +61,14 @@ def port(self):

def __enter__(self):
self.socket.settimeout(3)
print_d("Creating test TCP server")
log.info("Starting test TCP server")
self.thread = threading.Thread(target=self.serve_forever)

print_d("Starting test server")
self.thread.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.shutdown()
print_d("Destroyed test server")
log.info("Destroyed test server")
self.thread.join(1)


Expand Down
7 changes: 5 additions & 2 deletions tests/transport/fake_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#
# See LICENSE for full license

from logging import getLogger

from squeezealexa.transport.base import Transport
from squeezealexa.utils import print_d

log = getLogger(__name__)

REAL_FAVES = """title%3AFavorites id%3A0
name%3AChilled%20Jazz type%3Aaudio
Expand Down Expand Up @@ -70,7 +73,7 @@ def communicate(self, data, wait=True):
pid=self.player_id))

elif ' status ' in stripped:
print_d("Faking player status.")
log.debug("Faking player status.")
return stripped + self._status
elif 'login ' in stripped:
return 'login %s ******' % stripped.split()[1].replace(' ', '%20')
Expand Down
4 changes: 1 addition & 3 deletions tests/transport/mqtt_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from _ssl import PROTOCOL_TLSv1_2
from asyncio import ensure_future, IncompleteReadError
from datetime import datetime
from logging import basicConfig, getLogger, INFO
from logging import getLogger
from threading import Thread
from time import time

Expand Down Expand Up @@ -44,8 +44,6 @@
}
}

LOG_FORMAT = "[%(asctime)s] %(levelname)-7s [%(name)-20s] %(message)s"
basicConfig(level=INFO, format=LOG_FORMAT)
log = getLogger("tests")


Expand Down
8 changes: 6 additions & 2 deletions tests/transport/test_ssl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2017-18 Nick Boultbee
# Copyright 2017-19 Nick Boultbee
# This file is part of squeeze-alexa.
#
# squeeze-alexa is free software: you can redistribute it and/or modify
Expand All @@ -10,8 +10,9 @@
#
# See LICENSE for full license

from _socket import socket
from logging import getLogger
from socket import error as SocketError
from socket import socket
from unittest import TestCase

import pytest
Expand All @@ -21,6 +22,8 @@
from tests.transport.base import ServerResource, TimeoutServer, CertFiles, \
response_for

log = getLogger("tests")


class FailingSocket(socket):

Expand All @@ -29,6 +32,7 @@ def __init__(self, *args, **kwargs):
self.closed = False

def sendall(self, data, flags=None):
log.info("Failing send")
raise SocketError()

def close(self):
Expand Down

0 comments on commit be1f824

Please sign in to comment.