stakwork / sphinx-tribes

Backend for sphinx tribes and bounties. The bounty platform pays out in bitcoin. Sign up with Sphinx Chat, complete a bounty, and earn bitcoin! Go to our website for available bounties.
https://community.sphinx.chat/bounties
33 stars 60 forks source link

[Unit Tests] - ProcessWorkflowRequest #2009

Open tomsmith8 opened 5 hours ago

tomsmith8 commented 5 hours ago

Unit Test Coverage for " ProcessWorkflowRequest"


Stakwork Run


Unit Test Code


package workflow_processor_test

import (
    "testing"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/mock"
    "github.com/google/uuid"
    "workflow_processor"
)

// Mock for the lookupProcessingConfig function
type MockConfigLookup struct {
    mock.Mock
}

func (m *MockConfigLookup) lookupProcessingConfig(source string) error {
    args := m.Called(source)
    return args.Error(0)
}

// Mock for the processWithHandler function
type MockHandlerProcessor struct {
    mock.Mock
}

func (m *MockHandlerProcessor) processWithHandler(requestID string) (string, error) {
    args := m.Called(requestID)
    return args.String(0), args.Error(1)
}

func TestProcessWorkflowRequest_ValidRequestIDAndSource(t *testing.T) {
    mockConfig := new(MockConfigLookup)
    mockHandler := new(MockHandlerProcessor)

    mockConfig.On("lookupProcessingConfig", "validSource").Return(nil)
    mockHandler.On("processWithHandler", "12345").Return("12345", nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("12345", "validSource")

    assert.NoError(t, err)
    assert.Equal(t, "12345", requestID)
}

func TestProcessWorkflowRequest_EmptyRequestIDWithValidSource(t *testing.T) {
    mockConfig := new(MockConfigLookup)
    mockHandler := new(MockHandlerProcessor)

    mockConfig.On("lookupProcessingConfig", "validSource").Return(nil)
    mockHandler.On("processWithHandler", mock.Anything).Return(mock.Anything, nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("", "validSource")

    assert.NoError(t, err)
    assert.NotEmpty(t, requestID)
}

func TestProcessWorkflowRequest_EmptyRequestIDAndEmptySource(t *testing.T) {
    mockConfig := new(MockConfigLookup)

    mockConfig.On("lookupProcessingConfig", "").Return(nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("", "")

    assert.NoError(t, err)
    assert.NotEmpty(t, requestID)
}

func TestProcessWorkflowRequest_ValidRequestIDAndEmptySource(t *testing.T) {
    mockConfig := new(MockConfigLookup)

    mockConfig.On("lookupProcessingConfig", "").Return(nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("12345", "")

    assert.NoError(t, err)
    assert.Equal(t, "12345", requestID)
}

func TestProcessWorkflowRequest_InvalidSourceType(t *testing.T) {
    defer func() {
        if r := recover(); r == nil {
            t.Errorf("The code did not panic")
        }
    }()

    workflow_processor.ProcessWorkflowRequest("12345", 123)
}

func TestProcessWorkflowRequest_NullRequestID(t *testing.T) {
    defer func() {
        if r := recover(); r == nil {
            t.Errorf("The code did not panic")
        }
    }()

    workflow_processor.ProcessWorkflowRequest(nil, "validSource")
}

func TestProcessWorkflowRequest_NullSource(t *testing.T) {
    defer func() {
        if r := recover(); r == nil {
            t.Errorf("The code did not panic")
        }
    }()

    workflow_processor.ProcessWorkflowRequest("12345", nil)
}

func TestProcessWorkflowRequest_LargeRequestID(t *testing.T) {
    mockConfig := new(MockConfigLookup)
    mockHandler := new(MockHandlerProcessor)

    largeRequestID := string(make([]byte, 10000))
    mockConfig.On("lookupProcessingConfig", "validSource").Return(nil)
    mockHandler.On("processWithHandler", largeRequestID).Return(largeRequestID, nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest(largeRequestID, "validSource")

    assert.NoError(t, err)
    assert.Equal(t, largeRequestID, requestID)
}

func TestProcessWorkflowRequest_HighVolumeOfRequests(t *testing.T) {
    mockConfig := new(MockConfigLookup)
    mockHandler := new(MockHandlerProcessor)

    mockConfig.On("lookupProcessingConfig", "validSource").Return(nil)
    mockHandler.On("processWithHandler", mock.Anything).Return(mock.Anything, nil)

    for i := 0; i < 1000; i++ {
        requestID, err := workflow_processor.ProcessWorkflowRequest("", "validSource")
        assert.NoError(t, err)
        assert.NotEmpty(t, requestID)
    }
}

func TestProcessWorkflowRequest_SourceWithSpecialCharacters(t *testing.T) {
    mockConfig := new(MockConfigLookup)
    mockHandler := new(MockHandlerProcessor)

    mockConfig.On("lookupProcessingConfig", "!@#$%^&*()").Return(nil)
    mockHandler.On("processWithHandler", "12345").Return("12345", nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("12345", "!@#$%^&*()")

    assert.NoError(t, err)
    assert.Equal(t, "12345", requestID)
}

func TestProcessWorkflowRequest_SourceLeadingToNoConfiguration(t *testing.T) {
    mockConfig := new(MockConfigLookup)

    mockConfig.On("lookupProcessingConfig", "noConfigSource").Return(nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("12345", "noConfigSource")

    assert.NoError(t, err)
    assert.Equal(t, "12345", requestID)
}

func TestProcessWorkflowRequest_SourceWithConfigurationButNoHandler(t *testing.T) {
    mockConfig := new(MockConfigLookup)

    mockConfig.On("lookupProcessingConfig", "noHandlerSource").Return(nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("12345", "noHandlerSource")

    assert.NoError(t, err)
    assert.Equal(t, "12345", requestID)
}

func TestProcessWorkflowRequest_ValidRequestIDWithNonExistentSource(t *testing.T) {
    mockConfig := new(MockConfigLookup)

    mockConfig.On("lookupProcessingConfig", "nonExistentSource").Return(nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("12345", "nonExistentSource")

    assert.NoError(t, err)
    assert.Equal(t, "12345", requestID)
}

func TestProcessWorkflowRequest_EmptyRequestIDWithNonExistentSource(t *testing.T) {
    mockConfig := new(MockConfigLookup)

    mockConfig.On("lookupProcessingConfig", "nonExistentSource").Return(nil)

    requestID, err := workflow_processor.ProcessWorkflowRequest("", "nonExistentSource")

    assert.NoError(t, err)
    assert.NotEmpty(t, requestID)
}
MahtabBukhari commented 4 hours ago

?