Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(datastore): Flutter update #1801

Merged
merged 7 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteColumn;
import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable;
import com.amplifyframework.logging.Logger;
import com.amplifyframework.util.UserAgent;

import com.google.gson.Gson;

Expand Down Expand Up @@ -130,6 +131,9 @@ public static Object convertRawValueToTarget(
case DATE:
return value instanceof String ? value : ((Temporal.Date) value).format();
case DATE_TIME:
if (UserAgent.isFlutter() && value instanceof String) {
return value;
}
OffsetDateTime offsetDateTime;
if (value instanceof String) {
offsetDateTime = OffsetDateTime.parse((String) value);
Expand All @@ -141,6 +145,9 @@ public static Object convertRawValueToTarget(
.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
return offsetDateTime.toInstant().atOffset(ZoneOffset.UTC).format(dateTimeFormatter);
case TIME:
if (UserAgent.isFlutter() && value instanceof String) {
return value;
}
String timeValue;
if (value instanceof String) {
timeValue = (String) value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.datastore.storage.sqlite;

import com.amplifyframework.AmplifyException;
import com.amplifyframework.core.model.types.JavaFieldType;
import com.amplifyframework.util.GsonFactory;
import com.amplifyframework.util.UserAgent;

import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class SQLiteModelFieldTypeConverterTest {

/**
* Reset the user agent before each test.
*/
@Before
public void reset() {
UserAgent.reset();
}

/**
* Test TIME conversion for Android (default platform).
*/
@Test
public void testConvertRawValueToTargetTimeAndroid() {
final String value = "16:00:00.050020000";
final JavaFieldType fieldType = JavaFieldType.TIME;
final Gson gson = GsonFactory.instance();
final String expected = "16:00:00.050020000Z";
final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget(
value,
fieldType,
gson);
assertEquals(expected, actual);
}

/**
* Test TIME converter for Flutter.
* @throws AmplifyException Not expected.
*/
@Test
public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException {
setUserAgent();
final String value = "16:00:00.050020000";
final JavaFieldType fieldType = JavaFieldType.TIME;
final Gson gson = GsonFactory.instance();
final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget(
value,
fieldType,
gson);
final String expected = "16:00:00.050020000";
assertEquals(expected, actual);
}

/**
* Set user agent to Flutter.
* @throws AmplifyException not expected.
*/
private void setUserAgent() throws AmplifyException {
Map<UserAgent.Platform, String> map = new HashMap<>();
map.put(UserAgent.Platform.FLUTTER, "1.0");
UserAgent.configure(map);
}

/**
* Test DATE_TIME converter for Flutter.
* @throws AmplifyException Not expected.
*/
@Test
public void testConvertRawValueToTargetDateTimeFlutter() throws AmplifyException {
setUserAgent();
final String value = "2020-01-01T16:00:00.050020000";
final JavaFieldType fieldType = JavaFieldType.DATE_TIME;
final Gson gson = GsonFactory.instance();
final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget(
value,
fieldType,
gson);
final String expected = "2020-01-01T16:00:00.050020000";
assertEquals(expected, actual);
}
}
8 changes: 8 additions & 0 deletions core/src/main/java/com/amplifyframework/util/UserAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ private static String forAndroid() {
.toString();
}

/**
* Returns true if running on Flutter.
* @return Returns true if running on Flutter.
*/
public static boolean isFlutter() {
return string().contains(Platform.FLUTTER.libraryName);
}

/**
* Enum to represent various platforms that use Amplify library for tracking
* usage metrics.
Expand Down