-
Notifications
You must be signed in to change notification settings - Fork 57
/
request-builder.spec.ts
173 lines (155 loc) · 5.57 KB
/
request-builder.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { resetDataSource } from '@sap-cloud-sdk/test-services-e2e/TripPin/microsoft-o-data-service-sample-trippin-in-memory-models-service/operations';
import { PersonGender } from '@sap-cloud-sdk/test-services-e2e/TripPin/microsoft-o-data-service-sample-trippin-in-memory-models-service/PersonGender';
import {
microsoftODataServiceSampleTrippinInMemoryModelsService,
batch,
getNearestAirport
} from '@sap-cloud-sdk/test-services-e2e/TripPin/microsoft-o-data-service-sample-trippin-in-memory-models-service';
import {
any,
defaultDeSerializers,
entityDeserializer
} from '@sap-cloud-sdk/odata-v4';
import type { People } from '@sap-cloud-sdk/test-services-e2e/TripPin/microsoft-o-data-service-sample-trippin-in-memory-models-service';
const url = 'https://services.odata.org/';
const destination = { url };
const peopleApi =
microsoftODataServiceSampleTrippinInMemoryModelsService().peopleApi;
const entityBuilder = peopleApi.entityBuilder();
const requestBuilder = peopleApi.requestBuilder();
const schema = peopleApi.schema;
async function deletePerson(userName: string): Promise<void> {
const queried = await requestBuilder.getByKey(userName).execute(destination);
// The trippin service return not 404 exception but 204 if an entity is not found. Hence this check
if (queried.userName) {
return requestBuilder.delete(queried).execute(destination);
}
}
function createPeople(userName: string): People {
return entityBuilder
.firstName('SomeFirstName')
.lastName('SomeLastName')
.gender(PersonGender.Male)
.userName(userName)
.build();
}
xdescribe('Request builder', () => {
it('should return a collection of entities for get all request', async () => {
const people = await requestBuilder
.getAll()
.expand(schema.FRIENDS)
.execute(destination);
expect(people).toEqual(
expect.arrayContaining([
expect.objectContaining({
friends: expect.arrayContaining([expect.anything()])
})
])
);
});
it('function imports with batch', async () => {
const result = await batch(getNearestAirport({ lat: 0, lon: 0 }))
.withSubRequestPathType('absolute')
.execute(destination);
if (result[0].isReadResponse()) {
const parsed = getNearestAirport({ lat: 0, lon: 0 }).responseTransformer(
result[0].body
);
expect(parsed.name).toBe('Rome Ciampino Airport');
}
});
it('should return a collection all friends of a person', async () => {
const people = (
await requestBuilder
.getByKey('russellwhyte')
.appendPath('/Friends')
.executeRaw(destination)
).data.value as any[];
const actual = people.map(
person =>
entityDeserializer(defaultDeSerializers).deserializeEntity(
person,
peopleApi
) as People
);
expect(actual.length).toEqual(4);
expect(actual).toEqual(
expect.arrayContaining([
expect.objectContaining({
userName: expect.anything()
})
])
);
});
it('should return a collection of entities with filtered one to many navigation properties', async () => {
const people = await requestBuilder
.getAll()
.expand(
schema.FRIENDS.filter(
schema.FRIENDS.filter(any(schema.USER_NAME.equals('russellwhyte')))
)
)
.execute(destination);
expect(people).toEqual(
expect.arrayContaining([
expect.objectContaining({
friends: expect.arrayContaining([expect.anything()])
})
])
);
});
it('should execute the simple unbound action in present in the tripping service', async () => {
const result = await resetDataSource({}).execute(destination);
expect(result).toBe(undefined);
});
it('should handle nested expands', async () => {
const result = await requestBuilder
.getAll()
.expand(schema.FRIENDS.expand(schema.FRIENDS))
.execute(destination);
expect(result[0].friends[0].friends[0]).not.toBe(undefined);
});
it('should create a simple entity without navigation properties', async () => {
const myKey = 'KeyForCreation';
await deletePerson(myKey);
const entity = createPeople(myKey);
// This is not a navigation property
entity.addressInfo = [
{
address: 'Address1',
city: { name: 'Berlin', countryRegion: 'DE', region: '' }
},
{
address: 'Address2',
city: { name: 'Paris', countryRegion: 'FR', region: '' }
}
];
await requestBuilder.create(entity).execute(destination);
const expected = await requestBuilder.getByKey(myKey).execute(destination);
expect(expected.addressInfo!.length).toBe(2);
expect(expected.addressInfo!.map(address => address.address)).toEqual([
'Address1',
'Address2'
]);
}, 10000);
// This does not work for the demo service currently
xit('should create a simple entity with navigation i.e. "deep create"', async () => {
const keyRoot = 'KeyForCreationDeepRoot';
const keyChild1 = 'KeyForCreationChild1';
const keyChild2 = 'KeyForCreationChild2';
await deletePerson(keyRoot);
await deletePerson(keyChild1);
await deletePerson(keyChild2);
const entity = createPeople(keyRoot);
entity.friends = [createPeople(keyChild1), createPeople(keyChild2)];
await requestBuilder.create(entity).execute(destination);
const expected = await requestBuilder
.getByKey(keyRoot)
.execute(destination);
expect(expected.friends!.length).toBe(2);
expect(expected.friends!.map(friend => friend.userName)).toEqual([
keyChild1,
keyChild2
]);
}, 10000);
});