Skip to content

Commit

Permalink
fixed the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Jan 14, 2024
1 parent d6f34c2 commit 15e42f1
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 108 deletions.
14 changes: 7 additions & 7 deletions assetdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func New(dbType repository.DBType, dsn string) *AssetDB {
}
}

// GetDBType returns the type of the underlying database.
func (as *AssetDB) GetDBType() string {
return as.repository.GetDBType()
}

// Create creates a new asset in the database.
// If source is nil, the discovered asset will be created and relation will be ignored
// If source and relation are provided, the asset is created and linked to the source asset using the specified relation.
Expand Down Expand Up @@ -98,19 +103,14 @@ func (as *AssetDB) OutgoingRelations(asset *types.Asset, since time.Time, relati
return as.repository.OutgoingRelations(asset, since, relationTypes...)
}

// GetDBType returns the type of the underlying database.
func (as *AssetDB) GetDBType() string {
return as.repository.GetDBType()
}

// AssetQuery executes a query against the asset table of the db.
// For SQL databases, the query will start with "SELECT * FROM assets " and then add the provided constraints.
// For SQL databases, the query will start with "SELECT * FROM assets " and then add the necessary constraints.
func (as *AssetDB) AssetQuery(constraints string) ([]*types.Asset, error) {
return as.repository.AssetQuery(constraints)
}

// RelationQuery executes a query against the relation table of the db.
// For SQL databases, the query will start with "SELECT * FROM relations " and then add the provided constraints.
// For SQL databases, the query will start with "SELECT * FROM relations " and then add the necessary constraints.
func (as *AssetDB) RelationQuery(constraints string) ([]*types.Relation, error) {
return as.repository.RelationQuery(constraints)
}
79 changes: 33 additions & 46 deletions assetdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type mockAssetDB struct {
mock.Mock
}

func (m *mockAssetDB) GetDBType() string {
args := m.Called()
return args.String(0)
}

func (m *mockAssetDB) CreateAsset(asset oam.Asset) (*types.Asset, error) {
args := m.Called(asset)
return args.Get(0).(*types.Asset), args.Error(1)
Expand Down Expand Up @@ -85,11 +90,6 @@ func (m *mockAssetDB) OutgoingRelations(asset *types.Asset, since time.Time, rel
return args.Get(0).([]*types.Relation), args.Error(1)
}

func (m *mockAssetDB) GetDBType() string {
args := m.Called()
return args.String(0)
}

func (m *mockAssetDB) AssetQuery(query string) ([]*types.Asset, error) {
args := m.Called(query)
return args.Get(0).([]*types.Asset), args.Error(1)
Expand All @@ -101,9 +101,7 @@ func (m *mockAssetDB) RelationQuery(constraints string) ([]*types.Relation, erro
}

func TestMain(m *testing.M) {
exitVal := m.Run()

os.Exit(exitVal)
os.Exit(m.Run())
}

func TestAssetDB(t *testing.T) {
Expand All @@ -122,18 +120,18 @@ func TestAssetDB(t *testing.T) {
}{
{
description: "successfully create initial asset",
discovered: domain.FQDN{Name: "www.domain.com"},
discovered: &domain.FQDN{Name: "www.domain.com"},
source: nil,
relation: "",
expected: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
expected: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
expectedError: nil,
},
{
description: "successfully create an asset with relation",
discovered: network.AutonomousSystem{Number: 1},
source: &types.Asset{ID: "2", Asset: network.RIROrganization{Name: "RIPE NCC"}},
discovered: &network.AutonomousSystem{Number: 1},
source: &types.Asset{ID: "2", Asset: &network.RIROrganization{Name: "RIPE NCC"}},
relation: relationType,
expected: &types.Asset{ID: "3", Asset: network.AutonomousSystem{Number: 1}},
expected: &types.Asset{ID: "3", Asset: &network.AutonomousSystem{Number: 1}},
expectedError: nil,
},
}
Expand Down Expand Up @@ -170,7 +168,7 @@ func TestAssetDB(t *testing.T) {
expected *types.Asset
expectedError error
}{
{"an asset is found", "1", &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}, nil},
{"an asset is found", "1", &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}}, nil},
{"an asset is not found", "2", &types.Asset{}, fmt.Errorf("asset not found")},
}

