From 4e041ee0fd9fb6e414f89d79874625ec2289ddf8 Mon Sep 17 00:00:00 2001 From: Emily Lu Date: Sat, 10 Jun 2023 23:59:12 +1000 Subject: [PATCH 1/5] Updated stored procs to create metadata --- postgres/up/05-create_filesystem_table.sql | 10 ++++++---- postgres/up/06-create_dummy_data.sql | 7 +++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/postgres/up/05-create_filesystem_table.sql b/postgres/up/05-create_filesystem_table.sql index 68ced09a1..d54359f4a 100644 --- a/postgres/up/05-create_filesystem_table.sql +++ b/postgres/up/05-create_filesystem_table.sql @@ -21,7 +21,7 @@ CREATE TABLE filesystem ( CreatedAt TIMESTAMP NOT NULL DEFAULT NOW(), /* MetaData */ - -- MetadataID uuid NOT NULL, + MetadataID uuid NOT NULL, OwnedBy INT, /* Pain */ @@ -31,7 +31,7 @@ CREATE TABLE filesystem ( CONSTRAINT fk_owner FOREIGN KEY (OwnedBy) REFERENCES groups(UID), - -- CONSTRAINT fk_meta FOREIGN KEY (MetadataID) REFERENCES metadata(MetadataID), + CONSTRAINT fk_meta FOREIGN KEY (MetadataID) REFERENCES metadata(MetadataID), /* Unique name constraint: there should not exist an entity of the same type with the same parent and logical name. */ @@ -46,14 +46,16 @@ AS $$ DECLARE newEntityID filesystem.EntityID%type; parentIsDocument BOOLEAN := (SELECT IsDocument FROM filesystem WHERE EntityID = parentP LIMIT 1); + newMetadataID filesystem.MetadataID%type; BEGIN IF parentIsDocument THEN /* We shouldnt be delcaring that a document is our parent */ RAISE EXCEPTION SQLSTATE '90001' USING MESSAGE = 'cannot make parent a document'; END If; + INSERT INTO metadata DEFAULT VALUES RETURNING MetadataID INTO newMetadataID; WITH newEntity AS ( - INSERT INTO filesystem (LogicalName, IsDocument, OwnedBy, Parent) - VALUES (logicalNameP, isDocumentP, ownedByP, parentP) + INSERT INTO filesystem (LogicalName, IsDocument, MetadataID, OwnedBy, Parent) + VALUES (logicalNameP, isDocumentP, newMetadataID, ownedByP, parentP) RETURNING EntityID ) diff --git a/postgres/up/06-create_dummy_data.sql b/postgres/up/06-create_dummy_data.sql index ecc438997..60f127119 100644 --- a/postgres/up/06-create_dummy_data.sql +++ b/postgres/up/06-create_dummy_data.sql @@ -10,11 +10,14 @@ DO $$ DECLARE randomGroup groups.UID%type; rootID filesystem.EntityID%type; + newMetadataID filesystem.MetadataID%type; BEGIN SELECT groups.UID INTO randomGroup FROM groups WHERE Name = 'admin'::VARCHAR; + /* Create metadata */ + INSERT INTO metadata DEFAULT VALUES RETURNING MetadataID INTO newMetadataID; /* Insert the root directory */ - INSERT INTO filesystem (EntityID, LogicalName, OwnedBy) - VALUES (uuid_nil(), 'root', randomGroup); + INSERT INTO filesystem (EntityID, LogicalName, MetadataID, OwnedBy) + VALUES (uuid_nil(), 'root', newMetadataID, randomGroup); SELECT filesystem.EntityID INTO rootID FROM filesystem WHERE LogicalName = 'root'::VARCHAR; /* Set parent to uuid_nil() because postgres driver has issue supporting NULL values */ UPDATE filesystem SET Parent = uuid_nil() WHERE EntityID = rootID; From 94a293818e35b0b534efb9169c72955820276390 Mon Sep 17 00:00:00 2001 From: Emily Lu Date: Sat, 10 Jun 2023 23:59:35 +1000 Subject: [PATCH 2/5] update repo layer to expose metadata --- backend/database/repositories/filesystem.go | 22 ++++++++++++++++++- .../repositories/repository_interfaces.go | 8 +++++++ .../repositories/tests/filesystem_test.go | 22 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/backend/database/repositories/filesystem.go b/backend/database/repositories/filesystem.go index fa3178ff2..46bfcc950 100644 --- a/backend/database/repositories/filesystem.go +++ b/backend/database/repositories/filesystem.go @@ -24,7 +24,7 @@ func (rep filesystemRepository) query(query string, input ...interface{}) (Files err := rep.ctx.Query(query, input, &entity.EntityID, &entity.LogicalName, &entity.IsDocument, &entity.IsPublished, - &entity.CreatedAt, &entity.OwnerUserId, &entity.ParentFileID) + &entity.CreatedAt, &entity.MetadataID, &entity.OwnerUserId, &entity.ParentFileID) if err != nil { return FilesystemEntry{}, err } @@ -112,6 +112,26 @@ func (rep filesystemRepository) GetIDWithPath(path string) (uuid.UUID, error) { return parent.EntityID, err } +func (rep filesystemRepository) GetMetadataFromID(ID uuid.UUID) (MetadataEntry, error) { + // Get entry from ID + metadataId, err := rep.GetEntryWithID(ID) + if err != nil { + return MetadataEntry{}, err + } + + // Get metadata + entity := MetadataEntry{} + + err = rep.ctx.Query("SELECT * FROM metadata WHERE MetadataID = $1", + []interface{}{metadataId}, + &entity.MetadataID, &entity.CreatedAt) + if err != nil { + return MetadataEntry{}, err + } + return entity, nil + +} + func (rep filesystemRepository) DeleteEntryWithID(ID uuid.UUID) error { return rep.ctx.Exec("SELECT delete_entity($1)", []interface{}{ID}) } diff --git a/backend/database/repositories/repository_interfaces.go b/backend/database/repositories/repository_interfaces.go index a65c1f5f5..2a2d51298 100644 --- a/backend/database/repositories/repository_interfaces.go +++ b/backend/database/repositories/repository_interfaces.go @@ -19,11 +19,18 @@ type FilesystemEntry struct { IsPublished bool CreatedAt time.Time + MetadataID uuid.UUID + OwnerUserId int ParentFileID uuid.UUID ChildrenIDs []uuid.UUID } +type MetadataEntry struct { + MetadataID uuid.UUID + CreatedAt time.Time +} + type ( // Repository interface that all valid filesystem repositories // mocked/real should implement @@ -32,6 +39,7 @@ type ( GetRoot() (FilesystemEntry, error) GetEntryWithParentID(ID uuid.UUID) (FilesystemEntry, error) GetIDWithPath(path string) (uuid.UUID, error) + GetMetadataFromID(ID uuid.UUID) (MetadataEntry, error) CreateEntry(file FilesystemEntry) (FilesystemEntry, error) DeleteEntryWithID(ID uuid.UUID) error diff --git a/backend/database/repositories/tests/filesystem_test.go b/backend/database/repositories/tests/filesystem_test.go index 67e193037..397c77a23 100644 --- a/backend/database/repositories/tests/filesystem_test.go +++ b/backend/database/repositories/tests/filesystem_test.go @@ -264,6 +264,28 @@ func TestGetIDWithPath(t *testing.T) { }) } +func TestMetadataRetrieval(t *testing.T) { + assert := assert.New(t) + + testContext.RunTest(func() { + // ==== Setup ==== + newDoc, err := repo.CreateEntry(repositories.FilesystemEntry{ + LogicalName: "test_doc", ParentFileID: repositories.FilesystemRootID, + OwnerUserId: repositories.GROUPS_ADMIN, IsDocument: true, + }) + // ==== Assertions ==== + if err != nil { + log.Fatalf(err.Error()) + } + + // Query for metadata in database + if info, err := repo.GetMetadataFromID(newDoc.EntityID); assert.Nil(err) { + assert.Equal(newDoc.MetadataID, info.MetadataID) + assert.NotEmpty(info.CreatedAt) + } + }) +} + func scanArray[T any](rows pgx.Rows) []T { arr := []T{} for rows.Next() { From 826f3e3a8c372cd858d73ef51896af1db46cf058 Mon Sep 17 00:00:00 2001 From: Emily Lu Date: Tue, 18 Jul 2023 23:47:29 +1000 Subject: [PATCH 3/5] update mockgen --- .../repositories/mocks/repositories_mock.go | 485 ++++++++++++++++++ .../repositories/repository_interfaces.go | 3 +- 2 files changed, 486 insertions(+), 2 deletions(-) create mode 100644 backend/database/repositories/mocks/repositories_mock.go diff --git a/backend/database/repositories/mocks/repositories_mock.go b/backend/database/repositories/mocks/repositories_mock.go new file mode 100644 index 000000000..4ffd64b6e --- /dev/null +++ b/backend/database/repositories/mocks/repositories_mock.go @@ -0,0 +1,485 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: repository_interfaces.go + +// Package mocks is a generated GoMock package. +package mocks + +import ( + os "os" + reflect "reflect" + + contexts "cms.csesoc.unsw.edu.au/database/contexts" + repositories "cms.csesoc.unsw.edu.au/database/repositories" + gomock "github.com/golang/mock/gomock" + uuid "github.com/google/uuid" +) + +// MockFilesystemRepository is a mock of FilesystemRepository interface. +type MockFilesystemRepository struct { + ctrl *gomock.Controller + recorder *MockFilesystemRepositoryMockRecorder +} + +// MockFilesystemRepositoryMockRecorder is the mock recorder for MockFilesystemRepository. +type MockFilesystemRepositoryMockRecorder struct { + mock *MockFilesystemRepository +} + +// NewMockFilesystemRepository creates a new mock instance. +func NewMockFilesystemRepository(ctrl *gomock.Controller) *MockFilesystemRepository { + mock := &MockFilesystemRepository{ctrl: ctrl} + mock.recorder = &MockFilesystemRepositoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockFilesystemRepository) EXPECT() *MockFilesystemRepositoryMockRecorder { + return m.recorder +} + +// CreateEntry mocks base method. +func (m *MockFilesystemRepository) CreateEntry(file repositories.FilesystemEntry) (repositories.FilesystemEntry, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateEntry", file) + ret0, _ := ret[0].(repositories.FilesystemEntry) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateEntry indicates an expected call of CreateEntry. +func (mr *MockFilesystemRepositoryMockRecorder) CreateEntry(file interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateEntry", reflect.TypeOf((*MockFilesystemRepository)(nil).CreateEntry), file) +} + +// DeleteEntryWithID mocks base method. +func (m *MockFilesystemRepository) DeleteEntryWithID(ID uuid.UUID) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteEntryWithID", ID) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteEntryWithID indicates an expected call of DeleteEntryWithID. +func (mr *MockFilesystemRepositoryMockRecorder) DeleteEntryWithID(ID interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteEntryWithID", reflect.TypeOf((*MockFilesystemRepository)(nil).DeleteEntryWithID), ID) +} + +// GetContext mocks base method. +func (m *MockFilesystemRepository) GetContext() contexts.DatabaseContext { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetContext") + ret0, _ := ret[0].(contexts.DatabaseContext) + return ret0 +} + +// GetContext indicates an expected call of GetContext. +func (mr *MockFilesystemRepositoryMockRecorder) GetContext() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetContext", reflect.TypeOf((*MockFilesystemRepository)(nil).GetContext)) +} + +// GetEntryWithID mocks base method. +func (m *MockFilesystemRepository) GetEntryWithID(ID uuid.UUID) (repositories.FilesystemEntry, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetEntryWithID", ID) + ret0, _ := ret[0].(repositories.FilesystemEntry) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetEntryWithID indicates an expected call of GetEntryWithID. +func (mr *MockFilesystemRepositoryMockRecorder) GetEntryWithID(ID interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetEntryWithID", reflect.TypeOf((*MockFilesystemRepository)(nil).GetEntryWithID), ID) +} + +// GetEntryWithParentID mocks base method. +func (m *MockFilesystemRepository) GetEntryWithParentID(ID uuid.UUID) (repositories.FilesystemEntry, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetEntryWithParentID", ID) + ret0, _ := ret[0].(repositories.FilesystemEntry) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetEntryWithParentID indicates an expected call of GetEntryWithParentID. +func (mr *MockFilesystemRepositoryMockRecorder) GetEntryWithParentID(ID interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetEntryWithParentID", reflect.TypeOf((*MockFilesystemRepository)(nil).GetEntryWithParentID), ID) +} + +// GetIDWithPath mocks base method. +func (m *MockFilesystemRepository) GetIDWithPath(path string) (uuid.UUID, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetIDWithPath", path) + ret0, _ := ret[0].(uuid.UUID) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetIDWithPath indicates an expected call of GetIDWithPath. +func (mr *MockFilesystemRepositoryMockRecorder) GetIDWithPath(path interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetIDWithPath", reflect.TypeOf((*MockFilesystemRepository)(nil).GetIDWithPath), path) +} + +// GetMetadataFromID mocks base method. +func (m *MockFilesystemRepository) GetMetadataFromID(ID uuid.UUID) (repositories.MetadataEntry, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetMetadataFromID", ID) + ret0, _ := ret[0].(repositories.MetadataEntry) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetMetadataFromID indicates an expected call of GetMetadataFromID. +func (mr *MockFilesystemRepositoryMockRecorder) GetMetadataFromID(ID interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMetadataFromID", reflect.TypeOf((*MockFilesystemRepository)(nil).GetMetadataFromID), ID) +} + +// GetRoot mocks base method. +func (m *MockFilesystemRepository) GetRoot() (repositories.FilesystemEntry, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRoot") + ret0, _ := ret[0].(repositories.FilesystemEntry) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRoot indicates an expected call of GetRoot. +func (mr *MockFilesystemRepositoryMockRecorder) GetRoot() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRoot", reflect.TypeOf((*MockFilesystemRepository)(nil).GetRoot)) +} + +// RenameEntity mocks base method. +func (m *MockFilesystemRepository) RenameEntity(ID uuid.UUID, name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RenameEntity", ID, name) + ret0, _ := ret[0].(error) + return ret0 +} + +// RenameEntity indicates an expected call of RenameEntity. +func (mr *MockFilesystemRepositoryMockRecorder) RenameEntity(ID, name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RenameEntity", reflect.TypeOf((*MockFilesystemRepository)(nil).RenameEntity), ID, name) +} + +// MockUnpublishedVolumeRepository is a mock of UnpublishedVolumeRepository interface. +type MockUnpublishedVolumeRepository struct { + ctrl *gomock.Controller + recorder *MockUnpublishedVolumeRepositoryMockRecorder +} + +// MockUnpublishedVolumeRepositoryMockRecorder is the mock recorder for MockUnpublishedVolumeRepository. +type MockUnpublishedVolumeRepositoryMockRecorder struct { + mock *MockUnpublishedVolumeRepository +} + +// NewMockUnpublishedVolumeRepository creates a new mock instance. +func NewMockUnpublishedVolumeRepository(ctrl *gomock.Controller) *MockUnpublishedVolumeRepository { + mock := &MockUnpublishedVolumeRepository{ctrl: ctrl} + mock.recorder = &MockUnpublishedVolumeRepositoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUnpublishedVolumeRepository) EXPECT() *MockUnpublishedVolumeRepositoryMockRecorder { + return m.recorder +} + +// AddToVolume mocks base method. +func (m *MockUnpublishedVolumeRepository) AddToVolume(filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddToVolume", filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddToVolume indicates an expected call of AddToVolume. +func (mr *MockUnpublishedVolumeRepositoryMockRecorder) AddToVolume(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddToVolume", reflect.TypeOf((*MockUnpublishedVolumeRepository)(nil).AddToVolume), filename) +} + +// CopyToVolume mocks base method. +func (m *MockUnpublishedVolumeRepository) CopyToVolume(src *os.File, filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CopyToVolume", src, filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// CopyToVolume indicates an expected call of CopyToVolume. +func (mr *MockUnpublishedVolumeRepositoryMockRecorder) CopyToVolume(src, filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CopyToVolume", reflect.TypeOf((*MockUnpublishedVolumeRepository)(nil).CopyToVolume), src, filename) +} + +// DeleteFromVolume mocks base method. +func (m *MockUnpublishedVolumeRepository) DeleteFromVolume(filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteFromVolume", filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteFromVolume indicates an expected call of DeleteFromVolume. +func (mr *MockUnpublishedVolumeRepositoryMockRecorder) DeleteFromVolume(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteFromVolume", reflect.TypeOf((*MockUnpublishedVolumeRepository)(nil).DeleteFromVolume), filename) +} + +// GetFromVolume mocks base method. +func (m *MockUnpublishedVolumeRepository) GetFromVolume(filename string) (*os.File, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFromVolume", filename) + ret0, _ := ret[0].(*os.File) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetFromVolume indicates an expected call of GetFromVolume. +func (mr *MockUnpublishedVolumeRepositoryMockRecorder) GetFromVolume(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFromVolume", reflect.TypeOf((*MockUnpublishedVolumeRepository)(nil).GetFromVolume), filename) +} + +// GetFromVolumeTruncated mocks base method. +func (m *MockUnpublishedVolumeRepository) GetFromVolumeTruncated(filename string) (*os.File, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFromVolumeTruncated", filename) + ret0, _ := ret[0].(*os.File) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetFromVolumeTruncated indicates an expected call of GetFromVolumeTruncated. +func (mr *MockUnpublishedVolumeRepositoryMockRecorder) GetFromVolumeTruncated(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFromVolumeTruncated", reflect.TypeOf((*MockUnpublishedVolumeRepository)(nil).GetFromVolumeTruncated), filename) +} + +// MockPublishedVolumeRepository is a mock of PublishedVolumeRepository interface. +type MockPublishedVolumeRepository struct { + ctrl *gomock.Controller + recorder *MockPublishedVolumeRepositoryMockRecorder +} + +// MockPublishedVolumeRepositoryMockRecorder is the mock recorder for MockPublishedVolumeRepository. +type MockPublishedVolumeRepositoryMockRecorder struct { + mock *MockPublishedVolumeRepository +} + +// NewMockPublishedVolumeRepository creates a new mock instance. +func NewMockPublishedVolumeRepository(ctrl *gomock.Controller) *MockPublishedVolumeRepository { + mock := &MockPublishedVolumeRepository{ctrl: ctrl} + mock.recorder = &MockPublishedVolumeRepositoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockPublishedVolumeRepository) EXPECT() *MockPublishedVolumeRepositoryMockRecorder { + return m.recorder +} + +// AddToVolume mocks base method. +func (m *MockPublishedVolumeRepository) AddToVolume(filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddToVolume", filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddToVolume indicates an expected call of AddToVolume. +func (mr *MockPublishedVolumeRepositoryMockRecorder) AddToVolume(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddToVolume", reflect.TypeOf((*MockPublishedVolumeRepository)(nil).AddToVolume), filename) +} + +// CopyToVolume mocks base method. +func (m *MockPublishedVolumeRepository) CopyToVolume(src *os.File, filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CopyToVolume", src, filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// CopyToVolume indicates an expected call of CopyToVolume. +func (mr *MockPublishedVolumeRepositoryMockRecorder) CopyToVolume(src, filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CopyToVolume", reflect.TypeOf((*MockPublishedVolumeRepository)(nil).CopyToVolume), src, filename) +} + +// DeleteFromVolume mocks base method. +func (m *MockPublishedVolumeRepository) DeleteFromVolume(filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteFromVolume", filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteFromVolume indicates an expected call of DeleteFromVolume. +func (mr *MockPublishedVolumeRepositoryMockRecorder) DeleteFromVolume(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteFromVolume", reflect.TypeOf((*MockPublishedVolumeRepository)(nil).DeleteFromVolume), filename) +} + +// GetFromVolume mocks base method. +func (m *MockPublishedVolumeRepository) GetFromVolume(filename string) (*os.File, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFromVolume", filename) + ret0, _ := ret[0].(*os.File) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetFromVolume indicates an expected call of GetFromVolume. +func (mr *MockPublishedVolumeRepositoryMockRecorder) GetFromVolume(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFromVolume", reflect.TypeOf((*MockPublishedVolumeRepository)(nil).GetFromVolume), filename) +} + +// GetFromVolumeTruncated mocks base method. +func (m *MockPublishedVolumeRepository) GetFromVolumeTruncated(filename string) (*os.File, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFromVolumeTruncated", filename) + ret0, _ := ret[0].(*os.File) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetFromVolumeTruncated indicates an expected call of GetFromVolumeTruncated. +func (mr *MockPublishedVolumeRepositoryMockRecorder) GetFromVolumeTruncated(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFromVolumeTruncated", reflect.TypeOf((*MockPublishedVolumeRepository)(nil).GetFromVolumeTruncated), filename) +} + +// MockPersonRepository is a mock of PersonRepository interface. +type MockPersonRepository struct { + ctrl *gomock.Controller + recorder *MockPersonRepositoryMockRecorder +} + +// MockPersonRepositoryMockRecorder is the mock recorder for MockPersonRepository. +type MockPersonRepositoryMockRecorder struct { + mock *MockPersonRepository +} + +// NewMockPersonRepository creates a new mock instance. +func NewMockPersonRepository(ctrl *gomock.Controller) *MockPersonRepository { + mock := &MockPersonRepository{ctrl: ctrl} + mock.recorder = &MockPersonRepositoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockPersonRepository) EXPECT() *MockPersonRepositoryMockRecorder { + return m.recorder +} + +// GetPersonWithDetails mocks base method. +func (m *MockPersonRepository) GetPersonWithDetails(arg0 repositories.Person) repositories.Person { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPersonWithDetails", arg0) + ret0, _ := ret[0].(repositories.Person) + return ret0 +} + +// GetPersonWithDetails indicates an expected call of GetPersonWithDetails. +func (mr *MockPersonRepositoryMockRecorder) GetPersonWithDetails(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPersonWithDetails", reflect.TypeOf((*MockPersonRepository)(nil).GetPersonWithDetails), arg0) +} + +// PersonExists mocks base method. +func (m *MockPersonRepository) PersonExists(arg0 repositories.Person) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PersonExists", arg0) + ret0, _ := ret[0].(bool) + return ret0 +} + +// PersonExists indicates an expected call of PersonExists. +func (mr *MockPersonRepositoryMockRecorder) PersonExists(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PersonExists", reflect.TypeOf((*MockPersonRepository)(nil).PersonExists), arg0) +} + +// MockGroupsRepository is a mock of GroupsRepository interface. +type MockGroupsRepository struct { + ctrl *gomock.Controller + recorder *MockGroupsRepositoryMockRecorder +} + +// MockGroupsRepositoryMockRecorder is the mock recorder for MockGroupsRepository. +type MockGroupsRepositoryMockRecorder struct { + mock *MockGroupsRepository +} + +// NewMockGroupsRepository creates a new mock instance. +func NewMockGroupsRepository(ctrl *gomock.Controller) *MockGroupsRepository { + mock := &MockGroupsRepository{ctrl: ctrl} + mock.recorder = &MockGroupsRepositoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockGroupsRepository) EXPECT() *MockGroupsRepositoryMockRecorder { + return m.recorder +} + +// GetGroupInfo mocks base method. +func (m *MockGroupsRepository) GetGroupInfo(arg0 repositories.Groups) repositories.Groups { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetGroupInfo", arg0) + ret0, _ := ret[0].(repositories.Groups) + return ret0 +} + +// GetGroupInfo indicates an expected call of GetGroupInfo. +func (mr *MockGroupsRepositoryMockRecorder) GetGroupInfo(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroupInfo", reflect.TypeOf((*MockGroupsRepository)(nil).GetGroupInfo), arg0) +} + +// MockFrontendsRepository is a mock of FrontendsRepository interface. +type MockFrontendsRepository struct { + ctrl *gomock.Controller + recorder *MockFrontendsRepositoryMockRecorder +} + +// MockFrontendsRepositoryMockRecorder is the mock recorder for MockFrontendsRepository. +type MockFrontendsRepositoryMockRecorder struct { + mock *MockFrontendsRepository +} + +// NewMockFrontendsRepository creates a new mock instance. +func NewMockFrontendsRepository(ctrl *gomock.Controller) *MockFrontendsRepository { + mock := &MockFrontendsRepository{ctrl: ctrl} + mock.recorder = &MockFrontendsRepositoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockFrontendsRepository) EXPECT() *MockFrontendsRepositoryMockRecorder { + return m.recorder +} + +// GetFrontendFromURL mocks base method. +func (m *MockFrontendsRepository) GetFrontendFromURL(url string) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFrontendFromURL", url) + ret0, _ := ret[0].(int) + return ret0 +} + +// GetFrontendFromURL indicates an expected call of GetFrontendFromURL. +func (mr *MockFrontendsRepositoryMockRecorder) GetFrontendFromURL(url interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFrontendFromURL", reflect.TypeOf((*MockFrontendsRepository)(nil).GetFrontendFromURL), url) +} diff --git a/backend/database/repositories/repository_interfaces.go b/backend/database/repositories/repository_interfaces.go index 2a2d51298..a67b529f6 100644 --- a/backend/database/repositories/repository_interfaces.go +++ b/backend/database/repositories/repository_interfaces.go @@ -1,7 +1,6 @@ package repositories -//go:generate mockgen -source=models.go -destination=mocks/models_mock.go -package=mocks - +//go:generate mockgen -source=repository_interfaces.go -destination=mocks/repositories_mock.go -package=mocks import ( "os" "time" From ac88c4d2df7f65b398097c365f2af1c5f3bfa461 Mon Sep 17 00:00:00 2001 From: Emily Lu Date: Sat, 23 Sep 2023 15:17:47 +1000 Subject: [PATCH 4/5] add MetadataId to new_frontend() --- postgres/up/05-create_frontend_table.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/postgres/up/05-create_frontend_table.sql b/postgres/up/05-create_frontend_table.sql index 2e0ae39d5..bf8f052d0 100644 --- a/postgres/up/05-create_frontend_table.sql +++ b/postgres/up/05-create_frontend_table.sql @@ -22,10 +22,12 @@ AS $$ DECLARE frontendIDP frontend.ID%type; frontendRoot frontend.Root%type; + newMetadataID filesystem.MetadataID%type; BEGIN -- TODO: Temporarily setting the OwnedBy field. Going to be deprecated soon. - INSERT INTO filesystem (LogicalName, IsDocument, Parent, OwnedBy) - VALUES (logicalNameP, false, uuid_nil(), 1) + INSERT INTO metadata DEFAULT VALUES RETURNING MetadataID INTO newMetadataID; + INSERT INTO filesystem (LogicalName, IsDocument, Parent, OwnedBy, MetadataID) + VALUES (logicalNameP, false, uuid_nil(), 1, newMetadataID) RETURNING entityID INTO frontendRoot; INSERT INTO frontend From e8bee5f9829dec145e8de9586c0ec15666c7f37b Mon Sep 17 00:00:00 2001 From: Emily Lu Date: Sat, 23 Sep 2023 17:56:00 +1000 Subject: [PATCH 5/5] Add test for metadata retrieval --- backend/database/repositories/filesystem.go | 4 +- .../repositories/tests/filesystem_test.go | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/backend/database/repositories/filesystem.go b/backend/database/repositories/filesystem.go index 258209f6b..24afc9db7 100644 --- a/backend/database/repositories/filesystem.go +++ b/backend/database/repositories/filesystem.go @@ -100,7 +100,7 @@ func (rep filesystemRepository) GetIDWithPath(path string) (uuid.UUID, error) { func (rep filesystemRepository) GetMetadataFromID(ID uuid.UUID) (MetadataEntry, error) { // Get entry from ID - metadataId, err := rep.GetEntryWithID(ID) + entry, err := rep.GetEntryWithID(ID) if err != nil { return MetadataEntry{}, err } @@ -109,7 +109,7 @@ func (rep filesystemRepository) GetMetadataFromID(ID uuid.UUID) (MetadataEntry, entity := MetadataEntry{} err = rep.ctx.Query("SELECT * FROM metadata WHERE MetadataID = $1", - []interface{}{metadataId}, + []interface{}{entry.MetadataID}, &entity.MetadataID, &entity.CreatedAt) if err != nil { return MetadataEntry{}, err diff --git a/backend/database/repositories/tests/filesystem_test.go b/backend/database/repositories/tests/filesystem_test.go index 1516a8c37..a588f0ecb 100644 --- a/backend/database/repositories/tests/filesystem_test.go +++ b/backend/database/repositories/tests/filesystem_test.go @@ -283,27 +283,30 @@ func TestGetIDWithPath(t *testing.T) { }) } -// func TestMetadataRetrieval(t *testing.T) { -// assert := assert.New(t) - -// testContext.RunTest(func() { -// // ==== Setup ==== -// newDoc, err := repo.CreateEntry(repositories.FilesystemEntry{ -// LogicalName: "test_doc", ParentFileID: repositories.FilesystemRootID, -// OwnerUserId: repositories.GROUPS_ADMIN, IsDocument: true, -// }) -// // ==== Assertions ==== -// if err != nil { -// log.Fatalf(err.Error()) -// } - -// // Query for metadata in database -// if info, err := repo.GetMetadataFromID(newDoc.EntityID); assert.Nil(err) { -// assert.Equal(newDoc.MetadataID, info.MetadataID) -// assert.NotEmpty(info.CreatedAt) -// } -// }) -// } +func TestMetadataRetrieval(t *testing.T) { + assert := assert.New(t) + + testContext.RunTest(func() { + // ==== Setup ==== + repo, err := repositories.NewFilesystemRepo(frontendLogicalName, frontendURL, testContext) + assert.Nil(err) + root, _ := repo.GetRoot() + newDoc, err := repo.CreateEntry(repositories.FilesystemEntry{ + LogicalName: "test_doc", ParentFileID: root.EntityID, + OwnerUserId: repositories.GROUPS_ADMIN, IsDocument: true, + }) + // ==== Assertions ==== + if err != nil { + log.Fatalf(err.Error()) + } + + // Query for metadata in database + if info, err := repo.GetMetadataFromID(newDoc.EntityID); assert.Nil(err) { + assert.Equal(newDoc.MetadataID, info.MetadataID) + assert.NotEmpty(info.CreatedAt) + } + }) +} func TestMultiApplications(t *testing.T) { assert := assert.New(t)