Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
renamed - fixes #123
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekziade committed Aug 21, 2013
1 parent 01fafeb commit c1b2362
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 59 deletions.
48 changes: 6 additions & 42 deletions loads/case.py
@@ -1,8 +1,9 @@
import unittest
import functools

from requests.adapters import HTTPAdapter

from loads.measure import Session, TestApp
from loads.results import LoadsTestResult, UnitTestTestResult


class FakeTestApp(object):
Expand Down Expand Up @@ -47,7 +48,7 @@ def __init__(self, test_name, test_result=None, config=None):
self._loads_status = None

def defaultTestResult(self):
return DefaultTestResult()
return LoadsTestResult()

def incr_counter(self, name):
self._test_result.incr_counter(self, self._loads_status, name)
Expand All @@ -72,52 +73,15 @@ def tearDown(self):
def run(self, result=None, loads_status=None):
if (loads_status is not None
and result is None
and not isinstance(self._test_result, TestResultProxy)):
result = TestResultProxy(loads_status, self._test_result)
and not isinstance(self._test_result, LoadsTestResult)):
result = LoadsTestResult(loads_status, self._test_result)

if loads_status is not None:
self._loads_status = self.session.loads_status = loads_status

return super(TestCase, self).run(result)


class TestResultProxy(object):

def __init__(self, loads_status, result):
self.result = result
self.loads_status = loads_status

def __getattribute__(self, name):
result = super(TestResultProxy, self).__getattribute__('result')
attr = getattr(result, name)
if name in ('startTest', 'stopTest', 'addSuccess', 'addException',
'addError', 'addFailure', 'incr_counter'):
status = (super(TestResultProxy, self).
__getattribute__('loads_status'))
return functools.partial(attr, loads_status=status)
return attr


class DefaultTestResult(unittest.TestResult):
def startTest(self, test, *args, **kw):
unittest.TestResult.startTest(self, test)

def stopTest(self, test, *args, **kw):
unittest.TestResult.stopTest(self, test)

def addError(self, test, exc_info, *args, **kw):
unittest.TestResult.addError(self, test, exc_info)

def addFailure(self, test, exc_info, *args, **kw):
unittest.TestResult.addFailure(self, test, exc_info)

def addSuccess(self, test, *args, **kw):
unittest.TestResult.addSuccess(self, test)

def incr_counter(self, test, *args, **kw):
pass


def _patching():
# patching nose if present
try:
Expand Down Expand Up @@ -168,7 +132,7 @@ def addSuccess(self, test, *args, **kw):
# patch unittest TestResult object
try:
import unittest2.runner
unittest2.runner.TextTestResult = DefaultTestResult
unittest2.runner.TextTestResult = UnitTestTestResult
except ImportError:
pass

Expand Down
4 changes: 2 additions & 2 deletions loads/output/std.py
Expand Up @@ -3,7 +3,7 @@
import traceback
from collections import defaultdict

from loads.relay import ZMQRelay
from loads.results import ZMQTestResult


def get_terminal_width(fd=1):
Expand Down Expand Up @@ -117,7 +117,7 @@ def _print_tb(self, data):
sys.stderr.write(" %s: %s" % (name, exc))

def refresh(self, run_id=None):
if isinstance(self.results, ZMQRelay):
if isinstance(self.results, ZMQTestResult):
return
self._duration_progress(run_id)

Expand Down
4 changes: 4 additions & 0 deletions loads/results/__init__.py
@@ -0,0 +1,4 @@
from loads.results.adapter import LoadsTestResult # NOQA
from loads.results._unittest import UnitTestTestResult # NOQA
from loads.results.zmqrelay import ZMQTestResult # NOQA
from loads.results.base import TestResult, LazyTestResult # NOQA
26 changes: 26 additions & 0 deletions loads/results/_unittest.py
@@ -0,0 +1,26 @@
import unittest


class UnitTestTestResult(unittest.TestResult):
"""Used to make Loads test cases compatible with unittest
This class will ignore the extra options used by Loads, so
tests written for loads can also be run in Nose or Unittest(2)
"""
def startTest(self, test, *args, **kw):
unittest.TestResult.startTest(self, test)

def stopTest(self, test, *args, **kw):
unittest.TestResult.stopTest(self, test)

def addError(self, test, exc_info, *args, **kw):
unittest.TestResult.addError(self, test, exc_info)

def addFailure(self, test, exc_info, *args, **kw):
unittest.TestResult.addFailure(self, test, exc_info)

def addSuccess(self, test, *args, **kw):
unittest.TestResult.addSuccess(self, test)

def incr_counter(self, test, *args, **kw):
pass
21 changes: 21 additions & 0 deletions loads/results/adapter.py
@@ -0,0 +1,21 @@
import functools


