/** * Deserialize a byte array into a table schema. * * @param serializedTableSchema Byte array of the table schema [Not null] * @return An table schema for the byte array. */ public static TableSchema deserialize(byte[] serializedTableSchema) { checkNotNull(serializedTableSchema); checkArgument(serializedTableSchema.length > 0); SchemaVersionUtils.processSchemaVersion(serializedTableSchema[0], TableSchemaInfo.VER_CURRENT); return new TableSchema(Util.deserializeAvroObject(serializedTableSchema, reader)); }
/** * Produce a copy of the current schema such that the two schemas are independent * each other. A change to the copy doesn't affect the original. * * @return New independent table schema */ public TableSchema schemaCopy() { return new TableSchema(AvroTableSchema.newBuilder(avroTableSchema).build()); }
@Test(expected = NullPointerException.class) public void testTableSchemaCreationInvalidIndices() { new TableSchema(ImmutableList.<ColumnSchema>of(FAKE_COL_SCHEMA), null); }
@Before public void setupTestCase() { tableSchema = new TableSchema(COLUMNS, INDICES); }
@Test(expected = IllegalArgumentException.class) public void testTableSchemaCreationInvalidNumberColumns() { new TableSchema(ImmutableList.<ColumnSchema>of(), ImmutableList.<IndexSchema>of()); }
@Test public void testGetAutoIncColumnNoAutoIncColumns() { final TableSchema schema = new TableSchema(ImmutableList.<ColumnSchema>of(FAKE_COL_SCHEMA), INDICES); assertEquals(null, schema.getAutoIncrementColumn()); }
@Test public void testHasNoUniqueIndices() { final TableSchema schema = new TableSchema(COLUMNS, ImmutableList.<IndexSchema>of()); assertFalse(schema.hasUniqueIndices()); }
@Test public void testHasNoIndices() { final TableSchema schema = new TableSchema(COLUMNS, ImmutableList.<IndexSchema>of()); assertFalse(schema.hasIndices()); }
@Override public TableSchema next() { final List<ColumnSchema> columnSchemas = COLUMNS_GEN.next(); ImmutableList.Builder<String> columns = ImmutableList.builder(); for (ColumnSchema columnSchema : columnSchemas) { columns.add(columnSchema.getColumnName()); } final Generator<List<IndexSchema>> indexGen = CombinedGenerators.lists( new IndexSchemaGenerator(columns.build()), numIndicesGen); return new TableSchema(columnSchemas, indexGen.next()); } }
@Test public void testAddIndicesValidIndex() { final List<IndexSchema> indices = Lists.newArrayList(); final TableSchema schema = new TableSchema(COLUMNS, indices); assertEquals(0, schema.getIndices().size()); schema.addIndices(INDICES); assertEquals(INDICES.size(), schema.getIndices().size()); }
@Test public void testHasAutoIncrementColumnNotAutoInc() { final List<ColumnSchema> columns = ImmutableList.<ColumnSchema>of( ColumnSchema.builder(COLUMN_B, ColumnType.LONG).setIsAutoIncrement(false).build()); final TableSchema tableSchema = new TableSchema(columns, ImmutableList.<IndexSchema>of()); Verify.hasAutoIncrementColumn(tableSchema); } }
@Test public void testHasAutoIncrementColumn() { final List<ColumnSchema> columns = ImmutableList.<ColumnSchema>of( ColumnSchema.builder(COLUMN_B, ColumnType.LONG).setIsAutoIncrement(true).build()); final TableSchema tableSchema = new TableSchema(columns, ImmutableList.<IndexSchema>of()); Verify.hasAutoIncrementColumn(tableSchema); }
@Test public void testIsValidTableSchemaValidSchema() { final List<ColumnSchema> columns = ImmutableList.<ColumnSchema>of( ColumnSchema.builder(COLUMN_B, ColumnType.LONG).setIsAutoIncrement(true).build()); Verify.isValidTableSchema(new TableSchema(columns, ImmutableList.<IndexSchema>of())); }
@Test public void testRemoveIndexValidIndexName() { final TableSchema schema = new TableSchema(COLUMNS, INDICES); assertEquals(INDICES.size(), schema.getIndices().size()); // Remove all of the indexes from the schema for(final IndexSchema indexSchema : INDICES ) { schema.removeIndex(indexSchema.getIndexName()); } assertEquals(0, schema.getIndices().size()); }
@Test public void testGetColumnSchemaValidColumnName() { final TableSchema schema = new TableSchema(ImmutableList.<ColumnSchema>of(FAKE_COL_SCHEMA), INDICES); final ColumnSchema actual = schema.getColumnSchema(FAKE_COL_SCHEMA.getColumnName()); assertEquals(FAKE_COL_SCHEMA.getColumnName(), actual.getColumnName()); assertEquals(FAKE_COL_SCHEMA, actual); }