예제 #1
0
    def test_write_tstzrange(self):
        test_value = DateTimeTZRange(
            datetime.datetime(2014,
                              6,
                              8,
                              12,
                              12,
                              45,
                              tzinfo=psycopg2.tz.FixedOffsetTimezone(
                                  offset=720, name=None)),
            datetime.datetime(2016,
                              7,
                              6,
                              14,
                              12,
                              8,
                              tzinfo=psycopg2.tz.FixedOffsetTimezone(
                                  offset=720, name=None)))
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_TSTZRANGE
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len("[2014-06-08T12:12:45+12:00,2016-07-06T14:12:08+12:00)")),
            res)
예제 #2
0
    def setUp(self):
        """Set up the test by creating a query with multiple batches"""
        self.statement_list = statement_list = [
            'select version;', 'select * from t1;'
        ]
        self.statement_str = ''.join(statement_list)
        self.query_uri = 'test_uri'
        self.query = Query(
            self.query_uri, self.statement_str,
            QueryExecutionSettings(ExecutionPlanOptions(),
                                   ResultSetStorageType.FILE_STORAGE),
            QueryEvents())

        self.mock_query_results = [('Id1', 'Value1'), ('Id2', 'Value2')]
        self.cursor = MockCursor(self.mock_query_results)
        self.connection = MockPGServerConnection(cur=self.cursor)

        self.columns_info = []
        db_column_id = DbColumn()
        db_column_id.data_type = 'text'
        db_column_id.column_name = 'Id'
        db_column_id.provider = PG_PROVIDER_NAME
        db_column_value = DbColumn()
        db_column_value.data_type = 'text'
        db_column_value.column_name = 'Value'
        db_column_value.provider = PG_PROVIDER_NAME
        self.columns_info = [db_column_id, db_column_value]
        self.get_columns_info_mock = mock.Mock(return_value=self.columns_info)
    def setUp(self):
        self.request = SaveResultsAsCsvRequestParams()
        self.request.file_path = 'TestPath'
        self.request.include_headers = True

        self.mock_io = mock.MagicMock()

        self.row = [
            DbCellValue('Test', False, None, 0),
            DbCellValue(1023, False, None, 0),
            DbCellValue(False, False, None, 0)
        ]

        name_column = DbColumn()
        name_column.column_name = 'Name'

        id_column = DbColumn()
        id_column.column_name = 'Id'

        is_valid_column = DbColumn()
        is_valid_column.column_name = 'Valid'

        self.columns = [name_column, id_column, is_valid_column]

        self.writer = SaveAsCsvWriter(self.mock_io, self.request)
    def setUp(self):
        self.request = SaveResultsAsExcelRequestParams()
        self.request.file_path = 'TestPath'
        self.request.include_headers = True

        self.mock_io = mock.MagicMock()

        self.row = [
            DbCellValue('Test', False, None, 0),
            DbCellValue(1023, False, None, 0),
            DbCellValue(False, False, None, 0)
        ]

        name_column = DbColumn()
        name_column.column_name = 'Name'

        id_column = DbColumn()
        id_column.column_name = 'Id'

        is_valid_column = DbColumn()
        is_valid_column.column_name = 'Valid'

        self.columns = [name_column, id_column, is_valid_column]

        self.worksheet_mock = mock.MagicMock()
        self.workbook_mock = mock.MagicMock()
        self.workbook_mock.add_worksheet = mock.Mock(
            return_value=self.worksheet_mock)
        self.xlsxwriter_mock = mock.Mock(return_value=self.workbook_mock)
        self.mock_io.name = 'SomeName'

        with mock.patch('xlsxwriter.Workbook', new=self.xlsxwriter_mock):
            self.writer = SaveAsExcelWriter(self.mock_io, self.request)
예제 #5
0
    def test_write_short(self):
        test_value = 12345
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_SMALLINT
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(2), res)
예제 #6
0
    def setUp(self):
        self._metadata_factory = mock.MagicMock()
        self._mock_cursor = MockCursor(None)
        self._connection = MockPsycopgConnection({"port": "8080", "host": "test", "dbname": "test"}, self._mock_cursor)
        self._initialize_edit_request = InitializeEditParams()

        self._initialize_edit_request.schema_name = 'public'
        self._initialize_edit_request.object_name = 'Employee'
        self._initialize_edit_request.object_type = 'Table'

        db_column = DbColumn()

        column = EditColumnMetadata(db_column, None)

        self._columns_metadata = [column]
        self._schema_name = 'public'
        self._table_name = 'table'
        self._edit_table_metadata = EditTableMetadata(self._schema_name, self._table_name, self._columns_metadata)

        self._query_executer = mock.MagicMock()
        self._on_success = mock.MagicMock()
        self._on_failure = mock.MagicMock()
        self._data_editor_session = DataEditorSession(self._metadata_factory)

        self._metadata_factory.get = mock.Mock(return_value=self._edit_table_metadata)

        self._query = 'SELECT TESTCOLUMN FROM TESTTABLE LIMIT 100'

        self._data_editor_session._construct_initialize_query = mock.Mock(return_value=self._query)
