# -*- coding: utf-8 -*-
#
# © 2011 SimpleGeo, Inc All rights reserved.
# Author: Ian Eure <ian@simplegeo.com>
#

"""Tests for connection code."""

import unittest
import random
import time
from collections import defaultdict

from mock import Mock

try:
    from mock import patch
    patch_object = patch.object
except AttributeError:
    from mock import patch_object # < 0.7.0

import robustisoty as conns


class ThresholdSuppressTest(unittest.TestCase):

    def setUp(self):
        self.exceptions = (ValueError,)
        self.interval = 60
        self.threshold = 3
        self.manager = conns.make_suppress(
            self.exceptions, self.interval, self.threshold)

    def test_ignores_other_exceptions(self):
        try:
            with self.manager():
                raise KeyError("Whops.")
            self.fail("KeyError should have passed through.")
        except KeyError:
            pass

    def test_suppresses_exceptions(self):
        with self.manager():
            raise random.choice(self.exceptions)("Whops.")

    def test_raises_exceptions_over_thresh(self):
        ts = time.time()
        times = 0
        ex_type = random.choice(self.exceptions)
        try:
            with patch_object(conns.time, 'time') as time_:
                time_.return_value = ts
                for x in range(self.threshold):
                    with self.manager():
                        raise ex_type("Whops.")
                    times += 1
            self.fail(
                "Should have passed %s through after exceeding thresh %d" % (
                    ex_type, self.threshold))
        except ex_type:
            pass


class TeeTest(unittest.TestCase):

    def setUp(self):
        self.retval = "Hi"
        self.fs = [Mock(), Mock()]

    def test_functions_all_success(self):
        for f in self.fs:
            f.return_value = self.retval

        fproxy = conns.Tee(self.fs)
        self.assertEqual(fproxy(), self.retval)

    def test_objects_all_success(self):
        for f in self.fs:
            f.foo.return_value = self.retval

        fproxy = conns.Tee(self.fs)
        self.assertEqual(fproxy.foo(), self.retval)

    def test_objects_first_returns_none(self):
        self.fs[0].foo.return_value = None
        self.fs[1].foo.return_value = self.retval

        fproxy = conns.Tee(self.fs)
        self.assertEqual(fproxy.foo(), None)

    def test_attrs_all_success(self):
        for f in self.fs:
            f.foo = self.retval

        fproxy = conns.Tee(self.fs)
        self.assertEqual(fproxy.foo, self.retval)

    def test_attrs_first_fails(self):
        class Fake(object):
            def __getattr__(self, *args):
                raise Exception()

        self.fs[0] = Fake()
        self.fs[1].foo = self.retval

        self.assertEqual(conns.Tee(self.fs).foo, self.retval)

    def test_attrs_second_fails(self):
        class Fake(object):
            def __getattr__(self, *args):
                raise Exception()

        self.fs[0].foo = self.retval
        self.fs[1] = Fake()
        self.assertEqual(conns.Tee(self.fs).foo, self.retval)

    def test_attrs_all_fails(self):
        class Fake(object):
            def __getattr__(self, *args):
                raise Exception()

        self.assertRaises(Exception, getattr, conns.Tee([Fake(), Fake()]),
                          'foo')

    def test_first_fails(self):
        self.fs[0].foo.side_effect = Exception
        self.fs[1].foo.return_value = self.retval
        self.assertEqual(conns.Tee(self.fs).foo(), self.retval)

    def test_second_fails(self):
        self.fs[0].foo.return_value = self.retval
        self.fs[1].foo.side_effect = Exception
        self.assertEqual(conns.Tee(self.fs).foo(), self.retval)

    def test_all_fail(self):
        for f in self.fs:
            f.foo.side_effect = Exception

        self.assertRaises(Exception, conns.Tee(self.fs).foo)

    def test_dicts_both_succeed(self):
        self.assertEqual(conns.Tee([{'foo': self.retval},
                                    {'foo': self.retval}])['foo'],
                         self.retval)


if __name__ == '__main__':
    unittest.main()

@@ -1658,2475 +1658,8 @@
 s%0A%0A%0A
-class TeeTest(unittest.TestCase):%0A%0A    def setUp(self):%0A        self.retval = %22Hi%22%0A        self.fs = %5BMock(), Mock()%5D%0A%0A    def test_functions_all_success(self):%0A        for f in self.fs:%0A            f.return_value = self.retval%0A%0A        fproxy = conns.Tee(self.fs)%0A        self.assertEqual(fproxy(), self.retval)%0A%0A    def test_objects_all_success(self):%0A        for f in self.fs:%0A            f.foo.return_value = self.retval%0A%0A        fproxy = conns.Tee(self.fs)%0A        self.assertEqual(fproxy.foo(), self.retval)%0A%0A    def test_objects_first_returns_none(self):%0A        self.fs%5B0%5D.foo.return_value = None%0A        self.fs%5B1%5D.foo.return_value = self.retval%0A%0A        fproxy = conns.Tee(self.fs)%0A        self.assertEqual(fproxy.foo(), None)%0A%0A    def test_attrs_all_success(self):%0A        for f in self.fs:%0A            f.foo = self.retval%0A%0A        fproxy = conns.Tee(self.fs)%0A        self.assertEqual(fproxy.foo, self.retval)%0A%0A    def test_attrs_first_fails(self):%0A        class Fake(object):%0A            def __getattr__(self, *args):%0A                raise Exception()%0A%0A        self.fs%5B0%5D = Fake()%0A        self.fs%5B1%5D.foo = self.retval%0A%0A        self.assertEqual(conns.Tee(self.fs).foo, self.retval)%0A%0A    def test_attrs_second_fails(self):%0A        class Fake(object):%0A            def __getattr__(self, *args):%0A                raise Exception()%0A%0A        self.fs%5B0%5D.foo = self.retval%0A        self.fs%5B1%5D = Fake()%0A        self.assertEqual(conns.Tee(self.fs).foo, self.retval)%0A%0A    def test_attrs_all_fails(self):%0A        class Fake(object):%0A            def __getattr__(self, *args):%0A                raise Exception()%0A%0A        self.assertRaises(Exception, getattr, conns.Tee(%5BFake(), Fake()%5D),%0A                          'foo')%0A%0A    def test_first_fails(self):%0A        self.fs%5B0%5D.foo.side_effect = Exception%0A        self.fs%5B1%5D.foo.return_value = self.retval%0A        self.assertEqual(conns.Tee(self.fs).foo(), self.retval)%0A%0A    def test_second_fails(self):%0A        self.fs%5B0%5D.foo.return_value = self.retval%0A        self.fs%5B1%5D.foo.side_effect = Exception%0A        self.assertEqual(conns.Tee(self.fs).foo(), self.retval)%0A%0A    def test_all_fail(self):%0A        for f in self.fs:%0A            f.foo.side_effect = Exception%0A%0A        self.assertRaises(Exception, conns.Tee(self.fs).foo)%0A%0A    def test_dicts_both_succeed(self):%0A        self.assertEqual(conns.Tee(%5B%7B'foo': self.retval%7D,%0A                                    %7B'foo': self.retval%7D%5D)%5B'foo'%5D,%0A                         self.retval)%0A
 %0A%0Aif

Remove TeeTest.