parent
e234913d6a
commit
4471bf1b7f
51
db/query/return.sql
Normal file
51
db/query/return.sql
Normal file
@ -0,0 +1,51 @@
|
||||
-- name: GetReturn :one
|
||||
SELECT * FROM returns
|
||||
WHERE "ID" = sqlc.arg(ID) LIMIT 1;
|
||||
|
||||
-- name: CreateReturn :one
|
||||
INSERT INTO returns (
|
||||
"personID",
|
||||
"providerID",
|
||||
"name",
|
||||
"description",
|
||||
"category",
|
||||
"email",
|
||||
"status",
|
||||
"creator",
|
||||
"changer"
|
||||
) VALUES (
|
||||
sqlc.arg(personID),
|
||||
sqlc.arg(providerID),
|
||||
sqlc.arg(name),
|
||||
sqlc.arg(description),
|
||||
sqlc.arg(category),
|
||||
sqlc.arg(email),
|
||||
sqlc.arg(status),
|
||||
sqlc.arg(creator),
|
||||
sqlc.arg(changer)
|
||||
) RETURNING *;
|
||||
|
||||
-- name: ListReturns :many
|
||||
SELECT * FROM returns
|
||||
ORDER BY name
|
||||
LIMIT $1
|
||||
OFFSET $2;
|
||||
|
||||
-- name: UpdateReturn :one
|
||||
UPDATE returns
|
||||
SET
|
||||
"personID" = COALESCE(sqlc.narg(personID), "personID"),
|
||||
"providerID" = COALESCE(sqlc.narg(providerID), "providerID"),
|
||||
"name" = COALESCE(sqlc.narg(name), "name"),
|
||||
"description" = COALESCE(sqlc.narg(description), "description"),
|
||||
"category" = COALESCE(sqlc.narg(category), "category"),
|
||||
"email" = COALESCE(sqlc.narg(email), "email"),
|
||||
"status" = COALESCE(sqlc.narg(status), "status"),
|
||||
changer = sqlc.arg(changer),
|
||||
changed = now()
|
||||
WHERE "ID" = sqlc.arg(ID)
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteReturn :exec
|
||||
DELETE FROM returns
|
||||
WHERE "ID" = sqlc.arg(ID);
|
218
db/sqlc/return.sql.go
Normal file
218
db/sqlc/return.sql.go
Normal file
@ -0,0 +1,218 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.21.0
|
||||
// source: return.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const createReturn = `-- name: CreateReturn :one
|
||||
INSERT INTO returns (
|
||||
"personID",
|
||||
"providerID",
|
||||
"name",
|
||||
"description",
|
||||
"category",
|
||||
"email",
|
||||
"status",
|
||||
"creator",
|
||||
"changer"
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9
|
||||
) RETURNING "ID", "personID", "providerID", name, description, category, email, status, creator, created, changer, changed
|
||||
`
|
||||
|
||||
type CreateReturnParams struct {
|
||||
Personid int64 `json:"personid"`
|
||||
Providerid int64 `json:"providerid"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Category string `json:"category"`
|
||||
Email string `json:"email"`
|
||||
Status string `json:"status"`
|
||||
Creator string `json:"creator"`
|
||||
Changer string `json:"changer"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateReturn(ctx context.Context, arg CreateReturnParams) (Return, error) {
|
||||
row := q.db.QueryRowContext(ctx, createReturn,
|
||||
arg.Personid,
|
||||
arg.Providerid,
|
||||
arg.Name,
|
||||
arg.Description,
|
||||
arg.Category,
|
||||
arg.Email,
|
||||
arg.Status,
|
||||
arg.Creator,
|
||||
arg.Changer,
|
||||
)
|
||||
var i Return
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.PersonID,
|
||||
&i.ProviderID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Category,
|
||||
&i.Email,
|
||||
&i.Status,
|
||||
&i.Creator,
|
||||
&i.Created,
|
||||
&i.Changer,
|
||||
&i.Changed,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteReturn = `-- name: DeleteReturn :exec
|
||||
DELETE FROM returns
|
||||
WHERE "ID" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteReturn(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteReturn, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getReturn = `-- name: GetReturn :one
|
||||
SELECT "ID", "personID", "providerID", name, description, category, email, status, creator, created, changer, changed FROM returns
|
||||
WHERE "ID" = $1 LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetReturn(ctx context.Context, id int64) (Return, error) {
|
||||
row := q.db.QueryRowContext(ctx, getReturn, id)
|
||||
var i Return
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.PersonID,
|
||||
&i.ProviderID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Category,
|
||||
&i.Email,
|
||||
&i.Status,
|
||||
&i.Creator,
|
||||
&i.Created,
|
||||
&i.Changer,
|
||||
&i.Changed,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listReturns = `-- name: ListReturns :many
|
||||
SELECT "ID", "personID", "providerID", name, description, category, email, status, creator, created, changer, changed FROM returns
|
||||
ORDER BY name
|
||||
LIMIT $1
|
||||
OFFSET $2
|
||||
`
|
||||
|
||||
type ListReturnsParams struct {
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListReturns(ctx context.Context, arg ListReturnsParams) ([]Return, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listReturns, arg.Limit, arg.Offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Return{}
|
||||
for rows.Next() {
|
||||
var i Return
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.PersonID,
|
||||
&i.ProviderID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Category,
|
||||
&i.Email,
|
||||
&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 updateReturn = `-- name: UpdateReturn :one
|
||||
UPDATE returns
|
||||
SET
|
||||
"personID" = COALESCE($1, "personID"),
|
||||
"providerID" = COALESCE($2, "providerID"),
|
||||
"name" = COALESCE($3, "name"),
|
||||
"description" = COALESCE($4, "description"),
|
||||
"category" = COALESCE($5, "category"),
|
||||
"email" = COALESCE($6, "email"),
|
||||
"status" = COALESCE($7, "status"),
|
||||
changer = $8,
|
||||
changed = now()
|
||||
WHERE "ID" = $9
|
||||
RETURNING "ID", "personID", "providerID", name, description, category, email, status, creator, created, changer, changed
|
||||
`
|
||||
|
||||
type UpdateReturnParams struct {
|
||||
Personid sql.NullInt64 `json:"personid"`
|
||||
Providerid sql.NullInt64 `json:"providerid"`
|
||||
Name sql.NullString `json:"name"`
|
||||
Description sql.NullString `json:"description"`
|
||||
Category sql.NullString `json:"category"`
|
||||
Email sql.NullString `json:"email"`
|
||||
Status sql.NullString `json:"status"`
|
||||
Changer string `json:"changer"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateReturn(ctx context.Context, arg UpdateReturnParams) (Return, error) {
|
||||
row := q.db.QueryRowContext(ctx, updateReturn,
|
||||
arg.Personid,
|
||||
arg.Providerid,
|
||||
arg.Name,
|
||||
arg.Description,
|
||||
arg.Category,
|
||||
arg.Email,
|
||||
arg.Status,
|
||||
arg.Changer,
|
||||
arg.ID,
|
||||
)
|
||||
var i Return
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.PersonID,
|
||||
&i.ProviderID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Category,
|
||||
&i.Email,
|
||||
&i.Status,
|
||||
&i.Creator,
|
||||
&i.Created,
|
||||
&i.Changer,
|
||||
&i.Changed,
|
||||
)
|
||||
return i, err
|
||||
}
|
127
db/sqlc/return_test.go
Normal file
127
db/sqlc/return_test.go
Normal file
@ -0,0 +1,127 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/itsscb/df/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func createRandomReturn(t *testing.T) Return {
|
||||
|
||||
person := createRandomPerson(t)
|
||||
provider := createRandomProvider(t)
|
||||
|
||||
arg := CreateReturnParams{
|
||||
Personid: person.ID,
|
||||
Providerid: provider.ID,
|
||||
Status: util.RandomString(7),
|
||||
Name: util.RandomUser(),
|
||||
Description: util.RandomString(30),
|
||||
Category: util.RandomUser(),
|
||||
Email: util.RandomUser(),
|
||||
Creator: util.RandomUser(),
|
||||
}
|
||||
|
||||
ret, err := testQueries.CreateReturn(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, ret)
|
||||
|
||||
require.Equal(t, arg.Name, ret.Name)
|
||||
require.Equal(t, arg.Personid, ret.PersonID)
|
||||
require.Equal(t, arg.Providerid, ret.ProviderID)
|
||||
require.Equal(t, arg.Description, ret.Description)
|
||||
require.Equal(t, arg.Category, ret.Category)
|
||||
require.Equal(t, arg.Status, ret.Status)
|
||||
require.Equal(t, arg.Email, ret.Email)
|
||||
require.Equal(t, arg.Creator, ret.Creator)
|
||||
|
||||
require.NotZero(t, ret.ID)
|
||||
require.NotZero(t, ret.Created)
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func TestCreateReturn(t *testing.T) {
|
||||
createRandomReturn(t)
|
||||
}
|
||||
|
||||
func TestGetReturn(t *testing.T) {
|
||||
newReturn := createRandomReturn(t)
|
||||
require.NotEmpty(t, newReturn)
|
||||
|
||||
ret, err := testQueries.GetReturn(context.Background(), newReturn.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, ret)
|
||||
|
||||
require.Equal(t, newReturn.Name, ret.Name)
|
||||
require.Equal(t, newReturn.PersonID, ret.PersonID)
|
||||
require.Equal(t, newReturn.ProviderID, ret.ProviderID)
|
||||
require.Equal(t, newReturn.Status, ret.Status)
|
||||
require.Equal(t, newReturn.Description, ret.Description)
|
||||
require.Equal(t, newReturn.Category, ret.Category)
|
||||
require.Equal(t, newReturn.Email, ret.Email)
|
||||
require.Equal(t, newReturn.Created, ret.Created)
|
||||
require.Equal(t, newReturn.Changer, ret.Changer)
|
||||
require.Equal(t, newReturn.Changed, ret.Changed)
|
||||
require.Equal(t, newReturn.Creator, ret.Creator)
|
||||
|
||||
require.WithinDuration(t, newReturn.Created, ret.Created, time.Second)
|
||||
}
|
||||
|
||||
func TestDeleteReturn(t *testing.T) {
|
||||
ret1 := createRandomReturn(t)
|
||||
err := testQueries.DeleteReturn(context.Background(), ret1.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
ret2, err := testQueries.GetReturn(context.Background(), ret1.ID)
|
||||
require.Error(t, err)
|
||||
require.EqualError(t, err, sql.ErrNoRows.Error())
|
||||
require.Empty(t, ret2)
|
||||
}
|
||||
|
||||
func TestUpdateReturn(t *testing.T) {
|
||||
ret1 := createRandomReturn(t)
|
||||
require.NotEmpty(t, ret1)
|
||||
|
||||
newName := util.RandomString(15)
|
||||
|
||||
arg := UpdateReturnParams{
|
||||
ID: ret1.ID,
|
||||
Name: sql.NullString{
|
||||
String: newName,
|
||||
Valid: true,
|
||||
},
|
||||
}
|
||||
|
||||
ret2, err := testQueries.UpdateReturn(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, ret2)
|
||||
|
||||
require.Equal(t, ret1.ID, ret2.ID)
|
||||
require.Equal(t, ret2.Name, newName)
|
||||
require.Equal(t, ret2.Description, ret1.Description)
|
||||
require.NotEqual(t, ret1.Name, ret2.Name)
|
||||
}
|
||||
|
||||
func TestListReturns(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
createRandomReturn(t)
|
||||
}
|
||||
|
||||
arg := ListReturnsParams{
|
||||
Limit: 5,
|
||||
Offset: 5,
|
||||
}
|
||||
|
||||
rets, err := testQueries.ListReturns(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rets, 5)
|
||||
|
||||
for _, ret := range rets {
|
||||
require.NotEmpty(t, ret)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user