@Test public void shouldParseMultiAbsolutePath() { path = parse("/a/b/c/d/e"); assertThat(path.isRoot()).isFalse(); assertThat(path.isSingle()).isFalse(); assertThat(path.size()).isEqualTo(5); assertThat(path.segment(0)).isEqualTo("a"); assertThat(path.segment(1)).isEqualTo("b"); assertThat(path.segment(2)).isEqualTo("c"); assertThat(path.segment(3)).isEqualTo("d"); assertThat(path.segment(4)).isEqualTo("e"); }
@Test public void shouldParseCreateTableStatementWithCharacterSetForColumns() { String ddl = "CREATE TABLE t ( col1 VARCHAR(25) CHARACTER SET greek ); "; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table t = tables.forTable(new TableId(null, null, "t")); assertThat(t).isNotNull(); assertThat(t.retrieveColumnNames()).containsExactly("col1"); assertThat(t.primaryKeyColumnNames()).isEmpty(); assertColumn(t, "col1", "VARCHAR", Types.VARCHAR, 25, -1, true, false, false); }
protected void assertEnumType( String content, String expression ) { DataType type = parser.parse(text(content), null); assertThat(type).isNotNull(); assertThat(type.jdbcType()).isEqualTo(Types.CHAR); assertThat(type.name()).isEqualTo("ENUM"); assertThat(type.length()).isEqualTo(-1); assertThat(type.expression()).isEqualTo(expression); }
@Test public void shouldParseMultipleStatementsWithFullyQualifiedDatabase() { parser.setCurrentSchema("mydb"); String ddl = "CREATE TABLE other.foo ( " + System.lineSeparator() + " c1 INTEGER NOT NULL, " + System.lineSeparator() + " c2 VARCHAR(22) " + System.lineSeparator() + "); " + System.lineSeparator() + "-- This is a comment" + System.lineSeparator() + "DROP TABLE other.foo;" + System.lineSeparator(); parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(0); // table created and dropped changes.groupEventsByDatabase((dbName, list) -> { assertThat(dbName).isEqualTo("other"); assertThat(list.size()).isEqualTo(2); assertThat(list.get(0).type()).isEqualTo(EventType.CREATE_TABLE); assertThat(list.get(1).type()).isEqualTo(EventType.DROP_TABLE); }); }
private void validateFieldDef(Field expected) { ConfigDef configDef = connector.config(); assertThat(configDef.names()).contains(expected.name()); ConfigDef.ConfigKey key = configDef.configKeys().get(expected.name()); assertThat(key).isNotNull(); assertThat(key.name).isEqualTo(expected.name()); assertThat(key.displayName).isEqualTo(expected.displayName()); assertThat(key.importance).isEqualTo(expected.importance()); assertThat(key.documentation).isEqualTo(expected.description()); assertThat(key.type).isEqualTo(expected.type()); assertThat(key.defaultValue).isEqualTo(expected.defaultValue()); assertThat(key.dependents).isEqualTo(expected.dependents()); assertThat(key.width).isNotNull(); assertThat(key.group).isNotNull(); assertThat(key.orderInGroup).isGreaterThan(0); assertThat(key.validator).isNull(); assertThat(key.recommender).isNull(); } }
@Test public void shouldReturnOffsetForUnusedReplicaName() { assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME); assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(0); assertThat(offset.get(SourceInfo.OPERATION_ID)).isNull(); BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME); assertThat(ts.getTime()).isEqualTo(0); assertThat(ts.getInc()).isEqualTo(0); Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA")); assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(0); assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isNull(); assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA"); assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isNull(); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); }
@Test public void intTypes() throws Exception { Testing.debug("Inserted"); final SourceRecords records = consumeRecordsByTopic(EXPECTED_RECORD_COUNT); List<SourceRecord> testTableRecords = records.recordsForTopic("server1.dbo.type_int"); assertThat(testTableRecords).hasSize(1); // insert VerifyRecord.isValidRead(testTableRecords.get(0)); Struct after = (Struct) ((Struct)testTableRecords.get(0).value()).get("after"); assertRecord(after, EXPECTED_INT); }
protected void assertRows(WriteRowsEventData eventData, int numRowsInEvent, Serializable... expectedValuesInRows) { assertThat(eventData.getRows().size()).isEqualTo(numRowsInEvent); int valuePosition = 0; for (Serializable[] row : eventData.getRows()) { for (Serializable value : row) { assertThat(value).isEqualTo(expectedValuesInRows[valuePosition++]); } } }
@Test public void shouldTruncateStrings() { converter = new TruncateStrings(5).create(column); assertThat(converter.convert("1234567890").toString()).isEqualTo("12345"); assertThat(converter.convert("123456").toString()).isEqualTo("12345"); assertThat(converter.convert("12345").toString()).isEqualTo("12345"); assertThat(converter.convert("1234").toString()).isEqualTo("1234"); assertThat(converter.convert("123").toString()).isEqualTo("123"); assertThat(converter.convert("12").toString()).isEqualTo("12"); assertThat(converter.convert("1").toString()).isEqualTo("1"); assertThat(converter.convert(null)).isNull(); }
@Test @FixFor("DBZ-780") public void shouldRenameColumnWithoutDefinition() { parser = new MysqlDdlParserWithSimpleTestListener(listener, TableFilter.fromPredicate(x -> !x.table().contains("ignored"))); final String ddl = "CREATE TABLE foo (id int primary key, old INT);" + System.lineSeparator() + "ALTER TABLE foo RENAME COLUMN old to new "; parser.parse(ddl, tables); assertThat(((MysqlDdlParserWithSimpleTestListener)parser).getParsingExceptionsFromWalker()).isEmpty(); assertThat(tables.size()).isEqualTo(1); final Table t1 = tables.forTable(null, null, "foo"); assertThat(t1.columns()).hasSize(2); final Column c1 = t1.columns().get(0); final Column c2 = t1.columns().get(1); assertThat(c1.name()).isEqualTo("id"); assertThat(c1.typeName()).isEqualTo("INT"); assertThat(c2.name()).isEqualTo("new"); assertThat(c2.typeName()).isEqualTo("INT"); }
@Test public void shouldBuildWithSimpleOptionalTypesForBeforeAndAfter() { Envelope env = Envelope.defineSchema() .withName("someName") .withRecord(Schema.OPTIONAL_STRING_SCHEMA) .withSource(Schema.OPTIONAL_INT64_SCHEMA) .build(); assertThat(env.schema()).isNotNull(); assertThat(env.schema().name()).isEqualTo("someName"); assertThat(env.schema().doc()).isNull(); assertThat(env.schema().version()).isNull(); assertOptionalField(env, Envelope.FieldName.AFTER, Schema.OPTIONAL_STRING_SCHEMA); assertOptionalField(env, Envelope.FieldName.BEFORE, Schema.OPTIONAL_STRING_SCHEMA); assertOptionalField(env, Envelope.FieldName.SOURCE, Schema.OPTIONAL_INT64_SCHEMA); assertRequiredField(env, Envelope.FieldName.OPERATION, Schema.STRING_SCHEMA); }
protected void assertTableIncluded(String fullyQualifiedTableName) { TableId tableId = TableId.parse(fullyQualifiedTableName); TableSchema tableSchema = mysql.schemaFor(tableId); assertThat(tableSchema).isNotNull(); assertThat(tableSchema.keySchema().name()).isEqualTo(SchemaNameAdjuster.validFullname(SERVER_NAME + "." + fullyQualifiedTableName + ".Key")); assertThat(tableSchema.valueSchema().name()).isEqualTo(SchemaNameAdjuster.validFullname(SERVER_NAME + "." + fullyQualifiedTableName + ".Value")); }
@Test @FixFor("DBZ-474") public void shouldParseCreateTableStatementWithCollate() { String ddl = "CREATE TABLE c1 (pk INT PRIMARY KEY, v1 CHAR(36) NOT NULL COLLATE utf8_unicode_ci);"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table table = tables.forTable(new TableId(null, null, "c1")); assertThat(table).isNotNull(); assertColumn(table, "v1", "CHAR", Types.CHAR, 36, -1, false, false, false); Column column = table.columnWithName("v1"); assertThat(column.typeUsesCharset()).isTrue(); }
@Test public void shouldParseStatementsWithQuotedIdentifiers() { parser.parse(readFile("ddl/mysql-quoted.ddl"), tables); Testing.print(tables); assertThat(tables.size()).isEqualTo(4); assertThat(listener.total()).isEqualTo(10); assertThat(tables.forTable("connector_test_ro", null, "products")).isNotNull(); assertThat(tables.forTable("connector_test_ro", null, "products_on_hand")).isNotNull(); assertThat(tables.forTable("connector_test_ro", null, "customers")).isNotNull(); assertThat(tables.forTable("connector_test_ro", null, "orders")).isNotNull(); }
protected void assertSameGtidSet(String pathToField, Object actual, Object expected) { assertThat(actual).isInstanceOf(String.class); assertThat(expected).isInstanceOf(String.class); GtidSet actualGtidSet = new GtidSet((String) actual); GtidSet expectedGtidSet = new GtidSet((String) expected); assertThat(actualGtidSet.toString()).isEqualTo(expectedGtidSet.toString()); } }
@Test public void shouldParseCreateUserTable() { String ddl = "CREATE TABLE IF NOT EXISTS user ( Host char(60) binary DEFAULT '' NOT NULL, User char(32) binary DEFAULT '' NOT NULL, Select_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Insert_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Update_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Delete_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Drop_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Reload_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Shutdown_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Process_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, File_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Grant_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, References_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Index_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Alter_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Show_db_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Super_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_tmp_table_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Lock_tables_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Execute_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Repl_slave_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Repl_client_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_view_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Show_view_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_routine_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Alter_routine_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_user_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Event_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Trigger_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_tablespace_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, ssl_type enum('','ANY','X509', 'SPECIFIED') COLLATE utf8_general_ci DEFAULT '' NOT NULL, ssl_cipher BLOB NOT NULL, x509_issuer BLOB NOT NULL, x509_subject BLOB NOT NULL, max_questions int(11) unsigned DEFAULT 0 NOT NULL, max_updates int(11) unsigned DEFAULT 0 NOT NULL, max_connections int(11) unsigned DEFAULT 0 NOT NULL, max_user_connections int(11) unsigned DEFAULT 0 NOT NULL, plugin char(64) DEFAULT 'mysql_native_password' NOT NULL, authentication_string TEXT, password_expired ENUM('N', 'Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, password_last_changed timestamp NULL DEFAULT NULL, password_lifetime smallint unsigned NULL DEFAULT NULL, account_locked ENUM('N', 'Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, PRIMARY KEY Host (Host,User) ) engine=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Users and global privileges';"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table foo = tables.forTable(new TableId(null, null, "user")); assertThat(foo).isNotNull(); assertThat(foo.retrieveColumnNames()).contains("Host", "User", "Select_priv"); assertColumn(foo, "Host", "CHAR BINARY", Types.BINARY, 60, -1, false, false, false); parser.parse("DROP TABLE user", tables); assertThat(tables.size()).isEqualTo(0); }
@Test public void shouldStartSingleReaderThatStopsAutomatically() throws InterruptedException { reader = new ChainedReader.Builder() .addReader(new MockReader("r2", records())) .completionMessage("Stopped the r2 reader") .build(); reader.start(); assertThat(reader.state()).isEqualTo(State.RUNNING); assertThat(reader.poll()).isSameAs(RL1); assertThat(reader.poll()).isSameAs(RL2); assertThat(reader.poll()).isSameAs(RL3); assertThat(reader.poll()).isSameAs(RL4); assertThat(reader.poll()).isSameAs(RL5); assertThat(reader.poll()).isNull(); // cause the mock reader to stop itself assertThat(reader.state()).isEqualTo(State.STOPPED); assertPollReturnsNoMoreRecords(); }
@Test public void shouldHaveSchemaForSource() { Schema schema = source.schema(); assertThat(schema.name()).isNotEmpty(); assertThat(schema.version()).isNotNull(); assertThat(schema.field(SourceInfo.SERVER_NAME).schema()).isEqualTo(Schema.STRING_SCHEMA); assertThat(schema.field(SourceInfo.REPLICA_SET_NAME).schema()).isEqualTo(Schema.STRING_SCHEMA); assertThat(schema.field(SourceInfo.NAMESPACE).schema()).isEqualTo(Schema.STRING_SCHEMA); assertThat(schema.field(SourceInfo.TIMESTAMP).schema()).isEqualTo(Schema.INT32_SCHEMA); assertThat(schema.field(SourceInfo.ORDER).schema()).isEqualTo(Schema.INT32_SCHEMA); assertThat(schema.field(SourceInfo.OPERATION_ID).schema()).isEqualTo(Schema.OPTIONAL_INT64_SCHEMA); assertThat(schema.field(SourceInfo.INITIAL_SYNC).schema()).isEqualTo(SchemaBuilder.bool().optional().defaultValue(false).build()); }
@Test public void shouldNotHaveColumnsIfEmpty() { assertThat(editor.columnWithName("any")).isNull(); assertThat(editor.columns()).isEmpty(); assertThat(editor.primaryKeyColumnNames()).isEmpty(); }
@Test public void shouldNotSubdivideOneReplicaSet() { sets = ReplicaSets.parse("rs1/host1:27017,host2:27017"); List<ReplicaSets> divided = new ArrayList<>(); sets.subdivide(1, divided::add); assertThat(divided.size()).isEqualTo(1); assertThat(divided.get(0)).isSameAs(sets); }