Skip to content

Commit

Permalink
Add reusable test until functions to simplify test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Mar 16, 2022
1 parent 95b0727 commit a73398c
Show file tree
Hide file tree
Showing 12 changed files with 696 additions and 1,937 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* 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.
Expand All @@ -18,7 +18,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:amplify_flutter/amplify_flutter.dart';

import '../utils/setup_utils.dart';
import '../utils/wait_for_expected_event_from_hub.dart';
import '../utils/test_cloud_synced_model_operation.dart';
import 'models/basic_model_operation/ModelProvider.dart';

void main() {
Expand All @@ -28,6 +28,8 @@ void main() {
group(
'Basic model operation${enableCloudSync ? ' with API sync 🌩 enabled' : ''} -',
() {
Blog testBlog = Blog(name: 'test blog');

setUpAll(() async {
await configureDataStore(
enableCloudSync: enableCloudSync,
Expand All @@ -37,91 +39,76 @@ void main() {
testWidgets(
'should save a new model ${enableCloudSync ? 'and sync to cloud' : ''}',
(WidgetTester tester) async {
Blog testBlog = Blog(name: 'test blog');

if (enableCloudSync) {
// set an async getter to retrieve a desired hub event with a speicfic
// event matcher
var eventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) =>
(event.element.model as Blog).id == testBlog.id &&
// newly saved model at this step should have version: 1
event.element.version == 1,
await testCloudSyncedModelOperation(
rootModels: [testBlog],
expectedRootModelVersion: 1,
rootModelOperator: Amplify.DataStore.save,
rootModelEventsAssertor: (events) {
events.forEach((event) {
expect(event.element.deleted, isFalse);
});
},
);

// save model locally and to sync to cloud
await Amplify.DataStore.save(testBlog);

// wait for the desired event to arrive
var event = await eventGetter;
expect(event.element.deleted, isFalse);
} else {
await Amplify.DataStore.save(testBlog);
}

var blogs = await Amplify.DataStore.query(Blog.classType);
expect(blogs.length, 1);
expect(blogs.contains(testBlog), isTrue);
var queriedBlogs = await Amplify.DataStore.query(Blog.classType);
expect(queriedBlogs, contains(testBlog));
});

testWidgets(
'should update existing model ${enableCloudSync ? 'and sync to cloud' : ''}',
(WidgetTester tester) async {
// get previously saved model
var testBlog = (await Amplify.DataStore.query(Blog.classType))[0];
// update model
var updatedTestBlog = testBlog.copyWith(name: "updated test blog");

if (enableCloudSync) {
var eventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) =>
(event.element.model as Blog).id == updatedTestBlog.id &&
// updated model at this step should have version: 2
event.element.version == 2,
await testCloudSyncedModelOperation(
rootModels: [updatedTestBlog],
expectedRootModelVersion: 2,
rootModelOperator: Amplify.DataStore.save,
rootModelEventsAssertor: (events) {
events.forEach((event) {
expect(event.element.deleted, isFalse);
});
},
);

await Amplify.DataStore.save(updatedTestBlog);

var event = await eventGetter;
expect(event.element.deleted, isFalse);
} else {
await Amplify.DataStore.save(updatedTestBlog);
}

var updatedBlogs = await Amplify.DataStore.query(Blog.classType);
var queriedBlogs = await Amplify.DataStore.query(
Blog.classType,
where: Blog.ID.eq(updatedTestBlog.id),
);

// verify blog was updated
expect(updatedBlogs.length, 1);
expect(updatedBlogs.contains(updatedTestBlog), isTrue);
expect(queriedBlogs, contains(updatedTestBlog));
},
);

testWidgets(
'should delete existing model ${enableCloudSync ? 'and sync to cloud' : ''}',
(WidgetTester tester) async {
// get previously saved model
var testBlog = (await Amplify.DataStore.query(Blog.classType))[0];

if (enableCloudSync) {
var eventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) =>
(event.element.model as Blog).id == testBlog.id &&
// deleted model at this step should have version: 3
event.element.version == 3,
await testCloudSyncedModelOperation(
rootModels: [testBlog],
expectedRootModelVersion: 3,
rootModelOperator: Amplify.DataStore.delete,
rootModelEventsAssertor: (events) {
events.forEach((event) {
expect(event.element.deleted, isTrue);
});
},
);

await Amplify.DataStore.delete(testBlog);

var event = await eventGetter;
expect(event.element.deleted, isTrue);
} else {
await Amplify.DataStore.delete(testBlog);
}

var blogs = await Amplify.DataStore.query(Blog.classType);
var queriedBlogs = await Amplify.DataStore.query(Blog.classType);

// verify blog was deleted
expect(blogs, isEmpty);
expect(queriedBlogs, isNot(contains(testBlog)));
},
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
* permissions and limitations under the License.
*/

import 'package:amplify_datastore/amplify_datastore.dart';

import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:amplify_flutter/amplify_flutter.dart';

import '../utils/setup_utils.dart';
import '../utils/wait_for_expected_event_from_hub.dart';
import '../utils/test_cloud_synced_model_operation.dart';
import 'models/belongs_to/ModelProvider.dart';

void main() {
Expand All @@ -41,151 +38,22 @@ void main() {
// belongsToParent: BelongsToParent @belongsTo(fields: ["belongsToParentID"])
// }
final enableCloudSync = shouldEnableCloudSync();
var parent = BelongsToParent(name: 'belongs to parent');
var child = BelongsToChildExplicit(
name: 'belongs to child (explicit)', belongsToParent: parent);
late Future<SubscriptionEvent<BelongsToParent>> parentEvent;
late Future<SubscriptionEvent<BelongsToChildExplicit>> childEvent;

setUpAll(() async {
await configureDataStore(
enableCloudSync: enableCloudSync,
modelProvider: ModelProvider.instance);

parentEvent = Amplify.DataStore.observe(BelongsToParent.classType).first;
childEvent =
Amplify.DataStore.observe(BelongsToChildExplicit.classType).first;
});

testWidgets('precondition', (WidgetTester tester) async {
var queriedChildren =
await Amplify.DataStore.query(BelongsToChildExplicit.classType);
expect(queriedChildren, isEmpty);
var queriedParents =
await Amplify.DataStore.query(BelongsToParent.classType);
expect(queriedParents, isEmpty);
});

testWidgets('save parent', (WidgetTester tester) async {
if (enableCloudSync) {
var eventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) {
var model = event.element.model;
if (model is BelongsToParent) {
return model.id == parent.id && event.element.version == 1;
}

return false;
},
);

await Amplify.DataStore.save(parent);

var event = await eventGetter;
expect(event.element.deleted, isFalse);
} else {
await Amplify.DataStore.save(parent);
}

var parents = await Amplify.DataStore.query(BelongsToParent.classType);
expect(parents, isNotEmpty);
});

testWidgets('save child', (WidgetTester tester) async {
if (enableCloudSync) {
var eventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) {
var model = event.element.model;
if (model is BelongsToChildExplicit) {
return model.id == child.id && event.element.version == 1;
}

return false;
},
);

await Amplify.DataStore.save(child);

var event = await eventGetter;
expect(event.element.deleted, isFalse);
} else {
await Amplify.DataStore.save(child);
}

var children =
await Amplify.DataStore.query(BelongsToChildExplicit.classType);
expect(children, isNotEmpty);
});

testWidgets('query parent', (WidgetTester tester) async {
var parents = await Amplify.DataStore.query(BelongsToParent.classType);
var queriedParent = parents.single;
expect(queriedParent, parent);
});

testWidgets('query child', (WidgetTester tester) async {
var children =
await Amplify.DataStore.query(BelongsToChildExplicit.classType);
var queriedChild = children.single;
expect(queriedChild, child);
expect(queriedChild.belongsToParent, parent);
});

testWidgets('observe parent', (WidgetTester tester) async {
var event = await parentEvent;
var observedParent = event.item;
expect(observedParent, parent);
});

testWidgets('observe child', (WidgetTester tester) async {
var event = await childEvent;
var observedChild = event.item;
expect(observedChild, child);
expect(observedChild.belongsToParent, parent);
});

testWidgets('delete parent (cascade delete child)',
(WidgetTester tester) async {
if (enableCloudSync) {
var parentEventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) {
var model = event.element.model;
if (model is BelongsToParent) {
return model.id == parent.id && event.element.version == 2;
}

return false;
},
);

var childEventGetter = getExpectedSubscriptionDataProcessedEvent(
eventMatcher: (event) {
var model = event.element.model;
if (model is BelongsToChildExplicit) {
return model.id == child.id && event.element.version == 2;
}

return false;
},
);

await Amplify.DataStore.delete(parent);

var events = await Future.wait([parentEventGetter, childEventGetter]);
var parentEvent = events[0];
var childEvent = events[1];

expect(parentEvent.element.deleted, isTrue);
expect(childEvent.element.deleted, isTrue);
} else {
await Amplify.DataStore.delete(parent);
}

var parents = await Amplify.DataStore.query(BelongsToParent.classType);
expect(parents, isEmpty);
var children =
await Amplify.DataStore.query(BelongsToChildExplicit.classType);
expect(children, isEmpty);
});
var rootModels = [BelongsToParent(name: 'belongs to parent')];
var associatedModels = [
BelongsToChildExplicit(
name: 'belongs to child (explicit)',
belongsToParent: rootModels.first,
)
];

testRootAndAssociatedModelsRelationship(
modelProvider: ModelProvider.instance,
rootModelType: BelongsToParent.classType,
rootModels: rootModels,
associatedModelType: BelongsToChildExplicit.classType,
associatedModels: associatedModels,
supportCascadeDelete: true,
enableCloudSync: enableCloudSync,
);
});
}
Loading

0 comments on commit a73398c

Please sign in to comment.