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

pengen beli BMW tapi kok mahal ya :) #25

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 17 additions & 34 deletions business/payment_service/get_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ func TestMain(m *testing.M) {

exitCode := m.Run()

_, err = db.Exec("DELETE FROM transaction_log")
if err != nil {
log.Printf("Deleting transaction log: %s", err.Error())
}
_, err = db.Exec("DELETE FROM virtual_accounts")
if err != nil {
log.Printf("Deleting virtual accounts: %s", err.Error())
}
_, err = db.Exec("DELETE FROM virtual_account_entries")
if err != nil {
log.Printf("Deleting virtual account entries: %s", err.Error())
}
_, err = db.Exec("DELETE FROM emoney_entries")
if err != nil {
log.Printf("Deleting emoney entries: %s", err.Error())
}
WahidinAji marked this conversation as resolved.
Show resolved Hide resolved
err = db.Close()
if err != nil {
log.Printf("Closing database: %s", err.Error())
Expand Down Expand Up @@ -124,7 +140,7 @@ func parseConfig() config {

func TestBusinessGetDetails(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), time.Minute * 5)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

transactionRepository, err := transaction.NewTransactionRepository(db)
Expand Down Expand Up @@ -175,32 +191,22 @@ func TestBusinessGetDetails(t *testing.T) {
t.Run("GetDetails should return the correct details", func(t *testing.T) {
vaNumber, err := virtualAccountRepository.CreateOrGetVirtualAccountNumber(ctx, "[email protected]")
aldy505 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
delete(ctx, db, "virtual_accounts")
t.Errorf("unexpected error: %s", err.Error())
}

orderId := "order-id"
_, err = virtualAccountRepository.CreateCharge(ctx, vaNumber, orderId, 50000, time.Now().Add(time.Hour))
aldy505 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
delete(ctx, db, "virtual_accounts")
delete(ctx, db, "virtual_account_entries")
t.Errorf("unexpected error: %s", err.Error())
}

_, err = emoneyRepository.CreateCharge(ctx, orderId, 50000, time.Now().Add(time.Hour))
WahidinAji marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
delete(ctx, db, "virtual_accounts")
delete(ctx, db, "virtual_account_entries")
delete(ctx, db, "emoney_entries")
t.Errorf("unexpected error: %s", err.Error())
}

_, err = paymentService.GetDetail(ctx, vaNumber)
aldy505 marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
delete(ctx, db, "transaction_log")
delete(ctx, db, "virtual_accounts")
delete(ctx, db, "virtual_account_entries")
delete(ctx, db, "emoney_entries")
t.Errorf("expecting error to be not nil, but got nil")
}

Expand All @@ -212,36 +218,13 @@ func TestBusinessGetDetails(t *testing.T) {
ExpiredAt: time.Now().Add(time.Hour),
})
if err != nil {
if !errors.Is(err, repository.ErrDuplicate) {
delete(ctx, db, "transaction_log")
delete(ctx, db, "virtual_account_entries")
delete(ctx, db, "virtual_accounts")
delete(ctx, db, "emoney_entries")
}
t.Errorf("expecting not error when inserting transaction, instead got %s", err.Error())
}

_, err = paymentService.GetDetail(ctx, vaNumber)
if err != nil {
delete(ctx, db, "transaction_log")
delete(ctx, db, "virtual_accounts")
delete(ctx, db, "virtual_account_entries")
delete(ctx, db, "emoney_entries")
t.Errorf("unexpected error: %s", err.Error())
}

delete(ctx, db, "transaction_log")
delete(ctx, db, "virtual_account_entries")
delete(ctx, db, "virtual_accounts")
delete(ctx, db, "emoney_entries")

})

}

func delete(ctx context.Context, db *sql.DB, table string) {
_, err := db.ExecContext(ctx, "DELETE FROM "+table)
if err != nil {
log.Printf("deleting from %s: %s", table, err.Error())
}
}
4 changes: 1 addition & 3 deletions business/payment_service/mark_as_paid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func TestMarkAsPaid(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute * 5)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

transactionRepository, err := transaction.NewTransactionRepository(db)
Expand Down Expand Up @@ -114,6 +114,4 @@ func TestMarkAsPaid(t *testing.T) {
t.Errorf("expecting error to be nil, but got %v", err)
}
})

delete(ctx, db, "transaction_log")
}