Skip to content

Commit

Permalink
fixup: clean up and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrdn committed Dec 12, 2024
1 parent 467b489 commit d592c34
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 24 deletions.
10 changes: 5 additions & 5 deletions cmd/create_si.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
// createSiCmd represents the createSi command
var createSiCmd = &cobra.Command{
Use: "create-si",
Short: "Creates or updates a bleve search index",
Long: `Creates or updates a bleve search index for all TMs`,
Run: executeCreateSearchIndex,
Short: "Create or update a bleve search index",
Long: `Create or update a bleve search index for deep searching TMs with '--search <query> --deep'. Usually needs to
be called only once per repository. Afterwards, updates are performed automatically an outdated search index is detected.`,
Run: executeCreateSearchIndex,
}

func init() {
RootCmd.AddCommand(createSiCmd)
createSiCmd.Flags().StringP("repo", "r", "", "name of the remote to pull from")
createSiCmd.Flags().StringP("directory", "d", "", "TM repository directory to pull from")
AddRepoConstraintFlags(createSiCmd)
}

func executeCreateSearchIndex(cmd *cobra.Command, args []string) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.23
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/MicahParks/keyfunc/v3 v3.2.5
github.com/blevesearch/bleve/v2 v2.4.2
github.com/buger/jsonparser v1.1.1
github.com/gofrs/flock v0.8.1
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/google/renameio v1.0.1
github.com/google/uuid v1.5.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.1
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
Expand All @@ -27,7 +29,6 @@ require (
github.com/RoaringBitmap/roaring v1.9.3 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/bits-and-blooms/bitset v1.12.0 // indirect
github.com/blevesearch/bleve/v2 v2.4.2 // indirect
github.com/blevesearch/bleve_index_api v1.1.10 // indirect
github.com/blevesearch/geo v0.1.20 // indirect
github.com/blevesearch/go-faiss v1.0.20 // indirect
Expand All @@ -52,7 +53,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 0 additions & 2 deletions internal/app/cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func printIndex(res model.SearchResult) {
mpn := elideString(value.Mpn, colWidth)
auth := elideString(value.Author.Name, colWidth)
repo := elideString(fmt.Sprintf("%v", value.FoundIn), colWidth)

_, _ = fmt.Fprintf(table, "%s\t%s\t%s\t%s\t%s\n", name, auth, man, mpn, repo)

}
_ = table.Flush()
}
Expand Down
3 changes: 3 additions & 0 deletions internal/app/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/wot-oss/tmc/internal/app/http/mocks"
"github.com/wot-oss/tmc/internal/commands"
Expand Down Expand Up @@ -212,6 +213,7 @@ func Test_Inventory(t *testing.T) {
assert.Equal(t, 2, len(response.Data))
assertInventoryEntry(t, listResult1.Entries[0], response.Data[0])
assertInventoryEntry(t, listResult1.Entries[1], response.Data[1])
assert.Equal(t, listResult1.LastUpdated.Format(time.RFC3339), response.Meta.LastUpdated)
// and then result is ordered ascending by name
isSorted := sort.SliceIsSorted(response.Data, func(i, j int) bool {
return response.Data[i].TmName < response.Data[j].TmName
Expand Down Expand Up @@ -1416,6 +1418,7 @@ func assertAttachments(t *testing.T, linkPrefix string, expAtts []model.Attachme

var (
listResult1 = model.SearchResult{
LastUpdated: time.Date(2023, time.November, 10, 12, 32, 43, 0, time.UTC),
Entries: []model.FoundEntry{
{
Name: "a-corp/eagle/bt2000",
Expand Down
2 changes: 1 addition & 1 deletion internal/app/http/mocks/HandlerService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"slices"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/wot-oss/tmc/internal/utils"
Expand Down Expand Up @@ -233,7 +232,6 @@ type RepoDescription struct {
Name string
Type string
Description string
LastUpdated time.Time
}

type CheckResultType int
Expand Down
14 changes: 7 additions & 7 deletions internal/model/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var ErrSearchIndexNotFound = errors.New("search index not found. Use `tmc create
type SearchResult struct {
LastUpdated time.Time
Entries []FoundEntry
indexPath string
}

type FoundEntry struct {
Expand Down Expand Up @@ -110,6 +109,10 @@ type SearchParams struct {
Options SearchOptions
}

func (p *SearchParams) SetIndexPath(indexPath string) {
p.Options.indexPath = indexPath
}

func (p *SearchParams) Sanitize() {
p.Author = sanitizeList(p.Author)
p.Manufacturer = sanitizeList(p.Manufacturer)
Expand All @@ -124,6 +127,8 @@ type SearchOptions struct {
NameFilterType FilterType
// UseBleve indicates that the search query uses bleve query syntax
UseBleve bool

indexPath string
}

func (sr *SearchResult) Filter(search *SearchParams) error {
Expand Down Expand Up @@ -161,7 +166,7 @@ func (sr *SearchResult) Filter(search *SearchParams) error {
return nil
}

del, err := getSearchExclusionFunction(search, sr.indexPath)
del, err := getSearchExclusionFunction(search, search.Options.indexPath)
if err != nil {
return err
}
Expand All @@ -171,11 +176,6 @@ func (sr *SearchResult) Filter(search *SearchParams) error {
return nil
}

func (sr *SearchResult) WithSearchIndex(indexPath string) *SearchResult {
sr.indexPath = indexPath
return sr
}

func getSearchExclusionFunction(search *SearchParams, indexPath string) (func(e FoundEntry) bool, error) {
if search.Query == "" {
return nil, nil
Expand Down
2 changes: 2 additions & 0 deletions internal/repos/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ func TestFileRepo_Index(t *testing.T) {

idx, err := r.readIndex()
assert.NoError(t, err)
zeroTime := time.Time{}
assert.True(t, idx.Meta.Created.After(zeroTime))
assert.Equal(t, 1, len(idx.Data))
assert.Equal(t, "omnicorp-tm-department/omnicorp/omnilamp/subfolder", idx.Data[0].Name)
assert.Equal(t, 1, len(idx.Data[0].Versions))
Expand Down
5 changes: 5 additions & 0 deletions internal/repos/tmc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"net/http/httptest"
"net/url"
"os"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/wot-oss/tmc/internal/model"
Expand Down Expand Up @@ -222,6 +224,9 @@ func TestTmcRepo_List(t *testing.T) {
if test.expErr == "" {
assert.NoError(t, err)
assert.Equal(t, test.expRes, len(sr.Entries))
if reflect.DeepEqual(test.body, inventory) {
assert.Equal(t, time.Date(2024, 12, 1, 10, 0, 0, 0, time.UTC), sr.LastUpdated)
}
for _, e := range sr.Entries {
for _, v := range e.Versions {
assert.NotEmpty(t, v.TMID)
Expand Down
7 changes: 4 additions & 3 deletions internal/repos/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,17 @@ func listRepoWithDeepSearch(ctx context.Context, r Repo, search *model.SearchPar
}
}

// refine with bleve
// refine the result with bleve
filtered := &searchResult
search.Query = query
search.Options.UseBleve = true
err = filtered.WithSearchIndex(indexPath).Filter(search)
search.SetIndexPath(indexPath)
err = filtered.Filter(search)
if err != nil {
return model.SearchResult{}, err
}

return searchResult, err
return *filtered, err
}

func (u *Union) GetTMMetadata(ctx context.Context, tmID string) ([]model.FoundVersion, []*RepoAccessError) {
Expand Down
151 changes: 151 additions & 0 deletions internal/repos/union_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package repos

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/wot-oss/tmc/internal/config"
"github.com/wot-oss/tmc/internal/model"
"github.com/wot-oss/tmc/internal/testutils"
"github.com/wot-oss/tmc/internal/utils"
)

func TestUnion_List_WithSearch(t *testing.T) {
t.Run("simple search", func(t *testing.T) {
tempDir, _ := os.MkdirTemp("", "tmc-export")
defer os.RemoveAll(tempDir)
old := config.ConfigDir
config.ConfigDir = filepath.Join(tempDir, "config")
defer func() { config.ConfigDir = old }()
repoRoot := filepath.Join(tempDir, "repo")
r := &FileRepo{
root: repoRoot,
spec: model.NewRepoSpec("repo"),
}
u := NewUnion(r)
err := testutils.CopyDir("../../test/data/repos/file/attachments", repoRoot)
assert.NoError(t, err)

sp := &model.SearchParams{
Query: "query",
}
lu := time.Date(2024, 5, 3, 15, 20, 26, 0, time.UTC)

res, errs := u.List(context.Background(), sp)

assert.Empty(t, errs)
assert.Equal(t, lu, res.LastUpdated)
assert.Len(t, res.Entries, 0)
})
t.Run("no bleve index", func(t *testing.T) {
tempDir, _ := os.MkdirTemp("", "tmc-export")
defer os.RemoveAll(tempDir)
old := config.ConfigDir
config.ConfigDir = filepath.Join(tempDir, "config")
defer func() { config.ConfigDir = old }()
repoRoot := filepath.Join(tempDir, "repo")
r := &FileRepo{
root: repoRoot,
spec: model.NewRepoSpec("repo"),
}
u := NewUnion(r)
err := testutils.CopyDir("../../test/data/repos/file/attachments", repoRoot)
assert.NoError(t, err)

sp := &model.SearchParams{
Query: "query",
Options: model.SearchOptions{
UseBleve: true,
},
}

_, errs := u.List(context.Background(), sp)
if assert.Len(t, errs, 1) {
assert.ErrorIs(t, errs[0], model.ErrSearchIndexNotFound)
}
})
t.Run("existing bleve index", func(t *testing.T) {
tempDir, _ := os.MkdirTemp("", "tmc-export")
defer os.RemoveAll(tempDir)
old := config.ConfigDir
config.ConfigDir = filepath.Join(tempDir, "config")
defer func() { config.ConfigDir = old }()
repoRoot := filepath.Join(tempDir, "repo")
r := &FileRepo{
root: repoRoot,
spec: model.NewRepoSpec("repo"),
}
u := NewUnion(r)

err := testutils.CopyDir("../../test/data/repos/file/attachments", repoRoot)
assert.NoError(t, err)

err = UpdateRepoIndex(context.Background(), r)
assert.NoError(t, err)

t.Run("with no match", func(t *testing.T) {
sp := &model.SearchParams{
Query: "query",
Options: model.SearchOptions{
UseBleve: true,
},
}

res, errs := u.List(context.Background(), sp)
assert.Len(t, errs, 0)
assert.Len(t, res.Entries, 0)
})
t.Run("with match", func(t *testing.T) {
sp := &model.SearchParams{
Query: "\"Lamp reaches a critical temperature\"",
Options: model.SearchOptions{
UseBleve: true,
},
}

res, errs := u.List(context.Background(), sp)
assert.Len(t, errs, 0)
assert.Len(t, res.Entries, 1)
})

})
t.Run("outdated bleve index", func(t *testing.T) {
tempDir, _ := os.MkdirTemp("", "tmc-export")
defer os.RemoveAll(tempDir)
old := config.ConfigDir
config.ConfigDir = filepath.Join(tempDir, "config")
defer func() { config.ConfigDir = old }()
repoRoot := filepath.Join(tempDir, "repo")
r := &FileRepo{
root: repoRoot,
spec: model.NewRepoSpec("repo"),
}
u := NewUnion(r)

err := testutils.CopyDir("../../test/data/repos/file/attachments", repoRoot)
assert.NoError(t, err)

indexPath := BleveIndexPath(r)
_ = os.MkdirAll(indexPath, defaultDirPermissions)
_ = utils.WriteFileLines(
[]string{time.Date(2024, 1, 1, 1, 1, 1, 0, time.UTC).Format(time.RFC3339)},
filepath.Join(indexPath, "updated"),
defaultFilePermissions)

sp := &model.SearchParams{
Query: "\"Lamp reaches a critical temperature\"",
Options: model.SearchOptions{
UseBleve: true,
},
}

res, errs := u.List(context.Background(), sp)
assert.Len(t, errs, 0)
assert.Len(t, res.Entries, 1)

})
}
2 changes: 1 addition & 1 deletion test/data/repos/file/attachments/.tmc/tm-catalog.toc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"meta": {
"created": "2024-05-03T15:20:26.96061534+03:00"
"created": "2024-05-03T15:20:26Z"
},
"data": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/data/repos/inventory_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@
}
],
"meta": {
"created": "0001-01-01T00:00:00Z"
"lastUpdated": "2024-12-01T10:00:00Z"
}
}

0 comments on commit d592c34

Please sign in to comment.