-
Notifications
You must be signed in to change notification settings - Fork 125
Refactored internal/scorer/scorer #317
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
Merged
calebbrown
merged 7 commits into
ossf:main
from
nathannaveen:nathan/feat/refactorScorer
Feb 27, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eedd0e5
Refactored internal/scorer/scorer
nathannaveen 9f9c79d
Updated based on code review
nathannaveen 228388d
Included Test for scorer
nathannaveen 9c06b56
Merge branch 'main' of github.com:ossf/criticality_score into nathan/…
nathannaveen 0e819c4
Updated Tests for scorer
nathannaveen 9a0535a
Updated based on code review
nathannaveen be48c6d
Merge branch 'main' into nathan/feat/refactorScorer
calebbrown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,181 @@ | ||
// Copyright 2022 Criticality Score Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scorer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ossf/criticality_score/internal/collector/signal" | ||
"github.com/ossf/criticality_score/internal/scorer/algorithm" | ||
) | ||
|
||
type testAlgo struct { | ||
UpdatedCount signal.Field[int] `signal:"legacy"` | ||
} | ||
|
||
func (t testAlgo) Score(record map[string]float64) float64 { | ||
sum := 0.0 | ||
for _, v := range record { | ||
sum += v | ||
} | ||
return sum | ||
} | ||
|
||
func (t testAlgo) Namespace() signal.Namespace { | ||
return "" | ||
} | ||
|
||
func TestScorer_ScoreRaw(t *testing.T) { | ||
tests := []struct { //nolint:govet | ||
name string | ||
s *Scorer | ||
raw map[string]string | ||
want float64 | ||
}{ | ||
{ | ||
name: "average test", | ||
s: &Scorer{ | ||
name: "Valid", | ||
a: testAlgo{}, | ||
}, | ||
raw: map[string]string{ | ||
"one": "1", | ||
"two": "2", | ||
}, | ||
want: 3, | ||
}, | ||
{ | ||
name: "invalid", | ||
s: &Scorer{ | ||
name: "invalid", | ||
a: testAlgo{}, | ||
}, | ||
raw: map[string]string{ | ||
"invalid number": "abcd", | ||
}, | ||
want: 0, | ||
}, | ||
{ | ||
name: "empty", | ||
|
||
s: &Scorer{ | ||
name: "Valid", | ||
a: testAlgo{}, | ||
}, | ||
raw: map[string]string{}, | ||
want: 0, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.s.ScoreRaw(tt.raw); got != tt.want { | ||
t.Errorf("ScoreRaw() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNameFromFilepath(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filepath string | ||
want string | ||
}{ | ||
{ | ||
name: "empty", | ||
filepath: "", | ||
want: "_score", | ||
}, | ||
{ | ||
name: "without extension", | ||
filepath: "test", | ||
want: "test_score", | ||
}, | ||
{ | ||
name: "with extension", | ||
filepath: "test.json", | ||
want: "test_score", | ||
}, | ||
{ | ||
name: "with path", | ||
filepath: "path/to/test.json", | ||
want: "test_score", | ||
}, | ||
{ | ||
name: "invalid characters", | ||
filepath: "configuración-+=_básica.yaml", | ||
want: "configuración____básica_score", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if got := NameFromFilepath(test.filepath); got != test.want { | ||
t.Errorf("NameFromFilepath() = %v, want %v", got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestScorer_Name(t *testing.T) { | ||
test := struct { | ||
name string | ||
s *Scorer | ||
want string | ||
}{ | ||
name: "default", | ||
s: &Scorer{ | ||
name: "Valid", | ||
a: testAlgo{}, | ||
}, | ||
want: "Valid", | ||
} | ||
|
||
if got := test.s.Name(); got != test.want { | ||
t.Errorf("Name() = %v, want %v", got, test.want) | ||
} | ||
} | ||
|
||
func TestScorer_Score(t *testing.T) { | ||
type fields struct { | ||
a algorithm.Algorithm | ||
name string | ||
} | ||
test := struct { | ||
name string | ||
fields fields | ||
signals []signal.Set | ||
want float64 | ||
}{ | ||
name: "average test", | ||
fields: fields{ | ||
a: testAlgo{}, | ||
name: "Valid", | ||
}, | ||
signals: []signal.Set{ | ||
&testAlgo{ | ||
UpdatedCount: signal.Val(1), | ||
}, | ||
}, | ||
want: 1, | ||
} | ||
|
||
s := &Scorer{ | ||
a: test.fields.a, | ||
name: test.fields.name, | ||
} | ||
if got := s.Score(test.signals); got != test.want { | ||
t.Errorf("Score() = %v, want %v", got, test.want) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.