Expand Down Expand Up @@ -201,10 +199,10 @@ func TestAssetDB(t *testing.T) {
expected []*types.Asset
expectedError error
}{
{"an asset is found", domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{}, fmt.Errorf("asset not found")},
{"asset last seen after since", domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"asset last seen before since", domain.FQDN{Name: "www.domain.com"}, time.Now(), []*types.Asset{}, fmt.Errorf("asset last seen before since")},
{"an asset is found", &domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", &domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{}, fmt.Errorf("asset not found")},
{"asset last seen after since", &domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}}}, nil},
{"asset last seen before since", &domain.FQDN{Name: "www.domain.com"}, time.Now(), []*types.Asset{}, fmt.Errorf("asset last seen before since")},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -233,8 +231,8 @@ func TestAssetDB(t *testing.T) {
expected []*types.Asset
expectedError error
}{
{"an asset is found", []oam.Asset{domain.FQDN{Name: "domain.com"}}, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", []oam.Asset{domain.FQDN{Name: "domain.com"}}, []*types.Asset{}, fmt.Errorf("asset not found")},
{"an asset is found", []oam.Asset{&domain.FQDN{Name: "domain.com"}}, []*types.Asset{{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", []oam.Asset{&domain.FQDN{Name: "domain.com"}}, []*types.Asset{}, fmt.Errorf("asset not found")},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -263,7 +261,7 @@ func TestAssetDB(t *testing.T) {
expected []*types.Asset
expectedError error
}{
{"an asset is found", oam.FQDN, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is found", oam.FQDN, []*types.Asset{{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", oam.FQDN, []*types.Asset{}, fmt.Errorf("asset not found")},
}

Expand Down Expand Up @@ -297,36 +295,36 @@ func TestAssetDB(t *testing.T) {
}{
{
description: "successfully find incoming relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
asset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{
{
ID: "1",
Type: "ns_record",
FromAsset: &types.Asset{ID: "2", Asset: domain.FQDN{Name: "www.subdomain1.com"}},
ToAsset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
FromAsset: &types.Asset{ID: "2", Asset: &domain.FQDN{Name: "www.subdomain1.com"}},
ToAsset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
},
{
ID: "2",
Type: "cname_record",
FromAsset: &types.Asset{ID: "3", Asset: domain.FQDN{Name: "www.subdomain2.com"}},
ToAsset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
FromAsset: &types.Asset{ID: "3", Asset: &domain.FQDN{Name: "www.subdomain2.com"}},
ToAsset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
},
},
expectedError: nil,
},
{
description: "error finding incoming relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
asset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
expectedError: fmt.Errorf("error finding incoming relations"),
},
{
description: "incoming relations before since parameter",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
asset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
since: time.Now().Add(time.Minute),
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
Expand Down Expand Up @@ -364,36 +362,36 @@ func TestAssetDB(t *testing.T) {
}{
{
description: "successfully find outgoing relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
asset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{
{
ID: "1",
Type: "ns_record",
FromAsset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
ToAsset: &types.Asset{ID: "2", Asset: domain.FQDN{Name: "www.subdomain1.com"}},
FromAsset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
ToAsset: &types.Asset{ID: "2", Asset: &domain.FQDN{Name: "www.subdomain1.com"}},
},
{
ID: "2",
Type: "cname_record",
FromAsset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
ToAsset: &types.Asset{ID: "2", Asset: domain.FQDN{Name: "www.subdomain2.com"}},
FromAsset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
ToAsset: &types.Asset{ID: "2", Asset: &domain.FQDN{Name: "www.subdomain2.com"}},
},
},
expectedError: nil,
},
{
description: "error finding outgoing relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
asset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
expectedError: fmt.Errorf("error finding outgoing relations"),
},
{
description: "outgoing relations before since parameter",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
asset: &types.Asset{ID: "1", Asset: &domain.FQDN{Name: "www.domain.com"}},
since: time.Now().Add(time.Minute),
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
Expand Down Expand Up @@ -479,9 +477,7 @@ func TestAssetDB(t *testing.T) {

func TestAssetQuery(t *testing.T) {
// Set up the SQLite database for testing

db := newGraph("local", "test.db")

defer teardownSqlite("test.db")

createdAssets := createAssets(db)
Expand All @@ -490,29 +486,22 @@ func TestAssetQuery(t *testing.T) {
if err != nil {
panic(err)
}

// compare the assets
assert.Equal(t, createdAssets, queriedAssets)

}

func TestRelationQuery(t *testing.T) {

// Set up the SQLite database for testing

db := newGraph("local", "test.db")

defer teardownSqlite("test.db")

createdAssets := createAssets(db)

createdRelations := createRelations(createdAssets, db)

queriedRelations, err := db.RelationQuery("")
if err != nil {
panic(err)
}

// Verify the relations and assets are populated in the relation
// Have to do it this way because "CreatedAt" is not populated when creating relations.
for k, relation := range queriedRelations {
Expand All @@ -522,7 +511,6 @@ func TestRelationQuery(t *testing.T) {
assert.Contains(t, createdAssets, relation.FromAsset)
assert.Contains(t, createdAssets, relation.ToAsset)
}

}

func createRelations(assets []*types.Asset, db *AssetDB) []*types.Relation {
Expand Down Expand Up @@ -564,7 +552,6 @@ func createRelations(assets []*types.Asset, db *AssetDB) []*types.Relation {
}

func createAssets(db *AssetDB) []*types.Asset {

// Create test assets
assets := []oam.Asset{
&domain.FQDN{Name: "example.com"},
Expand Down
5 changes: 3 additions & 2 deletions repository/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Asset struct {

// Parse parses the content of the asset into the corresponding Open Asset Model (OAM) asset type.
// It returns the parsed asset and an error, if any.
func (a Asset) Parse() (oam.Asset, error) {
func (a *Asset) Parse() (oam.Asset, error) {
var err error
var asset oam.Asset

Expand Down Expand Up @@ -124,7 +124,7 @@ func (a Asset) Parse() (oam.Asset, error) {

// JSONQuery generates a JSON query expression based on the asset's content.
// It returns the generated JSON query expression and an error, if any.
func (a Asset) JSONQuery() (*datatypes.JSONQueryExpression, error) {
func (a *Asset) JSONQuery() (*datatypes.JSONQueryExpression, error) {
asset, err := a.Parse()
if err != nil {
return nil, err
Expand Down Expand Up @@ -165,6 +165,7 @@ func (a Asset) JSONQuery() (*datatypes.JSONQueryExpression, error) {
case *url.URL:
return jsonQuery.Equals(v.Raw, "url"), nil
}

return nil, fmt.Errorf("unknown asset type: %s", a.Type)
}

Expand Down
Loading

0 comments on commit 15e42f1

Please sign in to comment.