Skip to content

Commit

Permalink
Added not equal condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 20, 2024
1 parent 8c0f8ab commit db75118
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
conditionLessThan = "$lt" // Condition for less than ( < )
conditionLessThanOrEqual = "$lte" // Condition for less than or equal ( <= )
conditionMatch = "$match" // Condition for a MATCH command
conditionNotEquals = "$ne" // Condition for not equal ( != )
conditionOr = "$or" // Condition for an OR statement
conditionSet = "$set" // Condition for a SET command
conditionSum = "$sum" // Condition for a SUM command
Expand Down
4 changes: 4 additions & 0 deletions where.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func processConditions(client ClientInterface, tx CustomWhereInterface, conditio
varName := "var" + strconv.Itoa(*varNum)
tx.Where(*parentKey+" <= @"+varName, map[string]interface{}{varName: formatCondition(condition, engine)})
*varNum++
} else if key == conditionNotEquals { // Add the not equals condition here
varName := "var" + strconv.Itoa(*varNum)
tx.Where(*parentKey+" != @"+varName, map[string]interface{}{varName: formatCondition(condition, engine)})
*varNum++
} else if key == conditionExists {
if condition.(bool) {
tx.Where(*parentKey + " IS NOT NULL")
Expand Down
103 changes: 102 additions & 1 deletion where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"testing"
"time"

"github.com/mrz1836/go-datastore/custom_types"
customtypes "github.com/mrz1836/go-datastore/custom_types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -247,6 +248,7 @@ type mockSQLCtx struct {
Vars map[string]interface{}
}

// Where will append the where clause
func (f *mockSQLCtx) Where(query interface{}, args ...interface{}) {
f.WhereClauses = append(f.WhereClauses, query)
if len(args) > 0 {
Expand All @@ -258,6 +260,7 @@ func (f *mockSQLCtx) Where(query interface{}, args ...interface{}) {
}
}

// getGormTx will return the GORM transaction
func (f *mockSQLCtx) getGormTx() *gorm.DB {
return nil
}
Expand Down Expand Up @@ -679,3 +682,101 @@ func Test_escapeDBString(t *testing.T) {
str := escapeDBString(`SELECT * FROM 'table' WHERE 'field'=1;`)
assert.Equal(t, `SELECT * FROM \'table\' WHERE \'field\'=1;`, str)
}

// Mock CustomWhereInterface
type MockCustomWhereInterface struct {
mock.Mock
}

// Correct the Where method to match the interface
func (m *MockCustomWhereInterface) Where(query interface{}, args ...interface{}) {
m.Called(query, args)
}

// Mock the getGormTx method (assuming it returns *gorm.DB)
func (m *MockCustomWhereInterface) getGormTx() *gorm.DB {
// Return nil or mock *gorm.DB behavior if needed
return nil
}

// Test the processConditions function
func TestProcessConditions(t *testing.T) {
tests := []struct {
name string
conditions map[string]interface{}
expected string
}{
{
name: "Greater Than Condition",
conditions: map[string]interface{}{
"$gt": 100,
},
expected: "field > @var0",
},
{
name: "Less Than Condition",
conditions: map[string]interface{}{
"$lt": 50,
},
expected: "field < @var0",
},
{
name: "Greater Than or Equal Condition",
conditions: map[string]interface{}{
"$gte": 100,
},
expected: "field >= @var0",
},
{
name: "Less Than or Equal Condition",
conditions: map[string]interface{}{
"$lte": 50,
},
expected: "field <= @var0",
},
{
name: "Not Equals Condition",
conditions: map[string]interface{}{
"$ne": 10,
},
expected: "field != @var0",
},
{
name: "Exists Condition - True",
conditions: map[string]interface{}{
"$exists": true,
},
expected: "field IS NOT NULL",
},
{
name: "Exists Condition - False",
conditions: map[string]interface{}{
"$exists": false,
},
expected: "field IS NULL",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, deferFunc := testClient(context.Background(), t)
defer deferFunc()

// Initialize mocks
mockTx := &MockCustomWhereInterface{}

// Define mock behavior for tx.Where
mockTx.On("Where", mock.Anything, mock.Anything).Return(nil)

// Initialize variables
varNum := 0
parentKey := "field"

// Call the function being tested
processConditions(client, mockTx, tt.conditions, SQLite, &varNum, &parentKey)

// Assert that the correct SQL query was generated
mockTx.AssertCalled(t, "Where", tt.expected, mock.Anything)
})
}
}

0 comments on commit db75118

Please sign in to comment.