Skip to content

Addition of more comprehensive union tests #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
Test:
name: 'openapi_sec'
name: 'openapi_spec_tests'
runs-on: ubuntu-22.04

steps:
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.DEFAULT_TARGET: help
.PHONY: build test coverage docs example

TEST_ARGS ?=

help:
@echo "Package Makefile"

Expand Down Expand Up @@ -28,11 +30,14 @@ example:
rm -rf build
dart run example/example.dart

# Example use:
# make test
# make test TEST_ARGS="-n Unions"
test:
@dart pub get && \
rm -rf test/tmp && \
clear && \
dart test && \
dart test $(TEST_ARGS) && \
make build-test

format:
Expand Down
2 changes: 1 addition & 1 deletion test/openai/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
spec.toJsonFile(destination: testJson);
});

// / Test code generation of OpenAPI spec defined models
/// Test code generation of OpenAPI spec defined models
test('Generate Schema Code', () async {
await spec.generate(
package: 'openai',
Expand Down
2 changes: 2 additions & 0 deletions test/openapi_spec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'petstore/test.dart' as petstore;
import 'oas_examples/test.dart' as oas_examples;
import 'chromadb/test.dart' as chromadb;
import 'openai/test.dart' as openai;
import 'unions/test.dart' as unions;

void main() {
// Ensure a clean test tmp directory
Expand All @@ -17,4 +18,5 @@ void main() {
oas_examples.main();
chromadb.main();
openai.main();
unions.main();
}
120 changes: 120 additions & 0 deletions test/unions/test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import 'package:openapi_spec/openapi_spec.dart';

void main() {
final tmp = Directory(
p.join('test', 'tmp', 'unions'),
);

final truthJson = p.join('test', 'unions', 'unions.json');
final testJson = p.join(tmp.path, 'unions.json');

final genSchemaDir = p.join(tmp.path, 'gen_schema');
final genSchemaOptionsDir = p.join(tmp.path, 'gen_schema_options');
final genSchemaSingleDir = p.join(tmp.path, 'gen_schema_single');

late OpenApi spec;

group('Unions', () {
setUp(() {
spec = OpenApi.fromFile(source: truthJson);
if (!tmp.existsSync()) {
tmp.createSync(recursive: true);
}
});

/// Test JSON to Dart [OpenApi] object parsing
test('JSON -> Dart', () {
// Ensure generated file can be read back into Dart
final spec = OpenApi.fromFile(
source: truthJson,
);

// Write the Dart representation back to JSON
spec.toJsonFile(destination: testJson);
});

/// Test schema generation (multi-file)
test('Generate Schema Code', () async {
await spec.generate(
package: 'unions',
quiet: true,
destination: genSchemaDir,
);
final expectedFiles = [
'alpha_training.dart',
'beta_training.dart',
'alpha_transform.dart',
'beta_transform.dart',
'gamma_transform.dart',
'group_transform.dart',
'schema.dart',
'training_setup.dart',
'union_training.dart',
'union_transform.dart',
'union_transform2.dart',
];
final genFiles = Directory(p.join(genSchemaDir, 'schema'))
.listSync()
.map((e) => p.basename(e.path));
for (final file in expectedFiles) {
expect(genFiles, contains(file));
}
});

/// Test schema generation (single file)
test('Generate Schema Code', () async {
await spec.generate(
package: 'unions',
quiet: true,
destination: genSchemaSingleDir,
schemaOptions: SchemaGeneratorOptions(
singleFile: true,
),
);
final genFiles =
Directory(p.join(genSchemaSingleDir, 'schema')).listSync();
expect(genFiles.length, 1);
});

/// Test schema generation with union customizations
test('Generate Schema Code (Options)', () async {
await spec.generate(
package: 'unions',
quiet: true,
destination: genSchemaOptionsDir,
schemaOptions: SchemaGeneratorOptions(
onSchemaUnionName: (name, unions) {
if (name == 'UnionTransform' && unions.length == 3) {
return 'UnionTransformMember';
}
return name;
},
),
);
final expectedFiles = [
'alpha_training.dart',
'beta_training.dart',
'alpha_transform.dart',
'beta_transform.dart',
'gamma_transform.dart',
'group_transform.dart',
'schema.dart',
'training_setup.dart',
'union_training.dart',
'union_transform.dart',
// This file should be renamed from default "union_transform2.dart"
'union_transform_member.dart',
];
final genFiles = Directory(p.join(genSchemaOptionsDir, 'schema'))
.listSync()
.map((e) => p.basename(e.path));
for (final file in expectedFiles) {
expect(genFiles, contains(file));
}
});
});
}
Loading