package container

import (
	"fmt"
	"github.com/go-xorm/xorm"
	"github.com/kataras/iris/v12/context"
	"testing"
)

func TestRequestContext_New(t *testing.T) {
	requestId := "requestId"
	sessionContext, err := NewRequestContext(requestId, &context.Context{}, &xorm.Session{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if sessionContext == nil {
		t.Fatalf("创建失败!")
	}
	if sessionContext.requestId != requestId {
		t.Fatalf("用户标识错误!")
	}
}

func TestRequestContext_New_EmptyUserId(t *testing.T) {
	requestId := ""
	_, err := NewRequestContext(requestId, &context.Context{}, &xorm.Session{})
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("请求标识不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestRequestContext_New_NullUser(t *testing.T) {
	requestId := "requestId"
	_, err := NewRequestContext(requestId, nil, &xorm.Session{})
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("Web上下文不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestRequestContext_New_NullTransactionHandlerFactory(t *testing.T) {
	requestId := "requestId"
	_, err := NewRequestContext(requestId, &context.Context{}, nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("XORM会话不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestRequestContext_RequestId(t *testing.T) {
	requestId := "requestId1"
	sessionContext, err := NewRequestContext(requestId, &context.Context{}, &xorm.Session{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	requestId = "requestId2"
	sessionContext.requestId = requestId
	if sessionContext.RequestId() != requestId {
		t.Fatalf("获取请求标识错误!")
	}
}
