示例#1
0
    def test_context(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[]')
        requests_post_mock.return_value = response

        url = 'http://example.com/'
        query = 'SELECT * FROM table'
        context = {'source': 'unittest'}

        cursor = Cursor(url, user=None, password=None, context=context)
        cursor.execute(query)

        requests_post_mock.assert_called_with(
            'http://example.com/',
            auth=None,
            stream=True,
            headers={'Content-Type': 'application/json'},
            json={
                'query': query,
                'context': context,
                'header': False
            },
            verify=True,
            proxies=None,
        )
示例#2
0
    def test_context(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b"[]")
        requests_post_mock.return_value = response

        url = "http://example.com/"
        query = "SELECT * FROM table"
        context = {"source": "unittest"}

        cursor = Cursor(url, user=None, password=None, context=context)
        cursor.execute(query)

        requests_post_mock.assert_called_with(
            "http://example.com/",
            auth=None,
            stream=True,
            headers={"Content-Type": "application/json"},
            json={
                "query": query,
                "context": context,
                "header": False
            },
            verify=True,
            cert=None,
            proxies=None,
        )
示例#3
0
    def test_execute_empty_result(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[]')
        requests_post_mock.return_value = response

        cursor = Cursor('http://example.com/')
        cursor.execute('SELECT * FROM table')
        result = cursor.fetchall()
        expected = []
        self.assertEquals(result, expected)
示例#4
0
    def test_execute_empty_result(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[]')
        requests_post_mock.return_value = response

        cursor = Cursor('http://example.com/')
        cursor.execute('SELECT * FROM table')
        result = cursor.fetchall()
        expected = []
        self.assertEquals(result, expected)
示例#5
0
    def test_execute(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(
            b'[{"name": "alice"}, {"name": "bob"}, {"name": "charlie"}]')
        requests_post_mock.return_value = response
        Row = namedtuple("Row", ["name"])

        cursor = Cursor("http://example.com/")
        cursor.execute("SELECT * FROM table")
        result = cursor.fetchall()
        expected = [Row(name="alice"), Row(name="bob"), Row(name="charlie")]
        self.assertEquals(result, expected)
示例#6
0
    def test_header_true(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[{"name": null}, {"name": "alice"}]')
        requests_post_mock.return_value = response
        Row = namedtuple('Row', ['name'])

        url = 'http://example.com/'
        query = 'SELECT * FROM table'

        cursor = Cursor(url, header=True)
        cursor.execute(query)
        result = cursor.fetchall()
        self.assertEquals(result, [Row(name='alice')])
        self.assertEquals(cursor.description, [('name', None)])
示例#7
0
    def test_header_true(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[{"name": null}, {"name": "alice"}]')
        requests_post_mock.return_value = response
        Row = namedtuple('Row', ['name'])

        url = 'http://example.com/'
        query = 'SELECT * FROM table'

        cursor = Cursor(url, header=True)
        cursor.execute(query)
        result = cursor.fetchall()
        self.assertEquals(result, [Row(name='alice')])
        self.assertEquals(cursor.description, [('name', None)])
示例#8
0
    def test_names_with_underscores(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[{"_name": null}, {"_name": "alice"}]')
        requests_post_mock.return_value = response
        Row = namedtuple("Row", ["_name"], rename=True)

        url = "http://example.com/"
        query = "SELECT * FROM table"

        cursor = Cursor(url, header=True)
        cursor.execute(query)
        result = cursor.fetchall()
        self.assertEquals(result, [Row(_0="alice")])
        self.assertEquals(cursor.description, [("_name", None)])
示例#9
0
    def test_execute(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[{"name": "alice"}, {"name": "bob"}, {"name": "charlie"}]')
        requests_post_mock.return_value = response
        Row = namedtuple('Row', ['name'])

        cursor = Cursor('http://example.com/')
        cursor.execute('SELECT * FROM table')
        result = cursor.fetchall()
        expected = [
            Row(name='alice'),
            Row(name='bob'),
            Row(name='charlie'),
        ]
        self.assertEquals(result, expected)
示例#10
0
    def test_execute(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[{"name": "alice"}, {"name": "bob"}, {"name": "charlie"}]')
        requests_post_mock.return_value = response
        Row = namedtuple('Row', ['name'])

        cursor = Cursor('http://example.com/')
        cursor.execute('SELECT * FROM table')
        result = cursor.fetchall()
        expected = [
            Row(name='alice'),
            Row(name='bob'),
            Row(name='charlie'),
        ]
        self.assertEquals(result, expected)
示例#11
0
    def test_header_false(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[{"name": "alice"}]')
        requests_post_mock.return_value = response
        Row = namedtuple("Row", ["name"])

        url = "http://example.com/"
        query = "SELECT * FROM table"

        cursor = Cursor(url, header=False)
        cursor.execute(query)
        result = cursor.fetchall()
        self.assertEquals(result, [Row(name="alice")])

        self.assertEquals(cursor.description,
                          [("name", 1, None, None, None, None, True)])
示例#12
0
    def test_context(self, requests_post_mock):
        response = Response()
        response.status_code = 200
        response.raw = BytesIO(b'[]')
        requests_post_mock.return_value = response

        url = 'http://example.com/'
        query = 'SELECT * FROM table'
        context = {'source': 'unittest'}

        cursor = Cursor(url, user=None, password=None, context=context)
        cursor.execute(query)

        requests_post_mock.assert_called_with(
            'http://example.com/',
            auth=None,
            stream=True,
            headers={'Content-Type': 'application/json'},
            json={'query': query, 'context': context, 'header': False},
        )