Search in sources :

Example 1 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class RealmAnnotationTests method index.

// Tests if "index" annotation works with supported types.
@Test
public void index() {
    Table table = realm.getTable(AnnotationIndexTypes.class);
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexString")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexString")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexInt")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexInt")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexByte")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexByte")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexShort")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexShort")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexLong")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexLong")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexBoolean")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexBoolean")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexDate")));
    assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexDate")));
}
Also used : Table(io.realm.internal.Table) Test(org.junit.Test)

Example 2 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class RealmAnnotationTests method primaryKey_migration_longDuplicateValues.

// Tests migrating primary key from string to long with existing data.
@Test
public void primaryKey_migration_longDuplicateValues() {
    realm.beginTransaction();
    for (int i = 1; i <= 2; i++) {
        PrimaryKeyAsString obj = realm.createObject(PrimaryKeyAsString.class, "String" + i);
        // Creates duplicate values.
        obj.setId(1);
    }
    Table table = realm.getTable(PrimaryKeyAsString.class);
    try {
        table.setPrimaryKey("id");
        fail("It should not be possible to set a primary key column which already contains duplicate values.");
    } catch (IllegalArgumentException ignored) {
        assertEquals(0, table.getPrimaryKey());
    } finally {
        realm.cancelTransaction();
    }
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 3 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class RealmAnnotationTests method ignore.

@Test
public void ignore() {
    Table table = realm.getTable(AnnotationTypes.class);
    assertEquals(-1, table.getColumnIndex("ignoreString"));
}
Also used : Table(io.realm.internal.Table) Test(org.junit.Test)

Example 4 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class RealmAnnotationTests method primaryKey_migration_string.

// Tests migrating primary key from long to str with existing data.
@Test
public void primaryKey_migration_string() {
    realm.beginTransaction();
    for (int i = 1; i <= 2; i++) {
        PrimaryKeyAsLong obj = realm.createObject(PrimaryKeyAsLong.class, i);
        obj.setName("String" + i);
    }
    Table table = realm.getTable(PrimaryKeyAsLong.class);
    table.setPrimaryKey("name");
    assertEquals(1, table.getPrimaryKey());
    realm.cancelTransaction();
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test)

Example 5 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class RealmMigrationTests method renamePrimaryKeyFieldInMigration.

// Renaming the class should also rename the the class entry in the pk metadata table that tracks primary keys.
@Test
public void renamePrimaryKeyFieldInMigration() {
    buildInitialMigrationSchema(MigrationFieldRenamed.CLASS_NAME, false);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationFieldRenamed.CLASS_NAME).renameField(MigrationPrimaryKey.FIELD_PRIMARY, MigrationFieldRenamed.FIELD_PRIMARY);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationFieldRenamed.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationFieldRenamed.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationFieldRenamed.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationFieldRenamed.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    RealmObjectSchema objectSchema = realm.getSchema().getSchemaForClass(MigrationFieldRenamed.class);
    assertFalse(objectSchema.hasField(MigrationPrimaryKey.FIELD_PRIMARY));
    assertEquals(MigrationFieldRenamed.FIELD_PRIMARY, objectSchema.getPrimaryKey());
}
Also used : Table(io.realm.internal.Table) Test(org.junit.Test)

Aggregations

Table (io.realm.internal.Table)68 RealmObjectProxy (io.realm.internal.RealmObjectProxy)20 Test (org.junit.Test)19 Date (java.util.Date)10 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)5 RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)4 HashMap (java.util.HashMap)4 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)3 RealmException (io.realm.exceptions.RealmException)3 UncheckedRow (io.realm.internal.UncheckedRow)2 JSONObject (org.json.JSONObject)2 TargetApi (android.annotation.TargetApi)1 JsonReader (android.util.JsonReader)1 AllTypes (io.realm.entities.AllTypes)1 Cat (io.realm.entities.Cat)1 IOSAllTypes (io.realm.entities.IOSAllTypes)1 StringOnly (io.realm.entities.StringOnly)1 MigrationFieldTypeToInt (io.realm.entities.migration.MigrationFieldTypeToInt)1 MigrationFieldTypeToInteger (io.realm.entities.migration.MigrationFieldTypeToInteger)1 LinkView (io.realm.internal.LinkView)1