class LoadsTestResult(object):
"""Used to make unitest calls compatible with Loads.
This class will add to the API calls the loads_status option Loads uses.
"""
def __init__(self, loads_status, result):
self.result = result
self.loads_status = loads_status

def __getattribute__(self, name):
klass = super(LoadsTestResult, self)
result = klass.__getattribute__('result')
attr = getattr(result, name)
if name in ('startTest', 'stopTest', 'addSuccess', 'addException',
'addError', 'addFailure', 'incr_counter'):
status = klass.__getattribute__('loads_status')
return functools.partial(attr, loads_status=status)
return attr
File renamed without changes.
2 changes: 1 addition & 1 deletion loads/relay.py → loads/results/zmqrelay.py
Expand Up @@ -7,7 +7,7 @@
from loads.util import DateTimeJSONEncoder


class ZMQRelay(object):
class ZMQTestResult(object):
"""Relays all the method calls to a zmq endpoint"""

def __init__(self, args):
Expand Down
2 changes: 1 addition & 1 deletion loads/runners/distributed.py
Expand Up @@ -6,7 +6,7 @@
from loads.runners.local import LocalRunner
from loads.transport.util import DEFAULT_PUBLISHER
from loads.util import logger, split_endpoint
from loads.test_result import TestResult, LazyTestResult
from loads.results import TestResult, LazyTestResult
from loads.transport.client import Client


Expand Down
5 changes: 2 additions & 3 deletions loads/runners/local.py
Expand Up @@ -6,8 +6,7 @@
import gevent

from loads.util import resolve_name, glob, logger
from loads.test_result import TestResult
from loads.relay import ZMQRelay
from loads.results import ZMQTestResult, TestResult
from loads.output import create_output


Expand Down Expand Up @@ -83,7 +82,7 @@ def __init__(self, args):

# If we are in slave mode, set the test_result to a 0mq relay
if self.slave:
self._test_result = ZMQRelay(self.args)
self._test_result = ZMQTestResult(self.args)

# The normal behavior is to collect the results locally.
else:
Expand Down
6 changes: 4 additions & 2 deletions loads/tests/test_case.py
@@ -1,6 +1,8 @@
import unittest2
import mock
from loads.case import TestCase, DefaultTestResult

from loads.case import TestCase
from loads.results import UnitTestTestResult


class _MyTestCase(TestCase):
Expand All @@ -17,7 +19,7 @@ def test_three(self):
class TestTestCase(unittest2.TestCase):

def test_fake(self):
results = DefaultTestResult()
results = UnitTestTestResult()
loads_status = 1, 1, 1, 1

case = _MyTestCase('test_one', test_result=results)
Expand Down
2 changes: 1 addition & 1 deletion loads/tests/test_output.py
Expand Up @@ -14,7 +14,7 @@
from loads import output

from loads.tests.support import get_tb, hush
from loads.test_result import Hit, Test
from loads.results.base import Hit, Test


TIME1 = datetime.datetime(2013, 5, 14, 0, 51, 8)
Expand Down
Expand Up @@ -2,7 +2,7 @@
import traceback
from StringIO import StringIO

from loads.relay import ZMQRelay
from loads.results import ZMQTestResult
from loads.tests.support import get_tb, hush

import mock
Expand All @@ -11,13 +11,13 @@
class TestZmqRelay(TestCase):

def setUp(self):
self._old_init_socket = ZMQRelay._init_socket
ZMQRelay._init_socket = mock.Mock()
self.relay = ZMQRelay(args={})
self._old_init_socket = ZMQTestResult._init_socket
ZMQTestResult._init_socket = mock.Mock()
self.relay = ZMQTestResult(args={})
self.relay.push = mock.Mock()

def tearDown(self):
ZMQRelay._init_socket = self._old_init_socket
ZMQTestResult._init_socket = self._old_init_socket

def test_add_success(self):
self.relay.addSuccess(mock.sentinel.test, mock.sentinel.loads_status)
Expand Down
3 changes: 2 additions & 1 deletion loads/tests/test_test_result.py
@@ -1,9 +1,10 @@
from loads.test_result import TestResult, Hit, Test
from unittest2 import TestCase
from datetime import datetime, timedelta

from mock import Mock

from loads.results.base import TestResult, Hit, Test


TIME1 = datetime(2013, 5, 14, 0, 51, 8)
TIME2 = datetime(2013, 5, 14, 0, 53, 8)
Expand Down
2 changes: 1 addition & 1 deletion loads/transport/brokerctrl.py
Expand Up @@ -9,7 +9,7 @@
from loads.db import get_database
from loads.transport.client import DEFAULT_TIMEOUT_MOVF
from loads.util import logger, resolve_name
from loads.test_result import LazyTestResult
from loads.results import LazyTestResult


class NotEnoughWorkersError(Exception):
Expand Down

0 comments on commit c1b2362

Please sign in to comment.