@@ -18,8 +18,10 @@ import path, { dirname } from 'node:path';
18
18
import { fileURLToPath } from 'node:url' ;
19
19
import { expect } from 'chai' ;
20
20
import sinon from 'sinon' ;
21
- import { Messages , Org } from '@salesforce/core' ;
21
+ import { Connection , Messages , Org } from '@salesforce/core' ;
22
22
import { ImportApi , ImportConfig } from '../../../../src/api/data/tree/importApi.js' ;
23
+ import { SObjectTreeInput } from '../../../../src/types.js' ;
24
+ import { transformRecordTypeEntries } from '../../../../src/api/data/tree/importCommon.js' ;
23
25
// Json files
24
26
const accountsContactsTreeJSON = JSON . parse (
25
27
fs . readFileSync ( 'test/api/data/tree/test-files/accounts-contacts-tree.json' , 'utf-8' )
@@ -29,29 +31,13 @@ const accountsContactsPlanJSON = JSON.parse(
29
31
) ;
30
32
const dataImportPlanSchema = JSON . parse ( fs . readFileSync ( 'schema/dataImportPlanSchema.json' , 'utf-8' ) ) ;
31
33
32
- // Sample response data
33
- // const sampleResponseData = [
34
- // { referenceId: 'SampleAccountRef', id: '001xx000003DKpJAAW' },
35
- // { referenceId: 'SampleAcct2Ref', id: '001xx000003DKpKAAW' },
36
- // { referenceId: 'PresidentSmithRef', id: '003xx000004TtqCAAS' },
37
- // { referenceId: 'VPEvansRef', id: '003xx000004TtqIAAS' }
38
- // ];
39
-
40
34
const sampleSObjectTypes = {
41
35
SampleAccountRef : 'Account' ,
42
36
SampleAcct2Ref : 'Account' ,
43
37
PresidentSmithRef : 'Contact' ,
44
38
VPEvansRef : 'Contact' ,
45
39
} ;
46
40
47
- // Sample command display data
48
- // const sampleDisplayData = [
49
- // { refId: 'SampleAccountRef', type: 'Account', id: '001xx000003DKpJAAW' },
50
- // { refId: 'SampleAcct2Ref', type: 'Account', id: '001xx000003DKpKAAW' },
51
- // { refId: 'PresidentSmithRef', type: 'Contact', id: '003xx000004TtqCAAS' },
52
- // { refId: 'VPEvansRef', type: 'Contact', id: '003xx000004TtqIAAS' }
53
- // ];
54
-
55
41
const jsonRefRegex = / [ . ] * [ " | ' ] [ A - Z 0 - 9 _ ] * [ " | ' ] [ ] * : [ ] * [ " | ' ] @ ( [ A - Z 0 - 9 _ ] * ) [ " | ' ] [ . ] * / gim;
56
42
57
43
describe ( 'ImportApi' , ( ) => {
@@ -622,6 +608,32 @@ describe('ImportApi', () => {
622
608
) . to . equal ( true ) ;
623
609
} ) ;
624
610
611
+ it ( "should convert RecordType Name's to IDs" , async ( ) => {
612
+ const travelExpenseJson = JSON . parse (
613
+ fs . readFileSync ( 'test/api/data/tree/test-files/travel-expense.json' , 'utf-8' )
614
+ ) as { records : SObjectTreeInput [ ] } ;
615
+ sandbox . stub ( Connection . prototype , 'singleRecordQuery' ) . resolves ( { Id : 'updatedIdHere' } ) ;
616
+ const updated = await transformRecordTypeEntries ( Connection . prototype , travelExpenseJson . records ) ;
617
+ expect ( updated . length ) . to . equal ( 3 ) ;
618
+ expect ( updated . every ( ( e ) => e . RecordTypeId === 'updatedIdHere' ) ) . to . be . true ;
619
+ expect ( updated . every ( ( e ) => e . RecordType === undefined ) ) . to . be . true ;
620
+ } ) ;
621
+
622
+ it ( 'should throw an error when RecordType.Name is not available' , async ( ) => {
623
+ const travelExpenseJson = JSON . parse (
624
+ fs . readFileSync ( 'test/api/data/tree/test-files/travel-expense.json' , 'utf-8' )
625
+ ) as { records : SObjectTreeInput [ ] } ;
626
+ // @ts -ignore - just delete the entry, regardless of types
627
+ delete travelExpenseJson . records [ 0 ] . RecordType . Name ;
628
+ try {
629
+ await transformRecordTypeEntries ( Connection . prototype , travelExpenseJson . records ) ;
630
+ } catch ( e ) {
631
+ expect ( ( e as Error ) . message ) . to . equal (
632
+ 'This file contains an unresolvable RecordType ID. Try exporting the data by specifying RecordType.Name in the SOQL query, and then run the data import again.'
633
+ ) ;
634
+ }
635
+ } ) ;
636
+
625
637
it ( 'should call sendSObjectTreeRequest 3rd with correct args' , async ( ) => {
626
638
// @ts -ignore
627
639
await ImportApi . prototype . importSObjectTreeFile . call ( context , args ) ;
@@ -652,134 +664,3 @@ describe('ImportApi', () => {
652
664
} ) ;
653
665
} ) ;
654
666
} ) ;
655
-
656
- /**
657
- * Use these as the basis for integration tests.
658
- */
659
- // describe('data:import', () => {
660
- // let force;
661
- // let org;
662
-
663
- // before(() => {
664
- // workspace = new TestWorkspace();
665
- // org = new Org();
666
- // force = org.force;
667
-
668
- // sandbox.stub(force, 'describeData').callsFake(() => Promise.resolve());
669
-
670
- // // copy data files to workspace
671
- // fse.copySync(path.join(dir, 'data'), path.join(workspace.getWorkspacePath(), 'data'));
672
-
673
- // // Some test require a scratch org config in the workspace
674
- // return workspace
675
- // .configureHubOrg()
676
- // .then(() => org.saveConfig(orgConfig))
677
- // .then(() => workspace.configureWorkspaceScratchOrg());
678
- // });
679
-
680
- // after(() => {
681
- // workspace.clean();
682
- // });
683
-
684
- // afterEach(() => {
685
- // sandbox.restore();
686
- // });
687
-
688
- // describe('DataTreeImportCommand run()', () => {
689
- // it('should output the plan schema when confighelp flag specified', async () => {
690
- // const context = {
691
- // flags: {
692
- // confighelp: true,
693
- // json: true
694
- // },
695
- // org: new Org()
696
- // };
697
- // const dataTreeImportCommand = new DataTreeImportCommand([], null);
698
- // set(dataTreeImportCommand, 'ux', { log: sandbox.spy() });
699
- // sandbox.stub(dataTreeImportCommand as any, 'resolveLegacyContext').callsFake(() => Promise.resolve(context));
700
-
701
- // const outputJson = await dataTreeImportCommand.run();
702
- // expect(outputJson).to.deep.equal(dataImportPlanSchema);
703
- // });
704
- // });
705
-
706
- // // Test importing a single data file
707
- // describe('#File', () => {
708
- // beforeEach(() => {
709
- // sandbox.stub(force, 'request').callsFake(() =>
710
- // Promise.resolve({
711
- // hasErrors: false,
712
- // results: sampleResponseData
713
- // })
714
- // );
715
- // });
716
-
717
- // it('API: Should insert Accounts and child Contacts', () => {
718
- // const dataImportCmd = new DataImportApi(org);
719
- // const config = {
720
- // json: true,
721
- // username,
722
- // sobjecttreefiles: './data/accounts-contacts-tree.json'
723
- // };
724
-
725
- // return dataImportCmd.execute(config).then(result => {
726
- // expect(result).to.eql(sampleDisplayData);
727
- // });
728
- // });
729
- // });
730
-
731
- // // test importing via plan
732
- // describe('#Plan', () => {
733
- // const contactsOnly1 = [
734
- // { referenceId: 'FrontDeskRef', id: '003xx000004TtqwAAC' },
735
- // { referenceId: 'ManagerRef', id: '003xx000004TtqxAAC' }
736
- // ];
737
- // const contactsOnly2 = [
738
- // { referenceId: 'JanitorRef', id: '003xx000004Ttr1AAC' },
739
- // { referenceId: 'DeveloperRef', id: '003xx000004Ttr2AAC' }
740
- // ];
741
-
742
- // const sampleDisplayContacts1 = [
743
- // { refId: 'FrontDeskRef', type: 'Contact', id: '003xx000004TtqwAAC' },
744
- // { refId: 'ManagerRef', type: 'Contact', id: '003xx000004TtqxAAC' }
745
- // ];
746
-
747
- // const sampleDisplayContacts2 = [
748
- // { refId: 'JanitorRef', type: 'Contact', id: '003xx000004Ttr1AAC' },
749
- // { refId: 'DeveloperRef', type: 'Contact', id: '003xx000004Ttr2AAC' }
750
- // ];
751
-
752
- // let requestStub;
753
-
754
- // beforeEach(() => {
755
- // requestStub = sandbox.stub(force, 'request');
756
- // requestStub.onCall(0).returns(Promise.resolve({ hasErrors: false, results: sampleResponseData }));
757
- // requestStub.onCall(1).returns(Promise.resolve({ hasErrors: false, results: contactsOnly1 }));
758
- // requestStub.onCall(2).returns(Promise.resolve({ hasErrors: false, results: contactsOnly2 }));
759
- // });
760
-
761
- // it('API: Should insert Accounts and child Contacts', () => {
762
- // const dataImportCmd = new DataImportApi(org);
763
- // const config = {
764
- // json: true,
765
- // username,
766
- // plan: './data/accounts-contacts-plan.json',
767
- // importPlanConfig: accountsContactsPlanJSON
768
- // };
769
- // const expected = [...sampleDisplayData, ...sampleDisplayContacts1, ...sampleDisplayContacts2];
770
-
771
- // return dataImportCmd.execute(config).then(result => {
772
- // expect(result).to.eql(expected);
773
- // });
774
- // });
775
- // });
776
-
777
- // describe('DataImportConfigHelpCommand', () => {
778
- // it('should return the schema', () => {
779
- // const dataImportConfigHelpCommand = new DataImportConfigHelpCommand();
780
- // return dataImportConfigHelpCommand.execute({ org }).then(result => {
781
- // expect(result).to.deep.equal(dataImportPlanSchema);
782
- // });
783
- // });
784
- // });
785
- // });
0 commit comments