Skip to content
This repository was archived by the owner on Oct 31, 2021. It is now read-only.

Commit d71897b

Browse files
committed
Getting real tests working.
1 parent 3b546bf commit d71897b

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

.development.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
POSTGRES_PASSWORD=
22
POSTGRES_USER=postgres
33
POSTGRES_DB=postgres
4-
POSTGRES_HOST_AUTH_METHOD=trust
4+
POSTGRES_HOST_AUTH_METHOD=trust
5+
POSTGRES_HOST=localhost

pkg/models/registration.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Registration struct {
2222
RegistrationId string `json:"-" pg:"registration_id,notnull,pk,type:'uuid',default:uuid_generate_v4()"`
2323
LoginId uint64 `json:"loginId" pg:"login_id,notnull,on_delete:CASCADE"`
2424
Login *Login `json:"login,omitempty" pg:"rel:has-one"`
25-
IsComplete bool `json:"isComplete" pg:"is_complete,notnull,use-zero"`
25+
IsComplete bool `json:"isComplete" pg:"is_complete,notnull,use_zero"`
2626
DateCreated time.Time `json:"-" pg:"date_created,notnull"`
2727
DateExpires time.Time `json:"-" pg:"date_expires,notnull"`
2828
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package repository
2+
3+
import (
4+
"github.com/harderthanitneedstobe/rest-api/v0/pkg/testutils"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
"time"
8+
)
9+
10+
func GetTestUnauthenticatedRepository(t *testing.T) UnauthenticatedRepository {
11+
txn := testutils.GetPgDatabaseTxn(t)
12+
return NewUnauthenticatedRepository(txn)
13+
}
14+
15+
func TestUnauthenticatedRepo_CreateAccount(t *testing.T) {
16+
t.Run("simple", func(t *testing.T) {
17+
repo := GetTestUnauthenticatedRepository(t)
18+
account, err := repo.CreateAccount(time.UTC)
19+
assert.NoError(t, err, "should successfully create account")
20+
assert.NotEmpty(t, account, "new account should not be empty")
21+
})
22+
}

pkg/testutils/database.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package testutils
2+
3+
import (
4+
"context"
5+
"github.com/go-pg/pg/v10"
6+
"github.com/stretchr/testify/require"
7+
"os"
8+
"testing"
9+
)
10+
11+
func GetPgDatabaseTxn(t *testing.T) *pg.Tx {
12+
options := &pg.Options{
13+
Network: "tcp",
14+
Addr: os.Getenv("POSTGRES_HOST") + ":5432",
15+
User: os.Getenv("POSTGRES_USER"),
16+
Password: os.Getenv("POSTGRES_PASSWORD"),
17+
Database: os.Getenv("POSTGRES_DB"),
18+
ApplicationName: "harder - api - " + t.Name(),
19+
}
20+
db := pg.Connect(options)
21+
22+
require.NoError(t, db.Ping(context.Background()), "must ping database")
23+
24+
txn, err := db.Begin()
25+
require.NoError(t, err, "must begin transaction")
26+
27+
t.Cleanup(func() {
28+
require.NoError(t, txn.Rollback(), "must rollback database transaction")
29+
require.NoError(t, db.Close(), "must close database connection")
30+
})
31+
32+
return txn
33+
}

schema/00000000_Initial.up.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ CREATE EXTENSION IF NOT EXISTS "citext";
44

55
CREATE TABLE IF NOT EXISTS "logins" ("login_id" bigserial NOT NULL, "email" text NOT NULL UNIQUE, "password_hash" text NOT NULL, "phone_number" text, "is_enabled" boolean NOT NULL, "is_email_verified" boolean NOT NULL, "is_phone_verified" boolean NOT NULL, PRIMARY KEY ("login_id"), UNIQUE ("email"));
66

7-
CREATE TABLE IF NOT EXISTS "registrations" ("registration_id" uuid NOT NULL DEFAULT 'uuid_generate_v4()', "login_id" bigint NOT NULL, "date_created" timestamptz NOT NULL, "date_expires" timestamptz NOT NULL, PRIMARY KEY ("registration_id"), FOREIGN KEY ("login_id") REFERENCES "logins" ("login_id") ON DELETE CASCADE);
7+
CREATE TABLE IF NOT EXISTS "registrations" ("registration_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "login_id" bigint NOT NULL, "is_complete" boolean NOT NULL, "date_created" timestamptz NOT NULL, "date_expires" timestamptz NOT NULL, PRIMARY KEY ("registration_id"), FOREIGN KEY ("login_id") REFERENCES "logins" ("login_id") ON DELETE CASCADE);
88

99
CREATE TABLE IF NOT EXISTS "email_verifications" ("email_verification_id" bigserial NOT NULL, "login_id" bigint NOT NULL, "email_address" text NOT NULL, "is_verified" boolean NOT NULL, "created_at" timestamptz NOT NULL DEFAULT now(), "expires_at" timestamptz NOT NULL, "verified_at" timestamptz, PRIMARY KEY ("email_verification_id"), UNIQUE ("login_id", "email_address"), FOREIGN KEY ("login_id") REFERENCES "logins" ("login_id") ON DELETE CASCADE);
1010

1111
CREATE TABLE IF NOT EXISTS "phone_verifications" ("phone_verification_id" bigserial NOT NULL, "login_id" bigint NOT NULL, "code" text NOT NULL, "phone_number" text NOT NULL, "is_verified" boolean NOT NULL, "created_at" timestamptz NOT NULL DEFAULT now(), "expires_at" timestamptz NOT NULL, "verified_at" timestamptz, PRIMARY KEY ("phone_verification_id"), UNIQUE ("login_id", "code"), UNIQUE ("login_id", "phone_number"), FOREIGN KEY ("login_id") REFERENCES "logins" ("login_id") ON DELETE CASCADE);
1212

1313
CREATE TABLE IF NOT EXISTS "accounts" ("account_id" bigserial NOT NULL, "timezone" text NOT NULL DEFAULT 'UTC', PRIMARY KEY ("account_id"));
1414

15-
CREATE TABLE IF NOT EXISTS "users" ("user_id" bigserial NOT NULL, "login_id" bigint NOT NULL, "account_id" bigint NOT NULL, "stripe_customer_id" text, "first_name" text NOT NULL, "last_name" text, PRIMARY KEY ("user_id"), FOREIGN KEY ("login_id") REFERENCES "logins" ("login_id") ON DELETE CASCADE, FOREIGN KEY ("account_id") REFERENCES "accounts" ("account_id") ON DELETE CASCADE);
15+
CREATE TABLE IF NOT EXISTS "users" ("user_id" bigserial NOT NULL, "login_id" bigint NOT NULL, "account_id" bigint NOT NULL, "stripe_customer_id" text, "first_name" text NOT NULL, "last_name" text, PRIMARY KEY ("user_id"), FOREIGN KEY ("account_id") REFERENCES "accounts" ("account_id") ON DELETE CASCADE, FOREIGN KEY ("login_id") REFERENCES "logins" ("login_id") ON DELETE CASCADE);
1616

17-
CREATE TABLE IF NOT EXISTS "links" ("link_id" bigserial NOT NULL, "account_id" bigint NOT NULL, "plaid_item_id" text NOT NULL, "plaid_access_token" text NOT NULL, "plaid_products" text[] NOT NULL, "webhook_url" text, "institution_id" text NOT NULL, "institution_name" text NOT NULL, "custom_institution_name" text, "created_by_user_id" bigint NOT NULL, PRIMARY KEY ("link_id", "account_id"), FOREIGN KEY ("created_by_user_id") REFERENCES "users" ("user_id") ON DELETE CASCADE, FOREIGN KEY ("account_id") REFERENCES "accounts" ("account_id") ON DELETE CASCADE);
17+
CREATE TABLE IF NOT EXISTS "links" ("link_id" bigserial NOT NULL, "account_id" bigint NOT NULL, "plaid_item_id" text NOT NULL, "plaid_access_token" text NOT NULL, "plaid_products" text[] NOT NULL, "webhook_url" text, "institution_id" text NOT NULL, "institution_name" text NOT NULL, "custom_institution_name" text, "created_by_user_id" bigint NOT NULL, PRIMARY KEY ("link_id", "account_id"), FOREIGN KEY ("account_id") REFERENCES "accounts" ("account_id") ON DELETE CASCADE, FOREIGN KEY ("created_by_user_id") REFERENCES "users" ("user_id") ON DELETE CASCADE);
1818

1919
CREATE TABLE IF NOT EXISTS "bank_accounts" ("bank_account_id" bigserial NOT NULL, "account_id" bigserial NOT NULL, "link_id" bigint NOT NULL, "plaid_account_id" text NOT NULL, "available_balance" bigint NOT NULL, "current_balance" bigint NOT NULL, "mask" text NOT NULL, "name" text, "original_name" text NOT NULL, "official_name" text NOT NULL, "account_type" text NOT NULL, "account_sub_type" text NOT NULL, PRIMARY KEY ("bank_account_id", "account_id"), FOREIGN KEY ("account_id") REFERENCES "accounts" ("account_id") ON DELETE CASCADE, FOREIGN KEY ("link_id", "account_id") REFERENCES "links" ("link_id", "account_id") ON DELETE CASCADE);
2020

0 commit comments

Comments
 (0)