How to validate the nested api objects in Playwright spi testing - playwright

I want to validate the nested api objects. I want to check if the value of 'id' inside the 'data' object is "1"
{
page: 1,
per_page: 6,
total: 12,
total_pages: 2,
data: [
{
id: 1,
email: 'george.bluth#reqres.in',
first_name: 'George',
last_name: 'Bluth',
avatar: 'https://reqres.in/img/faces/1-image.jpg'
}
]
}
How can I achieve this?

First you need to convert your json to a valid one.
And after in java you can do something like this.
public class Validation {
public static void main(String[] args) throws IOException {
String jsonAnswer = "{'page': 1,'per_page': 6,'total': 12,'total_pages': 2,'data':" +
" [{'id': 1,'email': 'george.bluth#reqres.in','first_name': 'George','last_name':" +
" 'Bluth','avatar': 'https://reqres.in/img/faces/1-image.jpg'}]}";
JsonObject json = JsonParser.parseString(jsonAnswer).getAsJsonObject();
int id = json.get("data")
.getAsJsonArray().get(0)
.getAsJsonObject().get("id").getAsInt();
if(1 == id){
System.out.println("Validation passed!");
}else{
System.out.println("Validation failed!");
}
}
}

Related

Display list of map without duplication/repetition - Dart

I'm trying to display/print list of map without duplicate element. For example:
List userList = [
{'name': 'john', 'user_id': '251'},
{'name': 'will', 'user_id': '255'},
{'name': 'jack', 'user_id': '251'} // duplicate
];
this is what I'm trying to output/print
List userList = [
{'name': 'john', 'user_id': '251'},
{'name': 'will', 'user_id': '255'},
];
some of you may suggest to use toSet method but that is valid only for list (not list of map)
This is one of the reasons why we should not use maps in Dart as data structures since it makes it rather complicated to express what it means for elements to be equal to each other.
So I would suggest solving this issue by creating a User class like this:
class User {
String name;
String user_id;
User({required this.name, required this.user_id});
#override
int get hashCode => user_id.hashCode;
#override
bool operator ==(Object other) => other is User && user_id == other.user_id;
#override
String toString() => '{Name: $name, User ID: $user_id}';
}
class User {
String name;
String user_id;
User({required this.name, required this.user_id});
#override
int get hashCode => Object.hash(name, user_id);
#override
bool operator ==(Object other) =>
other is User && name == other.name && user_id == other.user_id;
#override
String toString() => '{Name: $name, User ID: $user_id}';
}
By doing so, we can do:
void main() {
List<User> userList = [
User(name: 'john', user_id: '251'),
User(name: 'will', user_id: '255'),
User(name: 'jack', user_id: '251'),
];
userList.forEach(print);
// {Name: john, User ID: 251}
// {Name: will, User ID: 255}
// {Name: jack, User ID: 251}
}
And we can then do the toSet() trick like this:
void main() {
List<User> userList = [
User(name: 'john', user_id: '251'),
User(name: 'will', user_id: '255'),
User(name: 'jack', user_id: '251'),
];
List<User> uniqueUserList = userList.toSet().toList();
uniqueUserList.forEach(print);
// {Name: john, User ID: 251}
// {Name: will, User ID: 255}
}
I notice that your example is made so you are only looking at the user_id to determine if you have a duplicate element. I don't know if your example is just wrong but if you want to compare both name and user_id your class would look like this:
class User {
String name;
String user_id;
User({required this.name, required this.user_id});
#override
int get hashCode => Object.hash(name, user_id);
#override
bool operator ==(Object other) =>
other is User && name == other.name && user_id == other.user_id;
#override
String toString() => '{Name: $name, User ID: $user_id}';
}
And finally, if you really want to solve this using Map objects, you could solve it using the following where you create a Set with your own definition of what it means to be equal:
void main() {
List<Map<String, String>> userList = [
{'name': 'john', 'user_id': '251'},
{'name': 'will', 'user_id': '255'},
{'name': 'jack', 'user_id': '251'} // duplicate
];
final set = LinkedHashSet<Map<String, String>>(
equals: (map1, map2) {
final id1 = map1['user_id'];
final id2 = map2['user_id'];
return id1 != null && id2 != null && id1 == id2;
},
hashCode: (map) => map['user_id'].hashCode,
);
set.addAll(userList);
List<Map<String, String>> uniqueUserList = set.toList();
uniqueUserList.forEach(print);
// {name: john, user_id: 251}
// {name: will, user_id: 255}
}
There are other ways to solve this but it will always get kinda ugly since Map is not intended for data structures as previous described.

