stillbox/pkg/talkgroups/xport/radioref/radioreference_test.go

87 lines
1.9 KiB
Go
Raw Normal View History

2024-11-21 13:23:21 -05:00
package radioref_test
2024-11-15 11:15:12 -05:00
import (
"context"
"encoding/json"
2024-11-15 13:06:37 -05:00
"math/rand"
2024-11-15 11:15:12 -05:00
"os"
"testing"
2024-11-15 13:06:37 -05:00
"github.com/google/uuid"
2024-11-15 11:15:12 -05:00
"github.com/stretchr/testify/assert"
2024-11-15 13:06:37 -05:00
"github.com/stretchr/testify/mock"
2024-11-15 11:15:12 -05:00
"github.com/stretchr/testify/require"
2024-11-15 12:18:32 -05:00
"dynatron.me/x/stillbox/pkg/database"
"dynatron.me/x/stillbox/pkg/database/mocks"
2024-11-15 13:06:37 -05:00
"dynatron.me/x/stillbox/pkg/talkgroups"
2024-11-20 22:13:23 -05:00
"dynatron.me/x/stillbox/pkg/talkgroups/tgstore"
2024-11-21 13:23:21 -05:00
"dynatron.me/x/stillbox/pkg/talkgroups/xport"
2024-11-15 11:15:12 -05:00
)
func getFixture(fixture string) []byte {
fixt, err := os.ReadFile("testdata/" + fixture)
if err != nil {
panic(err)
}
return fixt
}
2024-11-21 13:23:21 -05:00
func TestRadioRef(t *testing.T) {
2024-11-15 13:06:37 -05:00
// this is for deterministic UUIDs
uuid.SetRand(rand.New(rand.NewSource(1)))
tests := []struct {
name string
input []byte
impType string
sysID int
sysName string
jsExpect []byte
2024-11-15 11:15:12 -05:00
expectErr error
}{
{
2024-11-21 13:23:21 -05:00
name: "radioreference import",
2024-11-15 13:06:37 -05:00
impType: "radioreference",
input: getFixture("riscon.txt"),
2024-11-15 11:15:12 -05:00
jsExpect: getFixture("riscon.json"),
2024-11-15 13:06:37 -05:00
sysID: 197,
sysName: "RISCON",
},
2024-11-15 11:15:12 -05:00
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
2024-11-19 09:55:57 -05:00
dbMock := mocks.NewStore(t)
2024-11-15 13:06:37 -05:00
if tc.expectErr == nil {
dbMock.EXPECT().GetSystemName(mock.AnythingOfType("*context.valueCtx"), tc.sysID).Return(tc.sysName, nil)
}
ctx := database.CtxWithDB(context.Background(), dbMock)
2024-11-20 22:13:23 -05:00
ctx = tgstore.CtxWithStore(ctx, tgstore.NewCache())
2024-11-21 13:23:21 -05:00
ij := &xport.ImportJob{
Type: xport.Format(tc.impType),
2024-11-15 11:15:12 -05:00
SystemID: tc.sysID,
2024-11-15 13:06:37 -05:00
Body: string(tc.input),
2024-11-15 11:15:12 -05:00
}
tgs, err := ij.Import(ctx)
if tc.expectErr != nil {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.expectErr.Error())
} else {
require.NoError(t, err)
2024-11-15 13:06:37 -05:00
var fixt []talkgroups.Talkgroup
err = json.Unmarshal(tc.jsExpect, &fixt)
// jse, _ := json.Marshal(tgs); os.WriteFile("testdata/riscon.json", jse, 0600)
require.NoError(t, err)
2024-11-15 11:15:12 -05:00
2024-11-15 13:06:37 -05:00
assert.Equal(t, fixt, tgs)
2024-11-15 11:15:12 -05:00
}
})
}
}