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

View file

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