Skip to content

Commit 19a2643

Browse files
authored
Addition of more comprehensive union tests (#42)
1 parent 332120f commit 19a2643

File tree

6 files changed

+395
-3
lines changed

6 files changed

+395
-3
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
Test:
11-
name: 'openapi_sec'
11+
name: 'openapi_spec_tests'
1212
runs-on: ubuntu-22.04
1313

1414
steps:

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.DEFAULT_TARGET: help
22
.PHONY: build test coverage docs example
33

4+
TEST_ARGS ?=
5+
46
help:
57
@echo "Package Makefile"
68

@@ -28,11 +30,14 @@ example:
2830
rm -rf build
2931
dart run example/example.dart
3032

33+
# Example use:
34+
# make test
35+
# make test TEST_ARGS="-n Unions"
3136
test:
3237
@dart pub get && \
3338
rm -rf test/tmp && \
3439
clear && \
35-
dart test && \
40+
dart test $(TEST_ARGS) && \
3641
make build-test
3742

3843
format:

test/openai/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void main() {
3737
spec.toJsonFile(destination: testJson);
3838
});
3939

40-
// / Test code generation of OpenAPI spec defined models
40+
/// Test code generation of OpenAPI spec defined models
4141
test('Generate Schema Code', () async {
4242
await spec.generate(
4343
package: 'openai',

test/openapi_spec_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'petstore/test.dart' as petstore;
44
import 'oas_examples/test.dart' as oas_examples;
55
import 'chromadb/test.dart' as chromadb;
66
import 'openai/test.dart' as openai;
7+
import 'unions/test.dart' as unions;
78

89
void main() {
910
// Ensure a clean test tmp directory
@@ -17,4 +18,5 @@ void main() {
1718
oas_examples.main();
1819
chromadb.main();
1920
openai.main();
21+
unions.main();
2022
}

test/unions/test.dart

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import 'dart:io';
2+
import 'package:path/path.dart' as p;
3+
import 'package:test/test.dart';
4+
5+
import 'package:openapi_spec/openapi_spec.dart';
6+
7+
void main() {
8+
final tmp = Directory(
9+
p.join('test', 'tmp', 'unions'),
10+
);
11+
12+
final truthJson = p.join('test', 'unions', 'unions.json');
13+
final testJson = p.join(tmp.path, 'unions.json');
14+
15+
final genSchemaDir = p.join(tmp.path, 'gen_schema');
16+
final genSchemaOptionsDir = p.join(tmp.path, 'gen_schema_options');
17+
final genSchemaSingleDir = p.join(tmp.path, 'gen_schema_single');
18+
19+
late OpenApi spec;
20+
21+
group('Unions', () {
22+
setUp(() {
23+
spec = OpenApi.fromFile(source: truthJson);
24+
if (!tmp.existsSync()) {
25+
tmp.createSync(recursive: true);
26+
}
27+
});
28+
29+
/// Test JSON to Dart [OpenApi] object parsing
30+
test('JSON -> Dart', () {
31+
// Ensure generated file can be read back into Dart
32+
final spec = OpenApi.fromFile(
33+
source: truthJson,
34+
);
35+
36+
// Write the Dart representation back to JSON
37+
spec.toJsonFile(destination: testJson);
38+
});
39+
40+
/// Test schema generation (multi-file)
41+
test('Generate Schema Code', () async {
42+
await spec.generate(
43+
package: 'unions',
44+
quiet: true,
45+
destination: genSchemaDir,
46+
);
47+
final expectedFiles = [
48+
'alpha_training.dart',
49+
'beta_training.dart',
50+
'alpha_transform.dart',
51+
'beta_transform.dart',
52+
'gamma_transform.dart',
53+
'group_transform.dart',
54+
'schema.dart',
55+
'training_setup.dart',
56+
'union_training.dart',
57+
'union_transform.dart',
58+
'union_transform2.dart',
59+
];
60+
final genFiles = Directory(p.join(genSchemaDir, 'schema'))
61+
.listSync()
62+
.map((e) => p.basename(e.path));
63+
for (final file in expectedFiles) {
64+
expect(genFiles, contains(file));
65+
}
66+
});
67+
68+
/// Test schema generation (single file)
69+
test('Generate Schema Code', () async {
70+
await spec.generate(
71+
package: 'unions',
72+
quiet: true,
73+
destination: genSchemaSingleDir,
74+
schemaOptions: SchemaGeneratorOptions(
75+
singleFile: true,
76+
),
77+
);
78+
final genFiles =
79+
Directory(p.join(genSchemaSingleDir, 'schema')).listSync();
80+
expect(genFiles.length, 1);
81+
});
82+
83+
/// Test schema generation with union customizations
84+
test('Generate Schema Code (Options)', () async {
85+
await spec.generate(
86+
package: 'unions',
87+
quiet: true,
88+
destination: genSchemaOptionsDir,
89+
schemaOptions: SchemaGeneratorOptions(
90+
onSchemaUnionName: (name, unions) {
91+
if (name == 'UnionTransform' && unions.length == 3) {
92+
return 'UnionTransformMember';
93+
}
94+
return name;
95+
},
96+
),
97+
);
98+
final expectedFiles = [
99+
'alpha_training.dart',
100+
'beta_training.dart',
101+
'alpha_transform.dart',
102+
'beta_transform.dart',
103+
'gamma_transform.dart',
104+
'group_transform.dart',
105+
'schema.dart',
106+
'training_setup.dart',
107+
'union_training.dart',
108+
'union_transform.dart',
109+
// This file should be renamed from default "union_transform2.dart"
110+
'union_transform_member.dart',
111+
];
112+
final genFiles = Directory(p.join(genSchemaOptionsDir, 'schema'))
113+
.listSync()
114+
.map((e) => p.basename(e.path));
115+
for (final file in expectedFiles) {
116+
expect(genFiles, contains(file));
117+
}
118+
});
119+
});
120+
}

0 commit comments

Comments
 (0)