How to do a switchmap or any better way to replace values in RxDart

I have a collection with chatroom information. Something like this:
{
chatroomid: 59,
members: [2,3]
}
Now what I want to do is, get the collection stream, in the course of doing that be able to replace the members string ids with a corresponding firestore document based on member id.
End result should look something like this:
{
chatroomid: 59,
members: [{
id: 2,
username: Johndoe1
},
{
id: 3,
username: Jennydoe1
}]
}
Is this possible with Dart RxDart?
Trying something like this fails:
getChatroomStream(chatroomid)
.switchMap((i) {
return Stream.value(i.members.map((e) => i.members.add(Document(path: 'Global.userRef/$e').streamData())));
})
.listen((event) {print(event);});
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: Concurrent
modification during iteration: Instance of
'MappedListIterable<dynamic, void>'.
#0 ListIterator.moveNext (dart:_internal/iterable.dart:337:7)
class MemberInfo {
final int id;
final String? username;
final bool isLoading;
}
class Room {
final int chatroomid;
final List<int> members;
}
class RoomWithMemberInfos {
final int chatroomid;
final List<MemberInfo> infos;
factory RoomWithMemberInfos.initial(Room room) {
return RoomWithMemberInfos(
room.chatroomid,
room.members
.map((id) => MemberInfo(id, null, true))
.toList(growable: false)
);
}
RoomWithMemberInfos withInfo(MemberInfo info) {
return RoomWithMemberInfos(
chatroomid,
infos
.map((e) => e.id == info.id ? MemberInfo(e.id, info.name, false) : e)
.toList(growable: false)
);
}
}
Stream<MemberInfo> getMemberInfo(int id) { ... }
getChatroomStream()
.switchMap((Room room) {
final initial = RoomWithMemberInfos.initial(room);
return Stream
.fromIterable(room.members)
.flatMap(getMemberInfo)
.scan<RoomWithMemberInfos>(
(acc, info) => acc!.withInfo(info),
initial,
)
.startWith(initial);
});

how to filter null values from map in Dart

