Skip to content

Commit

Permalink
improvements to AssetQuery and RelationQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Jan 15, 2024
1 parent 15e42f1 commit cd160e9
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 128 deletions.
33 changes: 22 additions & 11 deletions assetdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assetdb

import (
"embed"
"errors"
"fmt"
"math/rand"
"net/netip"
Expand Down Expand Up @@ -477,30 +478,40 @@ func TestAssetDB(t *testing.T) {

func TestAssetQuery(t *testing.T) {
// Set up the SQLite database for testing
db := newGraph("local", "test.db")
db, err := newGraph("local", "test.db")
if err != nil {
t.Errorf("%v", err)
return
}
defer teardownSqlite("test.db")

createdAssets := createAssets(db)

queriedAssets, err := db.AssetQuery("")
if err != nil {
panic(err)
t.Errorf("%v", err)
return
}
// 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")
db, err := newGraph("local", "test.db")
if err != nil {
t.Errorf("%v", err)
return
}
defer teardownSqlite("test.db")

createdAssets := createAssets(db)
createdRelations := createRelations(createdAssets, db)

queriedRelations, err := db.RelationQuery("")
if err != nil {
panic(err)
t.Errorf("%v", err)
return
}
// Verify the relations and assets are populated in the relation
// Have to do it this way because "CreatedAt" is not populated when creating relations.
Expand Down Expand Up @@ -595,7 +606,7 @@ func teardownSqlite(dsn string) {
}
}

func newGraph(system, path string) *AssetDB {
func newGraph(system, path string) (*AssetDB, error) {
var dsn string
var dbtype repository.DBType

Expand All @@ -610,12 +621,12 @@ func newGraph(system, path string) *AssetDB {
dbtype = repository.Postgres
dsn = path
default:
return nil
return nil, errors.New("newGraph: no valid database type provided")
}

store := New(dbtype, dsn)
if store == nil {
return nil
return nil, errors.New("newGraph: failed to create the new database")
}

var name string
Expand All @@ -634,7 +645,7 @@ func newGraph(system, path string) *AssetDB {

sql, err := gorm.Open(database, &gorm.Config{})
if err != nil {
return nil
return nil, err
}

migrationsSource := migrate.EmbedFileSystemMigrationSource{
Expand All @@ -644,12 +655,12 @@ func newGraph(system, path string) *AssetDB {

sqlDb, err := sql.DB()
if err != nil {
panic(err)
return nil, err
}

_, err = migrate.Exec(sqlDb, name, migrationsSource, migrate.Up)
if err != nil {
panic(err)
return nil, err
}
return store
return store, nil
}
26 changes: 13 additions & 13 deletions repository/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ import (

// Asset represents an asset stored in the database.
type Asset struct {
ID int64 `gorm:"primaryKey;autoIncrement:true"` // The unique identifier of the asset.
ID uint64 `gorm:"primaryKey;autoIncrement:true"` // The unique identifier of the asset.
CreatedAt time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP();column=created_at"` // The creation timestamp of the asset.
LastSeen time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP();column=last_seen"` // The last seen timestamp of the asset.
Type string // The type of the asset.
Content datatypes.JSON // The JSON-encoded content of the asset.
}

// Relation represents a relationship between two assets stored in the database.
type Relation struct {
ID uint64 `gorm:"primaryKey;autoIncrement:true"` // The unique identifier of the relation.
CreatedAt time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP();"` // The creation timestamp of the relation.
LastSeen time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP();"` // The last seen timestamp of the relation.
Type string // The type of the relation.
FromAssetID uint64 // The ID of the asset from which the relation originates.
ToAssetID uint64 // The ID of the asset to which the relation points.
FromAsset Asset // The asset from which the relation originates.
ToAsset Asset // The asset to which the relation points.
}

// 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) {
Expand Down Expand Up @@ -168,15 +180,3 @@ func (a *Asset) JSONQuery() (*datatypes.JSONQueryExpression, error) {

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

// Relation represents a relationship between two assets stored in the database.
type Relation struct {
ID int64 `gorm:"primaryKey;autoIncrement:true"` // The unique identifier of the relation.
CreatedAt time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP();"` // The creation timestamp of the relation.
LastSeen time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP();"` // The last seen timestamp of the relation.
Type string // The type of the relation.
FromAssetID int64 // The ID of the asset from which the relation originates.
ToAssetID int64 // The ID of the asset to which the relation points.
FromAsset Asset // The asset from which the relation originates.
ToAsset Asset // The asset to which the relation points.
}
Loading

0 comments on commit cd160e9

Please sign in to comment.