@@ -2,10 +2,13 @@ import { beforeEach, describe, it, mock } from 'node:test';
2
2
import { SchemaGenerator , parseDatabaseUrl } from './generate_schema.js' ;
3
3
import assert from 'node:assert' ;
4
4
import {
5
+ EmptySchemaError ,
6
+ InvalidSchemaError ,
5
7
TypescriptDataSchemaGenerator ,
6
8
TypescriptDataSchemaGeneratorConfig ,
7
9
} from '@aws-amplify/graphql-schema-generator' ;
8
10
import fs from 'fs/promises' ;
11
+ import { AmplifyUserError } from '@aws-amplify/platform-core' ;
9
12
10
13
const mockGenerateMethod =
11
14
mock . fn < ( config : TypescriptDataSchemaGeneratorConfig ) => Promise < string > > ( ) ;
@@ -164,4 +167,53 @@ void describe('SchemaGenerator', () => {
164
167
'Unable to parse the database URL. Unsupported database engine: incorrect' ,
165
168
} ) ;
166
169
} ) ;
170
+
171
+ void it ( 'should throw error if database schema is incorrect' , async ( ) => {
172
+ mockGenerateMethod . mock . mockImplementationOnce ( ( ) => {
173
+ throw new InvalidSchemaError ( [ { } ] , [ 'missingColumn' ] ) ;
174
+ } ) ;
175
+ const schemaGenerator = new SchemaGenerator ( ) ;
176
+ await assert . rejects (
177
+ ( ) =>
178
+ schemaGenerator . generate ( {
179
+ connectionUri : {
180
+ secretName : 'FAKE_SECRET_NAME' ,
181
+ value : 'mysql://user:password@hostname:3306/db' ,
182
+ } ,
183
+ out : 'schema.ts' ,
184
+ } ) ,
185
+ ( error : AmplifyUserError ) => {
186
+ assert . strictEqual ( error . name , 'DatabaseSchemaError' ) ;
187
+ assert . strictEqual (
188
+ error . message ,
189
+ 'Imported SQL schema is invalid. Imported schema is missing columns: missingColumn'
190
+ ) ;
191
+ assert . strictEqual ( error . resolution , 'Check the database schema.' ) ;
192
+ return true ;
193
+ }
194
+ ) ;
195
+ } ) ;
196
+
197
+ void it ( 'should throw error if database schema is empty' , async ( ) => {
198
+ mockGenerateMethod . mock . mockImplementationOnce ( ( ) => {
199
+ throw new EmptySchemaError ( ) ;
200
+ } ) ;
201
+ const schemaGenerator = new SchemaGenerator ( ) ;
202
+ await assert . rejects (
203
+ ( ) =>
204
+ schemaGenerator . generate ( {
205
+ connectionUri : {
206
+ secretName : 'FAKE_SECRET_NAME' ,
207
+ value : 'mysql://user:password@hostname:3306/db' ,
208
+ } ,
209
+ out : 'schema.ts' ,
210
+ } ) ,
211
+ ( error : AmplifyUserError ) => {
212
+ assert . strictEqual ( error . name , 'DatabaseSchemaError' ) ;
213
+ assert . strictEqual ( error . message , 'Imported SQL schema is empty.' ) ;
214
+ assert . strictEqual ( error . resolution , 'Check the database schema.' ) ;
215
+ return true ;
216
+ }
217
+ ) ;
218
+ } ) ;
167
219
} ) ;
0 commit comments