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 = utils.MockCursor(self.mock_query_results) self.connection = utils.MockConnection(cursor=self.cursor) self.columns_info = [] db_column_id = DbColumn() db_column_id.data_type = 'text' db_column_id.column_name = 'Id' db_column_value = DbColumn() db_column_value.data_type = 'text' db_column_value.column_name = 'Value' 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._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( 'pgsqltoolsservice.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)
def test_write_int(self): test_value = 1234567890 test_columns_info = [] col = DbColumn() col.data_type = datatypes.DATATYPE_INTEGER 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(4), res)
def test_write_udt(self): test_value = "TestUserDefinedTypes" test_columns_info = [] col = DbColumn() col.data_type = 'UserDefinedTypes' 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)
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 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)
def test_write_int4range(self): test_value = NumericRange(10, 20) test_columns_info = [] col = DbColumn() col.data_type = datatypes.DATATYPE_INT4RANGE 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)
def test_write_json(self): test_value = {"Name": "TestName", "Schema": "TestSchema"} test_columns_info = [] col = DbColumn() col.data_type = datatypes.DATATYPE_JSON 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)
def test_write_bytea(self): test_value = memoryview(b'TestString') test_columns_info = [] col = DbColumn() col.data_type = datatypes.DATATYPE_BYTEA 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)
def test_write_uuid(self): test_value = uuid.uuid4() test_columns_info = [] col = DbColumn() col.data_type = datatypes.DATATYPE_UUID 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
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 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)
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 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)
def test_write_decimal(self): test_val = Decimal(123) test_columns_info = [] col = DbColumn() col.data_type = datatypes.DATATYPE_NUMERIC 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_val) res = self._writer.write_row(mock_storage_data_reader) self.assertEqual(self.get_expected_length_with_additional_buffer_for_size(len(str(test_val))), res)
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 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)
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)
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
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('pgsqltoolsservice.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)
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('pgsqltoolsservice.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)
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])
def validate_evaluated_properties(self, assertion_property, data_type: str, assertion): column = DbColumn() column.data_type = data_type assertion(getattr(column, assertion_property))