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] - GetConnectionCode #2354

Closed
tomsmith8 opened this issue Jan 8, 2025 · 1 comment · Fixed by #2367
Closed

[Unit Tests] - GetConnectionCode #2354

tomsmith8 opened this issue Jan 8, 2025 · 1 comment · Fixed by #2367
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "GetConnectionCode"


Stakwork Run


Unit Test Code


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


package db

import (
  "errors"
  "testing"

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

// Mock database and models
type MockDB struct {
  connectionCodes []ConnectionCodes
  updateCalled    bool
}

func (mdb *MockDB) Raw(sql string, values ...interface{}) *MockDB {
  return mdb
}

func (mdb *MockDB) Find(dest interface{}) *MockDB {
  if len(mdb.connectionCodes) > 0 {
  	*dest.(*ConnectionCodesShort) = ConnectionCodesShort{
  		ConnectionString: mdb.connectionCodes[0].ConnectionString,
  		DateCreated:      mdb.connectionCodes[0].DateCreated,
  	}
  }
  return mdb
}

func (mdb *MockDB) Model(value interface{}) *MockDB {
  return mdb
}

func (mdb *MockDB) Where(query interface{}, args ...interface{}) *MockDB {
  return mdb
}

func (mdb *MockDB) Updates(values interface{}) *MockDB {
  mdb.updateCalled = true
  return mdb
}

type database struct {
  db *MockDB
}

type ConnectionCodes struct {
  ConnectionString string
  DateCreated      string
  IsUsed           bool
}

type ConnectionCodesShort struct {
  ConnectionString string
  DateCreated      string
}

func TestGetConnectionCode(t *testing.T) {
  tests := []struct {
  	name             string
  	connectionCodes  []ConnectionCodes
  	expected         ConnectionCodesShort
  	expectUpdateCall bool
  }{
  	{
  		name: "Basic Functionality",
  		connectionCodes: []ConnectionCodes{
  			{ConnectionString: "code1", DateCreated: "2023-10-01", IsUsed: false},
  			{ConnectionString: "code2", DateCreated: "2023-10-02", IsUsed: false},
  		},
  		expected:         ConnectionCodesShort{ConnectionString: "code2", DateCreated: "2023-10-02"},
  		expectUpdateCall: true,
  	},
  	{
  		name: "No Unused Connection Codes",
  		connectionCodes: []ConnectionCodes{
  			{ConnectionString: "code1", DateCreated: "2023-10-01", IsUsed: true},
  		},
  		expected:         ConnectionCodesShort{},
  		expectUpdateCall: false,
  	},
  	{
  		name: "Single Unused Connection Code",
  		connectionCodes: []ConnectionCodes{
  			{ConnectionString: "code1", DateCreated: "2023-10-01", IsUsed: false},
  		},
  		expected:         ConnectionCodesShort{ConnectionString: "code1", DateCreated: "2023-10-01"},
  		expectUpdateCall: true,
  	},
  	{
  		name: "Multiple Unused Connection Codes",
  		connectionCodes: []ConnectionCodes{
  			{ConnectionString: "code1", DateCreated: "2023-10-01", IsUsed: false},
  			{ConnectionString: "code2", DateCreated: "2023-10-02", IsUsed: false},
  		},
  		expected:         ConnectionCodesShort{ConnectionString: "code2", DateCreated: "2023-10-02"},
  		expectUpdateCall: true,
  	},
  	{
  		name:             "Edge Case: Empty Database",
  		connectionCodes:  []ConnectionCodes{},
  		expected:         ConnectionCodesShort{},
  		expectUpdateCall: false,
  	},
  }

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

  		result := db.GetConnectionCode()

  		assert.Equal(t, tt.expected, result)
  		assert.Equal(t, tt.expectUpdateCall, mockDB.updateCalled)
  	})
  }
}

func TestGetConnectionCode_ErrorsAndEdgeCases(t *testing.T) {
  t.Run("Edge Case: Database Connection Error", func(t *testing.T) {
  	mockDB := &MockDB{}
  	db := database{db: mockDB}

  	// Simulate a database connection error
  	mockDB.Raw = func(sql string, values ...interface{}) *MockDB {
  		return nil
  	}

  	result := db.GetConnectionCode()
  	assert.Equal(t, ConnectionCodesShort{}, result)
  })

  t.Run("Edge Case: Large Number of Connection Codes", func(t *testing.T) {
  	largeList := make([]ConnectionCodes, 1000)
  	for i := 0; i < 1000; i++ {
  		largeList[i] = ConnectionCodes{ConnectionString: "code" + string(i), DateCreated: "2023-10-01", IsUsed: false}
  	}
  	mockDB := &MockDB{connectionCodes: largeList}
  	db := database{db: mockDB}

  	result := db.GetConnectionCode()
  	assert.Equal(t, largeList[999].ConnectionString, result.ConnectionString)
  })

  t.Run("Edge Case: Special Characters in Connection String", func(t *testing.T) {
  	mockDB := &MockDB{
  		connectionCodes: []ConnectionCodes{
  			{ConnectionString: "!@#$%^&*()", DateCreated: "2023-10-01", IsUsed: false},
  		},
  	}
  	db := database{db: mockDB}

  	result := db.GetConnectionCode()
  	assert.Equal(t, "!@#$%^&*()", result.ConnectionString)
  })
}
@MahtabBukhari
Copy link
Contributor

@humansinstitute 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.

2 participants