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

Included tests for internal/signalio/json.go #306

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth extending the comment to say that "if this fails in the future the assignment to nsData below will fail"


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
}
Comment on lines +34 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a bytes.Buffer to avoid needing to implement the io.Writer interface.
It also allows you to inspect what was written.


func Test_jsonWriter_WriteSignals(t *testing.T) {
type args struct {
signals []signal.Set
extra []Field
}
test := struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest restructuring these tests into the following the test functions as it isn't really clear what this function is trying to test:

  1. An end-to-end style test that:
  • creates the writer with a bytes.Buffer.
  • passes in test data for all the parameters.
  • checks the output matches a JSON string (e.g. want := "{...}").
  1. A test that checks a writer error with a io.Writer implementation that always returns an error (like iotest.ErrReader, but an equivalent for io.Writer doesn't exist).
  2. (Optional) a test that exercises encoding errors.

These should ideally not contain the test struct building at the start as it isn't really necessary.

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)
}
}