Skip to content

Commit

Permalink
standardized test
Browse files Browse the repository at this point in the history
  • Loading branch information
juriSacchetta committed Feb 25, 2023
1 parent f56250d commit 92770f8
Showing 1 changed file with 77 additions and 95 deletions.
172 changes: 77 additions & 95 deletions packages/dart/test/relation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,8 @@ void main() {
group('Relation', () {
test('addRelation', () async {
// arrange
await Parse().initialize('appId', 'https://test.parse.com',
debug: true,
// to prevent automatic detection
fileDirectory: 'someDirectory',
// to prevent automatic detection
appName: 'appName',
// to prevent automatic detection
appPackageName: 'somePackageName',
// to prevent automatic detection
appVersion: 'someAppVersion',
registeredSubClassMap: <String, ParseObjectConstructor>{
PostModel.keyClassName: () => PostModel(),
LikeModel.keyClassName: () => LikeModel(),
});

String res =
"{\"objectId\":\"mGGxAy3eek\",\"Likes\":{\"__type\":\"Relation\",\"className\":\"Like\"}}";
Map<String, dynamic> map = json.decode(res);

final PostModel post = PostModel.clone().fromJson(map);
final LikeModel like = LikeModel();
like.objectId = "like1";
final LikeModel like2 = LikeModel();
like2.objectId = "like2";

like.onPost = post;
like2.onPost = post;

post.likeRelation.add(like); // or post.addRelation('likes', [post]);
post.likeRelation.add(like2); // or post.addRelation('likes', [post]);

// desired output
String expectedResult =
"{\"Likes\":{\"__op\":\"AddRelation\",\"objects\":[{\"__type\":\"Pointer\",\"className\":\"Like\",\"objectId\":\"like1\"},{\"__type\":\"Pointer\",\"className\":\"Like\",\"objectId\":\"like2\"}]}}";
// act
dynamic actualResult = post.encode();
//assert
expect(actualResult.toString(), expectedResult);
});
});
test('removeRelation', () async {
// arrange
await Parse().initialize('appId', 'https://test.parse.com',
await Parse().initialize(
'appId', 'https://test.parse.com',
debug: true,
// to prevent automatic detection
fileDirectory: 'someDirectory',
Expand All @@ -59,61 +18,84 @@ void main() {
appPackageName: 'somePackageName',
// to prevent automatic detection
appVersion: 'someAppVersion',
registeredSubClassMap: <String, ParseObjectConstructor>{
PostModel.keyClassName: () => PostModel(),
LikeModel.keyClassName: () => LikeModel(),
});

String res =
"{\"objectId\":\"mGGxAy3eek\",\"Likes\":{\"__type\":\"Relation\",\"className\":\"Like\"}}";
Map<String, dynamic> map = json.decode(res);

final PostModel post = PostModel.clone().fromJson(map);
LikeModel like = LikeModel();
like.objectId = "like";
post.likeRelation.remove(like);

// desired output
String expectedResult =
"{\"Likes\":{\"__op\":\"RemoveRelation\",\"objects\":[{\"__type\":\"Pointer\",\"className\":\"Like\",\"objectId\":\"like\"}]}}";
// act
dynamic actualResult = post.encode();
//assert
expect(actualResult.toString(), expectedResult);
});
}
);

class PostModel extends ParseObject implements ParseCloneable {
static const String keyClassName = 'Post';
static const String keyRelationLike = "Likes";
var parentObj = {
"objectId": "mGGxAy3eek",
"relationKey": {"__type": "Relation", "className": "relationKey"}
};

PostModel() : super(keyClassName);
Map<String, dynamic> map = json.decode(jsonEncode(parentObj));
final ParseObject parent = ParseObject.clone("ParentClass").fromJson(map);

PostModel.clone() : this();
final ParseObject child1 = ParseObject("ChildClass");
child1.objectId = "child1";
final ParseObject child2 = ParseObject("ChildClass");
child2.objectId = "child2";
ParseRelation parseRelation =
ParseRelation(parent: parent, key: "relationKey");
parent.addRelation("relationKey", parseRelation, [child1, child2]);

@override
clone(Map<String, dynamic> map) => PostModel.clone()..fromJson(map);

ParseRelation<LikeModel> get likeRelation =>
getRelation<LikeModel>(keyRelationLike);

String encode() => json.encode(toJson(forApiRQ: true));
}

class LikeModel extends ParseObject implements ParseCloneable {
static const String keyClassName = 'Like';
static const String _keyOnPost = "OnPost";

LikeModel() : super(keyClassName);

LikeModel.clone() : this();

@override
clone(Map<String, dynamic> map) => LikeModel.clone()..fromJson(map);

PostModel? get onPost => get<PostModel>(_keyOnPost);
// desired output
var expectedResult = {
"relationKey": {
"__op": "AddRelation",
"objects": [
{
"__type": "Pointer",
"className": "ChildClass",
"objectId": "child1"
},
{
"__type": "Pointer",
"className": "ChildClass",
"objectId": "child2"
}
]
}
};

var act = parent.toJson(forApiRQ: true);
expect(act, expectedResult);
});
});
test('removeRelation', () async {
// arrange
await Parse().initialize(
'appId', 'https://test.parse.com',
debug: true,
// to prevent automatic detection
fileDirectory: 'someDirectory',
// to prevent automatic detection
appName: 'appName',
// to prevent automatic detection
appPackageName: 'somePackageName',
// to prevent automatic detection
appVersion: 'someAppVersion',
);

var parentObj = {
"objectId": "mGGxAy3eek",
"relationKey": {"__type": "Relation", "className": "relationKey"}
};

Map<String, dynamic> map = json.decode(jsonEncode(parentObj));
final ParseObject parent = ParseObject.clone("ParentClass").fromJson(map);
final ParseObject child1 = ParseObject("ChildClass");
child1.objectId = "child1";
parent.removeRelation("relationKey",
ParseRelation(parent: parent, key: "relationKey"), [child1]);

set onPost(PostModel? post) {
if (post != null) set<PostModel>(_keyOnPost, post);
}
// desired output
var expectedResult = {
"relationKey": {
"__op": "RemoveRelation",
"objects": [
{"__type": "Pointer", "className": "ChildClass", "objectId": "child1"}
]
}
};
var act = parent.toJson(forApiRQ: true);
expect(act, expectedResult);
});
}

0 comments on commit 92770f8

Please sign in to comment.