From bbd3a001026cce45f4aa1b05d42fee58dfc592e5 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Sat, 10 Aug 2024 09:26:30 -0400 Subject: [PATCH] tests(db): additional document tests --- README.md | 2 +- database/db.go | 2 +- database/documents_test.go | 114 +++++++++++++++++++++++++++++++++++++ database/manager_test.go | 53 +++++------------ database/models.go | 4 +- database/query.sql.go | 2 +- metadata/metadata.go | 4 +- 7 files changed, 134 insertions(+), 47 deletions(-) create mode 100644 database/documents_test.go diff --git a/README.md b/README.md index 62fa93d..0a981ce 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ See documentation in the `client` subfolder: [SyncNinja](https://gitea.va.reicha ## Development -SQLC Generation (v1.21.0): +SQLC Generation (v1.26.0): ```bash go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest diff --git a/database/db.go b/database/db.go index 61f5bf4..a3cc795 100644 --- a/database/db.go +++ b/database/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.25.0 +// sqlc v1.26.0 package database diff --git a/database/documents_test.go b/database/documents_test.go new file mode 100644 index 0000000..a688448 --- /dev/null +++ b/database/documents_test.go @@ -0,0 +1,114 @@ +package database + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/suite" + + "reichard.io/antholume/config" +) + +type DocumentsTestSuite struct { + suite.Suite + dbm *DBManager +} + +func TestDocuments(t *testing.T) { + suite.Run(t, new(DocumentsTestSuite)) +} + +func (suite *DocumentsTestSuite) SetupTest() { + cfg := config.Config{ + DBType: "memory", + } + + suite.dbm = NewMgr(&cfg) + + // Create Document + _, err := suite.dbm.Queries.UpsertDocument(suite.dbm.Ctx, UpsertDocumentParams{ + ID: documentID, + Title: &documentTitle, + Author: &documentAuthor, + Words: &documentWords, + }) + suite.NoError(err) +} + +// DOCUMENT - TODO: +// - 󰊕 (q *Queries) GetDocumentProgress +// - 󰊕 (q *Queries) GetDocumentWithStats +// - 󰊕 (q *Queries) GetDocumentsSize +// - 󰊕 (q *Queries) GetDocumentsWithStats +// - 󰊕 (q *Queries) GetMissingDocuments +func (suite *DocumentsTestSuite) TestGetDocument() { + doc, err := suite.dbm.Queries.GetDocument(suite.dbm.Ctx, documentID) + suite.Nil(err, "should have nil err") + suite.Equal(documentID, doc.ID, "should have changed the document") +} + +func (suite *DocumentsTestSuite) TestUpsertDocument() { + testDocID := "docid1" + + doc, err := suite.dbm.Queries.UpsertDocument(suite.dbm.Ctx, UpsertDocumentParams{ + ID: testDocID, + Title: &documentTitle, + Author: &documentAuthor, + }) + + suite.Nil(err, "should have nil err") + suite.Equal(testDocID, doc.ID, "should have document id") + suite.Equal(documentTitle, *doc.Title, "should have document title") + suite.Equal(documentAuthor, *doc.Author, "should have document author") +} + +func (suite *DocumentsTestSuite) TestDeleteDocument() { + changed, err := suite.dbm.Queries.DeleteDocument(suite.dbm.Ctx, documentID) + suite.Nil(err, "should have nil err") + suite.Equal(int64(1), changed, "should have changed the document") + + doc, err := suite.dbm.Queries.GetDocument(suite.dbm.Ctx, documentID) + suite.Nil(err, "should have nil err") + suite.True(doc.Deleted, "should have deleted the document") +} + +func (suite *DocumentsTestSuite) TestGetDeletedDocuments() { + changed, err := suite.dbm.Queries.DeleteDocument(suite.dbm.Ctx, documentID) + suite.Nil(err, "should have nil err") + suite.Equal(int64(1), changed, "should have changed the document") + + deletedDocs, err := suite.dbm.Queries.GetDeletedDocuments(suite.dbm.Ctx, []string{documentID}) + suite.Nil(err, "should have nil err") + suite.Len(deletedDocs, 1, "should have one deleted document") +} + +// TODO - Convert GetWantedDocuments -> (sqlc.slice('document_ids')); +func (suite *DocumentsTestSuite) TestGetWantedDocuments() { + wantedDocs, err := suite.dbm.Queries.GetWantedDocuments(suite.dbm.Ctx, fmt.Sprintf("[\"%s\"]", documentID)) + suite.Nil(err, "should have nil err") + suite.Len(wantedDocs, 1, "should have one wanted document") +} + +func (suite *DocumentsTestSuite) TestGetMissingDocuments() { + // Create Document + _, err := suite.dbm.Queries.UpsertDocument(suite.dbm.Ctx, UpsertDocumentParams{ + ID: documentID, + Filepath: &documentFilepath, + }) + suite.NoError(err) + + missingDocs, err := suite.dbm.Queries.GetMissingDocuments(suite.dbm.Ctx, []string{documentID}) + suite.Nil(err, "should have nil err") + suite.Len(missingDocs, 0, "should have no wanted document") + + missingDocs, err = suite.dbm.Queries.GetMissingDocuments(suite.dbm.Ctx, []string{"other"}) + suite.Nil(err, "should have nil err") + suite.Len(missingDocs, 1, "should have one missing document") + suite.Equal(documentID, missingDocs[0].ID, "should have missing doc") + + // TODO - https://github.com/sqlc-dev/sqlc/issues/3451 + // missingDocs, err = suite.dbm.Queries.GetMissingDocuments(suite.dbm.Ctx, []string{}) + // suite.Nil(err, "should have nil err") + // suite.Len(missingDocs, 1, "should have one missing document") + // suite.Equal(documentID, missingDocs[0].ID, "should have missing doc") +} diff --git a/database/manager_test.go b/database/manager_test.go index c55444b..8878360 100644 --- a/database/manager_test.go +++ b/database/manager_test.go @@ -12,14 +12,15 @@ import ( ) var ( - userID string = "testUser" - userPass string = "testPass" - deviceID string = "testDevice" - deviceName string = "testDeviceName" - documentID string = "testDocument" - documentTitle string = "testTitle" - documentAuthor string = "testAuthor" - documentWords int64 = 5000 + userID string = "testUser" + userPass string = "testPass" + deviceID string = "testDevice" + deviceName string = "testDeviceName" + documentID string = "testDocument" + documentTitle string = "testTitle" + documentAuthor string = "testAuthor" + documentFilepath string = "./testPath.epub" + documentWords int64 = 5000 ) type DatabaseTestSuite struct { @@ -54,10 +55,11 @@ func (suite *DatabaseTestSuite) SetupTest() { // Create Document _, err = suite.dbm.Queries.UpsertDocument(suite.dbm.Ctx, UpsertDocumentParams{ - ID: documentID, - Title: &documentTitle, - Author: &documentAuthor, - Words: &documentWords, + ID: documentID, + Title: &documentTitle, + Author: &documentAuthor, + Filepath: &documentFilepath, + Words: &documentWords, }) suite.NoError(err) @@ -97,33 +99,6 @@ func (suite *DatabaseTestSuite) SetupTest() { suite.NoError(err) } -// DOCUMENT - TODO: -// - 󰊕 (q *Queries) DeleteDocument -// - 󰊕 (q *Queries) GetDeletedDocuments -// - 󰊕 (q *Queries) GetDocument -// - 󰊕 (q *Queries) GetDocumentProgress -// - 󰊕 (q *Queries) GetDocumentWithStats -// - 󰊕 (q *Queries) GetDocuments -// - 󰊕 (q *Queries) GetDocumentsSize -// - 󰊕 (q *Queries) GetDocumentsWithStats -// - 󰊕 (q *Queries) GetMissingDocuments -// - 󰊕 (q *Queries) GetWantedDocuments -// - 󰊕 (q *Queries) UpsertDocument -func (suite *DatabaseTestSuite) TestDocument() { - testDocID := "docid1" - - doc, err := suite.dbm.Queries.UpsertDocument(suite.dbm.Ctx, UpsertDocumentParams{ - ID: testDocID, - Title: &documentTitle, - Author: &documentAuthor, - }) - - suite.Nil(err, "should have nil err") - suite.Equal(testDocID, doc.ID, "should have document id") - suite.Equal(documentTitle, *doc.Title, "should have document title") - suite.Equal(documentAuthor, *doc.Author, "should have document author") -} - // DEVICES - TODO: // - 󰊕 (q *Queries) GetDevice // - 󰊕 (q *Queries) GetDevices diff --git a/database/models.go b/database/models.go index 884921a..8d0cdea 100644 --- a/database/models.go +++ b/database/models.go @@ -1,11 +1,9 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.25.0 +// sqlc v1.26.0 package database -import () - type Activity struct { ID int64 `json:"id"` UserID string `json:"user_id"` diff --git a/database/query.sql.go b/database/query.sql.go index 30c667b..0423180 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.25.0 +// sqlc v1.26.0 // source: query.sql package database diff --git a/metadata/metadata.go b/metadata/metadata.go index 641b78a..f92c911 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -65,9 +65,9 @@ func SearchMetadata(s Source, metadataSearch MetadataInfo) ([]MetadataInfo, erro case SOURCE_GBOOK: return getGBooksMetadata(metadataSearch) case SOURCE_OLIB: - return nil, errors.New("Not implemented") + return nil, errors.New("not implemented") default: - return nil, errors.New("Not implemented") + return nil, errors.New("not implemented") } }