Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions lib/src/model/openai_model/openai_model_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ class ModelData {
final List<Permission>? permission;

ModelData(this.id, this.object, this.ownerBy, this.permission);
factory ModelData.fromJson(Map<String,dynamic> json) => ModelData(
json['id'] as String,
json['object'] as String,
json['owned_by'] as String,
json['permission'] == null ? null : (json['permission'] as List<dynamic>)
.map((e) => Permission.fromJson(e as Map<String, dynamic>))
.toList(),
);
factory ModelData.fromJson(Map<String, dynamic> json) => ModelData(
json['id'] as String,
json['object'] as String,
json['owned_by'] as String,
json['permission'] == null
? null
: (json['permission'] as List<dynamic>)
.map((e) => Permission.fromJson(e as Map<String, dynamic>))
.toList(),
);

Map<String,dynamic> toJson() =><String, dynamic>{
'id': id,
'object': object,
'owned_by': ownerBy,
'permission': permission,
};

}
Map<String, dynamic> toJson() => <String, dynamic>{
'id': id,
'object': object,
'owned_by': ownerBy,
'permission': permission?.map((e) => e.toJson()).toList(),
};
}
2 changes: 1 addition & 1 deletion lib/src/model/openai_model/openai_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AiModel {
);

Map<String, dynamic> toJson() => <String, dynamic>{
'data': data,
'data': data.map((e) => e.toJson()).toList(),
'object': object,
};
}
64 changes: 64 additions & 0 deletions test/model/complete_text/response/choices_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:chat_gpt_sdk/src/model/complete_text/response/choices.dart';

void main() {
test('Constructor variables should be equal to member variables', () {
final text = 'example text';
final index = 1;
final logprobs = {};
final finishReason = 'example reason';

final result = Choices(text, index, logprobs, finishReason);
expect(result.text, text);
expect(result.index, index);
expect(result.logprobs, logprobs);
expect(result.finishReason, finishReason);
});

test(
'fromJson should return a Choices object with values when given a valid JSON',
() {
final json = {
'text': 'example text',
'index': 1,
'logprobs': {},
'finish_reason': 'example reason'
};

final result = Choices.fromJson(json);

expect(result.text, 'example text');
expect(result.index, 1);
expect(result.logprobs, {});
expect(result.finishReason, 'example reason');
});

test(
'fromJson should return a Choices object with null for finishReason when given a JSON without it',
() {
final json = {
'text': 'example text',
'index': 1,
'logprobs': {},
};

final result = Choices.fromJson(json);
expect(result.finishReason, isNull);
});

test('fromJson should throw an exception when given an invalid JSON', () {
final json = {'text': 'example text'};
expect(() => Choices.fromJson(json), throwsA(TypeMatcher<TypeError>()));
});

test('choicesToJson should return a JSON map with values', () {
final choices = Choices('example text', 1, {}, 'example reason');

final result = choices.toJson();

expect(result['text'], 'example text');
expect(result['index'], 1);
expect(result['logprobs'], {});
expect(result['finish_reason'], 'example reason');
});
}
90 changes: 90 additions & 0 deletions test/model/complete_text/response/complete_response_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:chat_gpt_sdk/src/model/complete_text/response/choices.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
import 'package:chat_gpt_sdk/src/model/complete_text/response/usage.dart';

