npm

Share private packages across your team with npm Orgs, now with simplified billing via the aws marketplace!Learn more »

goldwasher-json

0.6.0 • Public • Published

goldwasher-json

lets you refine chunks of JSON data used in your tests by removing all values which don't inflience the result.

npm install --save-dev goldwasher-json

TL;DR;

 // when you have a unit test... 
 it('simplePrice calclculation calculates correctly', () => {
    
    // which uses a big chunk of json you took from runtime\newtwork monitoring
    let bigChunkOfJson = getTestData();
 
    // you can temporarily add godlwash to the test, to get the minimal subset of data
    // that still passes the test
    let goldwashedData = goldwash(bigChunkOfJson, (data) => {
        
        let result = blackboxRateCalulationFunction(data); // orifinally used bigChunkOfJson
 
        return result.simplePrice === 88.752;
    });
 
    //and start using goldwashed data in your test instead of original full chunk
    console.log(JSON.stringify(goldwashedData));
});

usage

Imagine a situation. In a car rental company, you start working on a 10 year old module for calculating different price rates. Your goal is to remove outdated irrelevant calculations and start adding new features to the module without braking still used ones. A typical calculation function that you have to clean is 500+ lines of code :-(.

Ofcourse, you start by trying to cover your module with unit tests for most frequent paths. You check logs, retrieve JSON data for a typical used request and its resulting rate.

You end up with a test like this:

let carRentalRequest = {
    renter: {
        name: "John Smith",
        loyaltyBonus: 0.07,
        rentalHistory: [
            { duration: 10, price: 1500, dates: [/**/] },
            { duration: 20, price: 3000, dates: [/**/] },
            { duration: 105, price: 12000, dates: [/**/] },
            // 2 years of rental history
        ]
    },
    rentOptions: {
        duration: 3,
        corporatePartner: 5,
        bonusMilage: 1000
        //...20 more options
    },
    carProfile: {
        rawPrice: 15000,
        inflationAdjustedPrice: 17200,
        amotrization: 0.72,
        taxationPremium: 0.02,
        articleC20Rate: 0.03,
        //... more of the same
        bonusCoeficients: {
            promo5for5: 0.12,
            miles1000: 2,
            coproratePartnership: 10,
            rideTheBrand: 1.2,
            //... 10 years of promo options
        }
    }
}
 
it('simplePrice calclculation calculates correctly', () => {
 
    let result = blackboxRateCalulationFunction(carRentalRequest);
 
    expect(result.simplePrice).to.equal(88.752);
});

Doesn't help much to understand, what is actually happening. So, modify the test to temorarily include golwashing:

 it('simplePrice calclculation calculates correctly', () => {
 
    let goldwashedData = goldwash(carRentalRequest, (data) => {
        
        let result = blackboxRateCalulationFunction(data);
 
        return result.simplePrice === 88.752;
    });
 
    console.log(JSON.stringify(goldwashedData));
});

And in console you will get the subset of your JSON that still produces result expected by test.

{ 
    "renter"{ 
        "name""John Smith", 
        "loyaltyBonus"0.07, 
        "rentalHistory"[
            null, 
            null, 
            { "duration": 105 }
        ] }, 
    "rentOptions"{ 
        "duration"3, 
        "corporatePartner"5 
    }, 
    "carProfile"{ 
        "inflationAdjustedPrice"17200, 
        "amotrization"0.72 
    } 
}

You can now use this data in your test instead of the previous intimidatingly big chunk. You can also use it to better understand, which values play actual role in each calculation, and remove unused values.

async?

Yes, via goldwashAsync, which expect testing function to return Promise<boolean>

install

npm i goldwasher-json

Downloadsweekly downloads

6

version

0.6.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability