package coperator import ( "strings" "testing" ) func TestArray_GetPathArray(t *testing.T) { tests := []struct { name string array Array path []string want interface{} }{ { name: "Get first element", array: Array{1, 2, 3}, path: []string{"0"}, want: 1, }, { name: "Get nested array value", array: Array{Array{4, 5}}, path: []string{"0", "1"}, want: 5, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.array.GetPathArray(tt.path) if got != tt.want { t.Errorf("Array.GetPathArray() = %v, want %v", got, tt.want) } }) } } func TestDocument_GetPathArray(t *testing.T) { tests := []struct { name string doc Document path []string want interface{} }{ { name: "Get top level field", doc: Document{ "name": "test", }, path: []string{"name"}, want: "test", }, { name: "Get nested field", doc: Document{ "user": Document{ "id": 1, }, }, path: []string{"user", "id"}, want: 1, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.doc.GetPathArray(tt.path) if got != tt.want { t.Errorf("Document.GetPathArray() = %v, want %v", got, tt.want) } }) } } func TestValueOperator(t *testing.T) { tests := []struct { name string doc Document path []string value interface{} want bool }{ { name: "Get top level field", doc: Document{ "name": "test", }, path: []string{"name"}, value: "test", want: true, }, { name: "Get nested field", doc: Document{ "user": Document{ "id": 1, }, }, path: []string{"user", "id"}, value: 1, want: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, _ := ValueOperator(&tt.doc, map[string]interface{}{}, strings.Join(tt.path, "."), tt.value) if got != tt.want { t.Errorf("Document.GetPathArray() = %v, want %v", got, tt.want) } }) } } func TestDocumentOperator(t *testing.T) { tests := []struct { name string doc Document filter Filter want bool }{ { name: "test", doc: Document{ "user": Document{ "id": 1, }, }, filter: map[string]interface{}{ "user.id": 1, }, want: true, }, { name: "test with operator", doc: Document{ "age": 18, }, filter: map[string]interface{}{ "age": map[string]interface{}{ "$gt": 17, }, }, want: true, }, { name: "test with multi operator", doc: Document{ "age": 18, }, filter: map[string]interface{}{ "age": map[string]interface{}{ "$gt": 17, "$lt": 16, }, }, want: false, }, { name: "test with multi operator", doc: Document{ "age": 18, }, filter: map[string]interface{}{ "age": map[string]interface{}{ "$in": []interface{}{16, 17, 18}, }, }, want: true, }, { name: "test with multi operator", doc: Document{ "age": 22, }, filter: map[string]interface{}{ "age": map[string]interface{}{ "$nin": []interface{}{16, 17, 18}, }, }, want: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, _ := DocumentOperator(&tt.doc, tt.filter, "", nil) if got != tt.want { t.Errorf("Document.GetPathArray() = %v, want %v", got, tt.want) } }) } }