Skip to content

Commit

Permalink
Included tests for internal/signalio/json.go
Browse files Browse the repository at this point in the history
- Included tests for internal/signalio/json.go
- Removed un-necessary error check

Signed-off-by: nathannaveen <[email protected]>
  • Loading branch information
nathannaveen committed Jan 11, 2023
1 parent 27ad7d2 commit d523bf1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
9 changes: 4 additions & 5 deletions internal/signalio/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package signalio

import (
"encoding/json"
"fmt"
"io"
"sync"

Expand Down Expand Up @@ -51,10 +50,9 @@ func (w *jsonWriter) WriteSignals(signals []signal.Set, extra ...Field) error {
d = make(map[string]any)
data[ns] = d
}
nsData, ok := d.(map[string]any)
if !ok {
return fmt.Errorf("failed to get map for namespace: %s", ns)
}
nsData := d.(map[string]any)
// we don't need to check for an error here (the above line) because d is always a map[string]any

for k, v := range innerM {
nsData[k] = v
}
Expand All @@ -63,6 +61,7 @@ func (w *jsonWriter) WriteSignals(signals []signal.Set, extra ...Field) error {
for _, f := range extra {
data[f.Key] = f.Value
}

w.mu.Lock()
defer w.mu.Unlock()
return w.encoder.Encode(data)
Expand Down
70 changes: 70 additions & 0 deletions internal/signalio/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 signalio

import (
"encoding/json"
"io"
"testing"

"github.com/ossf/criticality_score/internal/collector/signal"
)

type testJSONWriterSet struct { //nolint:govet
UpdatedCount signal.Field[int]
Field string
}

func (t testJSONWriterSet) Namespace() signal.Namespace {
return "test"
}

type mockWriterJSON struct{}

func (m *mockWriterJSON) Write(p []byte) (n int, err error) {
return 0, nil
}

func Test_jsonWriter_WriteSignals(t *testing.T) {
type args struct {
signals []signal.Set
extra []Field
}
test := struct {
name string
encoder *json.Encoder
args args
wantErr bool
}{
name: "default",
encoder: json.NewEncoder(io.Writer(&mockWriterJSON{})),
args: args{
signals: []signal.Set{
&testJSONWriterSet{},
},
extra: []Field{
{
Key: "extra",
Value: "value",
},
},
},
}

w := JSONWriter(&mockWriterJSON{})
if err := w.WriteSignals(test.args.signals, test.args.extra...); (err != nil) != test.wantErr {
t.Errorf("WriteSignals() error = %v, wantErr %v", err, test.wantErr)
}
}

0 comments on commit d523bf1

Please sign in to comment.