import type { CRUDEngine } from '../engine' ; import { RecordAlreadyExistsError , RecordTypeError } from '../engine/error' ; const TABLE_NAME = 'the-simpsons' ; export const createSpec = { "doesn't save empty values." : async ( engine ) => { const PRIMARY_KEY = 'primary-key' ; const entity = undefined ; try { await engine . create ( TABLE_NAME , PRIMARY_KEY , entity ) ; fail ( new Error ( 'Method is supposed to throw an error.' ) ) ; } catch ( error ) { expect ( error ) . toEqual ( jasmine . any ( RecordTypeError ) ) ; } } , "doesn't save null values." : async ( engine ) => { const PRIMARY_KEY = 'primary-key' ; const entity = undefined ; try { await engine . create ( TABLE_NAME , PRIMARY_KEY , entity ) ; fail ( new Error ( 'Method is supposed to throw an error.' ) ) ; } catch ( error ) { expect ( error ) . toEqual ( jasmine . any ( RecordTypeError ) ) ; } } , 'saves objects.' : async ( engine ) => { const PRIMARY_KEY = 'primary-key' ; const entity = { some : 'value' , } ; const primaryKey = await engine . create ( TABLE_NAME , PRIMARY_KEY , entity ) ; expect ( primaryKey ) . toEqual ( PRIMARY_KEY ) ; const record = await engine . read ( TABLE_NAME , PRIMARY_KEY ) ; expect ( record ) . toEqual ( entity ) ; } , 'saves strings and objects in the same table.' : async ( engine ) => { await engine . create ( TABLE_NAME , 'primary-key-1' , 'hello-world' ) ; const firstRecord = await engine . read ( TABLE_NAME , 'primary-key-1' ) ; expect ( firstRecord ) . toBe ( 'hello-world' ) ; await engine . create ( TABLE_NAME , 'primary-key-2' , { hello : 'world' } ) ; const secondRecord = await engine . read ( TABLE_NAME , 'primary-key-2' ) ; expect ( secondRecord ) . toEqual ( { hello : 'world' } ) ; } , 'saves strings.' : async ( engine ) => { const PRIMARY_KEY = 'primary-key' ; const entity = 'value' ; const primaryKey = await engine . create ( TABLE_NAME , PRIMARY_KEY , entity ) ; expect ( primaryKey ) . toEqual ( PRIMARY_KEY ) ; const record = await engine . read ( TABLE_NAME , PRIMARY_KEY ) ; expect ( record ) . toEqual ( entity ) ; } , 'throws an error when attempting to overwrite a record.' : async ( engine ) => { const PRIMARY_KEY = 'primary-key' ; const firstEntity = { some : 'value' , } ; const secondEntity = { some : 'newer-value' , } ; try { await engine . create ( TABLE_NAME , PRIMARY_KEY , firstEntity ) ; await engine . create ( TABLE_NAME , PRIMARY_KEY , secondEntity ) ; fail ( ) ; } catch ( error ) { expect ( error ) . toEqual ( jasmine . any ( RecordAlreadyExistsError ) ) ; } } , } ;