Skip to content

Commit ecdb6d0

Browse files
committed
fixup: clean up and add tests
1 parent 467b489 commit ecdb6d0

File tree

14 files changed

+183
-24
lines changed

14 files changed

+183
-24
lines changed

api/tm-catalog.openapi.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ components:
15711571
meta:
15721572
page:
15731573
elements: 2
1574+
lastUpdated: '2024-12-11T13:07:22+01:00'
15741575
data:
15751576
- 'links':
15761577
'self': './inventory/.tmName/siemens/siemens/poc1000'

cmd/create_si.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
// createSiCmd represents the createSi command
1212
var createSiCmd = &cobra.Command{
1313
Use: "create-si",
14-
Short: "Creates or updates a bleve search index",
15-
Long: `Creates or updates a bleve search index for all TMs`,
16-
Run: executeCreateSearchIndex,
14+
Short: "Create or update a bleve search index",
15+
Long: `Create or update a bleve search index for deep searching TMs with '--search <query> --deep'. Usually needs to
16+
be called only once per repository. Afterwards, updates are performed automatically an outdated search index is detected.`,
17+
Run: executeCreateSearchIndex,
1718
}
1819

1920
func init() {
2021
RootCmd.AddCommand(createSiCmd)
21-
createSiCmd.Flags().StringP("repo", "r", "", "name of the remote to pull from")
22-
createSiCmd.Flags().StringP("directory", "d", "", "TM repository directory to pull from")
22+
AddRepoConstraintFlags(createSiCmd)
2323
}
2424

