Skip to content

Commit

Permalink
Merge pull request #28 from samchon/features/checker
Browse files Browse the repository at this point in the history
Enhance test program and functions
  • Loading branch information
samchon authored Jul 5, 2024
2 parents e738a08 + dd8e5a5 commit 0cf6d4e
Show file tree
Hide file tree
Showing 19 changed files with 945 additions and 92 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 20.x
- uses: pnpm/action-setup@v2
with:
version: 8

- name: Install dependencies
run: pnpm install
run: npm install
- name: Build
run: npm run build
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samchon/openapi",
"version": "0.3.0",
"version": "0.3.1",
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions test/features/test_document_convert_v20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { OpenApi, SwaggerV2 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_convert_v20 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v2.0`;
for (const file of await fs.promises.readdir(path)) {
if (file.endsWith(".json") === false) continue;
const swagger: SwaggerV2.IDocument = typia.assert<SwaggerV2.IDocument>(
JSON.parse(await fs.promises.readFile(`${path}/${file}`, "utf8")),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
typia.assert(openapi);
}
};
15 changes: 15 additions & 0 deletions test/features/test_document_convert_v30.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { OpenApi, OpenApiV3 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_convert_v30 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v3.0`;
for (const file of await fs.promises.readdir(path)) {
if (file.endsWith(".json") === false) continue;
const swagger: OpenApiV3.IDocument = typia.assert<OpenApiV3.IDocument>(
JSON.parse(await fs.promises.readFile(`${path}/${file}`, "utf8")),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
typia.assert(openapi);
}
};
15 changes: 15 additions & 0 deletions test/features/test_document_convert_v31.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { OpenApi, OpenApiV3_1 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_convert_v31 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v3.1`;
for (const file of await fs.promises.readdir(path)) {
if (file.endsWith(".json") === false) continue;
const swagger: OpenApiV3_1.IDocument = typia.assert<OpenApiV3_1.IDocument>(
JSON.parse(await fs.promises.readFile(`${path}/${file}`, "utf8")),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
typia.assert(openapi);
}
};
27 changes: 27 additions & 0 deletions test/features/test_document_downgrade_v20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { OpenApi, OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_downgrade_v20 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v3.1`;
for (const directory of await fs.promises.readdir(path)) {
const stats: fs.Stats = await fs.promises.lstat(`${path}/${directory}`);
if (stats.isDirectory() === false) continue;
for (const file of await fs.promises.readdir(`${path}/${directory}`)) {
if (file.endsWith(".json") === false) continue;
const swagger:
| SwaggerV2.IDocument
| OpenApiV3.IDocument
| OpenApiV3_1.IDocument = typia.assert<
SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument
>(
JSON.parse(
await fs.promises.readFile(`${path}/${directory}/${file}`, "utf8"),
),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
const downgraded: SwaggerV2.IDocument = OpenApi.downgrade(openapi, "2.0");
typia.assert(downgraded);
}
}
};
27 changes: 27 additions & 0 deletions test/features/test_document_downgrade_v30.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { OpenApi, OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_downgrade_v30 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v3.1`;
for (const directory of await fs.promises.readdir(path)) {
const stats: fs.Stats = await fs.promises.lstat(`${path}/${directory}`);
if (stats.isDirectory() === false) continue;
for (const file of await fs.promises.readdir(`${path}/${directory}`)) {
if (file.endsWith(".json") === false) continue;
const swagger:
| SwaggerV2.IDocument
| OpenApiV3.IDocument
| OpenApiV3_1.IDocument = typia.assert<
SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument
>(
JSON.parse(
await fs.promises.readFile(`${path}/${directory}/${file}`, "utf8"),
),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
const downgraded: OpenApiV3.IDocument = OpenApi.downgrade(openapi, "3.0");
typia.assert(downgraded);
}
}
};
16 changes: 16 additions & 0 deletions test/features/test_document_migrate_v20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IMigrateDocument, OpenApi, SwaggerV2 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_migrate_v20 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v2.0`;
for (const file of await fs.promises.readdir(path)) {
if (file.endsWith(".json") === false) continue;
const swagger: SwaggerV2.IDocument = typia.assert<SwaggerV2.IDocument>(
JSON.parse(await fs.promises.readFile(`${path}/${file}`, "utf8")),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
const migrate: IMigrateDocument = OpenApi.migrate(openapi);
typia.assert(migrate);
}
};
16 changes: 16 additions & 0 deletions test/features/test_document_migrate_v30.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IMigrateDocument, OpenApi, OpenApiV3 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_migrate_v30 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v3.0`;
for (const file of await fs.promises.readdir(path)) {
if (file.endsWith(".json") === false) continue;
const swagger: OpenApiV3.IDocument = typia.assert<OpenApiV3.IDocument>(
JSON.parse(await fs.promises.readFile(`${path}/${file}`, "utf8")),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
const migrate: IMigrateDocument = OpenApi.migrate(openapi);
typia.assert(migrate);
}
};
16 changes: 16 additions & 0 deletions test/features/test_document_migrate_v31.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IMigrateDocument, OpenApi, OpenApiV3_1 } from "@samchon/openapi";
import fs from "fs";
import typia from "typia";

export const test_document_migrate_v31 = async (): Promise<void> => {
const path: string = `${__dirname}/../../../examples/v3.1`;
for (const file of await fs.promises.readdir(path)) {
if (file.endsWith(".json") === false) continue;
const swagger: OpenApiV3_1.IDocument = typia.assert<OpenApiV3_1.IDocument>(
JSON.parse(await fs.promises.readFile(`${path}/${file}`, "utf8")),
);
const openapi: OpenApi.IDocument = OpenApi.convert(swagger);
const migrate: IMigrateDocument = OpenApi.migrate(openapi);
typia.assert(migrate);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestValidator } from "@nestia/e2e";
import { OpenApi, SwaggerV2 } from "@samchon/openapi";
import { SwaggerV2Downgrader } from "@samchon/openapi/lib/internal/SwaggerV2Downgrader";

export const test_downgrade_v20 = () => {
export const test_json_schema_downgrade_v20 = () => {
const schema: OpenApi.IJsonSchema = {
oneOf: [{ const: "a" }, { const: "b" }, { const: "c" }],
title: "something",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestValidator } from "@nestia/e2e";
import { OpenApi, OpenApiV3 } from "@samchon/openapi";
import { OpenApiV3Downgrader } from "@samchon/openapi/lib/internal/OpenApiV3Downgrader";

export const test_downgrade_v30 = () => {
export const test_json_schema_downgrade_v30 = () => {
const schema: OpenApi.IJsonSchema = {
oneOf: [{ const: "a" }, { const: "b" }, { const: "c" }],
title: "something",
Expand Down
74 changes: 74 additions & 0 deletions test/features/test_json_schema_type_checker_cover_any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { TestValidator } from "@nestia/e2e";
import { OpenApiTypeChecker } from "@samchon/openapi";

export const test_json_schema_type_checker_cover_any = (): void => {
TestValidator.equals("any covers (string | null)")(true)(
OpenApiTypeChecker.covers({})(
{
type: undefined,
},
{
oneOf: [
{
type: "string",
},
{
type: "null",
},
],
},
),
);
TestValidator.equals("any covers union type")(true)(
OpenApiTypeChecker.covers({})(
{
type: undefined,
},
{
oneOf: [
{
type: "string",
},
{
type: "number",
},
],
},
),
);

TestValidator.equals("(string | null) can't cover any")(false)(
OpenApiTypeChecker.covers({})(
{
oneOf: [
{
type: "string",
},
{
type: "null",
},
],
},
{
type: undefined,
},
),
);
TestValidator.equals("union can't cover any")(false)(
OpenApiTypeChecker.covers({})(
{
oneOf: [
{
type: "string",
},
{
type: "number",
},
],
},
{
type: undefined,
},
),
);
};
Loading

0 comments on commit 0cf6d4e

Please sign in to comment.