Skip to content

Commit bbb178f

Browse files
committed
Merge branch 'main' of github.com:thenativeweb/temp--techlounge
2 parents abfaea6 + af2e9f6 commit bbb178f

File tree

12 files changed

+211
-46
lines changed

12 files changed

+211
-46
lines changed

.github/workflows/lint-pr-title.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
steps:
2020
- name: Lint PR Title
21-
uses: amannn/action-semantic-pull-request@v5
21+
uses: amannn/action-semantic-pull-request@v6
2222
env:
2323
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2424
with:

.github/workflows/qa.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
steps:
1919
- name: Clone repository
20-
uses: actions/checkout@v4
20+
uses: actions/checkout@v5
2121
- name: Install Go
2222
uses: actions/setup-go@v5
2323
with:

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
steps:
1818
- name: Clone repository
19-
uses: actions/checkout@v4
19+
uses: actions/checkout@v5
2020
with:
2121
fetch-depth: "0"
2222
- name: Install Go

api/alltodos/handle_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package alltodos_test
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/thenativeweb/techlounge-to-do/api/alltodos"
11+
)
12+
13+
func TestHandle(t *testing.T) {
14+
payload := &alltodos.ResponseBody{
15+
ToDos: []alltodos.ResponseBodyToDo{
16+
{ID: "1", Intention: "Buy milk", IsCompleted: false},
17+
{ID: "2", Intention: "Read book", IsCompleted: true},
18+
},
19+
}
20+
21+
req := httptest.NewRequest(http.MethodGet, "/todos", nil)
22+
rr := httptest.NewRecorder()
23+
24+
handler := alltodos.Handle(payload)
25+
handler.ServeHTTP(rr, req)
26+
27+
assert.Equal(t, http.StatusOK, rr.Code)
28+
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
29+
30+
var responseBody alltodos.ResponseBody
31+
err := json.NewDecoder(rr.Body).Decode(&responseBody)
32+
assert.NoError(t, err)
33+
34+
assert.Len(t, responseBody.ToDos, 2)
35+
assert.Equal(
36+
t,
37+
alltodos.ResponseBodyToDo{ID: "1", Intention: "Buy milk", IsCompleted: false},
38+
responseBody.ToDos[0],
39+
)
40+
assert.Equal(
41+
t,
42+
alltodos.ResponseBodyToDo{ID: "2", Intention: "Read book", IsCompleted: true},
43+
responseBody.ToDos[1],
44+
)
45+
}

