zuora to eloqua | zuora export catalog | zuora renewals query | Search

This code unit tests a module responsible for interacting with the Zuora API to initiate, monitor, and retrieve data exports. These tests use mocking to simulate API responses and verify the module's functionality in handling various export stages.

Run example

npm run import -- "zuora export service test"

zuora export service test

var assert = require('assert');
var sinon = require('sinon');
var importer = require('../Core');
var {
    createBulkExportJob,
    getBulkExportJobStatus,
    getBulkExportFile,
    csvToJson,
} = importer.import("zuora to eloqua.ipynb[0");
var request = importer.import("http request polyfill");

var sandbox = sinon.createSandbox();
var zuoraConfig = {
    "rest_api_user":"devteam@fakepage.com",
    "rest_api_password":"pass",
    "rest_api_url": "http://localhost:18888"
};

describe('zuora oauth', () => {
    
    afterEach(() => {
        sandbox.restore();
    })
    
    it('should connect to zuora using oauth', () => {
        const dummyId = '123';
        const dummyQuery = 'SELECT * FROM Accounts';
        
        const requestStub = sandbox.stub(request, "request")
            .returns(Promise.resolve({ body: {Id: dummyId } }));
        
        return createBulkExportJob(dummyQuery, zuoraConfig)
            .then(result => {
                assert.equal(result, dummyId);
                assert(requestStub.calledOnce, 'request should only be called once');
                const stubCall = requestStub.getCall(0);
                assert.equal(stubCall.args[0].json, dummyQuery);
            });
    })
    
    it('should wait for the export to complete', () => {
        const dummyId = '12345'
        
        const requestStub = sandbox.stub(request, "request")
            .returns(Promise.resolve({ body: {Status: 'Completed', FileId: dummyId } }));
        
        return getBulkExportJobStatus('123', zuoraConfig)
            .then(result => {
                assert.equal(result, dummyId);
                assert(requestStub.calledOnce, 'request should only be called once');
            })
    })
    
    it('should download the csv file', () => {
        const csvFile = 'some,csv,file';
        
        const requestStub = sandbox.stub(request, "request")
            .returns(Promise.resolve({ body: csvFile }));
        
        return getBulkExportFile('1234', zuoraConfig)
            .then(result => {
                assert.equal(result, csvFile);
                assert(requestStub.calledOnce, 'request should only be called once');
            })
    })
    
    it('should convert CSV to JSON', () => {
        const result = csvToJson('some,csv,file\n1,2,3');
        assert.equal(result[0].file, 3);
    })
    
})

What the code could have been:

// Import required modules
import { assert, sinon } from'sinon';
import { createBulkExportJob, getBulkExportJobStatus, getBulkExportFile, csvToJson } from './zuoraToEloqua';
import { HttpRequestPolyfill } from './httpRequestPolyfill';

// Configure Zuora API settings
const zuoraConfig = {
  rest_api_user: 'devteam@fakepage.com',
  rest_api_password: 'pass',
  rest_api_url: 'http://localhost:18888',
};

// Create a Sinon sandbox for stubbing and mocking
const sandbox = sinon.createSandbox();

// Restore the sandbox after each test
afterEach(() => {
  sandbox.restore();
});

describe('Zuora Integration Tests', () => {
  // Test createBulkExportJob function
  it('should connect to Zuora using OAuth', async () => {
    // Define dummy data
    const dummyQuery = 'SELECT * FROM Accounts';
    const dummyId = '123';

    // Stub the request function
    const requestStub = sandbox.stub(HttpRequestPolyfill,'request')
     .returns(Promise.resolve({ body: { Id: dummyId } }));

    // Call the createBulkExportJob function
    const result = await createBulkExportJob(dummyQuery, zuoraConfig);

    // Assert the result and request stub
    assert.equal(result, dummyId);
    assert(requestStub.calledOnce);
    const stubCall = requestStub.getCall(0);
    assert.equal(stubCall.args[0].json, dummyQuery);
  });

  // Test getBulkExportJobStatus function
  it('should wait for the export to complete', async () => {
    // Define dummy data
    const dummyId = '12345';

    // Stub the request function
    const requestStub = sandbox.stub(HttpRequestPolyfill,'request')
     .returns(Promise.resolve({ body: { Status: 'Completed', FileId: dummyId } }));

    // Call the getBulkExportJobStatus function
    const result = await getBulkExportJobStatus('123', zuoraConfig);

    // Assert the result and request stub
    assert.equal(result, dummyId);
    assert(requestStub.calledOnce);
  });

  // Test getBulkExportFile function
  it('should download the CSV file', async () => {
    // Define dummy data
    const csvFile ='some,csv,file';

    // Stub the request function
    const requestStub = sandbox.stub(HttpRequestPolyfill,'request')
     .returns(Promise.resolve({ body: csvFile }));

    // Call the getBulkExportFile function
    const result = await getBulkExportFile('1234', zuoraConfig);

    // Assert the result and request stub
    assert.equal(result, csvFile);
    assert(requestStub.calledOnce);
  });

  // Test csvToJson function
  it('should convert CSV to JSON', () => {
    // Define dummy data
    const csvData ='some,csv,file\n1,2,3';

    // Call the csvToJson function
    const result = csvToJson(csvData);

    // Assert the result
    assert.equal(result[0].file, 3);
  });
});

This code defines unit tests for a module that interacts with the Zuora API to export data and handle the export process.

Here's a breakdown:

  1. Setup:

  2. Test Suite:

  3. Test Cases:

  4. Conclusion:

    These tests verify the functionality of the Zuora API interaction module by mocking API responses and asserting the expected behavior.