Skip to content

Commit

Permalink
Use golden files for converter testings (#19168)
Browse files Browse the repository at this point in the history
* Use golden files for converter testings

* Add tips if test failed

* Make results of enum human-readable

* Add copyright
  • Loading branch information
mustard-mh authored Nov 30, 2023
1 parent 555c738 commit be35ce1
Show file tree
Hide file tree
Showing 87 changed files with 1,279 additions and 1,018 deletions.
1 change: 1 addition & 0 deletions components/public-api/typescript-common/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ packages:
- name: lib
type: yarn
srcs:
- fixtures/**
- src/**
- package.json
- tsconfig.json
Expand Down
37 changes: 37 additions & 0 deletions components/public-api/typescript-common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Public API TypeScript Common

## Golden tests

Golden tests are used to test the output of a function against a known good output. The output is stored in a file with the same name as the test file but with a `.golden` extension. The test will fail if the output does not match the golden file.

We use golden tests for public api data conversion functions. We put the fixtures input and golden results in the `../fixtures` folder.

See example below:

```ts
it("toOrganizationMember", async () => {
await startFixtureTest("../fixtures/toOrganizationMember_*.json", async (input) =>
converter.toOrganizationMember(input),
);
});
```

it will run the test for each file matching the glob pattern `../fixtures/toOrganizationMember_*.json`. The test will fail if the output does not match the `../fixtures/toOrganizationMember_*.golden` files.

### How to veirfy golden files

```bash
yarn test
```

### How to update golden files

```bash
yarn test:forceUpdate
```

### How to generate input json files

```bash
node scripts/new-fixtures.js [your_testing_name] [the_number_of_input_files]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "owner",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"OWNER"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "member",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"MEMBER"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"err": "unknown org member role 0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"UNSPECIFIED"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"err": "unknown org member role undefined"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"NONE"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"result": {
"id": "123",
"name": "My Config"
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "123",
"name": "My Config"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"result": {
"id": "123",
"settings": {
"prebuilds": {
"enable": true,
"prebuildInterval": 5
},
"workspaceClasses": {
"regular": "huge"
}
}
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "123",
"workspaceSettings": {
"workspaceClass": "huge"
},
"prebuildSettings": {
"enabled": true,
"prebuildInterval": 5
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"result": {
"id": "ap123",
"type": "AUTH_PROVIDER_TYPE_GITHUB",
"host": "localhost",
"icon": "unused icon",
"description": "unused description"
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"authProviderId": "ap123",
"authProviderType": "GitHub",
"host": "localhost",
"verified": true,
"icon": "unused icon",
"description": "unused description",
"settingsUrl": "unused",
"ownerId": "unused",
"organizationId": "unused"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "GITHUB",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"GitHub"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "GITLAB",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"GitLab"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "BITBUCKET",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Bitbucket"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "BITBUCKET_SERVER",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"BitbucketServer"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "UNSPECIFIED",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Other"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"result": {
"id": "ap123",
"organizationId": "orgId123",
"type": "AUTH_PROVIDER_TYPE_GITHUB",
"host": "localhost",
"icon": "",
"description": "",
"settingsUrl": "",
"verified": false,
"enableLogin": false,
"scopes": [],
"oauth2Config": {
"clientId": "clientId123",
"clientSecret": "should not appear in result"
}
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "ap123",
"type": "GitHub",
"host": "localhost",
"status": "pending",
"ownerId": "userId",
"organizationId": "orgId123",
"oauth": {
"clientId": "clientId123",
"clientSecret": "should not appear in result",
"callBackUrl": "localhost/callback",
"authorizationUrl": "auth.service/authorize",
"tokenUrl": "auth.service/token"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"result": {
"id": "",
"domain": "example.com",
"negative": false
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"domain": "example.com",
"negative": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"result": {
"id": 2023,
"urlRegexp": "*/*",
"blockUser": false,
"creationTime": "2023-11-29T18:17:16.194Z",
"updateTime": "2023-11-29T18:17:16.194Z"
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 2023,
"urlRegexp": "*/*",
"blockUser": false,
"createdAt": "2023-11-29T18:17:16.194Z",
"updatedAt": "2023-11-29T18:17:16.194Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "DEFAULT_BRANCH",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"default-branch"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "ALL_BRANCHES",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"all-branches"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "MATCHED_BRANCHES",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"matched-branches"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "DEFAULT_BRANCH",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"result": {
"id": "1",
"name": "FOO",
"configurationId": "1",
"admission": "ENVIRONMENT_VARIABLE_ADMISSION_PREBUILD"
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "1",
"name": "FOO",
"censored": true,
"projectId": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"result": {
"id": "123",
"organizationId": "456",
"name": "My Project",
"cloneUrl": "https://github.com/myorg/myproject.git",
"creationTime": "2023-11-29T17:52:27.467Z",
"prebuildSettings": {
"enabled": true,
"branchMatchingPattern": "main",
"branchStrategy": "BRANCH_MATCHING_STRATEGY_DEFAULT_BRANCH",
"prebuildInterval": 20,
"workspaceClass": "dev"
},
"workspaceSettings": {
"workspaceClass": "dev"
}
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": "123",
"teamId": "456",
"name": "My Project",
"cloneUrl": "https://github.com/myorg/myproject.git",
"appInstallationId": "",
"creationTime": "2023-11-29T17:52:27.467Z",
"settings": {
"workspaceClasses": {
"regular": "dev"
},
"prebuilds": {
"enable": true,
"branchMatchingPattern": "main",
"branchStrategy": "default-branch",
"prebuildInterval": 20,
"workspaceClass": "dev"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"result": [
{
"name": "FOO",
"value": "bar"
}
],
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"title": "title",
"envvars": [
{
"name": "FOO",
"value": "bar"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "OWNER",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"owner"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "MEMBER",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"result": "UNSPECIFIED",
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"unknown"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"result": {
"userId": "456",
"role": "ORGANIZATION_ROLE_OWNER",
"memberSince": "2022-01-01T00:00:00Z",
"avatarUrl": "https://example.com/avatar.jpg",
"fullName": "John Doe",
"email": "[email protected]",
"ownedByOrganization": true
},
"err": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"userId": "456",
"fullName": "John Doe",
"primaryEmail": "[email protected]",
"avatarUrl": "https://example.com/avatar.jpg",
"role": "owner",
"memberSince": "2022-01-01T00:00:00.000Z",
"ownedByOrganization": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"result": {
"id": "123",
"name": "My Org",
"creationTime": "2022-01-01T00:00:00Z",
"slug": "my-org"
},
"err": ""
}
Loading

0 comments on commit be35ce1

Please sign in to comment.