Skip to content

Commit

Permalink
Fixed up some issues with older versions in the tests. Logging works …
Browse files Browse the repository at this point in the history
…better
  • Loading branch information
MicroTransactionsMatterToo committed Aug 28, 2017
1 parent 8fe1721 commit bf4b910
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
24 changes: 21 additions & 3 deletions tests/__init__.py
Expand Up @@ -23,7 +23,10 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import logging
import time
from sys import version_info
from os import mkdir

try:
Expand All @@ -32,6 +35,16 @@
pass

fmt = logging.Formatter("[%(module)s] || %(asctime)s - %(levelname)s : %(module)s.%(funcName)s || %(message)s")


class ExceptionFilter(logging.Filter):
def filter(self, record: logging.LogRecord):
if record.msg.__repr__().__contains__("_AssertRaisesContext"):
return False
else:
return True


ch = logging.StreamHandler()

ch.setLevel(logging.CRITICAL)
Expand All @@ -49,7 +62,12 @@
ch.setFormatter(logging.Formatter("%(message)s"))
fh.setFormatter(logging.Formatter("%(message)s"))

logger.info("\033[1mTESTING STARTED\033[0m")

logger.info(
"\033[1mTESTING STARTED -- Python Version: {pver.major}.{pver.minor}.{pver.micro} -- Time: {time}\033[0m".format(
pver = version_info,
time = time.asctime()
))
fh.setFormatter(fmt)
ch.setFormatter(fmt)
ch.setFormatter(fmt)
fh.addFilter(ExceptionFilter())
ch.addFilter(ExceptionFilter())
32 changes: 29 additions & 3 deletions tests/test_events.py
Expand Up @@ -30,6 +30,8 @@

logger = logging.getLogger(__name__)

logger.info("test_events tests started")


class TestNoteOn(TestCase):
def test_validate(self):
Expand All @@ -41,8 +43,9 @@ def test_validate(self):
"Generic MIDI NoteOn message shouldn't have validated, but did. Value was 0x{:x}".format(
test_val)
)

logger.info("NoteOn .valid test passed")
def test_constructor(self):
logger.info("Starting NoteOn constructor tests")
# Test constructor of generic version
test_val = NoteOn(0x900000)
match_val = {
Expand All @@ -55,23 +58,46 @@ def test_constructor(self):
self.assertEqual(vars(test_val),
match_val, "MIDI NoteOn constructed from value 0x{:X} is incorrect".format(0x900000)
)
# --- Exception Testing --- #
logger.info("Starting NoteOn constructor exception tests")
# Test Length Exceptions
with self.assertRaises(LengthError,
msg="NoteOn did not raise LengthError when given value 0x123001929391923919"
) as exc:
NoteOn(0x123001929391923919)

logger.exception(exc)
with self.assertRaises(LengthError,
msg="NoteOn did not raise LengthError when given value 0x1"
) as exc:
NoteOn(0x1)

logger.exception(exc)
# Test validation exceptions
with self.assertRaises(ValueError,
msg="NoteOn given invalid data did not raise ValueError. Value was 0x290011"
) as exc:
NoteOn(0x290011)
logger.exception(exc)
with self.assertRaises(ValueError,
msg="NoteOn given invalid data but did not raise ValueError. Value was 0x999999"
) as exc:
NoteOn(0x899999)
logger.exception(exc)

# --- Parsing Testing --- #
logger.info("Starting NoteOn constructor parsing test")
for channel in range(0, 9):
channel_num = 0x900000 + (channel << 16)
logger.debug("Testing constructor with value {:x}".format(channel_num))
obj = NoteOn(channel_num)
match_val = {
'channel_number': channel,
'note_name': 'C',
'note_number': 0,
'note_velocity': 0,
'raw_data': channel_num
}
self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOn with channel value: {}, "
"parsing failed".format(channel >> 16))

if __name__ == "__main__":
nose2.main()
6 changes: 2 additions & 4 deletions tox.ini
Expand Up @@ -3,8 +3,6 @@ envlist = py30, py31, py33, py34, py35, py36, py37


[testenv]
depends =
nose2
usedevelop=True
command =
deps = nose2
commands =
nose2

0 comments on commit bf4b910

Please sign in to comment.