2525
func executeCreateSearchIndex(cmd *cobra.Command, args []string) {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ go 1.23
55
require (
66
github.com/Masterminds/semver/v3 v3.2.1
77
github.com/MicahParks/keyfunc/v3 v3.2.5
8+
github.com/blevesearch/bleve/v2 v2.4.2
89
github.com/buger/jsonparser v1.1.1
910
github.com/gofrs/flock v0.8.1
1011
github.com/golang-jwt/jwt/v5 v5.2.0
1112
github.com/google/renameio v1.0.1
13+
github.com/google/uuid v1.5.0
1214
github.com/gorilla/handlers v1.5.1
1315
github.com/gorilla/mux v1.8.1
1416
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
@@ -27,7 +29,6 @@ require (
2729
github.com/RoaringBitmap/roaring v1.9.3 // indirect
2830
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
2931
github.com/bits-and-blooms/bitset v1.12.0 // indirect
30-
github.com/blevesearch/bleve/v2 v2.4.2 // indirect
3132
github.com/blevesearch/bleve_index_api v1.1.10 // indirect
3233
github.com/blevesearch/geo v0.1.20 // indirect
3334
github.com/blevesearch/go-faiss v1.0.20 // indirect
@@ -52,7 +53,6 @@ require (
5253
github.com/golang/protobuf v1.5.3 // indirect
5354
github.com/golang/snappy v0.0.4 // indirect
5455
github.com/google/btree v1.1.3 // indirect
55-
github.com/google/uuid v1.5.0 // indirect
5656
github.com/hashicorp/hcl v1.0.0 // indirect
5757
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5858
github.com/json-iterator/go v1.1.12 // indirect

internal/app/cli/list.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ func printIndex(res model.SearchResult) {
5050
mpn := elideString(value.Mpn, colWidth)
5151
auth := elideString(value.Author.Name, colWidth)
5252
repo := elideString(fmt.Sprintf("%v", value.FoundIn), colWidth)
53-
5453
_, _ = fmt.Fprintf(table, "%s\t%s\t%s\t%s\t%s\n", name, auth, man, mpn, repo)
55-
5654
}
5755
_ = table.Flush()
5856
}

internal/app/http/handler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sort"
1313
"strings"
1414
"testing"
15+
"time"
1516

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

14171419
var (
14181420
listResult1 = model.SearchResult{
1421+
LastUpdated: time.Date(2023, time.November, 10, 12, 32, 43, 0, time.UTC),
14191422
Entries: []model.FoundEntry{
14201423
{
14211424
Name: "a-corp/eagle/bt2000",

internal/app/http/mocks/HandlerService.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/model/model.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"regexp"
99
"slices"
1010
"strings"
11-
"time"
1211

1312
"github.com/Masterminds/semver/v3"
1413
"github.com/wot-oss/tmc/internal/utils"
@@ -233,7 +232,6 @@ type RepoDescription struct {
233232
Name string
234233
Type string
235234
Description string
236-
LastUpdated time.Time
237235
}
238236

239237
type CheckResultType int

internal/model/search.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var ErrSearchIndexNotFound = errors.New("search index not found. Use `tmc create
2424
type SearchResult struct {
2525
LastUpdated time.Time
2626
Entries []FoundEntry
27-
indexPath string
2827
}
2928

3029
type FoundEntry struct {
@@ -110,6 +109,10 @@ type SearchParams struct {
110109
Options SearchOptions
111110
}
112111

112+
func (p *SearchParams) SetIndexPath(indexPath string) {
113+
p.Options.indexPath = indexPath
114+
}
115+
113116
func (p *SearchParams) Sanitize() {
114117
p.Author = sanitizeList(p.Author)
115118
p.Manufacturer = sanitizeList(p.Manufacturer)
@@ -124,6 +127,8 @@ type SearchOptions struct {
124127
NameFilterType FilterType
125128
// UseBleve indicates that the search query uses bleve query syntax
126129
UseBleve bool
130+
131+
indexPath string
127132
}
128133

129134
func (sr *SearchResult) Filter(search *SearchParams) error {
@@ -161,7 +166,7 @@ func (sr *SearchResult) Filter(search *SearchParams) error {
161166
return nil
162167
}
163168

164-
del, err := getSearchExclusionFunction(search, sr.indexPath)
169+
del, err := getSearchExclusionFunction(search, search.Options.indexPath)
165170
if err != nil {
166171
return err
167172
}
@@ -171,11 +176,6 @@ func (sr *SearchResult) Filter(search *SearchParams) error {
171176
return nil
172177
}
173178

174-
func (sr *SearchResult) WithSearchIndex(indexPath string) *SearchResult {
175-
sr.indexPath = indexPath
176-
return sr
177-
}
178-
179179
func getSearchExclusionFunction(search *SearchParams, indexPath string) (func(e FoundEntry) bool, error) {
180180
if search.Query == "" {
181181
return nil, nil

internal/repos/fs_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ func TestFileRepo_Index(t *testing.T) {
352352

353353
idx, err := r.readIndex()
354354
assert.NoError(t, err)
355+
zeroTime := time.Time{}
356+
assert.True(t, idx.Meta.Created.After(zeroTime))
355357
assert.Equal(t, 1, len(idx.Data))
356358
assert.Equal(t, "omnicorp-tm-department/omnicorp/omnilamp/subfolder", idx.Data[0].Name)
357359
assert.Equal(t, 1, len(idx.Data[0].Versions))

internal/repos/tmc_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"net/http/httptest"
1010
"net/url"
1111
"os"
12+
"reflect"
1213
"testing"
14+
"time"
1315

1416
"github.com/stretchr/testify/assert"
1517
"github.com/wot-oss/tmc/internal/model"
@@ -222,6 +224,9 @@ func TestTmcRepo_List(t *testing.T) {
222224
if test.expErr == "" {
223225
assert.NoError(t, err)
224226
assert.Equal(t, test.expRes, len(sr.Entries))
227+
if reflect.DeepEqual(test.body, inventory) {
228+
assert.Equal(t, time.Date(2024, 12, 1, 10, 0, 0, 0, time.UTC), sr.LastUpdated)
229+
}
225230
for _, e := range sr.Entries {
226231
for _, v := range e.Versions {
227232
assert.NotEmpty(t, v.TMID)

0 commit comments

Comments
 (0)