Skip to content

Commit

Permalink
Replace binary test files to json (#1252)
Browse files Browse the repository at this point in the history
The PR:
* replaces binary test files to json to make it human readable;
* leaves one `expected_seed.bin` ('set_seed_version' test case) to
ensure the serialization format is unchanged.
  • Loading branch information
atuchin-m authored Nov 6, 2024
1 parent ba72f4a commit 7949b20
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

seed.bin
serialnumber
*.json.failed
33 changes: 25 additions & 8 deletions src/seed_tools/commands/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ import { VariationsSeed } from 'src/proto/generated/variations_seed';
import { wsPath } from '../../base/path_utils';
import create from './create';

// Helper function to compare protobuf with expected json.
const compareProtobuf = (actual: Uint8Array, expectedFilename: string) => {
let expectedJson = '{}';
if (fs_sync.existsSync(expectedFilename)) {
expectedJson = fs_sync.readFileSync(expectedFilename, 'utf-8');
}
const expected = VariationsSeed.fromJsonString(expectedJson);
const actualObj = VariationsSeed.fromBinary(actual);
try {
expect(actualObj).toEqual(expected);
} catch (error) {
const failedFilename = expectedFilename + '.failed';
fs_sync.writeFileSync(
failedFilename,
VariationsSeed.toJsonString(actualObj, { prettySpaces: 2 }) + '\n',
);
throw error;
}
};

describe('create command', () => {
const testDataDir = wsPath('//src/test/data');

Expand Down Expand Up @@ -57,10 +77,7 @@ describe('create command', () => {
]);

const output = await fs.readFile(outputFile);
const expectedOutput = await fs.readFile(
path.join(testCaseDir, 'expected_seed.bin'),
);
expect(output).toEqual(expectedOutput);
compareProtobuf(output, path.join(testCaseDir, 'expected_seed.json'));

const outputSerialNumber = await fs.readFile(serialNumberPath, 'utf-8');
expect(outputSerialNumber).toEqual('1');
Expand Down Expand Up @@ -89,10 +106,7 @@ describe('create command', () => {
await create().parseAsync(args);

const output = await fs.readFile(outputFile);
const expectedOutput = await fs.readFile(
path.join(testCaseDir, 'expected_seed.bin'),
);
expect(output).toEqual(expectedOutput);
compareProtobuf(output, path.join(testCaseDir, 'expected_seed.json'));

expect(VariationsSeed.fromBinary(output).version).toEqual('1');
};
Expand Down Expand Up @@ -125,6 +139,9 @@ describe('create command', () => {
]);

const output = await fs.readFile(outputFile);
compareProtobuf(output, path.join(testCaseDir, 'expected_seed.json'));

// Check the binary output is the same as the expected output.
const expectedOutput = await fs.readFile(
path.join(testCaseDir, 'expected_seed.bin'),
);
Expand Down
Binary file removed src/test/data/perf_seeds/test1/expected_seed.bin
Binary file not shown.
36 changes: 36 additions & 0 deletions src/test/data/perf_seeds/test1/expected_seed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"serialNumber": "1",
"study": [
{
"name": "TestStudy",
"consistency": "PERMANENT",
"defaultExperimentName": "Disabled",
"experiment": [
{
"name": "Disabled",
"probabilityWeight": 100,
"featureAssociation": {
"disableFeature": [
"TestFeature"
]
}
}
],
"filter": {
"channel": [
"CANARY",
"BETA",
"STABLE"
],
"platform": [
"PLATFORM_WINDOWS",
"PLATFORM_MAC",
"PLATFORM_LINUX",
"PLATFORM_ANDROID"
]
},
"activationType": "ACTIVATE_ON_STARTUP"
}
],
"version": "1"
}
49 changes: 49 additions & 0 deletions src/test/data/set_seed_version/expected_seed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"serialNumber": "1",
"study": [
{
"name": "TestStudy",
"consistency": "PERMANENT",
"experiment": [
{
"name": "Enabled",
"probabilityWeight": 100,
"featureAssociation": {
"enableFeature": [
"TestFeature"
]
}
},
{
"name": "Disabled",
"probabilityWeight": 0,
"featureAssociation": {
"disableFeature": [
"TestFeature"
]
}
},
{
"name": "Default",
"probabilityWeight": 0
}
],
"filter": {
"minVersion": "92.1.30.57",
"channel": [
"CANARY",
"BETA",
"STABLE"
],
"platform": [
"PLATFORM_WINDOWS",
"PLATFORM_MAC",
"PLATFORM_LINUX",
"PLATFORM_ANDROID"
]
},
"activationType": "ACTIVATE_ON_STARTUP"
}
],
"version": "test version value"
}
Binary file removed src/test/data/valid_seeds/test1/expected_seed.bin
Binary file not shown.
49 changes: 49 additions & 0 deletions src/test/data/valid_seeds/test1/expected_seed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"serialNumber": "1",
"study": [
{
"name": "TestStudy",
"consistency": "PERMANENT",
"experiment": [
{
"name": "Enabled",
"probabilityWeight": 100,
"featureAssociation": {
"enableFeature": [
"TestFeature"
]
}
},
{
"name": "Disabled",
"probabilityWeight": 0,
"featureAssociation": {
"disableFeature": [
"TestFeature"
]
}
},
{
"name": "Default",
"probabilityWeight": 0
}
],
"filter": {
"minVersion": "92.1.30.57",
"channel": [
"CANARY",
"BETA",
"STABLE"
],
"platform": [
"PLATFORM_WINDOWS",
"PLATFORM_MAC",
"PLATFORM_LINUX",
"PLATFORM_ANDROID"
]
},
"activationType": "ACTIVATE_ON_STARTUP"
}
],
"version": "1"
}
Binary file removed src/test/data/valid_seeds/test2/expected_seed.bin
Binary file not shown.
87 changes: 87 additions & 0 deletions src/test/data/valid_seeds/test2/expected_seed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"serialNumber": "1",
"study": [
{
"name": "AnotherTestStudy",
"consistency": "PERMANENT",
"experiment": [
{
"name": "Enabled_Group",
"probabilityWeight": 0,
"featureAssociation": {
"enableFeature": [
"AnotherTestFeature"
]
}
},
{
"name": "Disabled_Group",
"probabilityWeight": 100,
"featureAssociation": {
"disableFeature": [
"AnotherTestFeature"
]
}
},
{
"name": "Default_Group",
"probabilityWeight": 0
}
],
"filter": {
"minVersion": "120.*",
"channel": [
"STABLE"
],
"platform": [
"PLATFORM_MAC"
]
},
"activationType": "ACTIVATE_ON_STARTUP"
},
{
"name": "TestStudy",
"consistency": "PERMANENT",
"experiment": [
{
"name": "Enabled",
"probabilityWeight": 100,
"featureAssociation": {
"enableFeature": [
"TestFeature"
]
}
},
{
"name": "Disabled",
"probabilityWeight": 0,
"featureAssociation": {
"disableFeature": [
"TestFeature"
]
}
},
{
"name": "Default",
"probabilityWeight": 0
}
],
"filter": {
"minVersion": "92.1.30.57",
"channel": [
"CANARY",
"BETA",
"STABLE"
],
"platform": [
"PLATFORM_WINDOWS",
"PLATFORM_MAC",
"PLATFORM_LINUX",
"PLATFORM_ANDROID"
]
},
"activationType": "ACTIVATE_ON_STARTUP"
}
],
"version": "1"
}
Binary file removed src/test/data/valid_seeds/test3/expected_seed.bin
Binary file not shown.
Loading

0 comments on commit 7949b20

Please sign in to comment.