@@ -84,6 +84,13 @@ describe.concurrent('Schema code generator', () => {
8484 amount: BigInt!
8585 account: Account!
8686 }
87+
88+ type Transfer @entity(immutable: true) {
89+ id: ID!
90+ from: Account!
91+ to: Account!
92+ amount: BigInt!
93+ }
8794 ` ) ;
8895
8996 const generatedTypes = codegen . generateTypes ( ) ;
@@ -92,7 +99,7 @@ describe.concurrent('Schema code generator', () => {
9299 const foo = generatedTypes . find ( ( type : any ) => type . name === 'Foo' ) ;
93100 expect ( foo ) . toBe ( undefined ) ;
94101 // Account and Wallet
95- expect ( generatedTypes . length ) . toBe ( 2 ) ;
102+ expect ( generatedTypes . length ) . toBe ( 3 ) ;
96103 } ) ;
97104
98105 test ( 'Account is an entity with the correct methods' , async ( ) => {
@@ -124,6 +131,21 @@ describe.concurrent('Schema code generator', () => {
124131 }
125132 ` ,
126133 } ,
134+ {
135+ name : 'remove' ,
136+ params : [ ] ,
137+ returnType : new NamedType ( 'void' ) ,
138+ body : `
139+ let id = this.get('id')
140+ assert(id != null, 'Cannot remove Account entity without an ID')
141+ if (id) {
142+ assert(
143+ id.kind == ValueKind.STRING,
144+ \`Entities of type Account must have an ID of type String but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
145+ store.remove('Account', id.toString())
146+ }
147+ ` ,
148+ } ,
127149 {
128150 name : 'loadInBlock' ,
129151 static : true ,
@@ -328,6 +350,21 @@ describe.concurrent('Schema code generator', () => {
328350 }
329351 ` ,
330352 } ,
353+ {
354+ name : 'remove' ,
355+ params : [ ] ,
356+ returnType : new NamedType ( 'void' ) ,
357+ body : `
358+ let id = this.get('id')
359+ assert(id != null, 'Cannot remove Wallet entity without an ID')
360+ if (id) {
361+ assert(
362+ id.kind == ValueKind.STRING,
363+ \`Entities of type Wallet must have an ID of type String but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
364+ store.remove('Wallet', id.toString())
365+ }
366+ ` ,
367+ } ,
331368 {
332369 name : 'loadInBlock' ,
333370 static : true ,
@@ -409,6 +446,137 @@ describe.concurrent('Schema code generator', () => {
409446 ] ,
410447 } ) ;
411448 } ) ;
449+
450+ test ( 'Transfer is an immutable entity with the correct methods' , async ( ) => {
451+ await testEntity ( generatedTypes , {
452+ name : 'Transfer' ,
453+ members : [ ] ,
454+ methods : [
455+ {
456+ name : 'constructor' ,
457+ params : [ new Param ( 'id' , new NamedType ( 'string' ) ) ] ,
458+ returnType : undefined ,
459+ body : `
460+ super()
461+ this.set('id', Value.fromString(id))
462+ ` ,
463+ } ,
464+ {
465+ name : 'save' ,
466+ params : [ ] ,
467+ returnType : new NamedType ( 'void' ) ,
468+ body : `
469+ let id = this.get('id')
470+ assert(id != null, 'Cannot save Transfer entity without an ID')
471+ if (id) {
472+ assert(
473+ id.kind == ValueKind.STRING,
474+ \`Entities of type Transfer must have an ID of type String but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
475+ store.set('Transfer', id.toString(), this)
476+ }
477+ ` ,
478+ } ,
479+ {
480+ name : 'loadInBlock' ,
481+ static : true ,
482+ params : [ new Param ( 'id' , new NamedType ( 'string' ) ) ] ,
483+ returnType : new NullableType ( new NamedType ( 'Transfer' ) ) ,
484+ body : `
485+ return changetype<Transfer | null>(store.get_in_block('Transfer', id))
486+ ` ,
487+ } ,
488+ {
489+ name : 'load' ,
490+ static : true ,
491+ params : [ new Param ( 'id' , new NamedType ( 'string' ) ) ] ,
492+ returnType : new NullableType ( new NamedType ( 'Transfer' ) ) ,
493+ body : `
494+ return changetype<Transfer | null>(store.get('Transfer', id))
495+ ` ,
496+ } ,
497+ {
498+ name : 'get id' ,
499+ params : [ ] ,
500+ returnType : new NamedType ( 'string' ) ,
501+ body : `let value = this.get('id')
502+ if (!value || value.kind == ValueKind.NULL) {
503+ throw new Error("Cannot return null for a required field.")
504+ } else {
505+ return value.toString()
506+ }
507+ ` ,
508+ } ,
509+ {
510+ name : 'set id' ,
511+ params : [ new Param ( 'value' , new NamedType ( 'string' ) ) ] ,
512+ returnType : undefined ,
513+ body : `
514+ this.set('id', Value.fromString(value))
515+ ` ,
516+ } ,
517+ {
518+ name : 'get from' ,
519+ params : [ ] ,
520+ returnType : new NamedType ( 'string' ) ,
521+ body : `let value = this.get('from')
522+ if (!value || value.kind == ValueKind.NULL) {
523+ throw new Error("Cannot return null for a required field.")
524+ } else {
525+ return value.toString()
526+ }
527+ ` ,
528+ } ,
529+ {
530+ name : 'set from' ,
531+ params : [ new Param ( 'value' , new NamedType ( 'string' ) ) ] ,
532+ returnType : undefined ,
533+ body : `
534+ this.set('from', Value.fromString(value))
535+ ` ,
536+ } ,
537+ {
538+ name : 'get to' ,
539+ params : [ ] ,
540+ returnType : new NamedType ( 'string' ) ,
541+ body : `let value = this.get('to')
542+ if (!value || value.kind == ValueKind.NULL) {
543+ throw new Error("Cannot return null for a required field.")
544+ } else {
545+ return value.toString()
546+ }
547+ ` ,
548+ } ,
549+ {
550+ name : 'set to' ,
551+ params : [ new Param ( 'value' , new NamedType ( 'string' ) ) ] ,
552+ returnType : undefined ,
553+ body : `
554+ this.set('to', Value.fromString(value))
555+ ` ,
556+ } ,
557+ {
558+ name : 'get amount' ,
559+ params : [ ] ,
560+ returnType : new NamedType ( 'BigInt' ) ,
561+ body : `let value = this.get('amount')
562+ if (!value || value.kind == ValueKind.NULL) {
563+ throw new Error("Cannot return null for a required field.")
564+ } else {
565+ return value.toBigInt()
566+ }
567+ ` ,
568+ } ,
569+ {
570+ name : 'set amount' ,
571+ params : [ new Param ( 'value' , new NamedType ( 'BigInt' ) ) ] ,
572+ returnType : undefined ,
573+ body : `
574+ this.set('amount', Value.fromBigInt(value))
575+ ` ,
576+ } ,
577+ ] ,
578+ } ) ;
579+ } ) ;
412580 } ) ;
413581
414582 test ( 'Should handle references with Bytes id types' , async ( ) => {
@@ -458,6 +626,21 @@ describe.concurrent('Schema code generator', () => {
458626 " store.set('Task', id.toBytes().toHexString(), this)\n" +
459627 ' }' ,
460628 } ,
629+ {
630+ name : 'remove' ,
631+ params : [ ] ,
632+ returnType : new NamedType ( 'void' ) ,
633+ body : `
634+ let id = this.get('id')
635+ assert(id != null, 'Cannot remove Task entity without an ID')
636+ if (id) {
637+ assert(
638+ id.kind == ValueKind.STRING,
639+ \`Entities of type Task must have an ID of type String but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
640+ store.remove('Task', id.toString())
641+ }
642+ ` ,
643+ } ,
461644 {
462645 name : 'loadInBlock' ,
463646 static : true ,
@@ -579,7 +762,19 @@ describe.concurrent('Schema code generator', () => {
579762 }
580763 ` ,
581764 } ,
582-
765+ {
766+ name : 'remove' ,
767+ params : [ ] ,
768+ returnType : new NamedType ( 'void' ) ,
769+ body : `
770+ let id = this.get('id');
771+ assert(id != null, 'Cannot remove WithBytes entity without an ID');
772+ if (id) {
773+ assert(id.kind == ValueKind.BYTES, \`Entities of type WithBytes must have an ID of type Bytes but the id '\${id.displayData()}' is of type \${id.displayKind()}\`);
774+ store.remove('WithBytes', id.toBytes().toHexString());
775+ }
776+ ` ,
777+ } ,
583778 {
584779 name : 'load' ,
585780 static : true ,
0 commit comments