From 3ca96edaf1f6b2b88b84f39eab5b9dfc740b9222 Mon Sep 17 00:00:00 2001 From: hyades Date: Thu, 8 Aug 2013 19:54:44 +0530 Subject: [PATCH] video_port unittests in helper.py --- python-api/gstswitch/exception.py | 5 ++++- python-api/gstswitch/helpers.py | 24 +++++++++++++++++++++ python-api/gstswitch/test_helpers.py | 31 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 python-api/gstswitch/test_helpers.py diff --git a/python-api/gstswitch/exception.py b/python-api/gstswitch/exception.py index 15eefe9..2841968 100644 --- a/python-api/gstswitch/exception.py +++ b/python-api/gstswitch/exception.py @@ -1,4 +1,7 @@ -__all__ = ['BaseError', 'PathError', 'ServerProcessError', 'ConnectionError'] +__all__ = [ + 'BaseError', 'PathError', 'ServerProcessError', 'ConnectionError', + 'ConnectionReturnError', 'RangeError', + ] class BaseError(Exception): diff --git a/python-api/gstswitch/helpers.py b/python-api/gstswitch/helpers.py index 6ad800f..00c15fa 100644 --- a/python-api/gstswitch/helpers.py +++ b/python-api/gstswitch/helpers.py @@ -1,4 +1,5 @@ from testsource import VideoSrc, Preview +from exception import RangeError __all__ = ["TestSources", "PreviewSinks"] @@ -18,6 +19,29 @@ def __init__(self, video_port): self.running_tests = [] self.video_port = video_port + @property + def video_port(self): + return self._video_port + + @video_port.setter + def video_port(self, video_port): + if not video_port: + raise ValueError("video_port: '{0}' cannot be blank" + .format(video_port)) + else: + try: + i = int(video_port) + if i < 1 or i > 65535: + raise RangeError('video_port must be in range 1 to 65535') + else: + self._video_port = video_port + except TypeError: + raise TypeError("video_port must be a string or number," + "not, '{0}'".format(type(video_port))) + except ValueError: + raise TypeError("Port must be a valid number") + + def new_test_video(self, width=300, height=200, diff --git a/python-api/gstswitch/test_helpers.py b/python-api/gstswitch/test_helpers.py new file mode 100644 index 0000000..8fd29ca --- /dev/null +++ b/python-api/gstswitch/test_helpers.py @@ -0,0 +1,31 @@ +from helpers import TestSources, PreviewSinks +from exception import RangeError +import pytest +from mock import Mock, patch + + +class TestTestSourcesVideoPort(object): + + def test_blank(self): + tests = ['', None, [], {}] + for test in tests: + with pytest.raises(ValueError): + TestSources(video_port=test) + + def test_range(self): + tests = [-100, 1e7, 65536] + for test in tests: + with pytest.raises(RangeError): + TestSources(video_port=test) + + def test_invalid(self): + tests = [[1, 2, 3, 4], {1: 2, 2: 3}, '1e10'] + for test in tests: + with pytest.raises(TypeError): + TestSources(video_port=test) + + def test_normal(self): + tests = [1, 65535, 1000] + for test in tests: + src = TestSources(video_port=test) + assert src.video_port == test