예제 #7
0
    def test_write_int4range(self):
        test_value = NumericRange(10, 20)
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_INT4RANGE
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len("[10,20)")), res)
예제 #8
0
    def test_write_json(self):
        test_value = {"Name": "TestName", "Schema": "TestSchema"}
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_JSON
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len(str(test_value))), res)
예제 #9
0
    def test_write_bytea(self):
        test_value = memoryview(b'TestString')
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_BYTEA
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len(test_value.tobytes())), res)
예제 #10
0
    def test_write_uuid(self):
        test_value = uuid.uuid4()
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_UUID
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(36),
            res)  # UUID standard len is 36
예제 #11
0
    def test_write_timedelta(self):
        test_value = datetime.timedelta(days=3, hours=4, minutes=5, seconds=6)
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_INTERVAL
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len(str(test_value))), res)
예제 #12
0
    def test_write_datetime(self):
        test_value = datetime.datetime(2004, 10, 19, 10, 23, 54)
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_TIMESTAMP
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len(test_value.isoformat())), res)
예제 #13
0
    def test_write_udt(self):
        test_value = "TestUserDefinedTypes"
        test_columns_info = []
        col = DbColumn()
        col.data_type = 'UserDefinedTypes'
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len(test_value)), res)
예제 #14
0
    def test_get_script(self):
        column_index = 0
        db_column = DbColumn()
        db_column.data_type = 'bool'
        new_cell_value = '0'

        self._row_create.new_cells[column_index] = CellUpdate(
            db_column, new_cell_value)

        script = self._row_create.get_script()

        self.assertEqual(
            script.query_template,
            'INSERT INTO "public"."TestTable"("IsValid") VALUES(%s) RETURNING *'
        )
        self.assertEquals(script.query_paramters[0], False)
예제 #15
0
    def test_write_daterange(self):
        test_value = DateRange(datetime.date(2015, 6, 6),
                               datetime.date(2016, 8, 8))
        test_columns_info = []
        col = DbColumn()
        col.data_type = datatypes.DATATYPE_DATERANGE
        col.provider = PG_PROVIDER_NAME
        test_columns_info.append(col)
        mock_storage_data_reader = MockStorageDataReader(
            self._cursor, test_columns_info)
        mock_storage_data_reader.get_value = mock.MagicMock(
            return_value=test_value)

        res = self._writer.write_row(mock_storage_data_reader)
        self.assertEqual(
            self.get_expected_length_with_additional_buffer_for_size(
                len("[2015-06-06,2016-08-08)")), res)
예제 #16
0
    def test_create_row(self):
        '''
        Validate that create row creates new row based with new row id and returns
        CreateRowResponse
        '''
        self._data_editor_session._last_row_id = 0

        calculated_column = DbColumn()
        calculated_column.is_updatable = False
        calculated_column_metadata = EditColumnMetadata(calculated_column, None)

        default_value_column = DbColumn()
        default_value_column.data_type = 'bool'
        default_value_column.is_updatable = True
        default_value_column_metadata = EditColumnMetadata(default_value_column, '0')

        columns_metadata = [calculated_column_metadata, default_value_column_metadata]

        self._data_editor_session.table_metadata = EditTableMetadata(self._schema_name, self._table_name, columns_metadata)

        result_set = self.get_result_set([(1, False)])

        result_set.columns_info = [calculated_column, default_value_column]

        self._data_editor_session._result_set = result_set

        self._data_editor_session._is_initialized = True
        response: CreateRowResponse = self._data_editor_session.create_row()

        self.assertEqual(1, response.new_row_id)
        self.assertEqual(len(columns_metadata), len(response.default_values))

        self.assertEqual('<TBD>', response.default_values[0])
        self.assertEqual('False', response.default_values[1])
