Importer test

This commit is contained in:
Daniel 2024-11-15 13:06:37 -05:00
parent c9a32cd4bf
commit 1b051c6ad9
6 changed files with 1684 additions and 27 deletions

10
.mockery.yaml Normal file
View file

@ -0,0 +1,10 @@
dir: '{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}/mocks'
mockname: "{{.InterfaceName}}"
outpkg: "mocks"
filename: "{{.InterfaceName}}.go"
with-expecter: true
packages:
dynatron.me/x/stillbox/pkg/database:
config:
interfaces:
DB:

View file

@ -16,6 +16,7 @@ import (
)
// DB is a database handle.
//go:generate mockery
type DB interface {
Querier

1631
pkg/database/mocks/DB.go Normal file

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,6 @@ import (
"regexp"
"strconv"
"strings"
"time"
"github.com/google/uuid"
@ -74,11 +73,6 @@ func (rr *radioReferenceImporter) importTalkgroups(ctx context.Context, sys int,
return nil, talkgroups.ErrNoSuchSystem
}
importedFrom := jsontypes.Metadata{
"from": "RadioReference",
"time": time.Now(),
}
var groupName string
state := rrsInitial
for sc.Scan() {
@ -107,12 +101,11 @@ func (rr *radioReferenceImporter) importTalkgroups(ctx context.Context, sys int,
if err != nil {
continue
}
metadata := jsontypes.Metadata{
"imported": importedFrom,
}
var metadata jsontypes.Metadata
tgt := talkgroups.TG(sys, tgid)
mode := fields[2]
if strings.Contains(mode, "E") {
metadata = make(jsontypes.Metadata)
metadata["encrypted"] = true
}
tags := []string{fields[5]}

View file

@ -3,15 +3,19 @@ package importer_test
import (
"context"
"encoding/json"
"math/rand"
"os"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"dynatron.me/x/stillbox/pkg/talkgroups/importer"
"dynatron.me/x/stillbox/pkg/database"
"dynatron.me/x/stillbox/pkg/database/mocks"
"dynatron.me/x/stillbox/pkg/talkgroups"
"dynatron.me/x/stillbox/pkg/talkgroups/importer"
)
func getFixture(fixture string) []byte {
@ -20,34 +24,49 @@ func getFixture(fixture string) []byte {
panic(err)
}
return fixt
}
func TestRadioReferenceImport(t *testing.T) {
tests := []struct{
name string
input []byte
sysID int
jsExpect []byte
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
expectErr error
}{
{
name: "base",
input: getFixture("riscon.txt"),
name: "radioreference",
impType: "radioreference",
input: getFixture("riscon.txt"),
jsExpect: getFixture("riscon.json"),
sysID: 197,
sysID: 197,
sysName: "RISCON",
},
{
name: "unknown importer",
impType: "nonexistent",
expectErr: importer.ErrBadImportType,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
ctx = database.CtxWithDB(ctx, mocks.NewDB())
dbMock := mocks.NewDB(t)
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())
ij := &importer.ImportJob{
Type: "radioreference",
Type: importer.ImportSource(tc.impType),
SystemID: tc.sysID,
Body: string(tc.input),
Body: string(tc.input),
}
tgs, err := ij.Import(ctx)
@ -58,10 +77,12 @@ func TestRadioReferenceImport(t *testing.T) {
} else {
require.NoError(t, err)
jse, jerr := json.Marshal(tgs)
require.NoError(t, jerr)
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)
assert.Equal(t, tc.jsExpect, jse)
assert.Equal(t, fixt, tgs)
}
})

File diff suppressed because one or more lines are too long