Following the map, having both key-value pair as dynamic, Write a logic to filter all the null values from Map without us?
Is there any other approach than traversing through the whole map and filtering out the values (Traversing whole map and getting Entry Object and discarding those pairs) ?
I need to remove all values that are null and return map
Map<String, dynamic> toMap() {
return {
'firstName': this.firstName,
'lastName': this.lastName
};
Use removeWhere on Map to remove entries you want to filter out:
void main() {
final map = {'text': null, 'body': 5, null: 'crap', 'number': 'ten'};
map.removeWhere((key, value) => key == null || value == null);
print(map); // {body: 5, number: ten}
}
And if you want to do it as part of your toMap() method you can do something like this with the cascade operator:
void main() {
print(A(null, 'Jensen').toMap()); // {lastName: Jensen}
}
class A {
final String? firstName;
final String? lastName;
A(this.firstName, this.lastName);
Map<dynamic, dynamic> toMap() {
return <dynamic, dynamic>{
'firstName': this.firstName,
'lastName': this.lastName
}..removeWhere(
(dynamic key, dynamic value) => key == null || value == null);
}
}
You can now use a map literal with conditional entries:
Map<String, dynamic> toMap() => {
if (firstName != null) 'firstName': firstName,
if (lastName != null) 'lastName': lastName,
};
I did this to make it easy remove nulls from map and list using removeWhere:
https://dartpad.dartlang.org/52902870f633da8959a39353e96fac25
Sample:
final data =
{
"name": "Carolina Ratliff",
"company": null,
"phone": "+1 (919) 488-2302",
"tags": [
"commodo",
null,
"dolore",
],
"friends": [
{
"id": 0,
"name": null,
"favorite_fruits": [
'apple', null, null, 'pear'
]
},
{
"id": 1,
"name": "Pearl Calhoun"
},
],
};
void main() {
// From map
print('Remove nulls from map:\n' + data.removeNulls().toString());
// From list
print('\nRemove nulls from list:\n' + [data].removeNulls().toString());
}
The limitation of removeWhere is that it does not check for nested values. Use this recursive solution if you want to remove all keys in the hierarchy.
dynamic removeNull(dynamic params) {
if (params is Map) {
var _map = {};
params.forEach((key, value) {
var _value = removeNull(value);
if (_value != null) {
_map[key] = _value;
}
});
// comment this condition if you want empty dictionary
if (_map.isNotEmpty)
return _map;
} else if (params is List) {
var _list = [];
for (var val in params) {
var _value = removeNull(val);
if (_value != null) {
_list.add(_value);
}
}
// comment this condition if you want empty list
if (_list.isNotEmpty)
return _list;
} else if (params != null) {
return params;
}
return null;
}
Example:
void main() {
Map<String, dynamic> myMap = {
"a": 1,
"b": 2,
"c": [
3,
4,
null,
{"d": 7, "e": null, "f": 5}
],
"g": {"h": null, "i": null},
"j": 6,
"h": []
};
print(removeNull(myMap));
}
Output:
{a: 1, b: 2, c: [3, 4, {d: 7, f: 5}], j: 6}
Note:
If you want an empty map and list when their child has null values, comment out an empty check for map and list in the code.
I suggest you to use removeWhere function
Map<String, dynamic> map = {
'1': 'one',
'2': null,
'3': 'three'
};
map.removeWhere((key, value) => key == null || value == null);
print(map);
Maybe, just like I did, someone might come looking, for How to remove null fields from a model that will be sent to the API using retrofit and dio in flutter
Here is what I used to remove null values from the Model object
#Body(nullToAbsent: true)
For example
#POST("/shops")
Future<Shop> createShop(#Body(nullToAbsent: true) Shop shop);
The nullToAbsent param does the trick
This is the method I am using with Flutter 3:
Map<String, Object?> removeAllNulls(Map<String, Object?> map) {
final data = {...map};
data.removeWhere((key, value) => value == null);
for (final entry in data.entries.toList()) {
final value = entry.value;
if (value is Map<String, Object?>) {
data[entry.key] = removeAllNulls(value);
} else if (value is List<Object?>) {
final list = List.from(value);
for (var i = 0; i < list.length; i++) {
final obj = list[i];
if (obj is Map<String, Object?>) {
list[i] = removeAllNulls(obj);
}
}
data[entry.key] = list;
}
}
return data;
}
Usage:
var jsonData = jsonDecode(jsonEncode(example.toJson()));
jsonData = removeAllNulls(jsonData);
Remove null from JSON Using Dart Extension Method
extension JsonExtension on Map<String, dynamic> {
Map<String, dynamic> get removeNull {
removeWhere((String key, dynamic value) => value == null);
return this;
}
}
final data = {
"hello":null,
"world":"test"
};
void main() {
print(data.removeNull);
}
Output
{world: test}

Flutter / GraphQL - Mutation with custom type as parameter

I'm new to flutter and graphQL and currently I'm integrating mutations into my app. So, I have the server side using some custom types defined in the schema, but I don't know how to specify them on the flutter side. Let's see some code:
input DiaryGroupPermission {
groupId: Int!
permission: Int!
}
input DiaryInsideCommunity {
communityId: Int!
permissions: [DiaryGroupPermission]!
}
createDiary(community: DiaryInsideCommunity, description: String, title: String!): Diary
But on the client I don't know how to specify the DiaryInsideCommunity inside the mutation.
I've tried something like this:
String createDiary = """
mutation CreateDiary(\$title: String!, \$description: String!, \$community: DiaryInsideCommunity) {
createDiary(
title: \$title,
description: \$description,
community: \$community
) {
id
}
)}""".replaceAll('\n', ' ');
And passing my runMutation as follows:
runMutation({
"title": _generalPage.title(),
"description": _generalPage.description(),
"community": {
"communityId": 1,
"permissions": _permissionPage.selectedGroups().map((group) {
return {
"groupId": group.id,
"permission": 1,
};
}).toList(),
}
});
Any idea? Can't find anything on google.
Love to see the community that is created around the graphql_flutter library.
class DiaryGroupPermission {
int groupId;
int permission;
DiaryGroupPermission.fromJson(Map json)
: groupId = json['groupId'],
permission = json['permission'];
}
class DiaryInsideCommunity {
int communityId;
List<DiaryGroupPermission> permissions;
DiaryInsideCommunity.fromJson(Map json)
: communityId = json['communityId'],
permissions = json['permissions']
.map<DiaryGroupPermission>((Map permisionJson) =>
DiaryGroupPermission.fromJson(permisionJson))
.toList();
}
class Diary {
String body;
Diary(dynamic value) : body = value.toString();
}
typedef Diary createDiaryFunction(
DiaryInsideCommunity community, String description, String title);
DiaryInsideCommunity community = DiaryInsideCommunity.fromJson({
'communityId': 1,
'permissions': [
{'groupId': 1, 'permission': 1}
]
});
Diary mutation(DiaryInsideCommunity community,
{String description, #required String title}) =>
Diary(community.permissions[0].groupId);
Diary mutationResult = mutation(community, description: "a", title: "b");
I implemented the types that you wanted to in dart and created a mockup mutation function to show you how to call it.
There is no easier way to do types in dart.
Cheers from the creator of this library,
Eus
Assuming you are using graphql_flutter,
You may specify it in variables
an example is say you have this definitions for a mutation
type Mutation {
createMeterReading(
createMeterReadingInput: CreateMeterReadingInput
): MeterReading }
your input type definition
input CreateMeterReadingInput {
reading: Float
companyId: Int
companyStaffId: Int
customerId: Int
readingDataType: String
latitude: Float
longitude: Float
masterMeterId: Int
analogMeterId: Int
type: String
editedStatus: String
base64Image: String
readingTime: String
}
In flutter have
final response = await client.value.mutate(MutationOptions(
variables: {
'createMeterReadingInput': {
'reading': double.parse(_meterReadingController.text),
'companyStaffId': _decodedToken["companyStaffId"],
'analogMeterId': waterMeter["analogMeterId"],
'type': "actual",
'base64Image': getBase64(),
'readingTime': timeNow,
'latitude': widget?.currentPosition?.latitude,
'longitude': widget?.currentPosition?.longitude
}
},
documentNode: gql(r"""
mutation createMeterReading($createMeterReadingInput:CreateMeterReadingInput ) {
createMeterReading(createMeterReadingInput: $createMeterReadingInput) {
meterReadingId
reading
companyId
companyStaffId
imageUrl
editedStatus
companyStaff{
firstName
}
}
}
"""),
));

How to use datasoure in ng2-ya-table?

In the documentation of ng2-ya-table the datasource function is written in this way :
public datasource: any = (request: any): Observable<any> => {
return this.service.getUsers(request);
}
And used like this :
<ng2-ya-table [options]="options" [columns]="columns" [datasource]="datasource" [paging]="paging">
</ng2-ya-table>
I don't want to use this function in this way because I have static
data = [
{
name: 'Patricia',
email: 'Julianne.OConner#kory.org',
username: 'Yes',
},
{
name: 'Chelsey Dietrich',
email: 'Lucio_Hettinger#annie.ca',
username: 'No',
}
]
Is that possible or I am obliged to render observable type?
I tried a lot using static data but in vain
public datasource: any = {
return this.data ;
}
why this function is not working?
Try with:
public datasource: any = (request: any): Observable<any> => {
return Observable.of({
recordsTotal: this.data.length,
recordsFiltered: this.data.length,
data: this.data
});
}
Anyway, you need to perform pagination, sorting and filtering client side (the data source is an Observable in order to perform this operation server side).
For example (pagination only):
public datasource: any = (request: any): Observable<any> => {
let page = (request.start / request.length) + 1;
return Observable.of({
recordsTotal: this.data.length,
recordsFiltered: this.data.length,
data: this.data.slice(request.length * (page - 1), request.length * page)
});
}
I tried:
public datasource: any = (request: any): Observable<any> => {
return Observable.of(this.data);
}
but it causes a cascade of errors starting with:
Ng2YaTableComponent.html:53 ERROR TypeError: Cannot read property
'length' of undefined
If someone can improve this answer perhaps we can find an solution

Resources