parent
313d539248
commit
d405a12ec0
@ -1,8 +1,8 @@
|
||||
CREATE TABLE "mails" (
|
||||
"ID" bigserial UNIQUE PRIMARY KEY NOT NULL,
|
||||
"from" varchar NOT NULL,
|
||||
"to" varchar NOT NULL,
|
||||
"cc" varchar,
|
||||
"to" varchar[] NOT NULL,
|
||||
"cc" varchar[],
|
||||
"timestamp" timestamptz NOT NULL DEFAULT (now()),
|
||||
"subject" varchar NOT NULL,
|
||||
"body" text NOT NULL,
|
||||
|
@ -38,7 +38,7 @@ func createRandomDocumentUpload(t *testing.T) Document {
|
||||
require.Equal(t, arg.Url, document.Url)
|
||||
require.Equal(t, arg.Creator, document.Creator)
|
||||
require.Equal(t, arg.Changer, document.Changer)
|
||||
require.Equal(t, false, document.Valid)
|
||||
require.Equal(t, document.Valid, false)
|
||||
require.Zero(t, document.ValidatedBy)
|
||||
require.Zero(t, document.ValidDate)
|
||||
|
||||
@ -48,6 +48,42 @@ func createRandomDocumentUpload(t *testing.T) Document {
|
||||
return document
|
||||
}
|
||||
|
||||
func TestCreateDocumentMail(t *testing.T) {
|
||||
mail := createRandomMail(t)
|
||||
require.NotEmpty(t, mail)
|
||||
|
||||
arg := CreateDocumentMailParams{
|
||||
MailID: sql.NullInt64{
|
||||
Valid: true,
|
||||
Int64: mail.ID,
|
||||
},
|
||||
Name: util.RandomUser(),
|
||||
Type: util.RandomUser(),
|
||||
Path: util.RandomString(50),
|
||||
Url: util.RandomString(60),
|
||||
Creator: util.RandomUser(),
|
||||
Changer: util.RandomUser(),
|
||||
}
|
||||
|
||||
document, err := testQueries.CreateDocumentMail(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, document)
|
||||
|
||||
require.Equal(t, arg.MailID, document.MailID)
|
||||
require.Equal(t, arg.Name, document.Name)
|
||||
require.Equal(t, arg.Type, document.Type)
|
||||
require.Equal(t, arg.Path, document.Path)
|
||||
require.Equal(t, arg.Url, document.Url)
|
||||
require.Equal(t, arg.Creator, document.Creator)
|
||||
require.Equal(t, arg.Changer, document.Changer)
|
||||
require.Equal(t, document.Valid, false)
|
||||
require.Zero(t, document.ValidatedBy)
|
||||
require.Zero(t, document.ValidDate)
|
||||
|
||||
require.NotZero(t, document.ID)
|
||||
require.NotZero(t, document.Created)
|
||||
}
|
||||
|
||||
func TestCreateDocumentUpload(t *testing.T) {
|
||||
createRandomDocumentUpload(t)
|
||||
}
|
||||
|
@ -7,8 +7,9 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
const createMail = `-- name: CreateMail :one
|
||||
@ -29,21 +30,21 @@ RETURNING "ID", "from", "to", cc, timestamp, subject, body, creator, created, ch
|
||||
`
|
||||
|
||||
type CreateMailParams struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Cc sql.NullString `json:"cc"`
|
||||
Subject string `json:"subject"`
|
||||
Body string `json:"body"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Creator string `json:"creator"`
|
||||
Changer string `json:"changer"`
|
||||
From string `json:"from"`
|
||||
To []string `json:"to"`
|
||||
Cc []string `json:"cc"`
|
||||
Subject string `json:"subject"`
|
||||
Body string `json:"body"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Creator string `json:"creator"`
|
||||
Changer string `json:"changer"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMail(ctx context.Context, arg CreateMailParams) (Mail, error) {
|
||||
row := q.db.QueryRowContext(ctx, createMail,
|
||||
arg.From,
|
||||
arg.To,
|
||||
arg.Cc,
|
||||
pq.Array(arg.To),
|
||||
pq.Array(arg.Cc),
|
||||
arg.Subject,
|
||||
arg.Body,
|
||||
arg.Timestamp,
|
||||
@ -54,8 +55,8 @@ func (q *Queries) CreateMail(ctx context.Context, arg CreateMailParams) (Mail, e
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.From,
|
||||
&i.To,
|
||||
&i.Cc,
|
||||
pq.Array(&i.To),
|
||||
pq.Array(&i.Cc),
|
||||
&i.Timestamp,
|
||||
&i.Subject,
|
||||
&i.Body,
|
||||
@ -104,8 +105,8 @@ func (q *Queries) GetMail(ctx context.Context, id int64) (Mail, error) {
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.From,
|
||||
&i.To,
|
||||
&i.Cc,
|
||||
pq.Array(&i.To),
|
||||
pq.Array(&i.Cc),
|
||||
&i.Timestamp,
|
||||
&i.Subject,
|
||||
&i.Body,
|
||||
@ -141,8 +142,8 @@ func (q *Queries) ListMails(ctx context.Context, arg ListMailsParams) ([]Mail, e
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.From,
|
||||
&i.To,
|
||||
&i.Cc,
|
||||
pq.Array(&i.To),
|
||||
pq.Array(&i.Cc),
|
||||
&i.Timestamp,
|
||||
&i.Subject,
|
||||
&i.Body,
|
||||
|
116
db/sqlc/mail_test.go
Normal file
116
db/sqlc/mail_test.go
Normal file
@ -0,0 +1,116 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/itsscb/df/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func createRandomMail(t *testing.T) Mail {
|
||||
|
||||
arg := CreateMailParams{
|
||||
From: util.RandomEmail(),
|
||||
To: []string{util.RandomEmail()},
|
||||
Cc: []string{util.RandomEmail()},
|
||||
Subject: util.RandomString(20),
|
||||
Body: util.RandomString(300),
|
||||
Timestamp: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
Creator: util.RandomUser(),
|
||||
Changer: util.RandomUser(),
|
||||
}
|
||||
|
||||
mail, err := testQueries.CreateMail(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, mail)
|
||||
|
||||
require.Equal(t, arg.From, mail.From)
|
||||
require.Equal(t, arg.To, mail.To)
|
||||
require.Equal(t, arg.Subject, mail.Subject)
|
||||
require.Equal(t, arg.Body, mail.Body)
|
||||
require.Equal(t, arg.Timestamp, mail.Timestamp)
|
||||
require.Equal(t, arg.Creator, mail.Creator)
|
||||
require.Equal(t, arg.Changer, mail.Changer)
|
||||
|
||||
require.NotZero(t, mail.ID)
|
||||
require.NotZero(t, mail.Created)
|
||||
|
||||
return mail
|
||||
}
|
||||
|
||||
func TestCreateMail(t *testing.T) {
|
||||
createRandomMail(t)
|
||||
}
|
||||
|
||||
func TestGetMail(t *testing.T) {
|
||||
newmail := createRandomMail(t)
|
||||
require.NotEmpty(t, newmail)
|
||||
|
||||
mail, err := testQueries.GetMail(context.Background(), newmail.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, mail)
|
||||
|
||||
require.Equal(t, newmail.From, mail.From)
|
||||
require.Equal(t, newmail.To, mail.To)
|
||||
require.Equal(t, newmail.Subject, mail.Subject)
|
||||
require.Equal(t, newmail.Body, mail.Body)
|
||||
require.Equal(t, newmail.Timestamp, mail.Timestamp)
|
||||
require.Equal(t, newmail.Creator, mail.Creator)
|
||||
require.Equal(t, newmail.Changer, mail.Changer)
|
||||
|
||||
require.WithinDuration(t, newmail.Created, mail.Created, time.Second)
|
||||
}
|
||||
|
||||
func TestDeleteMail(t *testing.T) {
|
||||
mail1 := createRandomMail(t)
|
||||
err := testQueries.DeleteMail(context.Background(), mail1.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
mail2, err := testQueries.GetMail(context.Background(), mail1.ID)
|
||||
require.Error(t, err)
|
||||
require.EqualError(t, err, sql.ErrNoRows.Error())
|
||||
require.Empty(t, mail2)
|
||||
}
|
||||
|
||||
/* func TestUpdateMail(t *testing.T) {
|
||||
mail1 := createRandomMail(t)
|
||||
require.NotEmpty(t, mail1)
|
||||
|
||||
arg := UpdateMailParams{
|
||||
ID: mail1.ID,
|
||||
Name: sql.NullString{
|
||||
String: util.RandomString(50),
|
||||
Valid: true,
|
||||
},
|
||||
}
|
||||
|
||||
mail2, err := testQueries.UpdateMail(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, mail2)
|
||||
|
||||
require.Equal(t, mail1.ID, mail2.ID)
|
||||
require.Equal(t, mail1.Path, mail2.Path)
|
||||
require.NotEqual(t, mail1.Name, mail2.Name)
|
||||
} */
|
||||
|
||||
func TestListMails(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
createRandomMail(t)
|
||||
}
|
||||
|
||||
arg := ListMailsParams{
|
||||
Limit: 5,
|
||||
Offset: 5,
|
||||
}
|
||||
|
||||
mails, err := testQueries.ListMails(context.Background(), arg)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, mails, 5)
|
||||
|
||||
for _, mail := range mails {
|
||||
require.NotEmpty(t, mail)
|
||||
}
|
||||
}
|
@ -51,17 +51,17 @@ type Document struct {
|
||||
}
|
||||
|
||||
type Mail struct {
|
||||
ID int64 `json:"ID"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Cc sql.NullString `json:"cc"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Subject string `json:"subject"`
|
||||
Body string `json:"body"`
|
||||
Creator string `json:"creator"`
|
||||
Created time.Time `json:"created"`
|
||||
Changer string `json:"changer"`
|
||||
Changed time.Time `json:"changed"`
|
||||
ID int64 `json:"ID"`
|
||||
From string `json:"from"`
|
||||
To []string `json:"to"`
|
||||
Cc []string `json:"cc"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Subject string `json:"subject"`
|
||||
Body string `json:"body"`
|
||||
Creator string `json:"creator"`
|
||||
Created time.Time `json:"created"`
|
||||
Changer string `json:"changer"`
|
||||
Changed time.Time `json:"changed"`
|
||||
}
|
||||
|
||||
type Payment struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user