Search in sources :

Example 6 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SqlCommandTest method queryWithCustomPaginationInput.

/**
 * Verifies that the correct SQL bindings are generated when specifying the
 * {@link Page#startingAt(int)} and {@link QueryPaginationInput#withLimit(Integer)}
 * page details.
 * @throws DataStoreException From {@link SQLCommandFactory#queryFor(ModelSchema, QueryOptions)}
 */
@Test
public void queryWithCustomPaginationInput() throws DataStoreException {
    final ModelSchema personSchema = getPersonModelSchema();
    final SqlCommand sqlCommand = sqlCommandFactory.queryFor(personSchema, Where.matchesAll().paginated(Page.startingAt(2).withLimit(20)));
    assertNotNull(sqlCommand);
    assertEquals(PERSON_BASE_QUERY + " LIMIT ? OFFSET ?;", sqlCommand.sqlStatement());
    final List<Object> bindings = sqlCommand.getBindings();
    assertEquals(2, bindings.size());
    assertEquals(20, bindings.get(0));
    assertEquals(40, bindings.get(1));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 7 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SqlCommandTest method queryWithFirstPagePaginationInput.

/**
 * Validates that the correct SQL bindings are created when using the
 * {@link Page#firstPage()} query option.
 * @throws DataStoreException From {@link SQLCommandFactory#queryFor(ModelSchema, QueryOptions)}
 */
@Test
public void queryWithFirstPagePaginationInput() throws DataStoreException {
    final ModelSchema personSchema = getPersonModelSchema();
    final SqlCommand sqlCommand = sqlCommandFactory.queryFor(personSchema, Where.matchesAll().paginated(Page.firstPage()));
    assertNotNull(sqlCommand);
    assertEquals(PERSON_BASE_QUERY + " LIMIT ? OFFSET ?;", sqlCommand.sqlStatement());
    final List<Object> bindings = sqlCommand.getBindings();
    assertEquals(2, bindings.size());
    assertEquals(100, bindings.get(0));
    assertEquals(0, bindings.get(1));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 8 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SQLiteTableTest method createSQLiteTableForPost.

/**
 * Test if a {@link ModelSchema} for {@link Post} returns an expected {@link SQLiteTable}.  This tests the general
 * use case, for an object with most data types (String, Integer, enum, BelongsTo, and HasMany relationships).
 * @throws AmplifyException on error deriving ModelSchema.
 */
@Test
public void createSQLiteTableForPost() throws AmplifyException {
    ModelSchema schema = ModelSchema.fromModelClass(Post.class);
    Map<String, SQLiteColumn> columns = new HashMap<>();
    columns.put("id", SQLiteColumn.builder().name("id").fieldName("id").dataType(SQLiteDataType.TEXT).isNonNull(true).tableName("Post").build());
    columns.put("title", SQLiteColumn.builder().name("title").fieldName("title").dataType(SQLiteDataType.TEXT).isNonNull(true).tableName("Post").build());
    columns.put("blog", SQLiteColumn.builder().name("postBlogId").fieldName("blog").dataType(SQLiteDataType.TEXT).isNonNull(false).tableName("Post").ownerOf("Blog").build());
    columns.put("status", SQLiteColumn.builder().name("status").fieldName("status").dataType(SQLiteDataType.TEXT).isNonNull(true).tableName("Post").build());
    columns.put("rating", SQLiteColumn.builder().name("rating").fieldName("rating").dataType(SQLiteDataType.INTEGER).isNonNull(true).tableName("Post").build());
    columns.put("createdAt", SQLiteColumn.builder().name("createdAt").fieldName("createdAt").dataType(SQLiteDataType.TEXT).isNonNull(false).tableName("Post").build());
    SQLiteTable expected = SQLiteTable.builder().columns(columns).name("Post").build();
    SQLiteTable actual = SQLiteTable.fromSchema(schema);
    assertEquals(expected, actual);
    assertEquals("id", actual.getPrimaryKey().getFieldName());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class GsonPendingMutationConverterTest method convertPendingMutationToRecordAndBack.

/**
 * Validate that the {@link GsonPendingMutationConverter} can be
 * used to convert a sample {@link PendingMutation} to a
 * {@link PendingMutation.PersistentRecord}, and vice-versa.
 * @throws DataStoreException from DataStore conversion
 * @throws AmplifyException On failure to arrange model schema
 */
@Test
public void convertPendingMutationToRecordAndBack() throws AmplifyException {
    // Arrange a PendingMutation<Blog>
    Blog blog = Blog.builder().name("A neat blog").owner(BlogOwner.builder().name("Joe Swanson").build()).build();
    ModelSchema schema = ModelSchema.fromModelClass(Blog.class);
    PendingMutation<Blog> originalMutation = PendingMutation.creation(blog, schema);
    String expectedMutationId = originalMutation.getMutationId().toString();
    // Instantiate the object under test
    PendingMutation.Converter converter = new GsonPendingMutationConverter();
    // Try to construct a record from the PendingMutation instance.
    PendingMutation.PersistentRecord record = converter.toRecord(originalMutation);
    assertNotNull(record);
    assertEquals(expectedMutationId, record.getId());
    // Now, try to convert it back...
    PendingMutation<Blog> reconstructedItemChange = converter.fromRecord(record);
    assertEquals(originalMutation, reconstructedItemChange);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 10 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class GsonPendingMutationConverterTest method convertPendingMutationWithSerializedModelToRecordAndBack.

/**
 * Validate that the {@link GsonPendingMutationConverter} can be
 * used to convert a sample {@link PendingMutation} to a
 * {@link PendingMutation.PersistentRecord}, and vice-versa.
 * @throws DataStoreException from DataStore conversion
 * @throws AmplifyException On failure to arrange model schema
 */
@Test
public void convertPendingMutationWithSerializedModelToRecordAndBack() throws AmplifyException {
    // Arrange a PendingMutation<SerializedModel>
    BlogOwner blogOwner = BlogOwner.builder().name("Joe Swanson").build();
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    SerializedModel serializedBlogOwner = SerializedModel.create(blogOwner, schema);
    PendingMutation<SerializedModel> originalMutation = PendingMutation.creation(serializedBlogOwner, schema);
    String expectedMutationId = originalMutation.getMutationId().toString();
    // Instantiate the object under test
    PendingMutation.Converter converter = new GsonPendingMutationConverter();
    // Try to construct a record from the PendingMutation instance.
    PendingMutation.PersistentRecord record = converter.toRecord(originalMutation);
    assertNotNull(record);
    assertEquals(expectedMutationId, record.getId());
    // Now, try to convert it back...
    PendingMutation<SerializedModel> reconstructedItemChange = converter.fromRecord(record);
    assertEquals(originalMutation, reconstructedItemChange);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Aggregations

ModelSchema (com.amplifyframework.core.model.ModelSchema)109 Test (org.junit.Test)69 SerializedModel (com.amplifyframework.core.model.SerializedModel)34 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)30 Model (com.amplifyframework.core.model.Model)28 DataStoreException (com.amplifyframework.datastore.DataStoreException)26 HashMap (java.util.HashMap)23 ArrayList (java.util.ArrayList)22 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)21 AmplifyException (com.amplifyframework.AmplifyException)19 Consumer (com.amplifyframework.core.Consumer)19 List (java.util.List)17 NonNull (androidx.annotation.NonNull)14 Cancelable (com.amplifyframework.core.async.Cancelable)14 TimeUnit (java.util.concurrent.TimeUnit)14 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)13 Action (com.amplifyframework.core.Action)12 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)12 Collections (java.util.Collections)12 ModelProvider (com.amplifyframework.core.model.ModelProvider)11