Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: rewrite models to use snapshots and tables #1382

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/models/__snapshots__/vulnerabilities_test.snap
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"
}
]
---
122 changes: 122 additions & 0 deletions pkg/models/__snapshots__/vulnerability_test.snap
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

---
16 changes: 16 additions & 0 deletions pkg/models/testmain_test.go
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)
}
44 changes: 13 additions & 31 deletions pkg/models/vulnerabilities_test.go
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)
})
}
}
Loading