Skip to content

Commit d523bf1

Browse files
committed
Included tests for internal/signalio/json.go
- Included tests for internal/signalio/json.go - Removed un-necessary error check Signed-off-by: nathannaveen <[email protected]>
1 parent 27ad7d2 commit d523bf1

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

internal/signalio/json.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package signalio
1616

1717
import (
1818
"encoding/json"
19-
"fmt"
2019
"io"
2120
"sync"
2221

@@ -51,10 +50,9 @@ func (w *jsonWriter) WriteSignals(signals []signal.Set, extra ...Field) error {
5150
d = make(map[string]any)
5251
data[ns] = d
5352
}
54-
nsData, ok := d.(map[string]any)
55-
if !ok {
56-
return fmt.Errorf("failed to get map for namespace: %s", ns)
57-
}
53+
nsData := d.(map[string]any)
54+
// we don't need to check for an error here (the above line) because d is always a map[string]any
55+
5856
for k, v := range innerM {
5957
nsData[k] = v
6058
}
@@ -63,6 +61,7 @@ func (w *jsonWriter) WriteSignals(signals []signal.Set, extra ...Field) error {
6361
for _, f := range extra {
6462
data[f.Key] = f.Value
6563
}
64+
6665
w.mu.Lock()
6766
defer w.mu.Unlock()
6867
return w.encoder.Encode(data)

internal/signalio/json_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2022 Criticality Score Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package signalio
16+
17+
import (
18+
"encoding/json"
19+
"io"
20+
"testing"
21+
22+
"github.com/ossf/criticality_score/internal/collector/signal"
23+
)
24+
25+
type testJSONWriterSet struct { //nolint:govet
26+
UpdatedCount signal.Field[int]
27+
Field string
28+
}
29+
30+
func (t testJSONWriterSet) Namespace() signal.Namespace {
31+
return "test"
32+
}
33+
34+
type mockWriterJSON struct{}
35+
36+
func (m *mockWriterJSON) Write(p []byte) (n int, err error) {
37+
return 0, nil
38+
}
39+
40+
func Test_jsonWriter_WriteSignals(t *testing.T) {
41+
type args struct {
42+
signals []signal.Set
43+
extra []Field
44+
}
45+
test := struct {
46+
name string
47+
encoder *json.Encoder
48+
args args
49+
wantErr bool
50+
}{
51+
name: "default",
52+
encoder: json.NewEncoder(io.Writer(&mockWriterJSON{})),
53+
args: args{
54+
signals: []signal.Set{
55+
&testJSONWriterSet{},
56+
},
57+
extra: []Field{
58+
{
59+
Key: "extra",
60+
Value: "value",
61+
},
62+
},
63+
},
64+
}
65+
66+
w := JSONWriter(&mockWriterJSON{})
67+
if err := w.WriteSignals(test.args.signals, test.args.extra...); (err != nil) != test.wantErr {
68+
t.Errorf("WriteSignals() error = %v, wantErr %v", err, test.wantErr)
69+
}
70+
}

0 commit comments

Comments
 (0)