Permalink
Browse files

Update for resume API change.

The resume.DmitriShuralyov function needs to be provided with the
current local time dependency explicitly. It no longer uses time.Now()
implicitly to figure it out. This was needed for mocking time for test
reproducibility purposes.

Follows shurcooL/resume@abdf68e.
  • Loading branch information...
dmitshur committed Oct 7, 2018
1 parent 2bfd91d commit 9c781c585b000571fbc577aaada13b8a509b83f8
Showing with 10 additions and 5 deletions.
  1. +3 −2 internal/page/resume/render.go
  2. +5 −2 internal/page/resume/render_test.go
  3. +2 −1 resume.go
@@ -5,6 +5,7 @@ import (
"context"
"io"
"log"
"time"
homecomponent "github.com/shurcooL/home/component"
"github.com/shurcooL/htmlg"
@@ -21,7 +22,7 @@ const ReactableURL = "dmitri.shuralyov.com/resume"
// RenderBodyInnerHTML renders the inner HTML of the <body> element of the page that displays the resume.
// It's safe for concurrent use.
func RenderBodyInnerHTML(ctx context.Context, w io.Writer, reactionsService reactions.Service, notifications notifications.Service, users users.Service, authenticatedUser users.User, returnURL string) error {
func RenderBodyInnerHTML(ctx context.Context, w io.Writer, reactionsService reactions.Service, notifications notifications.Service, users users.Service, now time.Time, authenticatedUser users.User, returnURL string) error {
var nc uint64
if authenticatedUser.ID != 0 {
var err error
@@ -59,7 +60,7 @@ func RenderBodyInnerHTML(ctx context.Context, w io.Writer, reactionsService reac
}
// Render the resume contents.
resume := resume.DmitriShuralyov(shurcool, reactions, authenticatedUser)
resume := resume.DmitriShuralyov(shurcool, now, reactions, authenticatedUser)
err = htmlg.RenderComponents(w, resume)
if err != nil {
return err
@@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/shurcooL/home/internal/page/resume"
"github.com/shurcooL/notifications"
@@ -23,7 +24,7 @@ var updateFlag = flag.Bool("update", false, "Update golden files.")
// TestBodyInnerHTML validates that resume.RenderBodyInnerHTML renders the body inner HTML as expected.
func TestBodyInnerHTML(t *testing.T) {
var buf bytes.Buffer
err := resume.RenderBodyInnerHTML(context.TODO(), &buf, mockReactions{}, mockNotifications{}, mockUsers{}, alice, "/")
err := resume.RenderBodyInnerHTML(context.TODO(), &buf, mockReactions{}, mockNotifications{}, mockUsers{}, mockTime, alice, "/")
if err != nil {
t.Fatal(err)
}
@@ -63,7 +64,7 @@ func BenchmarkRenderBodyInnerHTML(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := resume.RenderBodyInnerHTML(context.Background(), ioutil.Discard, reactions, notifications, users, authenticatedUser, returnURL)
err := resume.RenderBodyInnerHTML(context.Background(), ioutil.Discard, reactions, notifications, users, mockTime, authenticatedUser, returnURL)
if err != nil {
b.Fatal(err)
}
@@ -77,6 +78,8 @@ var (
Email: "dmitri@shuralyov.com",
}
mockTime = time.Date(2018, time.August, 26, 9, 41, 0, 0, time.UTC)
alice = users.User{UserSpec: users.UserSpec{ID: 1, Domain: "example.org"}, Login: "Alice"}
bob = users.User{UserSpec: users.UserSpec{ID: 2, Domain: "example.org"}, Login: "Bob"}
)
View
@@ -6,6 +6,7 @@ import (
"log"
"net/http"
"strconv"
"time"
"github.com/shurcooL/home/httputil"
"github.com/shurcooL/home/internal/page/resume"
@@ -63,7 +64,7 @@ func initResume(reactions reactions.Service, notifications notifications.Service
authenticatedUser = users.User{} // THINK: Should it be a fatal error or not? What about on frontend vs backend?
}
returnURL := req.RequestURI
err = resume.RenderBodyInnerHTML(req.Context(), w, reactions, notifications, usersService, authenticatedUser, returnURL)
err = resume.RenderBodyInnerHTML(req.Context(), w, reactions, notifications, usersService, time.Now(), authenticatedUser, returnURL)
if err != nil {
return err
}

0 comments on commit 9c781c5

Please sign in to comment.