예제 #17
0
def get_columns_info(cursor) -> List[DbColumn]:

    if cursor.description is None:
        raise ValueError('Cursor description is not available')

    if cursor.connection is None:
        # if no connection is provided, just return basic column info constructed from the cursor description
        return [
            DbColumn.from_cursor_description(index, column)
            for index, column in enumerate(cursor.description)
        ]

    if (hasattr(cursor, "provider")):
        # MySQL or MariaDB connections
        columns_info = []
        for index, column in enumerate(cursor.description):
            db_column = DbColumn.from_cursor_description(index, column)
            db_column.provider = cursor.provider
            columns_info.append(db_column)
        return columns_info

    else:
        column_type_oids = [
            column_info[1] for column_info in cursor.description
        ]

        query_template = sql.SQL(
            'SELECT {}, {} FROM {} WHERE {} IN ({})').format(
                sql.Identifier('oid'), sql.Identifier('typname'),
                sql.Identifier('pg_type'), sql.Identifier('oid'),
                sql.SQL(', ').join(sql.Placeholder() * len(column_type_oids)))
        columns_info = []
        with cursor.connection.cursor() as type_cursor:
            type_cursor.execute(query_template, column_type_oids)
            rows = type_cursor.fetchall()
            rows_dict = dict(rows)

            for index, column in enumerate(cursor.description):
                db_column = DbColumn.from_cursor_description(index, column)
                db_column.data_type = rows_dict.get(column[1])
                db_column.provider = constants.PG_PROVIDER_NAME
                columns_info.append(db_column)

        return columns_info
예제 #18
0
    def setUp(self):
        self._row_id = 1
        self._rows = [("Result1",), ("Result2",)]

        db_column = DbColumn()
        db_column.data_type = 'varchar'
        db_column.column_name = 'TextType'

        self._result_set = create_result_set(ResultSetStorageType.IN_MEMORY, 0, 0)
        cursor = MockCursor(self._rows, ['IsTrue'])

        with mock.patch('ossdbtoolsservice.query.in_memory_result_set.get_columns_info', new=mock.Mock()):
            self._result_set.read_result_to_end(cursor)

        self._result_set.columns_info = [db_column]

        self._table_metadata = None
        self._column_index = 0
        self._new_value = 'Updated'

        self._row_update = RowUpdate(self._row_id, self._result_set, self._table_metadata)
예제 #19
0
    def setUp(self):
        self._row_id = 1
        self._rows = [("False"), ("True")]
        self._result_set = create_result_set(ResultSetStorageType.IN_MEMORY, 0,
                                             0)
        self._cursor = MockCursor(self._rows, ['IsTrue'])

        with mock.patch(
                'ossdbtoolsservice.query.in_memory_result_set.get_columns_info',
                new=mock.Mock()):
            self._result_set.read_result_to_end(self._cursor)

        db_column = DbColumn()
        db_column.data_type = 'bool'
        db_column.column_name = 'IsValid'
        db_column.is_updatable = True

        self._result_set.columns_info = [db_column]
        self._table_metadata = EditTableMetadata('public', 'TestTable', [])

        self._row_create = RowCreate(self._row_id, self._result_set,
                                     self._table_metadata)
예제 #20
0
    def setUp(self):
        self._row_id = 1
        self._rows = [("False",), ("True",)]

        self._result_set = create_result_set(ResultSetStorageType.IN_MEMORY, 0, 0)
        cursor = MockCursor(self._rows, ['IsTrue'])

        with mock.patch('ossdbtoolsservice.query.in_memory_result_set.get_columns_info', new=mock.Mock()):
            self._result_set.read_result_to_end(cursor)

        db_column = DbColumn()
        db_column.data_type = 'bool'
        db_column.column_name = 'IsValid'
        db_column.is_key = True
        db_column.column_ordinal = 0

        self._result_set.columns_info = [db_column]

        self._columns_metadata = [EditColumnMetadata(db_column, 'Default Value')]

        self._table_metadata = EditTableMetadata('public', 'TestTable', self._columns_metadata)

        self._row_delete = RowDelete(self._row_id, self._result_set, self._table_metadata)
예제 #21
0
    def validate_evaluated_properties(self, assertion_property, data_type: str, assertion):
        column = DbColumn()
        column.data_type = data_type

        assertion(getattr(column, assertion_property))
    def create_db_column(self, column: Column) -> DbColumn:
        db_column = DbColumn()

        db_column.allow_db_null = column.not_null is False
        db_column.column_name = column.name
        db_column.column_ordinal = column.column_ordinal
        db_column.data_type = column.datatype
        db_column.is_key = column.is_key
        db_column.is_read_only = column.is_readonly
        db_column.is_unique = column.is_unique
        db_column.is_auto_increment = column.is_auto_increment
        db_column.is_updatable = column.is_readonly is False and column.is_auto_increment is False

        return db_column
예제 #23
0
 def setUp(self):
     self._db_column = DbColumn()
     self._db_column.data_type = 'varchar'
     self._new_cell_value = 'New Value'
     self._cell_update = CellUpdate(self._db_column, self._new_cell_value)