2024-11-15 11:15:12 -05:00
|
|
|
package importer_test
|
|
|
|
|
|
|
|
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"
|
|
|
|
"dynatron.me/x/stillbox/pkg/talkgroups/importer"
|
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-15 13:06:37 -05:00
|
|
|
func TestImport(t *testing.T) {
|
|
|
|
// 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-15 13:06:37 -05:00
|
|
|
name: "radioreference",
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown importer",
|
|
|
|
impType: "nonexistent",
|
|
|
|
expectErr: importer.ErrBadImportType,
|
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)
|
|
|
|
ctx = talkgroups.CtxWithStore(ctx, talkgroups.NewCache())
|
2024-11-15 11:15:12 -05:00
|
|
|
ij := &importer.ImportJob{
|
2024-11-15 13:06:37 -05:00
|
|
|
Type: importer.ImportSource(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
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|