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] - CreateConnectionCode #2357

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

[Unit Tests] - CreateConnectionCode #2357

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

Comments

@tomsmith8
Copy link

Unit Test Coverage for "CreateConnectionCode"


Stakwork Run


Unit Test Code


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


package db

import (
  "errors"
  "fmt"
  "testing"
  "time"

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

// Mock database struct
type mockDB struct {
  createdRecords []ConnectionCodes
}

func (m *mockDB) Create(value interface{}) *gorm.DB {
  codes, ok := value.(*[]ConnectionCodes)
  if !ok {
  	return &gorm.DB{Error: errors.New("invalid type")}
  }
  m.createdRecords = *codes
  return &gorm.DB{}
}

// ConnectionCodes struct for testing
type ConnectionCodes struct {
  ID          int
  DateCreated *time.Time
}

// Test function for CreateConnectionCode
func TestCreateConnectionCode(t *testing.T) {
  mockDatabase := &mockDB{}
  db := database{db: mockDatabase}

  tests := []struct {
  	name        string
  	input       []ConnectionCodes
  	expectError bool
  	validate    func(t *testing.T, result []ConnectionCodes)
  }{
  	{
  		name: "Basic Functionality",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: &time.Time{time.Date(2023, 10, 1, 0, 0, 0, 0, time.UTC)}},
  			{ID: 2, DateCreated: &time.Time{time.Date(2023, 10, 2, 0, 0, 0, 0, time.UTC)}},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 2, len(result))
  			assert.Equal(t, time.Date(2023, 10, 1, 0, 0, 0, 0, time.UTC), *result[0].DateCreated)
  			assert.Equal(t, time.Date(2023, 10, 2, 0, 0, 0, 0, time.UTC), *result[1].DateCreated)
  		},
  	},
  	{
  		name:        "Edge Case - Empty Input",
  		input:       []ConnectionCodes{},
  		expectError: true,
  		validate:    func(t *testing.T, result []ConnectionCodes) {},
  	},
  	{
  		name: "Edge Case - Nil DateCreated",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: nil},
  			{ID: 2, DateCreated: nil},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 2, len(result))
  			assert.NotNil(t, result[0].DateCreated)
  			assert.NotNil(t, result[1].DateCreated)
  		},
  	},
  	{
  		name: "Edge Case - Zero DateCreated",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: &time.Time{}},
  			{ID: 2, DateCreated: &time.Time{}},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 2, len(result))
  			assert.NotNil(t, result[0].DateCreated)
  			assert.NotNil(t, result[1].DateCreated)
  		},
  	},
  	{
  		name: "Mixed DateCreated Values",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: &time.Time{time.Date(2023, 10, 1, 0, 0, 0, 0, time.UTC)}},
  			{ID: 2, DateCreated: nil},
  			{ID: 3, DateCreated: &time.Time{}},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 3, len(result))
  			assert.Equal(t, time.Date(2023, 10, 1, 0, 0, 0, 0, time.UTC), *result[0].DateCreated)
  			assert.NotNil(t, result[1].DateCreated)
  			assert.NotNil(t, result[2].DateCreated)
  		},
  	},
  	{
  		name: "Performance and Scale",
  		input: func() []ConnectionCodes {
  			codes := make([]ConnectionCodes, 10000)
  			for i := range codes {
  				codes[i] = ConnectionCodes{ID: i + 1, DateCreated: nil}
  			}
  			return codes
  		}(),
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 10000, len(result))
  			for _, code := range result {
  				assert.NotNil(t, code.DateCreated)
  			}
  		},
  	},
  	{
  		name:        "Error Handling - Invalid Data Type",
  		input:       nil,
  		expectError: true,
  		validate:    func(t *testing.T, result []ConnectionCodes) {},
  	},
  	{
  		name: "Special Case - Database Mocking",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: nil},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 1, len(result))
  			assert.NotNil(t, result[0].DateCreated)
  			assert.Equal(t, 1, mockDatabase.createdRecords[0].ID)
  		},
  	},
  	{
  		name: "Edge Case - Duplicate IDs",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: nil},
  			{ID: 1, DateCreated: nil},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 2, len(result))
  			assert.NotNil(t, result[0].DateCreated)
  			assert.NotNil(t, result[1].DateCreated)
  		},
  	},
  	{
  		name: "Edge Case - Future DateCreated",
  		input: []ConnectionCodes{
  			{ID: 1, DateCreated: &time.Time{time.Date(2025, 10, 1, 0, 0, 0, 0, time.UTC)}},
  		},
  		expectError: false,
  		validate: func(t *testing.T, result []ConnectionCodes) {
  			assert.Equal(t, 1, len(result))
  			assert.Equal(t, time.Date(2025, 10, 1, 0, 0, 0, 0, time.UTC), *result[0].DateCreated)
  		},
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		result, err := db.CreateConnectionCode(tt.input)

  		if tt.expectError {
  			assert.Error(t, err)
  		} else {
  			assert.NoError(t, err)
  			assert.NotNil(t, result)
  			tt.validate(t, result)
  		}
  	})
  }
}
@MahtabBukhari
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.

2 participants