Example #1
0
    def test_iter(self):
        page1 = Mock()
        page1.items = [1, 2, 3]
        page2 = Mock()
        page2.items = [4, 5, 6]
        page3 = Mock()
        page3.items = [7, 8]
        page4 = Mock()
        page4.items = [9]
        page1.next = page2
        page2.next = page3
        page3.next = page4
        page4.next = None

        with patch.object(Resource1.client, "_op") as _op:
            with patch.object(Resource1, "page_cls") as page_cls:
                page_cls.return_value = page1
                resp = _op.return_value
                resp.data = {}

                page_cls.return_value = page1
                uri = "/a/uri"
                pagination = wac.Pagination(Resource1, uri, 25, None)
                pages = [p for p in pagination]
                self.assertEqual([page1, page2, page3, page4], pages)

                page_cls.return_value = page2
                uri = "/a/uri"
                pagination = wac.Pagination(Resource1, uri, 25, None)
                pages = [p for p in pagination]
                self.assertEqual([page2, page3, page4], pages)
Example #2
0
    def test_links(self, Page):
        page1 = Mock()
        page1.items = [1, 2, 3]
        page2 = Mock()
        page2.items = [4, 5, 6]
        page3 = Mock()
        page3.items = [7, 8]
        page1.previous = None
        page1.next = page2
        page2.previous = page1
        page2.next = page3
        page3.previous = page2
        page3.next = None

        Page.return_value = page1
        uri = "/a/uri"
        pagination = wac.Pagination(Resource1, uri, 25, page1)
        self.assertEqual(pagination.current, page1)

        for expected_page in [page2, page3]:
            page = pagination.next()
            self.assertEqual(page, expected_page)
            self.assertEqual(pagination.current, expected_page)
        page = pagination.next()
        self.assertEqual(page, None)
        self.assertEqual(pagination.current, expected_page)

        for expected_page in [page2, page1]:
            page = pagination.previous()
            self.assertEqual(page, expected_page)
            self.assertEqual(pagination.current, expected_page)
        page = pagination.previous()
        self.assertEqual(page, None)
        self.assertEqual(pagination.current, expected_page)
Example #3
0
File: tests.py Project: msherry/wac
    def test_iter(self, Page):
        page1 = Mock()
        page1.items = [1, 2, 3]
        page2 = Mock()
        page2.items = [4, 5, 6]
        page3 = Mock()
        page3.items = [7, 8]
        page4 = Mock()
        page4.items = [9]
        page1.next = page2
        page2.next = page3
        page3.next = page4
        page4.next = None

        Page.return_value = page1
        uri = "/a/uri"
        pagination = wac.Pagination(None, uri, 25)
        pages = [p for p in pagination]
        self.assertEqual([page1, page2, page3, page4], pages)

        Page.return_value = page2
        uri = "/a/uri"
        pagination = wac.Pagination(None, uri, 25)
        pages = [p for p in pagination]
        self.assertEqual([page2, page3, page4], pages)
Example #4
0
    def test_timeout_retry_find(self):
        col_mock = Mock()
        col_mock.name = "asdf"
        doc_mock = MagicMock()
        doc_mock.__iter__.return_value = ["a", "b"]
        cur_mock = Mock()
        cur_mock.collection = col_mock
        cur_mock.next = MagicMock(side_effect=pymongo.errors.ExecutionTimeout("asdf"))
        find_raw = MagicMock(return_value=(cur_mock, Mock()))
        Citizen.find_raw = find_raw

        with self.assertRaises(pymongo.errors.ExecutionTimeout):
            Citizen.find({}, max_time_ms=None)
        with self.assertRaises(pymongo.errors.ExecutionTimeout):
            Citizen.find({}, max_time_ms=Citizen.MAX_TIME_MS - 1)
        with self.assertRaises(pymongo.errors.ExecutionTimeout):
            Citizen.find({}, max_time_ms=Citizen.MAX_TIME_MS)
        with self.assertRaises(pymongo.errors.ExecutionTimeout):
            Citizen.find({}, max_time_ms=Citizen.MAX_TIME_MS + 1)

        # should retry on the first two, should not retry on the last two
        self.assertEquals(len(find_raw.call_args_list), 6)

        _, a, _, b, c, d = find_raw.call_args_list

        self.assertEquals(a[1]["max_time_ms"], Citizen.RETRY_MAX_TIME_MS)
        self.assertEquals(b[1]["max_time_ms"], Citizen.RETRY_MAX_TIME_MS)
Example #5
0
def test_rset():
    """Population.rset()"""
    p = MockPopulation()
    rd = Mock()
    rnums = numpy.arange(p.size)
    rd.next = Mock(return_value=rnums)
    p.tset = Mock()
    p.rset("cm", rd)
    rd.next.assert_called_with(**{"mask_local": False, "n": p.size})
    call_args = p.tset.call_args
    assert_equal(call_args[0][0], "cm")
    assert_arrays_equal(call_args[0][1], rnums)
