예제 #1
0
def test_do_execute_wait():
    td = cursor.Cursor(
        mock.MagicMock(),
        db="sample_datasets",
        wait_interval=5,
        wait_callback=mock.MagicMock(),
    )
    td._executed = "42"
    td._check_executed = mock.MagicMock(return_value=True)
    td.api.job_status = mock.MagicMock(side_effect=["queued", "running", "success"])
    td.api.job_result = mock.MagicMock(
        return_value=[["foo", 1], ["bar", 1], ["baz", 2]]
    )
    td.api.show_job = mock.MagicMock(
        return_value={"hive_result_schema": [["col0", "varchar"], ["col1", "long"]]}
    )
    with mock.patch("time.sleep") as t_sleep:
        td._do_execute()
        t_sleep.assert_called_with(5)
        assert td.wait_callback.called
        assert td._check_executed.called
        td.api.job_status.assert_called_with("42")
        td.api.job_result.assert_called_with("42")
        td.api.show_job.assert_called_with("42")
        assert td._rownumber == 0
        assert td._rowcount == 3
        assert td._description == [
            ("col0", None, None, None, None, None, None),
            ("col1", None, None, None, None, None, None),
        ]
예제 #2
0
def test_check_executed():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    assert td._executed is None
    with pytest.raises(errors.ProgrammingError) as error:
        td._check_executed()
    td._executed = "42"
    td._check_executed()
예제 #3
0
def test_fetchall():
    td = cursor.Cursor(mock.MagicMock())
    td._executed = "42"
    td._rows = [["foo", 1], ["bar", 1], ["baz", 2]]
    td._rownumber = 0
    td._rowcount = len(td._rows)
    assert td.fetchall() == [["foo", 1], ["bar", 1], ["baz", 2]]
    assert td.fetchall() == []
예제 #4
0
def test_fetchmany():
    td = cursor.Cursor(mock.MagicMock())
    td._executed = "42"
    td._rows = [["foo", 1], ["bar", 1], ["baz", 2]]
    td._rownumber = 0
    td._rowcount = len(td._rows)
    assert td.fetchmany(2) == [["foo", 1], ["bar", 1]]
    assert td.fetchmany() == [["baz", 2]]
    assert td.fetchmany() == []
    with pytest.raises(errors.InternalError) as error:
        td.fetchmany(1)
예제 #5
0
def test_cursor_execute():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    td.api.query = mock.MagicMock(return_value=42)
    td._do_execute = mock.MagicMock()
    assert td.execute("SELECT 1") == 42
    td.api.query.assert_called_with("SELECT 1", db="sample_datasets")
    assert td._do_execute.called
    assert td._rows is None
    assert td._rownumber == 0
    assert td._rowcount == -1
    assert td._description == []
예제 #6
0
def test_cursor_execute_format_dict():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    td.api.query = mock.MagicMock(return_value=42)
    td._do_execute = mock.MagicMock()
    assert td.execute("SELECT {i} FROM {t}", args={"i": 13, "t": "dual"}) == 42
    td.api.query.assert_called_with("SELECT 13 FROM dual", db="sample_datasets")
    assert td._do_execute.called
    assert td._rows is None
    assert td._rownumber == 0
    assert td._rowcount == -1
    assert td._description == []
예제 #7
0
def test_cursor_executemany():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    td.api.query = mock.MagicMock(side_effect=[1, 1, 2])
    td._do_execute = mock.MagicMock()
    assert td.executemany("SELECT {i}", [{"i": 1}, {"i": 2}, {"i": 3}]) == [1, 1, 2]
    td.api.query.assert_called_with("SELECT 3", db="sample_datasets")
    assert td._do_execute.called
    assert td._rows is None
    assert td._rownumber == 0
    assert td._rowcount == -1
    assert td._description == []
예제 #8
0
def test_do_execute_success():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    td._executed = "42"
    td._check_executed = mock.MagicMock(return_value=True)
    td.api.job_status = mock.MagicMock(return_value="success")
    td.api.job_result = mock.MagicMock(
        return_value=[["foo", 1], ["bar", 1], ["baz", 2]])
    td.api.show_job = mock.MagicMock(return_value={
        "hive_result_schema": [["col0", "varchar"], ["col1", "long"]]
    })
    td._do_execute()
    assert td._check_executed.called
    td.api.job_status.assert_called_with("42")
    td.api.job_result.assert_called_with("42")
    td.api.show_job.assert_called_with("42")
    assert td._rownumber == 0
    assert td._rowcount == 3
    assert td._description == [("col0", None, None, None, None, None, None),
                               ("col1", None, None, None, None, None, None)]
예제 #9
0
def test_cursor_execute_format_tuple():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    with pytest.raises(errors.NotSupportedError) as error:
        td.execute("SELECT %d FROM %s", args=(13, "dual"))
예제 #10
0
def test_job_result():
    td = cursor.Cursor(mock.MagicMock())
    td._executed = "42"
    td.job_result()
    td.api.job_result.assert_called_with("42")
예제 #11
0
def test_result_description():
    td = cursor.Cursor(mock.MagicMock())
    assert td._result_description(None) == []
    assert td._result_description([["col0", "int"]]) == [
        ("col0", None, None, None, None, None, None)
    ]
예제 #12
0
def test_cursor():
    td = cursor.Cursor(mock.MagicMock())
    assert td._rows is None
    assert td._rownumber == 0
    assert td.rowcount == -1
    assert td.description == []
예제 #13
0
def test_cursor_setoutputsize():
    td = cursor.Cursor(mock.MagicMock())
    with pytest.raises(errors.NotSupportedError) as error:
        td.setoutputsize(42)
예제 #14
0
def test_cursor_close():
    td = cursor.Cursor(mock.MagicMock())
    td.close()
    assert td.api.close.called
예제 #15
0
def test_cursor_nextset():
    td = cursor.Cursor(mock.MagicMock())
    with pytest.raises(errors.NotSupportedError) as error:
        td.nextset()
예제 #16
0
def test_do_execute_error():
    td = cursor.Cursor(mock.MagicMock(), db="sample_datasets")
    td._executed = "42"
    td.api.job_status = mock.MagicMock(side_effect=["error"])
    with pytest.raises(errors.InternalError) as error:
        td._do_execute()
예제 #17
0
def test_show_job():
    td = cursor.Cursor(mock.MagicMock())
    td._executed = "42"
    td.show_job()
    td.api.show_job.assert_called_with("42")
예제 #18
0
 def cursor(self):
     return cursor.Cursor(self._api, **self._cursor_kwargs)
예제 #19
0
def test_cursor_callproc():
    td = cursor.Cursor(mock.MagicMock())
    with pytest.raises(errors.NotSupportedError) as error:
        td.callproc("f")