aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/fetch/db_fetch/mock_cursor.py
blob: 10c67e19bce7225065796c15fefd2569b5bcbc87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################


def require_open(method):
    def wrapped(self, *args, **kwargs):
        if self.closed:
            raise ValueError("Cursor is closed")
        return method(self, *args, **kwargs)
    return wrapped


class MockCursor:

    def __init__(self, result):
        self.result = result
        self.current = 0
        self.closed = False

    @require_open
    def __next__(self):
        if self.current < len(self.result):
            nxt = self.result[self.current]
            self.current += 1
            return nxt
        else:
            raise StopIteration

    @require_open
    def __iter__(self):
        return self

    @require_open
    def fetchall(self):
        return self.result

    def close(self):
        self.closed = True