Skip to content

Commit

Permalink
removed scope lookup and added tag ID lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Nov 15, 2024
1 parent 74a6d1b commit 38c4d00
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 149 deletions.
8 changes: 0 additions & 8 deletions assetdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ func (as *AssetDB) FindEntityById(id string) (*types.Entity, error) {
return as.Repo.FindEntityById(id)
}

// FindByScope finds entities in the database by applying all the scope constraints provided
// and last seen after the since parameter.
// If since.IsZero(), the parameter will be ignored.
// It returns the matching entities and an error, if any.
func (as *AssetDB) FindByScope(constraints []oam.Asset, since time.Time) ([]*types.Entity, error) {
return as.Repo.FindEntitiesByScope(constraints, since)
}

// FindEntitiesByType finds all entities in the database of the provided asset type and last seen after the since parameter.
// If since.IsZero(), the parameter will be ignored.
// It returns the matching entities and an error, if any.
Expand Down
45 changes: 10 additions & 35 deletions assetdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,6 @@ func TestAssetDB(t *testing.T) {
}
})

t.Run("FindByScope", func(t *testing.T) {
testCases := []struct {
description string
assets []oam.Asset
expected []*types.Entity
expectedError error
}{
{"an entity is found", []oam.Asset{&domain.FQDN{Name: "domain.com"}}, []*types.Entity{{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an entity is not found", []oam.Asset{&domain.FQDN{Name: "domain.com"}}, []*types.Entity{}, fmt.Errorf("entity not found")},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
mockAssetDB := new(mockAssetDB)
adb := AssetDB{
Repo: mockAssetDB,
}

mockAssetDB.On("FindEntitiesByScope", tc.assets, start).Return(tc.expected, tc.expectedError)

result, err := adb.FindByScope(tc.assets, start)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)

mockAssetDB.AssertExpectations(t)
})
}
})

t.Run("FindEntitiesByType", func(t *testing.T) {
testCases := []struct {
description string
Expand Down Expand Up @@ -444,11 +414,6 @@ func (m *mockAssetDB) FindEntityByContent(asset oam.Asset, since time.Time) ([]*
return args.Get(0).([]*types.Entity), args.Error(1)
}

func (m *mockAssetDB) FindEntitiesByScope(constraints []oam.Asset, since time.Time) ([]*types.Entity, error) {
args := m.Called(constraints, since)
return args.Get(0).([]*types.Entity), args.Error(1)
}

func (m *mockAssetDB) FindEntitiesByType(atype oam.AssetType, since time.Time) ([]*types.Entity, error) {
args := m.Called(atype, since)
return args.Get(0).([]*types.Entity), args.Error(1)
Expand All @@ -474,6 +439,11 @@ func (m *mockAssetDB) CreateEntityTag(entity *types.Entity, property oam.Propert
return args.Get(0).(*types.EntityTag), args.Error(1)
}

func (m *mockAssetDB) FindEntityTagById(id string) (*types.EntityTag, error) {
args := m.Called(id)
return args.Get(0).(*types.EntityTag), args.Error(1)
}

func (m *mockAssetDB) GetEntityTags(entity *types.Entity, since time.Time, names ...string) ([]*types.EntityTag, error) {
args := m.Called(entity, since, names)
return args.Get(0).([]*types.EntityTag), args.Error(1)
Expand All @@ -489,6 +459,11 @@ func (m *mockAssetDB) CreateEdgeTag(edge *types.Edge, property oam.Property) (*t
return args.Get(0).(*types.EdgeTag), args.Error(1)
}

func (m *mockAssetDB) FindEdgeTagById(id string) (*types.EdgeTag, error) {
args := m.Called(id)
return args.Get(0).(*types.EdgeTag), args.Error(1)
}

func (m *mockAssetDB) GetEdgeTags(edge *types.Edge, since time.Time, names ...string) ([]*types.EdgeTag, error) {
args := m.Called(edge, since, names)
return args.Get(0).([]*types.EdgeTag), args.Error(1)
Expand Down
10 changes: 8 additions & 2 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ type Repository interface {
FindEntityById(id string) (*types.Entity, error)
FindEntityByContent(asset oam.Asset, since time.Time) ([]*types.Entity, error)
FindEntitiesByType(atype oam.AssetType, since time.Time) ([]*types.Entity, error)
FindEntitiesByScope(constraints []oam.Asset, since time.Time) ([]*types.Entity, error)
Link(edge *types.Edge) (*types.Edge, error)
IncomingEdges(entity *types.Entity, since time.Time, labels ...string) ([]*types.Edge, error)
OutgoingEdges(entity *types.Entity, since time.Time, labels ...string) ([]*types.Edge, error)
CreateEntityTag(entity *types.Entity, property oam.Property) (*types.EntityTag, error)
FindEntityTagById(id string) (*types.EntityTag, error)
GetEntityTags(entity *types.Entity, since time.Time, names ...string) ([]*types.EntityTag, error)
DeleteEntityTag(id string) error
CreateEdgeTag(edge *types.Edge, property oam.Property) (*types.EdgeTag, error)
FindEdgeTagById(id string) (*types.EdgeTag, error)
GetEdgeTags(edge *types.Edge, since time.Time, names ...string) ([]*types.EdgeTag, error)
DeleteEdgeTag(id string) error
Close() error
}

// New creates a new instance of the asset database repository.
func New(dbtype, dsn string) (Repository, error) {
if strings.EqualFold(dbtype, sqlrepo.Postgres) || strings.EqualFold(dbtype, sqlrepo.SQLite) {
switch strings.ToLower(dbtype) {
case strings.ToLower(sqlrepo.Postgres):
fallthrough
case strings.ToLower(sqlrepo.SQLite):
fallthrough
case strings.ToLower(sqlrepo.SQLiteMemory):
return sqlrepo.New(dbtype, dsn)
}
return nil, errors.New("unknown DB type")
Expand Down
104 changes: 0 additions & 104 deletions repository/sqlrepo/sql_scope.go

This file was deleted.

0 comments on commit 38c4d00

Please sign in to comment.