From de7c6f7223dde93c3ce34cfe505cc92e742ce7b6 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Wed, 23 Oct 2024 23:29:44 +0200 Subject: [PATCH] test: add event repo tests --- internal/howmuch/adapter/repo/event.go | 2 + internal/howmuch/adapter/repo/event_test.go | 122 ++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 internal/howmuch/adapter/repo/event_test.go diff --git a/internal/howmuch/adapter/repo/event.go b/internal/howmuch/adapter/repo/event.go index 53dcfce..52dcf93 100644 --- a/internal/howmuch/adapter/repo/event.go +++ b/internal/howmuch/adapter/repo/event.go @@ -114,6 +114,8 @@ func convToEventList(eventsDTO []sqlc.ListEventsByUserIDRow) ([]*model.EventList ID: int(evDTO.ID), Name: evDTO.Name, Description: evDTO.Description.String, + Owner: &owner, + CreatedAt: evDTO.CreatedAt, } events = append(events, ev) diff --git a/internal/howmuch/adapter/repo/event_test.go b/internal/howmuch/adapter/repo/event_test.go new file mode 100644 index 0000000..46bf549 --- /dev/null +++ b/internal/howmuch/adapter/repo/event_test.go @@ -0,0 +1,122 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package repo + +import ( + "database/sql" + "encoding/json" + "testing" + "time" + + "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/repo/sqlc" + "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model" + "github.com/stretchr/testify/assert" +) + +func TestConvToEventRetrieved(t *testing.T) { + input := &sqlc.GetEventByIDRow{ + ID: 123, + Name: "event", + Description: sql.NullString{Valid: false}, + TotalAmount: sql.NullInt32{Valid: false}, + DefaultCurrency: "EUR", + CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + UpdatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + Owner: json.RawMessage(`{"id":1, "first_name":"owner", "last_name":"owner"}`), + Users: json.RawMessage(`[{"id":1, "first_name":"owner", "last_name":"owner"}]`), + } + + want := &model.EventRetrieved{ + ID: 123, + Name: "event", + Description: "", + TotalAmount: model.Money{Amount: 0, Currency: "EUR"}, + DefaultCurrency: model.Currency("EUR"), + CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + UpdatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + Owner: &model.UserBaseRetrieved{ + ID: 1, + FirstName: "owner", + LastName: "owner", + }, + Users: []*model.UserBaseRetrieved{ + { + ID: 1, + FirstName: "owner", + LastName: "owner", + }, + }, + } + + got, err := convToEventRetrieved(input) + assert.NoError(t, err) + assert.Equal(t, want, got) +} + +func TestConvToEventList(t *testing.T) { + input := []sqlc.ListEventsByUserIDRow{ + { + ID: 123, + Name: "event", + Description: sql.NullString{Valid: false}, + CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + Owner: json.RawMessage(`{"id":1, "first_name":"owner", "last_name":"owner"}`), + }, + { + ID: 456, + Name: "event2", + Description: sql.NullString{String: "super event", Valid: true}, + CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + Owner: json.RawMessage(`{"id":1, "first_name":"owner", "last_name":"owner"}`), + }, + } + + want := []*model.EventListRetrieved{ + { + ID: 123, + Name: "event", + Description: "", + CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + Owner: &model.UserBaseRetrieved{ + ID: 1, + FirstName: "owner", + LastName: "owner", + }, + }, + { + ID: 456, + Name: "event2", + Description: "super event", + CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC), + Owner: &model.UserBaseRetrieved{ + ID: 1, + FirstName: "owner", + LastName: "owner", + }, + }, + } + + got, err := convToEventList(input) + assert.NoError(t, err) + assert.Equal(t, want, got) +}