Skip to content

Commit

Permalink
Merge branch 'main' into drochow/issue-310/issue_match_order
Browse files Browse the repository at this point in the history
  • Loading branch information
drochow authored Nov 8, 2024
2 parents ec360e3 + 13086c0 commit 0b05723
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/samber/lo v1.47.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/vektah/gqlparser/v2 v2.5.18
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
)
Expand Down Expand Up @@ -85,7 +86,6 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/urfave/cli/v2 v2.27.4 // indirect
github.com/vektah/gqlparser/v2 v2.5.18 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ func ComponentInstanceBaseResolver(app app.Heureka, ctx context.Context, filter

f := &entity.ComponentInstanceFilter{
Paginated: entity.Paginated{First: first, After: afterId},
CCRN: filter.Ccrn,
IssueMatchId: imId,
ServiceId: serviceId,
ServiceCcrn: filter.ServiceCcrn,
ComponentVersionId: copmonentVersionId,
Search: filter.Search,
}

opt := GetListOptions(requestedFields)
Expand Down Expand Up @@ -142,7 +144,8 @@ func CcrnBaseResolver(app app.Heureka, ctx context.Context, filter *model.Compon
}

f := &entity.ComponentInstanceFilter{
CCRN: filter.Ccrn,
CCRN: filter.Ccrn,
Search: filter.Search,
}

opt := GetListOptions(requestedFields)
Expand Down
10 changes: 10 additions & 0 deletions internal/database/mariadb/component_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ func (s *SqlDatabase) ensureComponentInstanceFilter(f *entity.ComponentInstanceF
return f
}

const (
componentInstanceWildCardFilterQuery = "CI.componentinstance_ccrn LIKE Concat('%',?,'%')"
)

func (s *SqlDatabase) getComponentInstanceFilterString(filter *entity.ComponentInstanceFilter) string {
var fl []string
fl = append(fl, buildFilterQuery(filter.Id, "CI.componentinstance_id = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.CCRN, "CI.componentinstance_ccrn = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.IssueMatchId, "IM.issuematch_id = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.ServiceId, "CI.componentinstance_service_id = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.ServiceCcrn, "S.service_ccrn = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.ComponentVersionId, "CI.componentinstance_component_version_id = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.CCRN, "CI.componentinstance_ccrn = ?", OP_OR))
fl = append(fl, buildFilterQuery(filter.Search, componentInstanceWildCardFilterQuery, OP_OR))
fl = append(fl, "CI.componentinstance_deleted_at IS NULL")

filterStr := combineFilterQueries(fl, OP_AND)
Expand Down Expand Up @@ -116,10 +123,13 @@ func (s *SqlDatabase) buildComponentInstanceStatement(baseQuery string, filter *
//adding parameters
var filterParameters []interface{}
filterParameters = buildQueryParameters(filterParameters, filter.Id)
filterParameters = buildQueryParameters(filterParameters, filter.CCRN)
filterParameters = buildQueryParameters(filterParameters, filter.IssueMatchId)
filterParameters = buildQueryParameters(filterParameters, filter.ServiceId)
filterParameters = buildQueryParameters(filterParameters, filter.ServiceCcrn)
filterParameters = buildQueryParameters(filterParameters, filter.ComponentVersionId)
filterParameters = buildQueryParameters(filterParameters, filter.CCRN)
filterParameters = buildQueryParameters(filterParameters, filter.Search)
if withCursor {
filterParameters = append(filterParameters, cursor.Value)
filterParameters = append(filterParameters, cursor.Limit)
Expand Down
55 changes: 55 additions & 0 deletions internal/database/mariadb/component_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ var _ = Describe("ComponentInstance - ", Label("database", "ComponentInstance"),
Expect(entry.ServiceId).To(BeEquivalentTo(ciRow.ServiceId.Int64))
}
})
It("can filter by a single ccrn that does exist", func() {
ciRow := seedCollection.ComponentInstanceRows[rand.Intn(len(seedCollection.ComponentInstanceRows))]
filter := &entity.ComponentInstanceFilter{
CCRN: []*string{&ciRow.CCRN.String},
}

entries, err := db.GetComponentInstances(filter)

By("throwing no error", func() {
Expect(err).To(BeNil())
})

By("returning expected number of results", func() {
Expect(entries).To(HaveLen(1))
})

By("returning expected ccrn", func() {
Expect(entries[0].CCRN).To(BeEquivalentTo(ciRow.CCRN.String))
})

})
It("can filter by a single componentVersion id that does exist", func() {
// select a component version
cvRow := seedCollection.ComponentVersionRows[rand.Intn(len(seedCollection.ComponentVersionRows))]
Expand All @@ -146,6 +167,40 @@ var _ = Describe("ComponentInstance - ", Label("database", "ComponentInstance"),
Expect(len(entries)).To(BeEquivalentTo(len(ciIds)))
})
})
It("can filter Component Instance Ccrn using wild card search", func() {
row := seedCollection.ComponentInstanceRows[rand.Intn(len(seedCollection.ComponentInstanceRows))]

const charactersToRemoveFromBeginning = 2
const charactersToRemoveFromEnd = 2
const minimalCharactersToKeep = 2

start := charactersToRemoveFromBeginning
end := len(row.CCRN.String) - charactersToRemoveFromEnd

Expect(start+minimalCharactersToKeep < end).To(BeTrue())

searchStr := row.CCRN.String[start:end]
filter := &entity.ComponentInstanceFilter{Search: []*string{&searchStr}}

entries, err := db.GetComponentInstances(filter)

ccrn := []string{}
for _, entry := range entries {
ccrn = append(ccrn, entry.CCRN)
}

By("throwing no error", func() {
Expect(err).To(BeNil())
})

By("at least one element was discarded (filtered)", func() {
Expect(len(seedCollection.ServiceRows) > len(ccrn)).To(BeTrue())
})

By("returning the expected elements", func() {
Expect(ccrn).To(ContainElement(row.CCRN.String))
})
})
})
})
})
Expand Down
11 changes: 8 additions & 3 deletions internal/database/mariadb/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,18 @@ var _ = Describe("Issue", Label("database", "Issue"), func() {
When("Add Component Version to Issue", Label("AddComponentVersionToIssue"), func() {
Context("and we have 10 Issues in the database", func() {
var seedCollection *test.SeedCollection
var newComponentVersionRow mariadb.ComponentVersionRow
var newComponentVersion entity.ComponentVersion
var componentVersion *entity.ComponentVersion
BeforeEach(func() {
seedCollection = seeder.SeedDbWithNFakeData(10)
newComponentVersionRow = test.NewFakeComponentVersion()
newComponentVersionRow.ComponentId = seedCollection.ComponentRows[0].Id
newComponentVersion = newComponentVersionRow.AsComponentVersion()
componentVersion, _ = db.CreateComponentVersion(&newComponentVersion)
})
It("can add component version correctly", func() {
issue := seedCollection.IssueRows[0].AsIssue()
componentVersion := seedCollection.ComponentVersionRows[0].AsComponentVersion()
componentVersion.ComponentId = seedCollection.ComponentRows[0].Id.Int64

err := db.AddComponentVersionToIssue(issue.Id, componentVersion.Id)

Expand All @@ -725,7 +730,7 @@ var _ = Describe("Issue", Label("database", "Issue"), func() {
Expect(err).To(BeNil())
})
By("returning issue", func() {
Expect(len(i)).To(BeEquivalentTo(1))
Expect(i).To(HaveLen(1))
})
})
})
Expand Down
1 change: 1 addition & 0 deletions internal/entity/component_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ComponentInstanceFilter struct {
ComponentVersionId []*int64 `json:"component_version_id"`
Id []*int64 `json:"id"`
CCRN []*string `json:"ccrn"`
Search []*string `json:"search"`
}

type ComponentInstanceAggregations struct{}
Expand Down

0 comments on commit 0b05723

Please sign in to comment.