Skip to content

Commit

Permalink
Chore/refactor duplicated string (#4)
Browse files Browse the repository at this point in the history
* Create FOUND_NULL_CONTEXT_ERR_MSG

* Add BUCHAREST_CONTEXT_TYPE_NAME, TEMP_ENV_PATH, CONTEXT_DONE_ERR_MSG
  • Loading branch information
sbpann committed Jun 8, 2024
1 parent 0c32adc commit 5e33436
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions bucharest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,54 @@ import (
"gorm.io/gorm"
)

const FOUND_NULL_CONTEXT_ERR_MSG = "Found null context."
const BUCHAREST_CONTEXT_TYPE_NAME = "bucharest.BuchatrestContext"
const TEMP_ENV_PATH = "/tmp/.env"
const CONTEXT_DONE_ERR_MSG = "Context is done before expected. Got %v."

func verifyContextTypeName(t *testing.T, ctx Context) {
if contextTypeName := fmt.Sprint(ctx); contextTypeName != BUCHAREST_CONTEXT_TYPE_NAME {
t.Errorf("Unexptected context type = %q, want %q.", contextTypeName, BUCHAREST_CONTEXT_TYPE_NAME)
}
}

func TestNewContextWithOptions(t *testing.T) {
ctx := NewContextWithOptions(nil)
if ctx == nil {
t.Fatalf("NewContext returned nil")
t.Fatal(FOUND_NULL_CONTEXT_ERR_MSG)
}
select {
case x := <-ctx.Done():
t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
t.Errorf(CONTEXT_DONE_ERR_MSG, x)
default:
}
if got, want := fmt.Sprint(ctx), "bucharest.BuchatrestContext"; got != want {
t.Errorf("NewContextWithOptions().String() = %q want %q", got, want)
}
verifyContextTypeName(t, ctx)
}

func TestNewContextWithOptionsFromParentContext(t *testing.T) {
ctx := NewContextWithOptions(&ContextOptions{Parent: context.Background()})
if ctx == nil {
t.Fatalf("NewContext returned nil")
t.Fatal(FOUND_NULL_CONTEXT_ERR_MSG)
}
select {
case x := <-ctx.Done():
t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
t.Errorf(CONTEXT_DONE_ERR_MSG, x)
default:
}
if got, want := fmt.Sprint(ctx), "bucharest.BuchatrestContext"; got != want {
t.Errorf("NewContextWithOptions().String() = %q want %q", got, want)
}
verifyContextTypeName(t, ctx)
}

func TestNewContextWithOptionsWithNoAddtionalOptions(t *testing.T) {
ctx := NewContextWithOptions(&ContextOptions{Parent: context.Background()})
if ctx == nil {
t.Fatalf("NewContext returned nil")
t.Fatal(FOUND_NULL_CONTEXT_ERR_MSG)
}
select {
case x := <-ctx.Done():
t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
t.Errorf(CONTEXT_DONE_ERR_MSG, x)
default:
}
if got, want := fmt.Sprint(ctx), "bucharest.BuchatrestContext"; got != want {
t.Errorf("NewContextWithOptions().String() = %q want %q", got, want)
}
verifyContextTypeName(t, ctx)

utils.AssertPanic(t, func() { ctx.ENV() }, ErrNoENV)
utils.AssertPanic(t, func() { ctx.GORM() }, ErrNoGORM)
Expand Down Expand Up @@ -101,15 +106,15 @@ func TestNewContextWithOptionsWithAllAddtionalOptions(t *testing.T) {
SQLX: sqlx,
})
if ctx == nil {
t.Fatalf("NewContext returned nil")
t.Fatal(FOUND_NULL_CONTEXT_ERR_MSG)
}
select {
case x := <-ctx.Done():
t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
default:
}
if got, want := fmt.Sprint(ctx), "bucharest.BuchatrestContext"; got != want {
t.Errorf("NewContextWithOptions().String() = %q want %q", got, want)
if contextTypeName := fmt.Sprint(ctx); contextTypeName != BUCHAREST_CONTEXT_TYPE_NAME {
t.Errorf("NewContextWithOptions().String() = %q want %q", contextTypeName, BUCHAREST_CONTEXT_TYPE_NAME)
}

assert.Same(t, env, ctx.ENV())
Expand All @@ -124,8 +129,7 @@ func TestNewContextWithOptionsWithAllAddtionalOptions(t *testing.T) {

func TestUpdateContext(t *testing.T) {
// prepare env
envTempPath := "/tmp/.env"
tempEnvFile, err := os.Create(envTempPath)
tempEnvFile, err := os.Create(TEMP_ENV_PATH)
assert.NoError(t, err)
assert.NotEmpty(t, tempEnvFile)

Expand All @@ -135,7 +139,7 @@ func TestUpdateContext(t *testing.T) {
err = tempEnvFile.Close()
assert.NoError(t, err)

env, err := NewENV(envTempPath)
env, err := NewENV(TEMP_ENV_PATH)
assert.NoError(t, err)
assert.NotNil(t, env)

Expand All @@ -148,24 +152,21 @@ func TestUpdateContext(t *testing.T) {
SQL: &sql.DB{},
SQLX: &sqlx.DB{},
})
err = os.Remove(envTempPath)
err = os.Remove(TEMP_ENV_PATH)
assert.NoError(t, err)

if ctx == nil {
t.Fatalf("NewContext returned nil")
t.Fatal(FOUND_NULL_CONTEXT_ERR_MSG)
}
select {
case x := <-ctx.Done():
t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
t.Errorf(CONTEXT_DONE_ERR_MSG, x)
default:
}
if got, want := fmt.Sprint(ctx), "bucharest.BuchatrestContext"; got != want {
t.Errorf("NewContextWithOptions().String() = %q want %q", got, want)
}
verifyContextTypeName(t, ctx)

// prepare env
envTempPath = "/tmp/.env"
tempEnvFile, err = os.Create(envTempPath)
tempEnvFile, err = os.Create(TEMP_ENV_PATH)
assert.NoError(t, err)
assert.NotEmpty(t, tempEnvFile)

Expand All @@ -175,7 +176,7 @@ func TestUpdateContext(t *testing.T) {
err = tempEnvFile.Close()
assert.NoError(t, err)

newENV, err := NewENV(envTempPath)
newENV, err := NewENV(TEMP_ENV_PATH)
assert.NoError(t, err)
assert.NotNil(t, newENV)

Expand All @@ -186,7 +187,7 @@ func TestUpdateContext(t *testing.T) {
sqlx := &sqlx.DB{}

assert.NotSame(t, newENV, ctx.ENV())
err = os.Remove(envTempPath)
err = os.Remove(TEMP_ENV_PATH)
assert.NoError(t, err)
assert.NotSame(t, gorm, ctx.GORM())
assert.NotSame(t, logrus, ctx.Log())
Expand Down

0 comments on commit 5e33436

Please sign in to comment.