chore: fix tests
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -19,26 +18,24 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.sendErrf(w, http.StatusBadRequest, "cannot read body: %v", err)
|
s.sendErr(w, http.StatusBadRequest, "cannot read body")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
repos, err := req.GetRepos()
|
repos, err := req.GetRepos()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.sendErr(w, 500, err.Error())
|
s.sendErr(w, http.StatusInternalServerError, "cannot get repos")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
invoice, report, err := s.invoice.Generate(req.Creditor, req.Debtor, time.Duration(req.DurationThreshold), req.HourlyRate, repos)
|
invoice, report, err := s.invoice.Generate(req.Creditor, req.Debtor, time.Duration(req.DurationThreshold), req.HourlyRate, repos)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error while processing invoice:", err)
|
s.sendErr(w, http.StatusInternalServerError, "internal server error")
|
||||||
fmt.Fprint(w, "internal server error")
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// if no time has to be billed aka if bill for 0 CHF
|
// if no time has to be billed aka if bill for 0 CHF
|
||||||
if report.Total() <= time.Duration(0) {
|
if report.Total() <= time.Duration(0) {
|
||||||
s.sendErr(w, http.StatusNotFound, "no suitable issues to be billed")
|
s.sendErrf(w, http.StatusNotFound, "no suitable issues to be billed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.schreifuchs.ch/lou-taylor/accounting/internal/email"
|
"git.schreifuchs.ch/lou-taylor/accounting/internal/email"
|
||||||
"git.schreifuchs.ch/lou-taylor/accounting/internal/jtype"
|
|
||||||
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice"
|
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice"
|
||||||
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/issue"
|
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/issue"
|
||||||
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model"
|
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model"
|
||||||
@@ -106,7 +105,7 @@ func TestCreateInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
pdfContent := "mock PDF content"
|
pdfContent := "mock PDF content"
|
||||||
@@ -124,7 +123,7 @@ func TestCreateInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "invalid", // Changed to string
|
DurationThreshold: "invalid", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
mockGenerate: nil, // Not called for invalid body
|
mockGenerate: nil, // Not called for invalid body
|
||||||
expectedStatus: http.StatusBadRequest,
|
expectedStatus: http.StatusBadRequest,
|
||||||
@@ -137,7 +136,7 @@ func TestCreateInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
return nil, nil, errors.New("failed to generate invoice")
|
return nil, nil, errors.New("failed to generate invoice")
|
||||||
@@ -152,7 +151,7 @@ func TestCreateInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
// Create a report with zero total duration
|
// Create a report with zero total duration
|
||||||
@@ -213,7 +212,7 @@ func TestSendInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
@@ -238,7 +237,7 @@ func TestSendInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
@@ -259,7 +258,7 @@ func TestSendInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
@@ -284,7 +283,7 @@ func TestSendInvoice(t *testing.T) {
|
|||||||
Debtor: model.Entity{Name: "Debtor"},
|
Debtor: model.Entity{Name: "Debtor"},
|
||||||
DurationThreshold: "1h", // Changed to string
|
DurationThreshold: "1h", // Changed to string
|
||||||
HourlyRate: 100,
|
HourlyRate: 100,
|
||||||
Repos: []string{"repo1"},
|
Repos: []string{"owner/repo1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
|
||||||
|
|||||||
@@ -57,13 +57,13 @@ func (s sendReq) ToEMail() email.Mail {
|
|||||||
func (s *Service) sendErrf(w http.ResponseWriter, statusCode int, format string, a ...any) {
|
func (s *Service) sendErrf(w http.ResponseWriter, statusCode int, format string, a ...any) {
|
||||||
msg := fmt.Sprintf(format, a...)
|
msg := fmt.Sprintf(format, a...)
|
||||||
s.log.Error(msg, slog.Any("statusCode", statusCode))
|
s.log.Error(msg, slog.Any("statusCode", statusCode))
|
||||||
w.Write([]byte(msg))
|
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
|
w.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) sendErr(w http.ResponseWriter, statusCode int, a ...any) {
|
func (s *Service) sendErr(w http.ResponseWriter, statusCode int, a ...any) {
|
||||||
msg := fmt.Sprint(a...)
|
msg := fmt.Sprint(a...)
|
||||||
s.log.Error(msg, slog.Any("statusCode", statusCode))
|
s.log.Error(msg, slog.Any("statusCode", statusCode))
|
||||||
w.Write([]byte(msg))
|
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
|
w.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user