Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - GetChatsForWorkspace #2350

Closed
tomsmith8 opened this issue Jan 7, 2025 · 3 comments · Fixed by #2374
Closed

[Unit Tests] - GetChatsForWorkspace #2350

tomsmith8 opened this issue Jan 7, 2025 · 3 comments · Fixed by #2374
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "GetChatsForWorkspace"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/db/chat.go


package db

import (
  "errors"
  "fmt"
  "testing"

  "github.com/stretchr/testify/assert"
)

// Mock database and Chat struct for testing
type MockDB struct {
  chats []Chat
  err   error
}

func (mdb *MockDB) Where(query string, args ...interface{}) *MockDB {
  // Simulate filtering based on workspaceID
  workspaceID := args[0].(string)
  if workspaceID == "workspaceWithError" {
  	mdb.err = errors.New("simulated database error")
  	return mdb
  }

  var filteredChats []Chat
  for _, chat := range mdb.chats {
  	if chat.WorkspaceID == workspaceID {
  		filteredChats = append(filteredChats, chat)
  	}
  }
  mdb.chats = filteredChats
  return mdb
}

func (mdb *MockDB) Order(order string) *MockDB {
  // Simulate ordering by updated_at DESC
  // Assuming chats are already sorted for simplicity
  return mdb
}

func (mdb *MockDB) Find(dest interface{}) *MockDB {
  if mdb.err != nil {
  	return mdb
  }
  *dest.(*[]Chat) = mdb.chats
  return mdb
}

func (mdb *MockDB) Error() error {
  return mdb.err
}

type Chat struct {
  WorkspaceID string
  UpdatedAt   string
}

type database struct {
  db *MockDB
}

func TestGetChatsForWorkspace(t *testing.T) {
  tests := []struct {
  	name        string
  	workspaceID string
  	mockChats   []Chat
  	mockError   error
  	expected    []Chat
  	expectError bool
  }{
  	{
  		name:        "Basic Functionality",
  		workspaceID: "workspace123",
  		mockChats: []Chat{
  			{WorkspaceID: "workspace123", UpdatedAt: "2023-10-01"},
  			{WorkspaceID: "workspace123", UpdatedAt: "2023-09-01"},
  		},
  		expected: []Chat{
  			{WorkspaceID: "workspace123", UpdatedAt: "2023-10-01"},
  			{WorkspaceID: "workspace123", UpdatedAt: "2023-09-01"},
  		},
  		expectError: false,
  	},
  	{
  		name:        "No Chats for Workspace",
  		workspaceID: "emptyWorkspace",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Invalid Workspace ID",
  		workspaceID: "nonExistentWorkspace",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Empty Workspace ID",
  		workspaceID: "",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Null Workspace ID",
  		workspaceID: "",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: true,
  	},
  	{
  		name:        "Database Error",
  		workspaceID: "workspaceWithError",
  		mockChats:   []Chat{},
  		mockError:   errors.New("simulated database error"),
  		expected:    nil,
  		expectError: true,
  	},
  	{
  		name:        "Large Number of Chats",
  		workspaceID: "largeWorkspace",
  		mockChats:   make([]Chat, 1000),
  		expected:    make([]Chat, 1000),
  		expectError: false,
  	},
  	{
  		name:        "Special Characters in Workspace ID",
  		workspaceID: "special!@#Workspace",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "SQL Injection Attempt",
  		workspaceID: "workspace123'; DROP TABLE chats; --",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Case Sensitivity",
  		workspaceID: "Workspace123",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Unicode Characters in Workspace ID",
  		workspaceID: "工作区123",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Maximum Length Workspace ID",
  		workspaceID: "a" * 255,
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  	{
  		name:        "Minimum Length Workspace ID",
  		workspaceID: "a",
  		mockChats:   []Chat{},
  		expected:    []Chat{},
  		expectError: false,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		mockDB := &MockDB{
  			chats: tt.mockChats,
  			err:   tt.mockError,
  		}
  		db := database{db: mockDB}

  		chats, err := db.GetChatsForWorkspace(tt.workspaceID)

  		if tt.expectError {
  			assert.Error(t, err)
  		} else {
  			assert.NoError(t, err)
  			assert.Equal(t, tt.expected, chats)
  		}
  	})
  }
}
@MahtabBukhari
Copy link
Contributor

MahtabBukhari commented Jan 7, 2025

@tomsmith8 assign me

@AhsanFarooqDev
Copy link
Contributor

AhsanFarooqDev commented Jan 7, 2025

@tomsmith8 please assign

@MirzaHanan
Copy link
Contributor

@tomsmith8 assign>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants