Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement entity field in /{software/,}logs #177

Merged
merged 1 commit into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions developers-italia.oas.yaml
Expand Up @@ -1577,6 +1577,12 @@ components:
type: string
maxLength: 255
pattern: '.*'
description: >
The resource this log is about (fe. a particular Publisher / Software).
It might be absent if the log is not about a specific resource or if
the resource doesn't exist yet, like in case of the error log caused by
failure to create it.
example: /software/7589be36-f046-45c6-9223-b7de9dbf06cd
readOnly: true
required:
- id
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Expand Up @@ -12,8 +12,8 @@ require (
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/stretchr/testify v1.7.5
gorm.io/driver/postgres v1.3.7
gorm.io/driver/sqlite v1.3.4
gorm.io/gorm v1.23.6
gorm.io/driver/sqlite v1.4.3
gorm.io/gorm v1.24.1
)

require (
Expand All @@ -26,7 +26,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/gofiber/adaptor/v2 v2.1.25 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/mattn/go-sqlite3 v1.14.13 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
15 changes: 8 additions & 7 deletions go.sum
Expand Up @@ -291,9 +291,9 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I=
github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -707,14 +707,15 @@ gorm.io/driver/postgres v1.0.8/go.mod h1:4eOzrI1MUfm6ObJU/UcmbXyiHSs8jSwH95G5P5d
gorm.io/driver/postgres v1.3.7 h1:FKF6sIMDHDEvvMF/XJvbnCl0nu6KSKUaPXevJ4r+VYQ=
gorm.io/driver/postgres v1.3.7/go.mod h1:f02ympjIcgtHEGFMZvdgTxODZ9snAHDb4hXfigBVuNI=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/driver/sqlite v1.3.4 h1:NnFOPVfzi4CPsJPH4wXr6rMkPb4ElHEqKMvrsx9c9Fk=
gorm.io/driver/sqlite v1.3.4/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U=
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.7/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0=
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs=
gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
6 changes: 4 additions & 2 deletions internal/handlers/logs.go
Expand Up @@ -221,11 +221,13 @@ func (p *Log) PostSoftwareLog(ctx *fiber.Ctx) error {
return common.ErrorWithValidationErrors(fiber.StatusUnprocessableEntity, "can't create Log", "invalid format", err)
}

table := models.Software{}.TableName()

log := models.Log{
ID: utils.UUIDv4(),
Message: logReq.Message,
EntityID: software.ID,
EntityType: models.Software{}.TableName(),
EntityID: &software.ID,
EntityType: &table,
}

if err := p.db.Create(&log).Error; err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/models/models.go
Expand Up @@ -25,8 +25,9 @@ type Log struct {
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`

// Entity this Log entry is about (fe. Publisher, Software, etc.)
EntityID string `json:"-"`
EntityType string `json:"-"`
EntityID *string `json:"-"`
EntityType *string `json:"-"`
Entity string `json:"entity,omitempty" gorm:"->;type:text GENERATED ALWAYS AS (CASE WHEN entity_id IS NULL THEN NULL ELSE ('/' || entity_type || '/' || entity_id) END) STORED;default:(-);"` //nolint:lll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curioso quel simbolo dentro gorm, cosa serve?

Copy link
Member Author

@bfabio bfabio Nov 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LorenzoS92 it specifies permissions for that field, in that case we set it read only since it's a automatically generated field.

}

type Publisher struct {
Expand Down
24 changes: 20 additions & 4 deletions main_test.go
Expand Up @@ -2101,6 +2101,8 @@ func TestSoftwareEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, log["updatedAt"].(string))
assert.Nil(t, err)

assert.Equal(t, "/software/c353756e-8597-4e46-a99b-7da2e141603b", log["entity"])

for key := range log {
assert.Contains(t, []string{"id", "createdAt", "updatedAt", "message", "entity"}, key)
}
Expand All @@ -2112,8 +2114,6 @@ func TestSoftwareEndpoints(t *testing.T) {

prevCreatedAt = &createdAt
}

// TODO assert.NotEmpty(t, firstLog["entity"])
},
},
{
Expand Down Expand Up @@ -2185,6 +2185,8 @@ func TestSoftwareEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, response["updatedAt"].(string))
assert.Nil(t, err)

assert.Equal(t, "/software/c353756e-8597-4e46-a99b-7da2e141603b", response["entity"])

// TODO: check the record was actually created in the database
},
},
Expand Down Expand Up @@ -2533,13 +2535,25 @@ func TestLogsEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, log["updatedAt"].(string))
assert.Nil(t, err)

// Only certain logs from the fixtures have an associated entity.
//
// FIXME: This is ugly, see the issue about improving tests:
// https://github.com/italia/developers-italia-api/issues/91
if log["id"] == "2dfb2bc2-042d-11ed-9338-d8bbc146d165" ||
log["id"] == "12f30d9e-042e-11ed-8ddc-d8bbc146d165" ||
log["id"] == "18a70362-042e-11ed-b793-d8bbc146d165" {
assert.Equal(t, "/software/c353756e-8597-4e46-a99b-7da2e141603b", log["entity"])
} else if log["id"] == "53650508-042e-11ed-9b84-d8bbc146d165" {
assert.Equal(t, "/publishers/2ded32eb-c45e-4167-9166-a44e18b8adde", log["entity"])
} else {
assert.Nil(t, log["entity"])
}

var prevCreatedAt *time.Time = nil
for key := range log {
assert.Contains(t, []string{"id", "createdAt", "updatedAt", "message", "entity"}, key)
}

// TODO assert.NotEmpty(t, firstLog["entity"])

// Check the logs are ordered by descending createdAt
if prevCreatedAt != nil {
assert.GreaterOrEqual(t, *prevCreatedAt, createdAt)
Expand Down Expand Up @@ -2708,6 +2722,8 @@ func TestLogsEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, response["updatedAt"].(string))
assert.Nil(t, err)

assert.Nil(t, response["entity"])

// TODO: check the record was actually created in the database
},
},
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/fixtures/logs.yml
Expand Up @@ -27,6 +27,8 @@
- id: 53650508-042e-11ed-9b84-d8bbc146d165
created_at: 2010-02-15 23:59:59
updated_at: 2010-12-31 23:59:59
entity_id: 2ded32eb-c45e-4167-9166-a44e18b8adde
entity_type: publishers
message: A log message

- id: 55438aac-042e-11ed-848a-d8bbc146d165
Expand Down