Skip to content

Commit

Permalink
db: Add tests for PublishPage
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Apr 9, 2024
1 parent 3c0aa68 commit 49f3b3d
Showing 1 changed file with 162 additions and 0 deletions.
162 changes: 162 additions & 0 deletions internal/db/page_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package db_test

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"time"

"github.com/carlmjohnson/be"
"github.com/carlmjohnson/be/testfile"
"github.com/jackc/pgx/v5/pgtype"
"github.com/spotlightpa/almanack/internal/db"
"github.com/spotlightpa/almanack/internal/github"
"github.com/spotlightpa/almanack/internal/index"
"github.com/spotlightpa/almanack/pkg/almanack"
"github.com/spotlightpa/almanack/pkg/almlog"
)

func TestToFromTOML(t *testing.T) {
Expand Down Expand Up @@ -262,3 +270,157 @@ func TestShouldPublishShouldNotify(t *testing.T) {
})
}
}

func TestServicePublish(t *testing.T) {
ctx := context.Background()
almlog.UseTestLogger(t)

p := createTestDB(t)
tmp := t.TempDir()
svc := almanack.Services{
Queries: db.New(p),
Tx: db.NewTxable(p),
ContentStore: github.NewMockClient(tmp),
Indexer: index.MockIndexer{},
}

// Success case
{
const path1 = "content/news/1.md"

be.NilErr(t, svc.Queries.CreatePage(ctx, db.CreatePageParams{
FilePath: path1,
SourceType: "manual",
SourceID: "n/a",
}))

p, err := svc.Queries.GetPageByFilePath(ctx, path1)
be.NilErr(t, err)
be.False(t, p.LastPublished.Valid)

_, err = os.Stat(filepath.Join(tmp, path1))
be.Nonzero(t, err)

p1 := &db.Page{
ID: 1,
FilePath: path1,
Frontmatter: map[string]any{},
Body: "hello",
ScheduleFor: pgtype.Timestamptz{},
LastPublished: pgtype.Timestamptz{},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
URLPath: pgtype.Text{
String: "/hello", Valid: true,
},
SourceType: "",
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
err, warning := svc.PublishPage(ctx, p1)
be.NilErr(t, err)
be.NilErr(t, warning)

p, err = svc.Queries.GetPageByFilePath(ctx, path1)
be.NilErr(t, err)
be.True(t, p.LastPublished.Valid)
}
{
const path2 = "content/news/2.md"

be.NilErr(t, svc.Queries.CreatePage(ctx, db.CreatePageParams{
FilePath: path2,
SourceType: "manual",
SourceID: "n/a",
}))

_, err := os.Stat(filepath.Join(tmp, path2))
be.Nonzero(t, err)

// Can't create another page with the same URLPath
p2 := &db.Page{
ID: 1,
FilePath: path2,
Frontmatter: map[string]any{},
Body: "hello",
ScheduleFor: pgtype.Timestamptz{},
LastPublished: pgtype.Timestamptz{},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
URLPath: pgtype.Text{
String: "/hello", Valid: true,
},
SourceType: "",
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
err, warning := svc.PublishPage(ctx, p2)
be.Nonzero(t, err)
be.NilErr(t, warning)
_, err = os.Stat(filepath.Join(tmp, path2))
be.Nonzero(t, err)

// Can create if the URL changes
p3 := &db.Page{
ID: 1,
FilePath: path2,
Frontmatter: map[string]any{},
Body: "hello",
ScheduleFor: pgtype.Timestamptz{},
LastPublished: pgtype.Timestamptz{},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
URLPath: pgtype.Text{
String: "/hello2", Valid: true,
},
SourceType: "",
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
err, warning = svc.PublishPage(ctx, p3)
be.NilErr(t, err)
be.NilErr(t, warning)
_, err = os.Stat(filepath.Join(tmp, path2))
be.NilErr(t, err)
}
// Test Github failure
{
const path3 = "content/news/3.md"
be.NilErr(t, svc.Queries.CreatePage(ctx, db.CreatePageParams{
FilePath: path3,
SourceType: "manual",
SourceID: "n/a",
}))

_, err := os.Stat(filepath.Join(tmp, path3))
be.Nonzero(t, err)

p4 := &db.Page{
ID: 1,
FilePath: path3,
Frontmatter: map[string]any{},
Body: "hello",
ScheduleFor: pgtype.Timestamptz{},
LastPublished: pgtype.Timestamptz{},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
URLPath: pgtype.Text{
String: "/hello3", Valid: true,
},
SourceType: "",
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
// Github returns an error
svc.ContentStore = github.ErrorClient{
Error: errors.New("bad client"),
}
err, warning := svc.PublishPage(ctx, p4)
be.Nonzero(t, err)
be.NilErr(t, warning)

p, err := svc.Queries.GetPageByFilePath(ctx, path3)
be.NilErr(t, err)
be.False(t, p.LastPublished.Valid)
}
}

0 comments on commit 49f3b3d

Please sign in to comment.