Skip to content

Commit b082f7b

Browse files
authored
chore: fix integration tests involving model comparison (#1260)
* chore: fix integration tests involving model comparison * address PR comment
1 parent aead85c commit b082f7b

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

aws-api/src/androidTest/java/com/amplifyframework/api/aws/CodeGenerationInstrumentationTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.amplifyframework.testmodels.ratingsblog.User;
3333
import com.amplifyframework.testmodels.teamproject.Projectfields;
3434
import com.amplifyframework.testmodels.teamproject.Team;
35+
import com.amplifyframework.testutils.FieldValue;
3536
import com.amplifyframework.testutils.sync.SynchronousApi;
3637

3738
import org.junit.BeforeClass;
@@ -87,10 +88,19 @@ public void queryMatchesMutationResult() throws ApiException, ParseException {
8788
.relationship(MaritalStatus.married)
8889
.build();
8990
Person createdPerson = api.create(PERSON_API_NAME, david);
91+
try {
92+
// API creates model with "createdAt" and "updatedAt" fields, which local models don't have.
93+
// Manually write these in.
94+
FieldValue.set(david, "createdAt", createdPerson.getCreatedAt());
95+
FieldValue.set(david, "updatedAt", createdPerson.getUpdatedAt());
96+
} catch (NoSuchFieldException | IllegalAccessException error) {
97+
// Failure to override these fields will fail the assertion anyways
98+
}
9099
assertEquals(david, createdPerson);
91100

92101
// Query for that created person, expect him to be there
93102
Person queriedPerson = api.get(PERSON_API_NAME, Person.class, createdPerson.getId());
103+
// Do NOT override createdAt/updatedAt fields here to confirm that synced items have same values.
94104
assertEquals(createdPerson, queriedPerson);
95105
}
96106

aws-datastore/src/androidTest/java/com/amplifyframework/datastore/AppSyncClientInstrumentationTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
import com.amplifyframework.testmodels.commentsblog.Post;
4343
import com.amplifyframework.testmodels.commentsblog.PostStatus;
4444
import com.amplifyframework.testutils.Await;
45+
import com.amplifyframework.testutils.FieldValue;
4546
import com.amplifyframework.testutils.Resources;
4647

4748
import org.junit.BeforeClass;
4849
import org.junit.Test;
4950

50-
import java.lang.reflect.Field;
5151
import java.util.Date;
5252
import java.util.List;
5353
import java.util.concurrent.TimeUnit;
@@ -110,8 +110,8 @@ public void testAllOperations() throws AmplifyException, NoSuchFieldException, I
110110

111111
// The response from AppSync has createdAt and updatedAt fields. We can't actually know what values to expect
112112
// for these, so just null them out.
113-
setField(actual, "createdAt", null);
114-
setField(actual, "updatedAt", null);
113+
FieldValue.set(actual, "createdAt", null);
114+
FieldValue.set(actual, "updatedAt", null);
115115

116116
assertEquals(owner, actual);
117117
assertEquals(new Integer(1), blogOwnerCreateResult.getSyncMetadata().getVersion());
@@ -361,11 +361,4 @@ private <T extends Model> Observable<GraphQLResponse<ModelWithMetadata<T>>> onCr
361361
});
362362
});
363363
}
364-
365-
private <T extends Model> void setField(T instance, String fieldName, Object value)
366-
throws NoSuchFieldException, IllegalAccessException {
367-
Field privateField = instance.getClass().getDeclaredField(fieldName);
368-
privateField.setAccessible(true);
369-
privateField.set(instance, value);
370-
}
371364
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.testutils;
17+
18+
import com.amplifyframework.core.model.Model;
19+
20+
import java.lang.reflect.Field;
21+
22+
/**
23+
* Helper class to manipulate model field data for testing purposes.
24+
*/
25+
public final class FieldValue {
26+
private FieldValue() {}
27+
28+
/**
29+
* Sets the field value of a model to new value.
30+
* @param instance model instance
31+
* @param fieldName name of the field being written to
32+
* @param value new value to override field value with
33+
* @param <T> type of model
34+
* @throws NoSuchFieldException if fieldName is not a field of given instance
35+
* @throws IllegalAccessException if object's field is not accessible
36+
*/
37+
public static <T extends Model> void set(T instance, String fieldName, Object value)
38+
throws NoSuchFieldException, IllegalAccessException {
39+
Field privateField = instance.getClass().getDeclaredField(fieldName);
40+
privateField.setAccessible(true);
41+
privateField.set(instance, value);
42+
}
43+
}

0 commit comments

Comments
 (0)