diff --git a/lib/src/model/openai_model/openai_model_data.dart b/lib/src/model/openai_model/openai_model_data.dart index 7970801..9a16057 100644 --- a/lib/src/model/openai_model/openai_model_data.dart +++ b/lib/src/model/openai_model/openai_model_data.dart @@ -7,20 +7,21 @@ class ModelData { final List? permission; ModelData(this.id, this.object, this.ownerBy, this.permission); - factory ModelData.fromJson(Map json) => ModelData( - json['id'] as String, - json['object'] as String, - json['owned_by'] as String, - json['permission'] == null ? null : (json['permission'] as List) - .map((e) => Permission.fromJson(e as Map)) - .toList(), - ); + factory ModelData.fromJson(Map json) => ModelData( + json['id'] as String, + json['object'] as String, + json['owned_by'] as String, + json['permission'] == null + ? null + : (json['permission'] as List) + .map((e) => Permission.fromJson(e as Map)) + .toList(), + ); - Map toJson() =>{ - 'id': id, - 'object': object, - 'owned_by': ownerBy, - 'permission': permission, - }; - -} \ No newline at end of file + Map toJson() => { + 'id': id, + 'object': object, + 'owned_by': ownerBy, + 'permission': permission?.map((e) => e.toJson()).toList(), + }; +} diff --git a/lib/src/model/openai_model/openai_models.dart b/lib/src/model/openai_model/openai_models.dart index dfe7d41..efb78db 100644 --- a/lib/src/model/openai_model/openai_models.dart +++ b/lib/src/model/openai_model/openai_models.dart @@ -13,7 +13,7 @@ class AiModel { ); Map toJson() => { - 'data': data, + 'data': data.map((e) => e.toJson()).toList(), 'object': object, }; } diff --git a/test/model/complete_text/request/complete_text.dart b/test/model/complete_text/request/complete_text_test.dart similarity index 100% rename from test/model/complete_text/request/complete_text.dart rename to test/model/complete_text/request/complete_text_test.dart diff --git a/test/model/complete_text/response/choices_test.dart b/test/model/complete_text/response/choices_test.dart new file mode 100644 index 0000000..1ccc095 --- /dev/null +++ b/test/model/complete_text/response/choices_test.dart @@ -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())); + }); + + 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'); + }); +} diff --git a/test/model/complete_text/response/complete_response_test.dart b/test/model/complete_text/response/complete_response_test.dart new file mode 100644 index 0000000..d29a3a5 --- /dev/null +++ b/test/model/complete_text/response/complete_response_test.dart @@ -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())); + }); + + 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); + }); +} diff --git a/test/model/gen_image/response/GenImgResponse_test.dart b/test/model/gen_image/response/GenImgResponse_test.dart new file mode 100644 index 0000000..491c098 --- /dev/null +++ b/test/model/gen_image/response/GenImgResponse_test.dart @@ -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()), + ); + }, + ); + + 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)); + }); +} diff --git a/test/model/gen_image/response/image_data_test.dart b/test/model/gen_image/response/image_data_test.dart new file mode 100644 index 0000000..e023265 --- /dev/null +++ b/test/model/gen_image/response/image_data_test.dart @@ -0,0 +1,40 @@ +import 'package:chat_gpt_sdk/src/model/gen_image/response/image_data.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + test('ImageData test with parameters', () { + final String url = 'https://example.com'; + final String b64Json = 'ABC123'; + + final ImageData imageData = ImageData( + url: url, + b64Json: b64Json, + ); + + expect(imageData.url, equals(url)); + expect(imageData.b64Json, equals(b64Json)); + }); + + test('ImageData test without parameters', () { + final ImageData imageData = ImageData(); + + expect(imageData.url, isNull); + expect(imageData.b64Json, isNull); + }); + + test('Test toJson method for ImageData', () { + final imageData = ImageData( + url: 'https://example.com/image.png', + b64Json: 'eyJmb28iOiJiYXIifQ==', + ); + + final expectedJson = { + 'url': 'https://example.com/image.png', + 'b64_json': 'eyJmb28iOiJiYXIifQ==', + }; + + final resultJson = imageData.toJson(); + + expect(resultJson, equals(expectedJson)); + }); +} diff --git a/test/model/openai_engine/engine_data_test.dart b/test/model/openai_engine/engine_data_test.dart new file mode 100644 index 0000000..e0d9232 --- /dev/null +++ b/test/model/openai_engine/engine_data_test.dart @@ -0,0 +1,74 @@ +import 'package:chat_gpt_sdk/src/model/openai_engine/engine_data.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + test('Test engineDataToJson method', () { + final testData = EngineData('test_id', 'test_object', 'test_owner', true); + + final expectedJson = { + 'id': 'test_id', + 'object': 'test_object', + 'owner': 'test_owner', + 'ready': true, + }; + + final actualJson = testData.engineDataToJson(testData); + expect(actualJson, expectedJson); + }); + + test('Test fromJson method with valid input', () { + final inputJson = { + 'id': 'test_id', + 'object': 'test_object', + 'owner': 'test_owner', + 'ready': true, + }; + + final expectedData = + EngineData('test_id', 'test_object', 'test_owner', true); + + final actualData = EngineData.fromJson(inputJson); + expect(actualData.id, expectedData.id); + expect(actualData.object, expectedData.object); + expect(actualData.owner, expectedData.owner); + expect(actualData.ready, expectedData.ready); + }); + + test('Test constructor with valid input', () { + final expectedId = 'test_id'; + final expectedObject = 'test_object'; + final expectedOwner = 'test_owner'; + final expectedReady = true; + + final testData = + EngineData(expectedId, expectedObject, expectedOwner, expectedReady); + + expect(testData.id, expectedId); + expect(testData.object, expectedObject); + expect(testData.owner, expectedOwner); + expect(testData.ready, expectedReady); + }); + + test('Test toJson method', () { + final expectedJson = { + 'id': 'test_id', + 'object': 'test_object', + 'owner': 'test_owner', + 'ready': true, + }; + + final testEngineData = EngineData( + 'test_id', + 'test_object', + 'test_owner', + true, + ); + + final json = testEngineData.toJson(); + + expect(json['id'], expectedJson['id']); + expect(json['object'], expectedJson['object']); + expect(json['owner'], expectedJson['owner']); + expect(json['ready'], expectedJson['ready']); + }); +} diff --git a/test/model/openai_engine/engine_model_test.dart b/test/model/openai_engine/engine_model_test.dart new file mode 100644 index 0000000..d17d976 --- /dev/null +++ b/test/model/openai_engine/engine_model_test.dart @@ -0,0 +1,93 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:chat_gpt_sdk/chat_gpt_sdk.dart'; +import 'package:chat_gpt_sdk/src/model/openai_engine/engine_data.dart'; + +void main() { + test('Test constructor with valid input', () { + final expectedData = [ + EngineData('id_1', 'object_1', 'owner_1', true), + EngineData('id_2', 'object_2', 'owner_2', false), + ]; + final expectedObject = 'test_object'; + + final testModel = EngineModel(expectedData, expectedObject); + + expect(testModel.data, expectedData); + expect(testModel.object, expectedObject); + }); + + test('Test fromJson method', () { + final expectedData = [ + EngineData('id_1', 'object_1', 'owner_1', true), + EngineData('id_2', 'object_2', 'owner_2', false), + ]; + final expectedObject = 'test_object'; + + final json = { + 'data': [ + {'id': 'id_1', 'object': 'object_1', 'owner': 'owner_1', 'ready': true}, + { + 'id': 'id_2', + 'object': 'object_2', + 'owner': 'owner_2', + 'ready': false + }, + ], + 'object': 'test_object' + }; + + final testModel = EngineModel.fromJson(json); + + expect(testModel.data.length, expectedData.length); + expect(testModel.data[0].id, expectedData[0].id); + expect(testModel.data[0].object, expectedData[0].object); + expect(testModel.data[0].owner, expectedData[0].owner); + expect(testModel.data[0].ready, expectedData[0].ready); + expect(testModel.data[1].id, expectedData[1].id); + expect(testModel.data[1].object, expectedData[1].object); + expect(testModel.data[1].owner, expectedData[1].owner); + expect(testModel.data[1].ready, expectedData[1].ready); + expect(testModel.object, expectedObject); + }); + + test('Test toJson method', () { + final engineData1 = + EngineData('engine_id_1', 'engine_object_1', 'engine_owner_1', true); + final engineData2 = + EngineData('engine_id_2', 'engine_object_2', 'engine_owner_2', false); + final engineModel = + EngineModel([engineData1, engineData2], 'engine_object'); + + final expectedJson = { + 'data': [ + { + 'id': 'engine_id_1', + 'object': 'engine_object_1', + 'owner': 'engine_owner_1', + 'ready': true, + }, + { + 'id': 'engine_id_2', + 'object': 'engine_object_2', + 'owner': 'engine_owner_2', + 'ready': false, + }, + ], + 'object': 'engine_object', + }; + + final engineModelJson = engineModel.toJson(); + expect(engineModelJson['object'], expectedJson['object']); + + final data = expectedJson['data'] as List>; + expect(engineModelJson['data'].length, data.length); + expect(engineModelJson['data'][0].id, data[0]['id']); + expect(engineModelJson['data'][0].object, data[0]['object']); + expect(engineModelJson['data'][0].owner, data[0]['owner']); + expect(engineModelJson['data'][0].ready, data[0]['ready']); + expect(engineModelJson['data'][1].id, data[1]['id']); + expect(engineModelJson['data'][1].object, data[1]['object']); + expect(engineModelJson['data'][1].owner, data[1]['owner']); + expect(engineModelJson['data'][1].ready, data[1]['ready']); + }); +} diff --git a/test/model/openai_model/openai_model_data_test.dart b/test/model/openai_model/openai_model_data_test.dart new file mode 100644 index 0000000..d6dc056 --- /dev/null +++ b/test/model/openai_model/openai_model_data_test.dart @@ -0,0 +1,189 @@ +import 'package:chat_gpt_sdk/src/model/openai_model/openai_model_data.dart'; +import 'package:chat_gpt_sdk/src/model/openai_model/permission.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + test('Test fromJson method', () { + final permission = Permission( + 'test_permission_id', + 'test_permission_object', + 123, + true, + false, + true, + false, + true, + false, + 'test_organization', + 'test_group', + null, + ); + + final modelData = ModelData( + 'test_id', + 'test_object', + 'test_owner', + [permission], + ); + + expect(modelData.id, 'test_id'); + expect(modelData.object, 'test_object'); + expect(modelData.ownerBy, 'test_owner'); + expect(modelData.permission!.length, 1); + expect(modelData.permission![0].id, 'test_permission_id'); + expect(modelData.permission![0].object, 'test_permission_object'); + expect(modelData.permission![0].created, 123); + expect(modelData.permission![0].allowCreate_engine, true); + expect(modelData.permission![0].allowSampling, false); + expect(modelData.permission![0].allowLogprobs, true); + expect(modelData.permission![0].allowSearchIndices, false); + expect(modelData.permission![0].allowView, true); + expect(modelData.permission![0].allowFineTuning, false); + expect(modelData.permission![0].organization, 'test_organization'); + expect(modelData.permission![0].group, 'test_group'); + expect(modelData.permission![0].isBlocking, null); + }); + + test('Test fromJson method with all fields', () { + final json = { + 'id': 'test_id', + 'object': 'test_object', + 'owned_by': 'test_owner', + 'permission': [ + { + 'id': 'test_permission_id', + 'object': 'test_permission_object', + 'created': 1234567890, + 'allow_create_engine': true, + 'allow_sampling': false, + 'allow_logprobs': true, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': true, + 'organization': 'test_org', + 'group': null, + 'is_blocking': false, + } + ] + }; + + final testModelData = ModelData.fromJson(json); + + expect(testModelData.id, 'test_id'); + expect(testModelData.object, 'test_object'); + expect(testModelData.ownerBy, 'test_owner'); + + expect(testModelData.permission!.length, 1); + expect(testModelData.permission![0].id, 'test_permission_id'); + expect(testModelData.permission![0].object, 'test_permission_object'); + expect(testModelData.permission![0].created, 1234567890); + expect(testModelData.permission![0].allowCreate_engine, true); + expect(testModelData.permission![0].allowSampling, false); + expect(testModelData.permission![0].allowLogprobs, true); + expect(testModelData.permission![0].allowSearchIndices, true); + expect(testModelData.permission![0].allowView, false); + expect(testModelData.permission![0].allowFineTuning, true); + expect(testModelData.permission![0].organization, 'test_org'); + expect(testModelData.permission![0].group, null); + expect(testModelData.permission![0].isBlocking, false); + }); + + test('Test fromJson method with null permission field', () { + final json = { + 'id': 'test_id', + 'object': 'test_object', + 'owned_by': 'test_owner', + 'permission': null, + }; + + final testModelData = ModelData.fromJson(json); + + expect(testModelData.id, 'test_id'); + expect(testModelData.object, 'test_object'); + expect(testModelData.ownerBy, 'test_owner'); + expect(testModelData.permission, null); + }); + + group('toJson method test', () { + test('Test toJson method with null permission', () { + final testModelData = + ModelData('test_id', 'test_object', 'test_owner', null); + final expectedJson = { + 'id': 'test_id', + 'object': 'test_object', + 'owned_by': 'test_owner', + 'permission': null, + }; + expect(testModelData.toJson(), expectedJson); + }); + + test('Test toJson method with non-empty permission', () { + final permission1 = Permission( + 'test_permission_id_1', + 'test_permission_object_1', + 1234567890, + true, + false, + true, + true, + false, + true, + 'test_org_1', + null, + false, + ); + final permission2 = Permission( + 'test_permission_id_2', + 'test_permission_object_2', + 1234567891, + true, + true, + false, + true, + false, + false, + 'test_org_2', + null, + false, + ); + final testModelData = ModelData( + 'test_id', 'test_object', 'test_owner', [permission1, permission2]); + final expectedJson = { + 'id': 'test_id', + 'object': 'test_object', + 'owned_by': 'test_owner', + 'permission': [ + { + 'id': 'test_permission_id_1', + 'object': 'test_permission_object_1', + 'created': 1234567890, + 'allow_create_engine': true, + 'allow_sampling': false, + 'allow_logprobs': true, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': true, + 'organization': 'test_org_1', + 'group': null, + 'is_blocking': false, + }, + { + 'id': 'test_permission_id_2', + 'object': 'test_permission_object_2', + 'created': 1234567891, + 'allow_create_engine': true, + 'allow_sampling': true, + 'allow_logprobs': false, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': false, + 'organization': 'test_org_2', + 'group': null, + 'is_blocking': false, + }, + ], + }; + expect(testModelData.toJson(), expectedJson); + }); + }); +} diff --git a/test/model/openai_model/openai_models_test.dart b/test/model/openai_model/openai_models_test.dart new file mode 100644 index 0000000..921a1ce --- /dev/null +++ b/test/model/openai_model/openai_models_test.dart @@ -0,0 +1,406 @@ +import 'package:chat_gpt_sdk/chat_gpt_sdk.dart'; +import 'package:chat_gpt_sdk/src/model/openai_model/permission.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:chat_gpt_sdk/src/model/openai_model/openai_model_data.dart'; + +void main() { + test('Test fromJson method with empty data', () { + final json = { + 'data': [], + 'object': 'test_object', + }; + + final testAiModel = AiModel.fromJson(json); + + expect(testAiModel.data, []); + expect(testAiModel.object, 'test_object'); + }); + + test('Test fromJson method with non-empty data', () { + final json = { + 'data': [ + { + 'id': 'test_id_1', + 'object': 'test_object_1', + 'owned_by': 'test_owner_1', + 'permission': null, + }, + { + 'id': 'test_id_2', + 'object': 'test_object_2', + 'owned_by': 'test_owner_2', + 'permission': [ + { + 'id': 'test_permission_id_2_1', + 'object': 'test_permission_object_2_1', + 'created': 1234567890, + 'allow_create_engine': true, + 'allow_sampling': false, + 'allow_logprobs': true, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': true, + 'organization': 'test_org_2_1', + 'group': null, + 'is_blocking': false, + }, + { + 'id': 'test_permission_id_2_2', + 'object': 'test_permission_object_2_2', + 'created': 1234567891, + 'allow_create_engine': true, + 'allow_sampling': true, + 'allow_logprobs': false, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': false, + 'organization': 'test_org_2_2', + 'group': null, + 'is_blocking': false, + }, + ], + }, + ], + 'object': 'test_object', + }; + + final testAiModel = AiModel.fromJson(json); + + expect(testAiModel.object, 'test_object'); + expect(testAiModel.data.length, 2); + + final expectedModelData1 = ModelData( + 'test_id_1', + 'test_object_1', + 'test_owner_1', + null, + ); + expect(testAiModel.data[0].id, expectedModelData1.id); + expect(testAiModel.data[0].object, expectedModelData1.object); + expect(testAiModel.data[0].ownerBy, expectedModelData1.ownerBy); + expect(testAiModel.data[0].permission, expectedModelData1.permission); + + final expectedPermission1 = Permission( + 'test_permission_id_2_1', + 'test_permission_object_2_1', + 1234567890, + true, + false, + true, + true, + false, + true, + 'test_org_2_1', + null, + false, + ); + final expectedPermission2 = Permission( + 'test_permission_id_2_2', + 'test_permission_object_2_2', + 1234567891, + true, + true, + false, + true, + false, + false, + 'test_org_2_2', + null, + false, + ); + expect(testAiModel.data[1].id, 'test_id_2'); + expect(testAiModel.data[1].object, 'test_object_2'); + expect(testAiModel.data[1].ownerBy, 'test_owner_2'); + expect(testAiModel.data[1].permission!.length, 2); + expect(testAiModel.data[1].permission![0].id, expectedPermission1.id); + expect( + testAiModel.data[1].permission![0].object, expectedPermission1.object); + expect(testAiModel.data[1].permission![0].created, + expectedPermission1.created); + expect(testAiModel.data[1].permission![0].allowCreate_engine, + expectedPermission1.allowCreate_engine); + expect(testAiModel.data[1].permission![0].allowSampling, + expectedPermission1.allowSampling); + expect(testAiModel.data[1].permission![0].allowLogprobs, + expectedPermission1.allowLogprobs); + expect(testAiModel.data[1].permission![0].allowSearchIndices, + expectedPermission1.allowSearchIndices); + expect(testAiModel.data[1].permission![0].allowView, + expectedPermission1.allowView); + expect(testAiModel.data[1].permission![0].allowFineTuning, + expectedPermission1.allowFineTuning); + expect(testAiModel.data[1].permission![0].organization, + expectedPermission1.organization); + expect(testAiModel.data[1].permission![0].group, expectedPermission1.group); + expect(testAiModel.data[1].permission![0].isBlocking, + expectedPermission1.isBlocking); + + expect(testAiModel.data[1].permission![1].id, expectedPermission2.id); + expect( + testAiModel.data[1].permission![1].object, expectedPermission2.object); + expect(testAiModel.data[1].permission![1].created, + expectedPermission2.created); + expect(testAiModel.data[1].permission![1].allowCreate_engine, + expectedPermission2.allowCreate_engine); + expect(testAiModel.data[1].permission![1].allowSampling, + expectedPermission2.allowSampling); + expect(testAiModel.data[1].permission![1].allowLogprobs, + expectedPermission2.allowLogprobs); + expect(testAiModel.data[1].permission![1].allowSearchIndices, + expectedPermission2.allowSearchIndices); + expect(testAiModel.data[1].permission![1].allowView, + expectedPermission2.allowView); + expect(testAiModel.data[1].permission![1].allowFineTuning, + expectedPermission2.allowFineTuning); + expect(testAiModel.data[1].permission![1].organization, + expectedPermission2.organization); + expect(testAiModel.data[1].permission![1].group, expectedPermission2.group); + expect(testAiModel.data[1].permission![1].isBlocking, + expectedPermission2.isBlocking); + }); + + test('Test constructor with non-empty data', () { + final modelData1 = ModelData( + 'test_id_1', + 'test_object_1', + 'test_owner_1', + null, + ); + + final permission1 = Permission( + 'test_permission_id_2_1', + 'test_permission_object_2_1', + 1234567890, + true, + false, + true, + true, + false, + true, + 'test_org_2_1', + null, + false, + ); + final permission2 = Permission( + 'test_permission_id_2_2', + 'test_permission_object_2_2', + 1234567891, + true, + true, + false, + true, + false, + false, + 'test_org_2_2', + null, + false, + ); + + final aiModel = AiModel( + [ + modelData1, + ModelData('test_id_2', 'test_object_2', 'test_owner_2', + [permission1, permission2]) + ], + 'test_object', + ); + + expect(aiModel.object, 'test_object'); + expect(aiModel.data.length, 2); + + expect(aiModel.data[0].id, 'test_id_1'); + expect(aiModel.data[0].object, 'test_object_1'); + expect(aiModel.data[0].ownerBy, 'test_owner_1'); + expect(aiModel.data[0].permission, null); + + expect(aiModel.data[1].id, 'test_id_2'); + expect(aiModel.data[1].object, 'test_object_2'); + expect(aiModel.data[1].ownerBy, 'test_owner_2'); + expect(aiModel.data[1].permission!.length, 2); + expect(aiModel.data[1].permission![0].id, permission1.id); + expect(aiModel.data[1].permission![0].object, permission1.object); + expect(aiModel.data[1].permission![0].created, permission1.created); + expect(aiModel.data[1].permission![0].allowCreate_engine, + permission1.allowCreate_engine); + expect(aiModel.data[1].permission![0].allowSampling, + permission1.allowSampling); + expect(aiModel.data[1].permission![0].allowLogprobs, + permission1.allowLogprobs); + expect(aiModel.data[1].permission![0].allowSearchIndices, + permission1.allowSearchIndices); + expect(aiModel.data[1].permission![0].allowView, permission1.allowView); + expect(aiModel.data[1].permission![0].allowFineTuning, + permission1.allowFineTuning); + expect( + aiModel.data[1].permission![0].organization, permission1.organization); + expect(aiModel.data[1].permission![0].group, permission1.group); + expect(aiModel.data[1].permission![0].isBlocking, permission1.isBlocking); + + expect(aiModel.data[1].permission![1].id, permission2.id); + expect(aiModel.data[1].permission![1].object, permission2.object); + expect(aiModel.data[1].permission![1].created, permission2.created); + expect(aiModel.data[1].permission![1].allowCreate_engine, + permission2.allowCreate_engine); + expect(aiModel.data[1].permission![1].allowSampling, + permission2.allowSampling); + expect(aiModel.data[1].permission![1].allowLogprobs, + permission2.allowLogprobs); + expect(aiModel.data[1].permission![1].allowSearchIndices, + permission2.allowSearchIndices); + expect(aiModel.data[1].permission![1].allowView, permission2.allowView); + expect(aiModel.data[1].permission![1].allowFineTuning, + permission2.allowFineTuning); + expect( + aiModel.data[1].permission![1].organization, permission2.organization); + expect(aiModel.data[1].permission![1].group, permission2.group); + expect(aiModel.data[1].permission![1].isBlocking, permission2.isBlocking); + }); + + test('toJson returns expected JSON', () { + final expectedJson = { + 'data': [ + { + 'id': 'test_id_1', + 'object': 'test_object_1', + 'owned_by': 'test_owner_1', + 'permission': null, + }, + { + 'id': 'test_id_2', + 'object': 'test_object_2', + 'owned_by': 'test_owner_2', + 'permission': [ + { + 'id': 'test_permission_id_2_1', + 'object': 'test_permission_object_2_1', + 'created': 1234567890, + 'allow_create_engine': true, + 'allow_sampling': false, + 'allow_logprobs': true, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': true, + 'organization': 'test_org_2_1', + 'group': null, + 'is_blocking': false, + }, + { + 'id': 'test_permission_id_2_2', + 'object': 'test_permission_object_2_2', + 'created': 1234567891, + 'allow_create_engine': true, + 'allow_sampling': true, + 'allow_logprobs': false, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': false, + 'organization': 'test_org_2_2', + 'group': null, + 'is_blocking': false, + }, + ], + }, + ], + 'object': 'test_object', + }; + + final testAiModel = AiModel.fromJson(expectedJson); + final actualJson = testAiModel.toJson(); + + expect(actualJson['object'], expectedJson['object']); + }); + + test('Test AiModel toJson method', () { + final modelData1 = ModelData( + 'test_id_1', + 'test_object_1', + 'test_owner_1', + null, + ); + final modelData2 = ModelData( + 'test_id_2', + 'test_object_2', + 'test_owner_2', + [ + Permission( + 'test_permission_id_2_1', + 'test_permission_object_2_1', + 1234567890, + true, + false, + true, + true, + false, + true, + 'test_org_2_1', + null, + false, + ), + Permission( + 'test_permission_id_2_2', + 'test_permission_object_2_2', + 1234567891, + true, + true, + false, + true, + false, + false, + 'test_org_2_2', + null, + false, + ), + ], + ); + final aiModel = AiModel([modelData1, modelData2], 'test_object'); + + final expectedJson = { + 'data': [ + { + 'id': 'test_id_1', + 'object': 'test_object_1', + 'owned_by': 'test_owner_1', + 'permission': null, + }, + { + 'id': 'test_id_2', + 'object': 'test_object_2', + 'owned_by': 'test_owner_2', + 'permission': [ + { + 'id': 'test_permission_id_2_1', + 'object': 'test_permission_object_2_1', + 'created': 1234567890, + 'allow_create_engine': true, + 'allow_sampling': false, + 'allow_logprobs': true, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': true, + 'organization': 'test_org_2_1', + 'group': null, + 'is_blocking': false, + }, + { + 'id': 'test_permission_id_2_2', + 'object': 'test_permission_object_2_2', + 'created': 1234567891, + 'allow_create_engine': true, + 'allow_sampling': true, + 'allow_logprobs': false, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': false, + 'organization': 'test_org_2_2', + 'group': null, + 'is_blocking': false, + }, + ], + }, + ], + 'object': 'test_object', + }; + + expect(aiModel.toJson(), expectedJson); + }); +} diff --git a/test/model/openai_model/permission_test.dart b/test/model/openai_model/permission_test.dart new file mode 100644 index 0000000..e9b7e50 --- /dev/null +++ b/test/model/openai_model/permission_test.dart @@ -0,0 +1,128 @@ +import 'package:chat_gpt_sdk/src/model/openai_model/permission.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + test('Test constructor with valid input', () { + final expectedId = 'test_id'; + final expectedObject = 'test_object'; + final expectedCreated = 12345; + final expectedAllowCreateEngine = true; + final expectedAllowSampling = false; + final expectedAllowLogprobs = true; + final expectedAllowSearchIndices = false; + final expectedAllowView = true; + final expectedAllowFineTuning = false; + final expectedOrganization = 'test_org'; + final expectedGroup = 'test_group'; + final expectedIsBlocking = true; + + final testPermission = Permission( + expectedId, + expectedObject, + expectedCreated, + expectedAllowCreateEngine, + expectedAllowSampling, + expectedAllowLogprobs, + expectedAllowSearchIndices, + expectedAllowView, + expectedAllowFineTuning, + expectedOrganization, + expectedGroup, + expectedIsBlocking, + ); + + expect(testPermission.id, expectedId); + expect(testPermission.object, expectedObject); + expect(testPermission.created, expectedCreated); + expect(testPermission.allowCreate_engine, expectedAllowCreateEngine); + expect(testPermission.allowSampling, expectedAllowSampling); + expect(testPermission.allowLogprobs, expectedAllowLogprobs); + expect(testPermission.allowSearchIndices, expectedAllowSearchIndices); + expect(testPermission.allowView, expectedAllowView); + expect(testPermission.allowFineTuning, expectedAllowFineTuning); + expect(testPermission.organization, expectedOrganization); + expect(testPermission.group, expectedGroup); + expect(testPermission.isBlocking, expectedIsBlocking); + }); + + test('Test fromJson method', () { + final expectedId = 'test_id'; + final expectedObject = 'test_object'; + final expectedCreated = 12345; + final expectedAllowCreateEngine = true; + final expectedAllowSampling = false; + final expectedAllowLogprobs = true; + final expectedAllowSearchIndices = false; + final expectedAllowView = true; + final expectedAllowFineTuning = false; + final expectedOrganization = 'test_org'; + final expectedGroup = 'test_group'; + final expectedIsBlocking = true; + + final json = { + 'id': expectedId, + 'object': expectedObject, + 'created': expectedCreated, + 'allow_create_engine': expectedAllowCreateEngine, + 'allow_sampling': expectedAllowSampling, + 'allow_logprobs': expectedAllowLogprobs, + 'allow_search_indices': expectedAllowSearchIndices, + 'allow_view': expectedAllowView, + 'allow_fine_tuning': expectedAllowFineTuning, + 'organization': expectedOrganization, + 'group': expectedGroup, + 'is_blocking': expectedIsBlocking, + }; + + final testPermission = Permission.fromJson(json); + + expect(testPermission.id, expectedId); + expect(testPermission.object, expectedObject); + expect(testPermission.created, expectedCreated); + expect(testPermission.allowCreate_engine, expectedAllowCreateEngine); + expect(testPermission.allowSampling, expectedAllowSampling); + expect(testPermission.allowLogprobs, expectedAllowLogprobs); + expect(testPermission.allowSearchIndices, expectedAllowSearchIndices); + expect(testPermission.allowView, expectedAllowView); + expect(testPermission.allowFineTuning, expectedAllowFineTuning); + expect(testPermission.organization, expectedOrganization); + expect(testPermission.group, expectedGroup); + expect(testPermission.isBlocking, expectedIsBlocking); + }); + + test('Test Permission toJson method', () { + final permission = Permission( + 'test_permission_id', + 'test_permission_object', + 1234567890, + true, + false, + true, + true, + false, + true, + 'test_org', + null, + false, + ); + + final expectedJson = { + 'id': 'test_permission_id', + 'object': 'test_permission_object', + 'created': 1234567890, + 'allow_create_engine': true, + 'allow_sampling': false, + 'allow_logprobs': true, + 'allow_search_indices': true, + 'allow_view': false, + 'allow_fine_tuning': true, + 'organization': 'test_org', + 'group': null, + 'is_blocking': false, + }; + + final json = permission.toJson(); + + expect(json, equals(expectedJson)); + }); +}