Загрузка данных


package logger

import (
	"context"
	"testing"
	"time"
)

func TestSetAndGetServiceName(t *testing.T) {
	expected := "test-service"
	SetServiceName(expected)

	got := GetServiceName()
	if got != expected {
		t.Errorf("expected service name %q, got %q", expected, got)
	}
}

func TestGetRequestDataFromCtx(t *testing.T) {
	t.Run("success scenario: data exists in context", func(t *testing.T) {
		expectedDTO := &RequestDTO{
			Method:    "GET",
			URL:       "/test",
			StartTime: time.Now(),
			RequestID: "123",
			Cached:    false,
		}

		ctx := context.WithValue(context.Background(), RequestContextKey, expectedDTO)

		got, err := GetRequestDataFromCtx(ctx)
		if err != nil {
			t.Fatalf("expected successful extraction from context, got error: %v", err)
		}

		if got.RequestID != expectedDTO.RequestID {
			t.Errorf("expected RequestID %q, got %q", expectedDTO.RequestID, got.RequestID)
		}
		if got.Method != expectedDTO.Method {
			t.Errorf("expected Method %q, got %q", expectedDTO.Method, got.Method)
		}
	})

	t.Run("error scenario: missing data in context", func(t *testing.T) {
		ctx := context.Background()

		got, err := GetRequestDataFromCtx(ctx)
		if err == nil {
			t.Error("expected error due to missing context data, but got err == nil")
		}
		if got != nil {
			t.Errorf("expected nil result, got %#v", got)
		}
	})
}

func TestLoggerInitialization(t *testing.T) {
	t.Run("Get returns valid logger instance", func(t *testing.T) {
		log := Get()
		if log == nil {
			t.Fatal("Get() returned nil")
		}
	})

	t.Run("LogWithUserData correctly creates logrus.Entry", func(t *testing.T) {
		ctx := context.Background()
		entry := LogWithUserData(ctx)
		if entry == nil {
			t.Fatal("LogWithUserData() returned nil")
		}
	})
}