Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #7

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
go-version: 1.20.3
- name: Tests
run: go test ./tests
run: go test ./tests/...

build:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dc-down:
# Test the application
test:
@echo "Testing..."
@go test ./tests -v
@go test ./tests/... -v

# Clean the binary
clean:
Expand Down
6 changes: 5 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ func main() {
}

db, err := database.Connect()
migration := database.NewMigration(db)
if err != nil {
log.Println("Failed to connect to db")
}
migration := database.NewMigration(db, "")
if err = migration.Migrate(); err != nil {
fmt.Printf("Failed to migrate db: %s", err)
}

if err = db.Close(); err != nil {
log.Println("Failed to close db connection after migration")
}

r := router.Serve()
log.Printf("📡 Server start on port %s \n", os.Getenv("PORT"))
if err := r.Run(); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions cmd/database/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type Migration struct {
dbPool *sql.DB
dbPool *sql.DB
basePath string
}

func NewMigration(db *sql.DB) *Migration {
func NewMigration(db *sql.DB, basePath string) *Migration {
return &Migration{
db,
basePath,
}
}

Expand All @@ -26,7 +28,7 @@ func (m Migration) Migrate() error {

func (m Migration) migrateFromFile(filename string) error {
workingDir, _ := os.Getwd()
fileOpen, err := os.Open(workingDir + "/cmd/database/migrations/" + filename)
fileOpen, err := os.Open(workingDir + m.basePath + "/cmd/database/migrations/" + filename)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion dev.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WORKDIR /app
COPY . .

RUN mkdir data
RUN go mod download

RUN go mod download

CMD ["make", "watch"]
2 changes: 1 addition & 1 deletion src/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MailRequest[T interface{}] struct {
}

func NewApiController() *ApiController {
newRepository, err := repository.NewRepository()
newRepository, err := repository.NewRepository(nil)
if err != nil {
log.Printf("Repositority initialisation failed : %s", err)
}
Expand Down
24 changes: 16 additions & 8 deletions src/repository/mail.repository.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package repository

type MailQuery struct {
ID int `db:"_id"`
Date string `db:"date"`
To string `db:"to"`
Subject string `db:"subject"`
Sent bool `db:"sent"`
Error string `db:"error"`
Viewed string `db:"viewed"`
ID int `db:"_id"`
Date string `db:"date"`
To string `db:"to"`
Subject string `db:"subject"`
Sent bool `db:"sent"`
Error *string `db:"error"`
Viewed string `db:"viewed"`
}

type MailSaveWithoutError struct {
Expand Down Expand Up @@ -70,9 +70,17 @@ func (r Repository) ListMail() ([]MailQuery, error) {
}

func (r Repository) convertToToString(args []string) string {
if len(args) == 0 {
return ""
}

if len(args) == 1 {
return args[0]
}

res := ""
for _, dest := range args {
res += dest
res = res + dest + ";"
}
return res
}
22 changes: 14 additions & 8 deletions src/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ type Repository struct {
dbPool *sql.DB
}

func NewRepository() (*Repository, error) {
dbPool, err := database.Connect()
if err != nil {
return nil, err
}
func NewRepository(customSource *sql.DB) (*Repository, error) {
if customSource != nil {
return &Repository{
customSource,
}, nil
} else {
dbPool, err := database.Connect()
if err != nil {
return nil, err
}

return &Repository{
dbPool,
}, nil
return &Repository{
dbPool,
}, nil
}
}
2 changes: 1 addition & 1 deletion tests/main_test.go → tests/e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests_test
package e2e_test

import (
"mailer_ms/src/router"
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package integration_test

import (
"mailer_ms/src/controllers"
"os"
"testing"
)

func TestNewController(t *testing.T) {
err := os.Setenv("DB_URL", "../../data/mailerTest.db")
if err != nil {
t.Error("Failed to set db env")
}

a := controllers.NewApiController()
if a == nil {
t.Error("Repository is null")
}
}
151 changes: 151 additions & 0 deletions tests/integration/mailRepository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package integration_test

import (
"errors"
"mailer_ms/src/repository"
"os"
"testing"
)

func TestWithoutError(t *testing.T) {
// Setup
err := os.Setenv("DB_URL", "./data/mailer_test.db")

if err != nil {
t.Error("Failed to set db env")
}

dbPool, err := connect()
if err != nil {
t.Error("Failed to connect to db")
}

r, err := repository.NewRepository(dbPool)
if err != nil {
t.Error("repository instantiation failed")
}
if r == nil {
t.Error("Repository is null")
}

// testing method
to := []string{"[email protected]", "[email protected]"}
subject := "Test subject"

err = r.SaveWithoutError(to, subject)
if err != nil {
t.Errorf("SaveWithoutError failed: %s", err)
}
}

func TestSaveWithError(t *testing.T) {
// Setup
err := os.Setenv("DB_URL", "./data/mailer_test.db")

if err != nil {
t.Error("Failed to set db env")
}

dbPool, err := connect()
if err != nil {
t.Error("Failed to connect to db")
}

r, err := repository.NewRepository(dbPool)
if err != nil {
t.Error("repository instantiation failed")
}
if r == nil {
t.Error("Repository is null")
}

// testing method
to := []string{"[email protected]", "[email protected]"}
subject := "Test subject"

err = r.SaveWithError(to, subject, errors.New("failed"))
if err != nil {
t.Errorf("SaveWithoutError failed: %s", err)
}
}

func TestListMail(t *testing.T) {
// Setup
if err := os.Setenv("DB_URL", "./data/mailer_test.db"); err != nil {
t.Error("Failed to set db env")
}

dbPool, err := connect()
if err != nil {
t.Error("Failed to connect to db")
}

if _, err = dbPool.Exec(`DELETE FROM "mail"`); err != nil {
t.Error("Failed to reset db")
}

r, err := repository.NewRepository(dbPool)
if err != nil {
t.Error("repository instantiation failed")
}
if r == nil {
t.Error("Repository is null")
}

// >Insert some data with the 2 methods
toWithoutError := []string{"[email protected]", "[email protected]"}
subjectWithoutError := "Test subject without error"
err = r.SaveWithoutError(toWithoutError, subjectWithoutError)
if err != nil {
t.Errorf("SaveWithoutError failed: %s", err)
}
toWithError := []string{"[email protected]", "[email protected]"}
subjectWithError := "Test subject with error"

err = r.SaveWithError(toWithError, subjectWithError, errors.New("error"))
if err != nil {
t.Errorf("SaveWithError failed: %s", err)
}

// Test ListMail > must retrieve data inserted before
mails, err := r.ListMail()
if err != nil {
t.Errorf("ListMail failed: %s", err)
}

if len(mails) < 2 {
t.Error("Expected 2 min mails, got", len(mails))
}

mailWithErrIsContained := false
mailWithoutErrIsContained := false

for _, mail := range mails {
if mail.Subject == "Test subject with error" {
mailWithErrIsContained = true

if mail.To != "[email protected];[email protected];" {
t.Errorf(" Expect [email protected];[email protected];, got %v", mail.To)
}

if mail.Sent != false {
t.Errorf("false, got %v", mail.Sent)
}

} else if mail.Subject == "Test subject without error" {
mailWithoutErrIsContained = true

if mail.To != "[email protected];[email protected];" {
t.Errorf(" Expect [email protected];[email protected];, got %v", mail.To)
}

if mail.Sent != true {
t.Errorf("true, got %v", mail.Sent)
}
}
}

if !mailWithErrIsContained || !mailWithoutErrIsContained {
t.Error("Expected 2 mail inserted")
}
}
49 changes: 49 additions & 0 deletions tests/integration/repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package integration_test

import (
"database/sql"
"mailer_ms/cmd/database"
"mailer_ms/src/repository"
"os"
"testing"
)

func connect() (*sql.DB, error) {
err := os.Setenv("DB_URL", "../../data/mailerTest.db")
if err != nil {
return nil, err
}

db, errConnect := database.Connect()
if errConnect != nil {
return nil, err
}

migration := database.NewMigration(db, "/../..")
if err = migration.Migrate(); err != nil {
return nil, err
}

return db, nil
}

func TestNewRepository(t *testing.T) {
err := os.Setenv("DB_URL", "./data/mailer_test.db")

if err != nil {
t.Error("Failed to set db env")
}

dbPool, err := connect()
if err != nil {
t.Error("Failed to connect to db")
}

r, err := repository.NewRepository(dbPool)
if err != nil {
t.Error("repository instantiation failed")
}
if r == nil {
t.Error("Repository is null")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests_test
package integration_test

import (
GoMailer "mailer_ms/src/mailer"
Expand Down
Loading
Loading