api/completetodo/handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Handle(client *eventsourcingdb.Client) http.HandlerFunc {
3636
// taking events back, and writing them to the database
3737
// should happen there. This is a bit of a mess right now.
3838

39-
todo := domain.NewTodo(id)
39+
todo := domain.NewToDo(id)
4040

4141
latestEventID := ""
4242
for event, err := range client.ReadEvents(

api/memorizetodo/handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Handle(client *eventsourcingdb.Client) http.HandlerFunc {
4141
// taking events back, and writing them to the database
4242
// should happen there. This is a bit of a mess right now.
4343

44-
todo := domain.NewTodo(id)
44+
todo := domain.NewToDo(id)
4545
toDoMemorized, err := todo.Memorize(intention, details)
4646
if err != nil {
4747
logging.Error("failed to memorize todo", "error", err)

api/memorizetodo/handle_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package memorizetodo_test
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"github.com/google/uuid"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/thenativeweb/eventsourcingdb-client-golang/eventsourcingdb"
13+
"github.com/thenativeweb/techlounge-to-do/api/memorizetodo"
14+
"github.com/thenativeweb/techlounge-to-do/domain"
15+
)
16+
17+
func TestHandle(t *testing.T) {
18+
container := eventsourcingdb.NewContainer().
19+
WithImageTag("1.1.0").
20+
WithAPIToken("secret")
21+
22+
err := container.Start(t.Context())
23+
assert.NoError(t, err)
24+
defer container.Stop(t.Context())
25+
26+
client, err := container.GetClient(t.Context())
27+
assert.NoError(t, err)
28+
29+
requestBody := `{"intention":"Buy milk","details":"Remember to buy milk on the way home."}`
30+
req := httptest.NewRequest(http.MethodPost, "/memorize-to-do", strings.NewReader(requestBody))
31+
rr := httptest.NewRecorder()
32+
33+
handler := memorizetodo.Handle(client)
34+
handler.ServeHTTP(rr, req)
35+
36+
assert.Equal(t, http.StatusOK, rr.Code)
37+
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
38+
39+
var responseBody memorizetodo.ResponseBody
40+
err = json.NewDecoder(rr.Body).Decode(&responseBody)
41+
assert.NoError(t, err)
42+
assert.NotEqual(t, uuid.Nil, responseBody.ID)
43+
44+
var events []eventsourcingdb.Event
45+
for event, err := range client.ReadEvents(
46+
t.Context(),
47+
"/",
48+
eventsourcingdb.ReadEventsOptions{Recursive: true},
49+
) {
50+
assert.NoError(t, err)
51+
events = append(events, event)
52+
}
53+
54+
assert.Len(t, events, 1)
55+
56+
event := events[0]
57+
assert.Equal(t, domain.ToDoMemorizedEventType, event.Type)
58+
59+
var memorizedEvent domain.ToDoMemorized
60+
err = json.Unmarshal(event.Data, &memorizedEvent)
61+
assert.NoError(t, err)
62+
63+
assert.Equal(t, "Buy milk", memorizedEvent.Intention)
64+
assert.Equal(t, "Remember to buy milk on the way home.", memorizedEvent.Details)
65+
}

api/ping/handle_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ping_test
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/thenativeweb/techlounge-to-do/api/ping"
11+
)
12+
13+
func TestHandle(t *testing.T) {
14+
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
15+
rr := httptest.NewRecorder()
16+
17+
handler := ping.Handle()
18+
handler.ServeHTTP(rr, req)
19+
20+
assert.Equal(t, http.StatusOK, rr.Code)
21+
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
22+
23+
var responseBody ping.ResponseBody
24+
err := json.NewDecoder(rr.Body).Decode(&responseBody)
25+
assert.NoError(t, err)
26+
assert.Equal(t, "pong", responseBody.Ping)
27+
}

domain/to_do.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type ToDo struct {
1212
IsCompleted bool
1313
}
1414

15-
func NewTodo(id uuid.UUID) *ToDo {
15+
func NewToDo(id uuid.UUID) *ToDo {
1616
return &ToDo{
1717
ID: id,
1818
IsCompleted: false,

domain/to_do_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package domain_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/uuid"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/thenativeweb/techlounge-to-do/domain"
9+
)
10+
11+
func TestNewToDo(t *testing.T) {
12+
id := uuid.New()
13+
toDo := domain.NewToDo(id)
14+
15+
assert.Equal(t, id, toDo.ID)
16+
assert.False(t, toDo.IsCompleted)
17+
}
18+
19+
func TestMemorize(t *testing.T) {
20+
t.Run("returns a memorized event", func(t *testing.T) {
21+
id := uuid.New()
22+
toDo := domain.NewToDo(id)
23+
24+
event, err := toDo.Memorize("Buy milk", "Remember to buy milk on the way home.")
25+
assert.NoError(t, err)
26+
assert.Equal(t, id, event.ToDoID)
27+
assert.Equal(t, "Buy milk", event.Intention)
28+
assert.Equal(t, "Remember to buy milk on the way home.", event.Details)
29+
})
30+
31+
t.Run("returns an error if intention is empty", func(t *testing.T) {
32+
id := uuid.New()
33+
toDo := domain.NewToDo(id)
34+
35+
_, err := toDo.Memorize("", "Some details")
36+
assert.Error(t, err)
37+
})
38+
}

0 commit comments

Comments
 (0)