Migrate request models to a single file to test across codegens

This commit is contained in:
Ankit Mahato
2023-07-20 06:09:42 +05:30
parent 38bcbc9dcf
commit 4436e84102
2 changed files with 54 additions and 43 deletions

View File

@ -1,18 +1,12 @@
import 'package:apidash/codegen/kotlin/pkg_okhttp.dart';
import 'package:apidash/models/models.dart' show KVRow, RequestModel;
import 'package:test/test.dart';
import 'package:apidash/consts.dart';
import 'request_models.dart';
void main() {
group('KotlinOkHttpCodeGen', () {
final kotlinOkHttpCodeGen = KotlinOkHttpCodeGen();
test('getCode returns valid code for GET request', () {
const requestModel = RequestModel(
url: 'https://api.foss42.com',
method: HTTPVerb.get,
id: '',
);
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
@ -30,17 +24,10 @@ val response = client.newCall(request).execute()
println(response.body!!.string())
""";
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
expect(kotlinOkHttpCodeGen.getCode(requestModelGet1), expectedCode);
});
test('getCode returns valid code for POST request', () {
const requestModel = RequestModel(
url: 'https://api.foss42.com/case/lower',
method: HTTPVerb.post,
requestBody: '{"text": "IS Upper"}',
requestBodyContentType: ContentType.json,
id: '1',
);
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
@ -61,17 +48,10 @@ val response = client.newCall(request).execute()
println(response.body!!.string())
""";
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
expect(kotlinOkHttpCodeGen.getCode(requestModelPost1), expectedCode);
});
test('getCode returns valid code for DELETE request', () {
const requestModel = RequestModel(
url: 'https://jsonplaceholder.typicode.com/posts/1',
method: HTTPVerb.delete,
requestBody: '{"title": "foo","body": "bar","userId": 1}',
requestBodyContentType: ContentType.json,
id: '1',
);
const expectedCode = r"""import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
@ -92,15 +72,10 @@ val response = client.newCall(request).execute()
println(response.body!!.string())
""";
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
expect(kotlinOkHttpCodeGen.getCode(requestModelDelete1), expectedCode);
});
test('getCode returns valid code for HEAD request', () {
const requestModel = RequestModel(
url: 'https://jsonplaceholder.typicode.com/posts/1',
method: HTTPVerb.head,
id: '1',
);
const expectedCode = """import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
@ -119,24 +94,12 @@ val response = client.newCall(request).execute()
println(response.body!!.string())
""";
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
expect(kotlinOkHttpCodeGen.getCode(requestModelHead1), expectedCode);
});
test(
'getCode returns valid code for requests with headers and query parameters',
() {
const requestModel = RequestModel(
url: 'https://jsonplaceholder.typicode.com/posts',
method: HTTPVerb.get,
requestParams: [
KVRow('userId', 1),
],
requestHeaders: [
KVRow('Custom-Header-1', 'Value-1'),
KVRow('Custom-Header-2', 'Value-2')
],
id: '1',
);
const expectedCode = """import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
@ -157,7 +120,7 @@ val response = client.newCall(request).execute()
println(response.body!!.string())
""";
expect(kotlinOkHttpCodeGen.getCode(requestModel), expectedCode);
expect(kotlinOkHttpCodeGen.getCode(requestModelGet2), expectedCode);
});
});
}