Example #6
0
    def test_first(self, _page):
        page1 = Mock(items=[1, 2, 3], total=8)
        page2 = Mock(items=[4, 5, 6], total=8)
        page3 = Mock(items=[7, 8], total=8)
        page1.previous = None
        page1.next = page2
        page2.previous = page1
        page2.next = page3
        page3.previous = page2
        page3.next = None

        def _page_patch(key, size=None, data=None):
            return [page1, page2, page3][key]

        _page.side_effect = _page_patch

        uri = "/ur/is"
        q = wac.Query(Resource1, uri, 3)
        expected_item = 1
        item = q.first()
        self.assertEqual(expected_item, item)
Example #7
0
    def test_iter(self):
        page1 = Mock()
        page1.items = [0, 1, 2, 3]
        page1.total = 10
        page2 = Mock()
        page2.items = [4, 5, 6, 7]
        page2.total = 10
        page3 = Mock()
        page3.items = [8, 9]
        page3.total = 10

        page1.next = page2
        page2.next = page3
        page3.next = None

        with patch("wac.Page") as Page:
            Page.side_effect = [page1, page1, page2, page3]
            uri = "/a/uri"
            q = wac.Query(None, uri, 4)
            vs = [v for v in q]
            self.assertEqual(range(10), vs)
Example #8
0
def test_randomize_weights():
    orig_len = common.Projection.__len__
    common.Projection.__len__ = Mock(return_value=42)
    p1 = MockPopulation()
    p2 = MockPopulation()
    prj = common.Projection(p1, p2, method=Mock())
    prj.set = Mock()
    rd = Mock()
    rd.next = Mock(return_value=777)
    prj.randomizeWeights(rd)
    rd.next.assert_called_with(len(prj))
    prj.set.assert_called_with("weight", 777)
    common.Projection.__len__ = orig_len
Example #9
0
    def test_all(self, _page):
        page1 = Mock(items=[1, 2, 3], total=8)
        page2 = Mock(items=[4, 5, 6], total=8)
        page3 = Mock(items=[7, 8], total=8)
        page1.previous = None
        page1.next = page2
        page2.previous = page1
        page2.next = page3
        page3.previous = page2
        page3.next = None

        def _page_patch(key, data=None):
            return [page1, page2, page3][key]

        _page.side_effect = _page_patch

        uri = "/ur/is"
        q = wac.Query(Resource1, uri, 3)
        expected_items = range(1, 9)
        items = q.all()
        self.assertEqual(expected_items, items)
        self.assertEqual(q.pagination.current, page1)
Example #10
0
    def test_iter(self):
        page1 = Mock()
        page1.items = [0, 1, 2, 3]
        page1.total = 10
        page2 = Mock()
        page2.items = [4, 5, 6, 7]
        page2.total = 10
        page3 = Mock()
        page3.items = [8, 9]
        page3.total = 10

        page1.next = page2
        page2.next = page3
        page3.next = None

        with patch.object(Resource1, "page_cls") as page_cls:
            with patch.object(Resource1.client, "_op") as _op:
                page_cls.side_effect = [page1, page1, page2, page3]
                resp = _op.return_value
                resp.data = {"_type": "page"}
                uri = "/a/uri"
                q = wac.Query(Resource1, uri, 4)
                vs = [v for v in q]
                self.assertEqual(range(10), vs)
Example #11
0
    def test_timeout_value_find(self):
        col_mock = Mock()
        col_mock.name = "asdf"
        doc_mock = MagicMock()
        doc_mock.__iter__.return_value = ["a", "b"]
        cur_mock = Mock()
        cur_mock.collection = col_mock
        cur_mock.next = MagicMock(side_effect=pymongo.errors.ExecutionTimeout("asdf"))
        find_raw = MagicMock(return_value=(cur_mock, Mock()))
        Citizen.find_raw = find_raw

        self.assertEquals([], Citizen.find({}, timeout_value=[]))
        self.assertEquals({}, Citizen.find({}, timeout_value={}))
        self.assertEquals(1, Citizen.find({}, timeout_value=1))
        self.assertEquals("asdf", Citizen.find({}, timeout_value="asdf"))
        with self.assertRaises(pymongo.errors.ExecutionTimeout):
            Citizen.find({})
Example #12
0
    def test_max_time_ms_find(self):
        col_mock = Mock()
        col_mock.name = "asdf"
        doc_mock = MagicMock()
        doc_mock.__iter__.return_value = ["a", "b"]
        cur_mock = Mock()
        cur_mock.collection = col_mock
        cur_mock.next = MagicMock(side_effect=[doc_mock])
        find_raw = MagicMock(return_value=(cur_mock, Mock()))
        Citizen.find_raw = find_raw

        Citizen.find({}, max_time_ms=None)
        Citizen.find({}, max_time_ms=0)
        Citizen.find({}, max_time_ms=-1)
        Citizen.find({}, max_time_ms=1000)

        a, b, c, d = find_raw.call_args_list
        self.assertEquals(a[1]["max_time_ms"], None)
        self.assertEquals(b[1]["max_time_ms"], 0)
        self.assertEquals(c[1]["max_time_ms"], -1)
        self.assertEquals(d[1]["max_time_ms"], 1000)