Skip to content

Commit

Permalink
test: rewrite models to use snapshots and tables
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Nov 5, 2024
1 parent 8509b99 commit 3d38626
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 126 deletions.
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

0 comments on commit 3d38626

Please sign in to comment.