void main() {
test('Constructor should create a CTResponse object with values', () {
final choices = [Choices('first text', 1, {}, 'first reason')];
final usage = Usage(1, 2, 3);
final response = CTResponse(
'example id', 'example object', 100, 'example model', choices, usage);

expect(response.id, 'example id');
expect(response.object, 'example object');
expect(response.created, 100);
expect(response.model, 'example model');
expect(response.choices, isNotEmpty);
expect(response.choices.first.text, 'first text');
expect(response.choices.first.index, 1);
expect(response.choices.first.logprobs, {});
expect(response.choices.first.finishReason, 'first reason');
expect(response.usage.promptTokens, 1);
expect(response.usage.completionTokens, 2);
expect(response.usage.totalTokens, 3);
});

test(
'fromJson should return a CTResponse object with values when given a valid JSON',
() {
final json = {
'id': 'example id',
'object': 'example object',
'created': 1,
'model': 'example model',
'choices': [
{
'text': 'example text',
'index': 1,
'logprobs': {},
'finish_reason': 'example reason'
}
],
'usage': {'prompt_tokens': 1, 'completion_tokens': 0, 'total_tokens': 1}
};

final result = CTResponse.fromJson(json);

expect(result.id, 'example id');
expect(result.object, 'example object');
expect(result.created, 1);
expect(result.model, 'example model');
expect(result.choices, isNotEmpty);
expect(result.choices.first.text, 'example text');
expect(result.choices.first.index, 1);
expect(result.choices.first.logprobs, {});
expect(result.choices.first.finishReason, 'example reason');
expect(result.usage.promptTokens, 1);
expect(result.usage.completionTokens, 0);
expect(result.usage.totalTokens, 1);
});

test('fromJson should throw a TypeError when given an invalid JSON', () {
final json = {'id': 'example id'};
expect(() => CTResponse.fromJson(json), throwsA(TypeMatcher<TypeError>()));
});

test('toJson should return a JSON map with values', () {
final choices = [Choices('example text', 1, {}, 'example reason')];
final usage = Usage(0, 1, 2);
final response = CTResponse(
'example id', 'example object', 1, 'example model', choices, usage);

final result = response.toJson();

expect(result['id'], 'example id');
expect(result['object'], 'example object');
expect(result['created'], 1);
expect(result['model'], 'example model');
expect(result['choices'], isNotEmpty);
expect(result['choices'][0], isNotNull);
expect(result['choices'][0].text, 'example text');
expect(result['choices'][0].index, 1);
expect(result['choices'][0].logprobs, {});
expect(result['choices'][0].finishReason, 'example reason');
expect(result['usage'].promptTokens, 0);
expect(result['usage'].completionTokens, 1);
expect(result['usage'].totalTokens, 2);
});
}
158 changes: 158 additions & 0 deletions test/model/gen_image/response/GenImgResponse_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
import 'package:chat_gpt_sdk/src/model/gen_image/response/image_data.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('should correctly assign member variables when valid data is provided',
() {
final response = GenImgResponse(
created: 1616327482,
data: [
ImageData(
url: 'https://example.com/image1.jpg',
b64Json:
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiQ2hhdCBH',
),
ImageData(
url: 'https://example.com/image2.jpg',
b64Json:
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiVGVzdCBH',
),
],
);

expect(response.created, 1616327482);
expect(response.data!.length, 2);
expect(response.data![0]!.url, 'https://example.com/image1.jpg');
expect(response.data![0]!.b64Json,
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiQ2hhdCBH');
expect(response.data![1]!.url, 'https://example.com/image2.jpg');
expect(response.data![1]!.b64Json,
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiVGVzdCBH');
});

test('should return null when data is incomplete or invalid', () {
final response1 = GenImgResponse(
created: null,
data: null,
);
expect(response1.created, isNull);
expect(response1.data, isNull);

final response2 = GenImgResponse(
created: 1616327482,
data: [
ImageData(url: 'https://example.com/image1.jpg'),
ImageData(b64Json: 'invalid'),
],
);
expect(response2.created, 1616327482);
expect(response2.data!.length, 2);
expect(response2.data![0]!.url, 'https://example.com/image1.jpg');
expect(response2.data![0]!.b64Json, isNull);
expect(response2.data![1]!.url, isNull);
expect(response2.data![1]!.b64Json, 'invalid');
});

test('fromJson() should correctly parse JSON data', () {
final jsonData = {
'created': 1616327482,
'data': [
{
'url': 'https://example.com/image1.jpg',
'b64_json':
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiQ2hhdCBH',
},
{
'url': 'https://example.com/image2.jpg',
'b64_json':
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiVGVzdCBH',
},
]
};

final response = GenImgResponse.fromJson(jsonData);

expect(response.created, 1616327482);
expect(response.data!.length, 2);
expect(response.data![0]!.url, 'https://example.com/image1.jpg');
expect(response.data![0]!.b64Json,
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiQ2hhdCBH');
expect(response.data![1]!.url, 'https://example.com/image2.jpg');
expect(response.data![1]!.b64Json,
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiVGVzdCBH');
});

test('fromJson() should correctly parse JSON data when with data is null',
() {
final jsonData = {
'created': 1616327482,
'data': null,
};

final response = GenImgResponse.fromJson(jsonData);

expect(response.created, 1616327482);
expect(response.data!.length, 0);
});

test(
'fromJson() should return null when JSON data is invalid',
skip:
'Not performed because the operation stops with the following message before checking for an exception:'
"type 'String' is not a subtype of type 'int?'",
() {
final jsonData = {
'created': 'invalid',
'data': [
{
'url': 'https://example.com/image1.jpg',
'b64_json':
'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAiQ2hhdCBH',
},
{
'url': 'https://example.com/image2.jpg',
'b64_json': 12345, // invalid type
},
]
};

expect(
GenImgResponse.fromJson(jsonData),
throwsA(TypeMatcher<TypeError>()),
);
},
);

test('Test toJson method', () {
final imageData1 = ImageData(url: 'test_url_1', b64Json: 'test_b64json_1');
final imageData2 = ImageData(url: 'test_url_2', b64Json: 'test_b64json_2');
final genImgResponse =
GenImgResponse(created: 1234567890, data: [imageData1, imageData2]);

final expectedJson = {
'created': 1234567890,
'data': [
{'url': 'test_url_1', 'b64_json': 'test_b64json_1'},
{'url': 'test_url_2', 'b64_json': 'test_b64json_2'}
]
};

expect(genImgResponse.toJson(), expectedJson);
});

test('should convert object to json when data is null', () {
final genImgResponse = GenImgResponse(
created: 123456,
data: null,
);
final expectedJson = {
"created": 123456,
"data": [],
};

final json = genImgResponse.toJson();

expect(json, equals(expectedJson));
});
}
Loading