|
| 1 | +//typescript |
| 2 | +import { |
| 3 | + dbToOpenApi, |
| 4 | + GeneratePropertyModel, |
| 5 | + ConvertOpenApiToDatabaseModel, |
| 6 | +} from "../../src/utils/nosqlUtils"; |
| 7 | +import { JSONSchema4 } from "json-schema"; |
| 8 | +import { |
| 9 | + OpenApiSchemaTypeDefinition, |
| 10 | +} from "openapi-json-schema"; |
| 11 | +import { |
| 12 | + DatabaseModelResult, |
| 13 | + TableEntity, |
| 14 | +} from "../../src/types/sql-plugin-types"; |
| 15 | +import { GenerateDatabaseModel } from "../../src/utils/sharedUtils"; |
| 16 | + |
| 17 | +describe("dbToOpenApi", () => { |
| 18 | + it("should handle empty entities correctly", () => { |
| 19 | + const dbResult = { |
| 20 | + getEntities: () => ({}), |
| 21 | + } as DatabaseModelResult; |
| 22 | + |
| 23 | + const result = dbToOpenApi(dbResult); |
| 24 | + expect(result).toEqual( |
| 25 | + expect.objectContaining({ |
| 26 | + openapi: "3.0.0", |
| 27 | + info: expect.anything(), |
| 28 | + paths: {}, |
| 29 | + components: { |
| 30 | + schemas: {}, |
| 31 | + }, |
| 32 | + }) |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should process entities and populate schemas", () => { |
| 37 | + const entities: Record<string, TableEntity> = { |
| 38 | + User: { |
| 39 | + name: "User", |
| 40 | + attributes: [ |
| 41 | + { attributeName: "id", attributeType: "string" }, |
| 42 | + { attributeName: "name", attributeType: "string" }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + }; |
| 46 | + const dbResult = GenerateDatabaseModel(entities, []); |
| 47 | + |
| 48 | + const result = dbToOpenApi(dbResult); |
| 49 | + expect(result.components?.schemas).toHaveProperty("User"); |
| 50 | + expect(result.components?.schemas?.User).toEqual( |
| 51 | + expect.objectContaining({ |
| 52 | + type: "object", |
| 53 | + properties: { |
| 54 | + id: expect.any(Object), |
| 55 | + name: expect.any(Object), |
| 56 | + }, |
| 57 | + }) |
| 58 | + ); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("GeneratePropertyModel", () => { |
| 63 | + it("should properly construct a PropertyModel from JSONSchema", () => { |
| 64 | + const jsonSchema: JSONSchema4 = { |
| 65 | + type: "string", |
| 66 | + nullable: true, |
| 67 | + }; |
| 68 | + const propertyName = "username"; |
| 69 | + const tableName = "User"; |
| 70 | + |
| 71 | + const propertyModel = GeneratePropertyModel( |
| 72 | + tableName, |
| 73 | + propertyName, |
| 74 | + jsonSchema |
| 75 | + ); |
| 76 | + expect(propertyModel.TableName).toEqual("`User`"); |
| 77 | + expect(propertyModel.Name).toBe("`username`"); |
| 78 | + expect(propertyModel.ColumnProperties).toContain("string nullable"); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("ConvertOpenApiToDatabaseModel", () => { |
| 83 | + it("should convert empty schemas to minimal DatabaseModel", () => { |
| 84 | + const schemas: Record<string, OpenApiSchemaTypeDefinition> = {}; |
| 85 | + |
| 86 | + const model = ConvertOpenApiToDatabaseModel(schemas); |
| 87 | + expect(model).toEqual( |
| 88 | + expect.objectContaining({ |
| 89 | + Dialect: "nosql", |
| 90 | + TableList: [], |
| 91 | + PrimaryKeyList: [], |
| 92 | + ForeignKeyList: [], |
| 93 | + }) |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should convert populated schemas to DatabaseModel", () => { |
| 98 | + const schemas: Record<string, OpenApiSchemaTypeDefinition> = { |
| 99 | + User: { |
| 100 | + properties: { |
| 101 | + id: { type: "string", $ref: "#/components/schemas/Id" }, |
| 102 | + }, |
| 103 | + type: "object", |
| 104 | + }, |
| 105 | + }; |
| 106 | + |
| 107 | + const model = ConvertOpenApiToDatabaseModel(schemas); |
| 108 | + expect(model.TableList).toHaveLength(1); |
| 109 | + expect(model.ForeignKeyList).toHaveLength(2); // Primary and foreign key relationships |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +// |
0 commit comments