Skip to content

Commit 3438148

Browse files
chore: added test cases
1 parent 053e22e commit 3438148

File tree

5 files changed

+1258
-1
lines changed

5 files changed

+1258
-1
lines changed

test/migration/globalautonumber.test.ts

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { OrgPreferences } from '../../src/utils/orgPreferences';
1313
import { NetUtils } from '../../src/utils/net';
1414
import { QueryTools } from '../../src/utils';
1515
import { DebugTimer } from '../../src/utils/logging/debugtimer';
16+
import * as dataModelService from '../../src/utils/dataModelService';
1617

1718
describe('GlobalAutoNumberMigrationTool', () => {
1819
let globalAutoNumberMigrationTool: GlobalAutoNumberMigrationTool;
@@ -537,4 +538,230 @@ describe('GlobalAutoNumberMigrationTool', () => {
537538
expect((toolWithoutNamespace as any).namespacePrefix).to.equal('');
538539
});
539540
});
541+
542+
describe('isFoundationPackage Integration', () => {
543+
let isFoundationPackageStub: sinon.SinonStub;
544+
545+
beforeEach(() => {
546+
isFoundationPackageStub = sandbox.stub(dataModelService, 'isFoundationPackage');
547+
});
548+
549+
describe('truncate with isFoundationPackage', () => {
550+
it('should return early when isFoundationPackage is true', async () => {
551+
// Arrange
552+
isFoundationPackageStub.returns(true);
553+
const performPreMigrationChecksStub = sandbox.stub(
554+
globalAutoNumberMigrationTool as any,
555+
'performPreMigrationChecks'
556+
);
557+
const getAllGlobalAutoNumberSettingsStub = sandbox.stub(
558+
globalAutoNumberMigrationTool as any,
559+
'getAllGlobalAutoNumberSettings'
560+
);
561+
562+
// Act
563+
await globalAutoNumberMigrationTool.truncate();
564+
565+
// Assert
566+
expect(isFoundationPackageStub.calledOnce).to.be.true;
567+
expect(performPreMigrationChecksStub.called).to.be.false; // Should not be called
568+
expect(getAllGlobalAutoNumberSettingsStub.called).to.be.false; // Should not be called
569+
});
570+
571+
it('should proceed with truncate when isFoundationPackage is false', async () => {
572+
// Arrange
573+
isFoundationPackageStub.returns(false);
574+
const mockGlobalAutoNumbers = [
575+
{
576+
Id: '001',
577+
Name: 'TestGAN1',
578+
Increment__c: 1,
579+
},
580+
];
581+
const performPreMigrationChecksStub = sandbox
582+
.stub(globalAutoNumberMigrationTool as any, 'performPreMigrationChecks')
583+
.resolves();
584+
const getAllGlobalAutoNumberSettingsStub = sandbox
585+
.stub(globalAutoNumberMigrationTool as any, 'getAllGlobalAutoNumberSettings')
586+
.resolves(mockGlobalAutoNumbers);
587+
sandbox.stub(QueryTools, 'queryIds').resolves([]);
588+
sandbox.stub(NetUtils, 'delete').resolves(true);
589+
590+
// Act
591+
await globalAutoNumberMigrationTool.truncate();
592+
593+
// Assert
594+
expect(isFoundationPackageStub.calledOnce).to.be.true;
595+
expect(performPreMigrationChecksStub.calledOnce).to.be.true;
596+
expect(getAllGlobalAutoNumberSettingsStub.calledOnce).to.be.true;
597+
});
598+
});
599+
600+
describe('migrate with isFoundationPackage', () => {
601+
it('should return empty result when isFoundationPackage is true', async () => {
602+
// Arrange
603+
isFoundationPackageStub.returns(true);
604+
const performPreMigrationChecksStub = sandbox.stub(
605+
globalAutoNumberMigrationTool as any,
606+
'performPreMigrationChecks'
607+
);
608+
const migrateGlobalAutoNumberDataStub = sandbox.stub(
609+
globalAutoNumberMigrationTool as any,
610+
'migrateGlobalAutoNumberData'
611+
);
612+
613+
// Act
614+
const result = await globalAutoNumberMigrationTool.migrate();
615+
616+
// Assert
617+
expect(isFoundationPackageStub.calledOnce).to.be.true;
618+
expect(performPreMigrationChecksStub.called).to.be.false; // Should not be called
619+
expect(migrateGlobalAutoNumberDataStub.called).to.be.false; // Should not be called
620+
expect(result).to.be.an('array').with.length(1);
621+
expect(result[0].name).to.equal('Omni Global Auto Number');
622+
expect(result[0].results.size).to.equal(0); // Empty Map
623+
expect(result[0].records.size).to.equal(0); // Empty Map
624+
});
625+
626+
it('should proceed with migration when isFoundationPackage is false', async () => {
627+
// Arrange
628+
isFoundationPackageStub.returns(false);
629+
const mockGlobalAutoNumbers = [
630+
{
631+
Id: '001',
632+
Name: 'TestGAN1',
633+
Increment__c: 1,
634+
LastGeneratedNumber__c: 100,
635+
LeftPadDigit__c: 5,
636+
MinimumLength__c: 10,
637+
Prefix__c: 'TEST',
638+
Separator__c: '-',
639+
},
640+
];
641+
642+
sandbox.stub(QueryTools, 'queryAll').resolves(mockGlobalAutoNumbers);
643+
sandbox.stub(NetUtils, 'createOne').resolves({
644+
referenceId: '001',
645+
id: 'new001',
646+
success: true,
647+
errors: [],
648+
warnings: [],
649+
hasErrors: false,
650+
});
651+
sandbox.stub(OrgPreferences, 'checkRollbackFlags').resolves([]);
652+
sandbox.stub(globalAutoNumberMigrationTool as any, 'performPreMigrationChecks').resolves();
653+
sandbox.stub(QueryTools, 'queryIds').resolves([]);
654+
sandbox.stub(NetUtils, 'delete').resolves(true);
655+
(globalAutoNumberMigrationTool as any).globalAutoNumberSettings = mockGlobalAutoNumbers;
656+
657+
getMessageStub.withArgs('foundGlobalAutoNumbersToMigrate', [1]).returns('Found 1');
658+
getMessageStub.withArgs('startingPostMigrationCleanup').returns('Starting cleanup');
659+
getMessageStub.withArgs('postMigrationCleanupCompleted').returns('Cleanup completed');
660+
getMessageStub.withArgs('omniGlobalAutoNumberPrefEnabled').returns('Preference enabled');
661+
662+
// Act
663+
const result = await globalAutoNumberMigrationTool.migrate();
664+
665+
// Assert
666+
expect(isFoundationPackageStub.calledOnce).to.be.true;
667+
expect(result).to.be.an('array').with.length(1);
668+
expect(result[0].name).to.equal('Omni Global Auto Number');
669+
expect(result[0].results.size).to.equal(1);
670+
});
671+
});
672+
673+
describe('assess with isFoundationPackage', () => {
674+
it('should return empty array when isFoundationPackage is true', async () => {
675+
// Arrange
676+
isFoundationPackageStub.returns(true);
677+
const queryAllStub = sandbox.stub(QueryTools, 'queryAll');
678+
const processGlobalAutoNumberComponentsStub = sandbox.stub(
679+
globalAutoNumberMigrationTool as any,
680+
'processGlobalAutoNumberComponents'
681+
);
682+
683+
// Act
684+
const result = await globalAutoNumberMigrationTool.assess();
685+
686+
// Assert
687+
expect(isFoundationPackageStub.calledOnce).to.be.true;
688+
expect(queryAllStub.called).to.be.false; // Should not query when foundation package
689+
expect(processGlobalAutoNumberComponentsStub.called).to.be.false; // Should not process
690+
expect(result).to.be.an('array').that.is.empty;
691+
});
692+
693+
it('should proceed with assessment when isFoundationPackage is false', async () => {
694+
// Arrange
695+
isFoundationPackageStub.returns(false);
696+
const mockGlobalAutoNumbers = [
697+
{
698+
Id: '001',
699+
Name: 'TestGAN1',
700+
Increment__c: 1,
701+
LastGeneratedNumber__c: 100,
702+
LeftPadDigit__c: 5,
703+
MinimumLength__c: 10,
704+
Prefix__c: 'TEST',
705+
Separator__c: '-',
706+
},
707+
];
708+
709+
const queryAllStub = sandbox.stub(QueryTools, 'queryAll').resolves(mockGlobalAutoNumbers);
710+
getMessageStub.withArgs('startingGlobalAutoNumberAssessment').returns('Starting assessment');
711+
getMessageStub.withArgs('foundGlobalAutoNumbersToAssess', [1]).returns('Found 1');
712+
713+
// Act
714+
const result = await globalAutoNumberMigrationTool.assess();
715+
716+
// Assert
717+
expect(isFoundationPackageStub.calledOnce).to.be.true;
718+
expect(queryAllStub.calledOnce).to.be.true;
719+
expect(result).to.be.an('array').with.length(1);
720+
expect(result[0].name).to.equal('TestGAN1');
721+
expect(result[0].id).to.equal('001');
722+
});
723+
});
724+
725+
describe('Foundation Package Edge Cases', () => {
726+
it('should handle foundation package check consistently across multiple calls to truncate', async () => {
727+
// Arrange
728+
isFoundationPackageStub.returns(true);
729+
730+
// Act - Call truncate multiple times
731+
await globalAutoNumberMigrationTool.truncate();
732+
await globalAutoNumberMigrationTool.truncate();
733+
734+
// Assert
735+
expect(isFoundationPackageStub.callCount).to.equal(2);
736+
});
737+
738+
it('should handle foundation package check consistently across multiple calls to migrate', async () => {
739+
// Arrange
740+
isFoundationPackageStub.returns(true);
741+
742+
// Act - Call migrate multiple times
743+
const result1 = await globalAutoNumberMigrationTool.migrate();
744+
const result2 = await globalAutoNumberMigrationTool.migrate();
745+
746+
// Assert
747+
expect(isFoundationPackageStub.callCount).to.equal(2);
748+
expect(result1).to.be.an('array').with.length(1);
749+
expect(result2).to.be.an('array').with.length(1);
750+
});
751+
752+
it('should handle foundation package check consistently across multiple calls to assess', async () => {
753+
// Arrange
754+
isFoundationPackageStub.returns(true);
755+
756+
// Act - Call assess multiple times
757+
const result1 = await globalAutoNumberMigrationTool.assess();
758+
const result2 = await globalAutoNumberMigrationTool.assess();
759+
760+
// Assert
761+
expect(isFoundationPackageStub.callCount).to.equal(2);
762+
expect(result1).to.be.an('array').that.is.empty;
763+
expect(result2).to.be.an('array').that.is.empty;
764+
});
765+
});
766+
});
540767
});

0 commit comments

Comments
 (0)