Itsscb/issue_ftadd-returnsLog-2 (#14)

This commit is contained in:
itsscb 2023-09-20 21:40:32 +02:00 committed by GitHub
parent 97e5b4c293
commit cd43a6f23a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 378 additions and 19 deletions

View File

@ -115,7 +115,7 @@ CREATE TABLE "returns" (
CREATE TABLE "returnsLog" ( CREATE TABLE "returnsLog" (
"ID" bigserial UNIQUE PRIMARY KEY NOT NULL, "ID" bigserial UNIQUE PRIMARY KEY NOT NULL,
"returnsID" bigint NOT NULL, "returnID" bigint NOT NULL,
"mailID" bigint NOT NULL, "mailID" bigint NOT NULL,
"status" varchar, "status" varchar,
"creator" varchar NOT NULL, "creator" varchar NOT NULL,
@ -136,6 +136,6 @@ ALTER TABLE "returns" ADD FOREIGN KEY ("personID") REFERENCES "persons" ("ID");
ALTER TABLE "returns" ADD FOREIGN KEY ("providerID") REFERENCES "providers" ("ID"); ALTER TABLE "returns" ADD FOREIGN KEY ("providerID") REFERENCES "providers" ("ID");
ALTER TABLE "returnsLog" ADD FOREIGN KEY ("returnsID") REFERENCES "returns" ("ID"); ALTER TABLE "returnsLog" ADD FOREIGN KEY ("returnID") REFERENCES "returns" ("ID");
ALTER TABLE "returnsLog" ADD FOREIGN KEY ("mailID") REFERENCES "mails" ("ID"); ALTER TABLE "returnsLog" ADD FOREIGN KEY ("mailID") REFERENCES "mails" ("ID");

39
db/query/returnsLog.sql Normal file
View File

@ -0,0 +1,39 @@
-- name: GetReturnsLog :one
SELECT * FROM "returnsLog"
WHERE "ID" = sqlc.arg(ID) LIMIT 1;
-- name: CreateReturnsLog :one
INSERT INTO "returnsLog" (
"returnID",
"mailID",
"status",
"creator",
"changer"
) VALUES (
sqlc.arg(returnID),
sqlc.arg(mailID),
sqlc.arg(status),
sqlc.arg(creator),
sqlc.arg(creator)
) RETURNING *;
-- name: ListReturnsLogs :many
SELECT * FROM "returnsLog"
ORDER BY status
LIMIT $1
OFFSET $2;
-- name: UpdateReturnsLog :one
UPDATE "returnsLog"
SET
"returnID" = COALESCE(sqlc.narg(returnID), "returnID"),
"mailID" = COALESCE(sqlc.narg(mailID), "mailID"),
"status" = COALESCE(sqlc.narg(status), "status"),
changer = $1,
changed = now()
WHERE "ID" = sqlc.arg(ID)
RETURNING *;
-- name: DeleteReturnsLog :exec
DELETE FROM "returnsLog"
WHERE "ID" = sqlc.arg(ID);

View File

@ -11,6 +11,9 @@ import (
) )
func createRandomCustomer(t *testing.T) Customer { func createRandomCustomer(t *testing.T) Customer {
creator := util.RandomUser()
arg := CreateCustomerParams{ arg := CreateCustomerParams{
Username: util.RandomUser(), Username: util.RandomUser(),
Passwordhash: util.RandomString(30), Passwordhash: util.RandomString(30),
@ -26,7 +29,8 @@ func createRandomCustomer(t *testing.T) Customer {
Zip: util.RandomString(5), Zip: util.RandomString(5),
Street: util.RandomString(20), Street: util.RandomString(20),
Country: util.RandomString(15), Country: util.RandomString(15),
Creator: util.RandomUser(), Creator: creator,
Changer: creator,
} }
account, err := testQueries.CreateCustomer(context.Background(), arg) account, err := testQueries.CreateCustomer(context.Background(), arg)
@ -45,6 +49,7 @@ func createRandomCustomer(t *testing.T) Customer {
require.Equal(t, arg.Street, account.Street) require.Equal(t, arg.Street, account.Street)
require.Equal(t, arg.Country, account.Country) require.Equal(t, arg.Country, account.Country)
require.Equal(t, arg.Creator, account.Creator) require.Equal(t, arg.Creator, account.Creator)
require.Equal(t, arg.Changer, account.Changer)
require.NotZero(t, account.ID) require.NotZero(t, account.ID)
require.NotZero(t, account.Created) require.NotZero(t, account.Created)
@ -76,6 +81,7 @@ func TestGetCustomer(t *testing.T) {
require.Equal(t, newAccount.Street, account.Street) require.Equal(t, newAccount.Street, account.Street)
require.Equal(t, newAccount.Country, account.Country) require.Equal(t, newAccount.Country, account.Country)
require.Equal(t, newAccount.Creator, account.Creator) require.Equal(t, newAccount.Creator, account.Creator)
require.Equal(t, newAccount.Changer, account.Changer)
require.WithinDuration(t, newAccount.Created, account.Created, time.Second) require.WithinDuration(t, newAccount.Created, account.Created, time.Second)
} }
@ -110,6 +116,7 @@ func TestUpdateCustomer(t *testing.T) {
require.Equal(t, account1.ID, account2.ID) require.Equal(t, account1.ID, account2.ID)
require.Equal(t, account1.Username, account2.Username) require.Equal(t, account1.Username, account2.Username)
require.NotEqual(t, account1.Phone, account2.Phone) require.NotEqual(t, account1.Phone, account2.Phone)
require.NotEqual(t, account1.Changer, account2.Changer)
} }
func TestListCustomers(t *testing.T) { func TestListCustomers(t *testing.T) {

View File

@ -14,6 +14,8 @@ func createRandomDocumentUpload(t *testing.T) Document {
person := createRandomPerson(t) person := createRandomPerson(t)
require.NotEmpty(t, person) require.NotEmpty(t, person)
creator := util.RandomUser()
arg := CreateDocumentUploadParams{ arg := CreateDocumentUploadParams{
PersonID: sql.NullInt64{ PersonID: sql.NullInt64{
Valid: true, Valid: true,
@ -23,8 +25,8 @@ func createRandomDocumentUpload(t *testing.T) Document {
Type: util.RandomUser(), Type: util.RandomUser(),
Path: util.RandomString(50), Path: util.RandomString(50),
Url: util.RandomString(60), Url: util.RandomString(60),
Creator: util.RandomUser(), Creator: creator,
Changer: util.RandomUser(), Changer: creator,
} }
document, err := testQueries.CreateDocumentUpload(context.Background(), arg) document, err := testQueries.CreateDocumentUpload(context.Background(), arg)

View File

@ -12,6 +12,8 @@ import (
func createRandomMail(t *testing.T) Mail { func createRandomMail(t *testing.T) Mail {
creator := util.RandomUser()
arg := CreateMailParams{ arg := CreateMailParams{
From: util.RandomEmail(), From: util.RandomEmail(),
To: []string{util.RandomEmail()}, To: []string{util.RandomEmail()},
@ -19,8 +21,8 @@ func createRandomMail(t *testing.T) Mail {
Subject: util.RandomString(20), Subject: util.RandomString(20),
Body: util.RandomString(300), Body: util.RandomString(300),
Timestamp: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC), Timestamp: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
Creator: util.RandomUser(), Creator: creator,
Changer: util.RandomUser(), Changer: creator,
} }
mail, err := testQueries.CreateMail(context.Background(), arg) mail, err := testQueries.CreateMail(context.Background(), arg)

View File

@ -125,12 +125,12 @@ type Return struct {
} }
type ReturnsLog struct { type ReturnsLog struct {
ID int64 `json:"ID"` ID int64 `json:"ID"`
ReturnsID int64 `json:"returnsID"` ReturnID int64 `json:"returnID"`
MailID int64 `json:"mailID"` MailID int64 `json:"mailID"`
Status sql.NullString `json:"status"` Status sql.NullString `json:"status"`
Creator string `json:"creator"` Creator string `json:"creator"`
Created time.Time `json:"created"` Created time.Time `json:"created"`
Changer string `json:"changer"` Changer string `json:"changer"`
Changed time.Time `json:"changed"` Changed time.Time `json:"changed"`
} }

View File

@ -14,6 +14,8 @@ func createRandomPayment(t *testing.T) Payment {
account := createRandomCustomer(t) account := createRandomCustomer(t)
require.NotEmpty(t, account) require.NotEmpty(t, account)
creator := util.RandomUser()
arg := CreatePaymentParams{ arg := CreatePaymentParams{
CustomerID: account.ID, CustomerID: account.ID,
PaymentCategory: util.RandomUser(), PaymentCategory: util.RandomUser(),
@ -42,7 +44,8 @@ func createRandomPayment(t *testing.T) Payment {
String: util.RandomUser(), String: util.RandomUser(),
}, },
Type: util.RandomUser(), Type: util.RandomUser(),
Creator: util.RandomUser(), Creator: creator,
Changer: creator,
} }
person, err := testQueries.CreatePayment(context.Background(), arg) person, err := testQueries.CreatePayment(context.Background(), arg)

View File

@ -14,6 +14,8 @@ func createRandomPerson(t *testing.T) Person {
account := createRandomCustomer(t) account := createRandomCustomer(t)
require.NotEmpty(t, account) require.NotEmpty(t, account)
creator := util.RandomUser()
arg := CreatePersonParams{ arg := CreatePersonParams{
CustomerID: account.ID, CustomerID: account.ID,
Firstname: util.RandomUser(), Firstname: util.RandomUser(),
@ -23,7 +25,8 @@ func createRandomPerson(t *testing.T) Person {
Zip: util.RandomString(5), Zip: util.RandomString(5),
Street: util.RandomString(20), Street: util.RandomString(20),
Country: util.RandomString(15), Country: util.RandomString(15),
Creator: util.RandomUser(), Creator: creator,
Changer: creator,
} }
person, err := testQueries.CreatePerson(context.Background(), arg) person, err := testQueries.CreatePerson(context.Background(), arg)

View File

@ -11,12 +11,16 @@ import (
) )
func createRandomProvider(t *testing.T) Provider { func createRandomProvider(t *testing.T) Provider {
creator := util.RandomUser()
arg := CreateProviderParams{ arg := CreateProviderParams{
Name: util.RandomUser(), Name: util.RandomUser(),
Description: util.RandomString(30), Description: util.RandomString(30),
Category: util.RandomUser(), Category: util.RandomUser(),
Email: util.RandomUser(), Email: util.RandomUser(),
Creator: util.RandomUser(), Creator: creator,
Changer: creator,
} }
provider, err := testQueries.CreateProvider(context.Background(), arg) provider, err := testQueries.CreateProvider(context.Background(), arg)

View File

@ -15,6 +15,8 @@ func createRandomReturn(t *testing.T) Return {
person := createRandomPerson(t) person := createRandomPerson(t)
provider := createRandomProvider(t) provider := createRandomProvider(t)
creator := util.RandomUser()
arg := CreateReturnParams{ arg := CreateReturnParams{
Personid: person.ID, Personid: person.ID,
Providerid: provider.ID, Providerid: provider.ID,
@ -23,7 +25,8 @@ func createRandomReturn(t *testing.T) Return {
Description: util.RandomString(30), Description: util.RandomString(30),
Category: util.RandomUser(), Category: util.RandomUser(),
Email: util.RandomUser(), Email: util.RandomUser(),
Creator: util.RandomUser(), Creator: creator,
Changer: creator,
} }
ret, err := testQueries.CreateReturn(context.Background(), arg) ret, err := testQueries.CreateReturn(context.Background(), arg)

174
db/sqlc/returnsLog.sql.go Normal file
View File

@ -0,0 +1,174 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.21.0
// source: returnsLog.sql
package db
import (
"context"
"database/sql"
)
const createReturnsLog = `-- name: CreateReturnsLog :one
INSERT INTO "returnsLog" (
"returnID",
"mailID",
"status",
"creator",
"changer"
) VALUES (
$1,
$2,
$3,
$4,
$5
) RETURNING "ID", "returnID", "mailID", status, creator, created, changer, changed
`
type CreateReturnsLogParams struct {
Returnid int64 `json:"returnid"`
Mailid int64 `json:"mailid"`
Status sql.NullString `json:"status"`
Creator string `json:"creator"`
Changer string `json:"changer"`
}
func (q *Queries) CreateReturnsLog(ctx context.Context, arg CreateReturnsLogParams) (ReturnsLog, error) {
row := q.db.QueryRowContext(ctx, createReturnsLog,
arg.Returnid,
arg.Mailid,
arg.Status,
arg.Creator,
arg.Changer,
)
var i ReturnsLog
err := row.Scan(
&i.ID,
&i.ReturnID,
&i.MailID,
&i.Status,
&i.Creator,
&i.Created,
&i.Changer,
&i.Changed,
)
return i, err
}
const deleteReturnsLog = `-- name: DeleteReturnsLog :exec
DELETE FROM "returnsLog"
WHERE "ID" = $1
`
func (q *Queries) DeleteReturnsLog(ctx context.Context, id int64) error {
_, err := q.db.ExecContext(ctx, deleteReturnsLog, id)
return err
}
const getReturnsLog = `-- name: GetReturnsLog :one
SELECT "ID", "returnID", "mailID", status, creator, created, changer, changed FROM "returnsLog"
WHERE "ID" = $1 LIMIT 1
`
func (q *Queries) GetReturnsLog(ctx context.Context, id int64) (ReturnsLog, error) {
row := q.db.QueryRowContext(ctx, getReturnsLog, id)
var i ReturnsLog
err := row.Scan(
&i.ID,
&i.ReturnID,
&i.MailID,
&i.Status,
&i.Creator,
&i.Created,
&i.Changer,
&i.Changed,
)
return i, err
}
const listReturnsLogs = `-- name: ListReturnsLogs :many
SELECT "ID", "returnID", "mailID", status, creator, created, changer, changed FROM "returnsLog"
ORDER BY status
LIMIT $1
OFFSET $2
`
type ListReturnsLogsParams struct {
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
}
func (q *Queries) ListReturnsLogs(ctx context.Context, arg ListReturnsLogsParams) ([]ReturnsLog, error) {
rows, err := q.db.QueryContext(ctx, listReturnsLogs, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ReturnsLog{}
for rows.Next() {
var i ReturnsLog
if err := rows.Scan(
&i.ID,
&i.ReturnID,
&i.MailID,
&i.Status,
&i.Creator,
&i.Created,
&i.Changer,
&i.Changed,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateReturnsLog = `-- name: UpdateReturnsLog :one
UPDATE "returnsLog"
SET
"returnID" = COALESCE($2, "returnID"),
"mailID" = COALESCE($3, "mailID"),
"status" = COALESCE($4, "status"),
changer = $1,
changed = now()
WHERE "ID" = $5
RETURNING "ID", "returnID", "mailID", status, creator, created, changer, changed
`
type UpdateReturnsLogParams struct {
Changer string `json:"changer"`
Returnid sql.NullInt64 `json:"returnid"`
Mailid sql.NullInt64 `json:"mailid"`
Status sql.NullString `json:"status"`
ID int64 `json:"id"`
}
func (q *Queries) UpdateReturnsLog(ctx context.Context, arg UpdateReturnsLogParams) (ReturnsLog, error) {
row := q.db.QueryRowContext(ctx, updateReturnsLog,
arg.Changer,
arg.Returnid,
arg.Mailid,
arg.Status,
arg.ID,
)
var i ReturnsLog
err := row.Scan(
&i.ID,
&i.ReturnID,
&i.MailID,
&i.Status,
&i.Creator,
&i.Created,
&i.Changer,
&i.Changed,
)
return i, err
}

122
db/sqlc/returnsLog_test.go Normal file
View File

@ -0,0 +1,122 @@
package db
import (
"context"
"database/sql"
"testing"
"time"
"github.com/itsscb/df/util"
"github.com/stretchr/testify/require"
)
func createRandomReturnsLog(t *testing.T) ReturnsLog {
ret := createRandomReturn(t)
mail := createRandomMail(t)
creator := util.RandomUser()
arg := CreateReturnsLogParams{
Returnid: ret.ID,
Mailid: mail.ID,
Status: sql.NullString{
Valid: true,
String: util.RandomString(7),
},
Creator: creator,
Changer: creator,
}
returnsLog, err := testQueries.CreateReturnsLog(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, returnsLog)
require.Equal(t, arg.Returnid, returnsLog.ReturnID)
require.Equal(t, arg.Mailid, returnsLog.MailID)
require.Equal(t, arg.Status, returnsLog.Status)
require.Equal(t, arg.Creator, returnsLog.Creator)
require.NotZero(t, returnsLog.ID)
require.NotZero(t, returnsLog.Created)
return returnsLog
}
func TestCreateReturnsLog(t *testing.T) {
createRandomReturnsLog(t)
}
func TestGetReturnsLog(t *testing.T) {
newReturnsLog := createRandomReturnsLog(t)
require.NotEmpty(t, newReturnsLog)
returnsLog, err := testQueries.GetReturnsLog(context.Background(), newReturnsLog.ID)
require.NoError(t, err)
require.NotEmpty(t, returnsLog)
require.Equal(t, newReturnsLog.ReturnID, returnsLog.ReturnID)
require.Equal(t, newReturnsLog.MailID, returnsLog.MailID)
require.Equal(t, newReturnsLog.Status, returnsLog.Status)
require.Equal(t, newReturnsLog.Created, returnsLog.Created)
require.Equal(t, newReturnsLog.Changer, returnsLog.Changer)
require.Equal(t, newReturnsLog.Changed, returnsLog.Changed)
require.Equal(t, newReturnsLog.Creator, returnsLog.Creator)
require.WithinDuration(t, newReturnsLog.Created, returnsLog.Created, time.Second)
}
func TestDeleteReturnsLog(t *testing.T) {
returnsLog1 := createRandomReturnsLog(t)
err := testQueries.DeleteReturnsLog(context.Background(), returnsLog1.ID)
require.NoError(t, err)
returnsLog2, err := testQueries.GetReturnsLog(context.Background(), returnsLog1.ID)
require.Error(t, err)
require.EqualError(t, err, sql.ErrNoRows.Error())
require.Empty(t, returnsLog2)
}
func TestUpdateReturnsLog(t *testing.T) {
returnsLog1 := createRandomReturnsLog(t)
require.NotEmpty(t, returnsLog1)
status := util.RandomString(15)
arg := UpdateReturnsLogParams{
ID: returnsLog1.ID,
Status: sql.NullString{
String: status,
Valid: true,
},
}
returnsLog2, err := testQueries.UpdateReturnsLog(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, returnsLog2)
require.Equal(t, returnsLog1.ID, returnsLog2.ID)
require.Equal(t, returnsLog2.ReturnID, returnsLog1.ReturnID)
require.NotEqual(t, returnsLog1.Status, returnsLog2.Status)
require.NotEqual(t, returnsLog1.Changed, returnsLog2.Changed)
require.NotEqual(t, returnsLog1.Changer, returnsLog2.Changer)
}
func TestListReturnsLogs(t *testing.T) {
for i := 0; i < 10; i++ {
createRandomReturnsLog(t)
}
arg := ListReturnsLogsParams{
Limit: 5,
Offset: 5,
}
returnsLogs, err := testQueries.ListReturnsLogs(context.Background(), arg)
require.NoError(t, err)
require.Len(t, returnsLogs, 5)
for _, returnsLog := range returnsLogs {
require.NotEmpty(t, returnsLog)
}
}