resolved reqpreprocessing review comments
1. added logs for contextKey parsing and unmarshaling yml file. 2. resolved router test case issue. 2. resolved logger test case issues.
This commit is contained in:
@@ -3,8 +3,8 @@ package model
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v2"
|
||||
@@ -198,3 +198,54 @@ func TestSchemaValidationErr_BecknError_NoErrors(t *testing.T) {
|
||||
t.Errorf("beErr.Code = %s, want %s", beErr.Code, expectedCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseContextKey_ValidKeys(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected ContextKey
|
||||
}{
|
||||
{"message_id", ContextKeyMsgID},
|
||||
{"subscriber_id", ContextKeySubscriberID},
|
||||
{"model_id", ContextKeyModelID},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
key, err := ParseContextKey(tt.input)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for input %s: %v", tt.input, err)
|
||||
}
|
||||
if key != tt.expected {
|
||||
t.Errorf("expected %s, got %s", tt.expected, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseContextKey_InvalidKey(t *testing.T) {
|
||||
_, err := ParseContextKey("invalid_key")
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid context key, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextKey_UnmarshalYAML_Valid(t *testing.T) {
|
||||
yamlData := []byte("message_id")
|
||||
var key ContextKey
|
||||
|
||||
err := yaml.Unmarshal(yamlData, &key)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if key != ContextKeyMsgID {
|
||||
t.Errorf("expected %s, got %s", ContextKeyMsgID, key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextKey_UnmarshalYAML_Invalid(t *testing.T) {
|
||||
yamlData := []byte("invalid_key")
|
||||
var key ContextKey
|
||||
|
||||
err := yaml.Unmarshal(yamlData, &key)
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid context key, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,9 @@ const (
|
||||
type ContextKey string
|
||||
|
||||
const (
|
||||
// ContextKeyTxnID is the context key used to store and retrieve the transaction ID in a request context.
|
||||
ContextKeyTxnID ContextKey = "transaction_id"
|
||||
|
||||
// ContextKeyMsgID is the context key used to store and retrieve the message ID in a request context.
|
||||
ContextKeyMsgID ContextKey = "message_id"
|
||||
|
||||
@@ -52,6 +55,38 @@ const (
|
||||
ContextKeyModelID ContextKey = "model_id"
|
||||
)
|
||||
|
||||
var contextKeys = map[string]ContextKey{
|
||||
"message_id": ContextKeyMsgID,
|
||||
"subscriber_id": ContextKeySubscriberID,
|
||||
"model_id": ContextKeyModelID,
|
||||
}
|
||||
|
||||
// ParseContextKey converts a string into a valid ContextKey.
|
||||
func ParseContextKey(v string) (ContextKey, error) {
|
||||
key, ok := contextKeys[v]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("invalid context key: %s", key)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// UnmarshalYAML ensures that only known context keys are accepted during YAML unmarshalling.
|
||||
func (k *ContextKey) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var keyStr string
|
||||
if err := unmarshal(&keyStr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsedKey, err := ParseContextKey(keyStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*k = parsedKey
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// Role defines the type of participant in the network.
|
||||
type Role string
|
||||
|
||||
|
||||
Reference in New Issue
Block a user