-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: rewrite
models
to use snapshots and tables
- Loading branch information
Showing
5 changed files
with
262 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
[TestVulnerabilities_MarshalJSON/multiple_vulnerabilities - 1] | ||
[ | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "GHSA-1" | ||
}, | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "GHSA-2" | ||
}, | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "GHSA-3" | ||
} | ||
] | ||
--- | ||
|
||
[TestVulnerabilities_MarshalJSON/nil - 1] | ||
[] | ||
--- | ||
|
||
[TestVulnerabilities_MarshalJSON/no_vulnerabilities - 1] | ||
[] | ||
--- | ||
|
||
[TestVulnerabilities_MarshalJSON/one_vulnerability - 1] | ||
[ | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "GHSA-1" | ||
} | ||
] | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
|
||
[TestAffected_MarshalJSON/with_package - 1] | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "TEST-0000", | ||
"affected": [ | ||
{ | ||
"package": { | ||
"ecosystem": "PyPI", | ||
"name": "requests" | ||
}, | ||
"versions": [ | ||
"1.0.0" | ||
] | ||
} | ||
] | ||
} | ||
--- | ||
|
||
[TestAffected_MarshalJSON/without_package - 1] | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "TEST-0000", | ||
"affected": [ | ||
{ | ||
"package": { | ||
"ecosystem": "PyPI", | ||
"name": "requests" | ||
}, | ||
"versions": [ | ||
"1.0.0" | ||
] | ||
} | ||
] | ||
} | ||
--- | ||
|
||
[TestAffected_MarshalYAML/with_package - 1] | ||
id: TEST-0000 | ||
modified: 0001-01-01T00:00:00Z | ||
affected: | ||
- package: | ||
ecosystem: PyPI | ||
name: requests | ||
versions: | ||
- 1.0.0 | ||
|
||
--- | ||
|
||
[TestAffected_MarshalYAML/without_package - 1] | ||
id: TEST-0000 | ||
modified: 0001-01-01T00:00:00Z | ||
affected: | ||
- package: | ||
ecosystem: PyPI | ||
name: requests | ||
versions: | ||
- 1.0.0 | ||
|
||
--- | ||
|
||
[TestVulnerability_MarshalJSON_WithTimes/all_Los_Angeles - 1] | ||
{ | ||
"modified": "2023-12-01T20:30:30Z", | ||
"published": "2021-06-30T08:00:00Z", | ||
"withdrawn": "2022-01-16T07:59:59Z", | ||
"id": "TEST-0000" | ||
} | ||
--- | ||
|
||
[TestVulnerability_MarshalJSON_WithTimes/all_UTC - 1] | ||
{ | ||
"modified": "2023-12-01T12:30:30Z", | ||
"published": "2021-06-30T01:00:00Z", | ||
"withdrawn": "2022-01-15T23:59:59Z", | ||
"id": "TEST-0000" | ||
} | ||
--- | ||
|
||
[TestVulnerability_MarshalJSON_WithTimes/empty - 1] | ||
{ | ||
"modified": "0001-01-01T00:00:00Z", | ||
"id": "TEST-0000" | ||
} | ||
--- | ||
|
||
[TestVulnerability_MarshalJSON_WithTimes/no_withdraw - 1] | ||
{ | ||
"modified": "2023-12-01T12:30:30Z", | ||
"published": "2021-06-30T01:00:00Z", | ||
"id": "TEST-0000" | ||
} | ||
--- | ||
|
||
[TestVulnerability_MarshalYAML_WithTimes/all_Los_Angeles - 1] | ||
id: TEST-0000 | ||
modified: 2023-12-01T20:30:30Z | ||
published: 2021-06-30T08:00:00Z | ||
withdrawn: 2022-01-16T07:59:59Z | ||
|
||
--- | ||
|
||
[TestVulnerability_MarshalYAML_WithTimes/all_UTC - 1] | ||
id: TEST-0000 | ||
modified: 2023-12-01T12:30:30Z | ||
published: 2021-06-30T01:00:00Z | ||
withdrawn: 2022-01-15T23:59:59Z | ||
|
||
--- | ||
|
||
[TestVulnerability_MarshalYAML_WithTimes/empty - 1] | ||
id: TEST-0000 | ||
modified: 0001-01-01T00:00:00Z | ||
|
||
--- | ||
|
||
[TestVulnerability_MarshalYAML_WithTimes/no_withdraw - 1] | ||
id: TEST-0000 | ||
modified: 2023-12-01T12:30:30Z | ||
published: 2021-06-30T01:00:00Z | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package models_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/osv-scanner/internal/testutility" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
code := m.Run() | ||
|
||
testutility.CleanSnapshots(m) | ||
|
||
os.Exit(code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,44 @@ | ||
package models_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/google/osv-scanner/internal/testutility" | ||
"github.com/google/osv-scanner/pkg/models" | ||
) | ||
|
||
func TestVulnerabilities_MarshalJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
osv := models.Vulnerability{ID: "GHSA-1"} | ||
asJSON, err := json.Marshal(osv) | ||
|
||
if err != nil { | ||
t.Fatalf("Unable to marshal osv to JSON: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
vs models.Vulnerabilities | ||
want string | ||
}{ | ||
{ | ||
name: "", | ||
name: "nil", | ||
vs: nil, | ||
want: "[]", | ||
}, | ||
{ | ||
name: "", | ||
vs: models.Vulnerabilities(nil), | ||
want: "[]", | ||
name: "no vulnerabilities", | ||
vs: models.Vulnerabilities{}, | ||
}, | ||
{ | ||
name: "", | ||
vs: models.Vulnerabilities{osv}, | ||
want: "[" + string(asJSON) + "]", | ||
name: "one vulnerability", | ||
vs: models.Vulnerabilities{models.Vulnerability{ID: "GHSA-1"}}, | ||
}, | ||
{ | ||
name: "", | ||
vs: models.Vulnerabilities{osv, osv}, | ||
want: "[" + string(asJSON) + "," + string(asJSON) + "]", | ||
name: "multiple vulnerabilities", | ||
vs: models.Vulnerabilities{ | ||
models.Vulnerability{ID: "GHSA-1"}, | ||
models.Vulnerability{ID: "GHSA-2"}, | ||
models.Vulnerability{ID: "GHSA-3"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
got, err := tt.vs.MarshalJSON() | ||
if err != nil { | ||
t.Errorf("MarshalJSON() error = %v", err) | ||
|
||
return | ||
} | ||
|
||
if gotStr := string(got); gotStr != tt.want { | ||
t.Errorf("MarshalJSON() got = %v, want %v", gotStr, tt.want) | ||
} | ||
testutility.NewSnapshot().MatchJSON(t, tt.vs) | ||
}) | ||
} | ||
} |
Oops, something went wrong.