示例#1
0
 def test_invalid_credentials(self, mocker):
     connect_mock = mocker.patch('giraffez.Export._connect')
     export = giraffez.Export()
     export.export = mocker.MagicMock()
     export.export.status = CLI_ERR_INVALID_USER
     with pytest.raises(InvalidCredentialsError):
         export._handle_error()
示例#2
0
    def test_header(self, mocker):
        connect_mock = mocker.patch('giraffez.Export._connect')
        columns = [
            ("col1", VARCHAR_NN, 50, 0, 0),
            ("col2", VARCHAR_N, 50, 0, 0),
            ("col3", VARCHAR_N, 50, 0, 0),
        ]
        export = giraffez.Export()
        export.export = mocker.MagicMock()
        export.export.status = 0
        export.export.columns.return_value = columns

        export.query = "select * from db1.info"
        export.close()

        assert export.header == "|".join(x[0] for x in columns)
示例#3
0
    def test_parse_sql(self, mocker):
        connect_mock = mocker.patch('giraffez.Export._connect')
        columns = [
            ("col1", VARCHAR_NN, 50, 0, 0),
            ("col2", VARCHAR_N, 50, 0, 0),
            ("col3", VARCHAR_N, 50, 0, 0),
        ]
        export = giraffez.Export()
        export.export = mocker.MagicMock()
        export.export.status = 0
        export.export.columns.return_value = columns

        export.query = """select col1 as ‘c1’, col2 as “c2”from db1.info a\n join db2.info b\n on a.id = b.id"""
        export.close()

        assert '\n' not in export.query
        assert '‘' not in export.query
        assert '’' not in export.query
        assert '“' not in export.query
        assert '”' not in export.query
示例#4
0
    def test_export_results(self, mocker):
        connect_mock = mocker.patch('giraffez.Export._connect')
        query = "select * from db1.info"
        columns = [
            ("col1", VARCHAR_NN, 50, 0, 0),
            ("col2", VARCHAR_N, 50, 0, 0),
            ("col3", VARCHAR_N, 50, 0, 0),
        ]
        rows = [
            ["value1", "value2", "value3"],
            ["value1", "value2", "value3"],
            ["value1", "value2", "value3"],
        ]
        expected_results = ["|".join(row) for row in rows]

        export = giraffez.Export()
        export.export = mocker.MagicMock()
        export.export.status = 0
        export.export.get_buffer_str.side_effect = [expected_results]
        export.export.columns.return_value = columns

        export.query = query
        export._initiate()
        #export.processor = lambda a: a
        results = list(export.results())
        export.close()

        # This ensures that the config was proper mocked
        connect_mock.assert_called_with('db1', 'user123', 'pass456')

        assert results == expected_results
        assert isinstance(export.columns, giraffez.types.Columns) == True
        assert export.export.columns.called == True
        assert len(export.columns) == 3
        assert export.export.get_buffer_str.call_count == 2
        assert export.export.close.called == True
示例#5
0
 def test_giraffez_not_found(self, mocker):
     with pytest.raises(GiraffeNotFound):
         giraffez.export.GIRAFFE_NOT_FOUND = True
         export = giraffez.Export()
     giraffez.export.GIRAFFE_NOT_FOUND = False