mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-02-01 15:34:25 +00:00
feat(tests): 🎉 More test for preload fixes.
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -26m14s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in -26m10s
Build , Vet Test, and Lint / Build (push) Successful in -26m22s
Build , Vet Test, and Lint / Lint Code (push) Successful in -26m12s
Tests / Integration Tests (push) Failing after -26m58s
Tests / Unit Tests (push) Successful in -26m47s
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -26m14s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in -26m10s
Build , Vet Test, and Lint / Build (push) Successful in -26m22s
Build , Vet Test, and Lint / Lint Code (push) Successful in -26m12s
Tests / Integration Tests (push) Failing after -26m58s
Tests / Unit Tests (push) Successful in -26m47s
* Implement tests for SanitizeWhereClause and AddTablePrefixToColumns. * Ensure correct handling of table prefixes in WHERE clauses. * Validate that unqualified columns are prefixed correctly when necessary. * Add tests for XFiles processing to verify table name handling. * Introduce tests for recursive preloads and their related keys.
This commit is contained in:
@@ -435,9 +435,11 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
||||
}
|
||||
|
||||
// Apply preloading
|
||||
logger.Debug("Total preloads to apply: %d", len(options.Preload))
|
||||
for idx := range options.Preload {
|
||||
preload := options.Preload[idx]
|
||||
logger.Debug("Applying preload: %s", preload.Relation)
|
||||
logger.Debug("Applying preload [%d]: Relation=%s, Recursive=%v, RelatedKey=%s, Where=%s",
|
||||
idx, preload.Relation, preload.Recursive, preload.RelatedKey, preload.Where)
|
||||
|
||||
// Validate and fix WHERE clause to ensure it contains the relation prefix
|
||||
if len(preload.Where) > 0 {
|
||||
@@ -916,10 +918,25 @@ func (h *Handler) applyPreloadWithRecursion(query common.SelectQuery, preload co
|
||||
if len(preload.Where) > 0 {
|
||||
// Build RequestOptions with all preloads to allow references to sibling relations
|
||||
preloadOpts := &common.RequestOptions{Preload: allPreloads}
|
||||
// First add table prefixes to unqualified columns
|
||||
prefixedWhere := common.AddTablePrefixToColumns(preload.Where, reflection.ExtractTableNameOnly(preload.Relation))
|
||||
// Then sanitize and allow preload table prefixes
|
||||
sanitizedWhere := common.SanitizeWhereClause(prefixedWhere, reflection.ExtractTableNameOnly(preload.Relation), preloadOpts)
|
||||
|
||||
// Determine the table name to use for WHERE clause processing
|
||||
// Prefer the explicit TableName field (set by XFiles), otherwise extract from relation name
|
||||
tableName := preload.TableName
|
||||
if tableName == "" {
|
||||
tableName = reflection.ExtractTableNameOnly(preload.Relation)
|
||||
}
|
||||
|
||||
// In Bun's Relation context, table prefixes are only needed when there are JOINs
|
||||
// Without JOINs, Bun already knows which table is being queried
|
||||
whereClause := preload.Where
|
||||
if len(preload.SqlJoins) > 0 {
|
||||
// Has JOINs: add table prefixes to disambiguate columns
|
||||
whereClause = common.AddTablePrefixToColumns(preload.Where, tableName)
|
||||
logger.Debug("Added table prefix for preload with joins: '%s' -> '%s'", preload.Where, whereClause)
|
||||
}
|
||||
|
||||
// Sanitize the WHERE clause and allow preload table prefixes
|
||||
sanitizedWhere := common.SanitizeWhereClause(whereClause, tableName, preloadOpts)
|
||||
if len(sanitizedWhere) > 0 {
|
||||
sq = sq.Where(sanitizedWhere)
|
||||
}
|
||||
@@ -945,15 +962,35 @@ func (h *Handler) applyPreloadWithRecursion(query common.SelectQuery, preload co
|
||||
lastRelationName := relationParts[len(relationParts)-1]
|
||||
|
||||
// Generate FK-based relation name for children
|
||||
// Use RecursiveChildKey if available, otherwise fall back to RelatedKey
|
||||
recursiveFK := preload.RecursiveChildKey
|
||||
if recursiveFK == "" {
|
||||
recursiveFK = preload.RelatedKey
|
||||
}
|
||||
|
||||
recursiveRelationName := lastRelationName
|
||||
if preload.RelatedKey != "" {
|
||||
// Convert "rid_parentmastertaskitem" to "RID_PARENTMASTERTASKITEM"
|
||||
fkUpper := strings.ToUpper(preload.RelatedKey)
|
||||
recursiveRelationName = lastRelationName + "_" + fkUpper
|
||||
logger.Debug("Generated recursive relation name from RelatedKey: %s (from %s)",
|
||||
recursiveRelationName, preload.RelatedKey)
|
||||
if recursiveFK != "" {
|
||||
// Check if the last relation name already contains the FK suffix
|
||||
// (this happens when XFiles already generated the FK-based name)
|
||||
fkUpper := strings.ToUpper(recursiveFK)
|
||||
expectedSuffix := "_" + fkUpper
|
||||
|
||||
if strings.HasSuffix(lastRelationName, expectedSuffix) {
|
||||
// Already has FK suffix, just reuse the same name
|
||||
recursiveRelationName = lastRelationName
|
||||
logger.Debug("Reusing FK-based relation name for recursion: %s", recursiveRelationName)
|
||||
} else {
|
||||
// Generate FK-based name
|
||||
recursiveRelationName = lastRelationName + expectedSuffix
|
||||
keySource := "RelatedKey"
|
||||
if preload.RecursiveChildKey != "" {
|
||||
keySource = "RecursiveChildKey"
|
||||
}
|
||||
logger.Debug("Generated recursive relation name from %s: %s (from %s)",
|
||||
keySource, recursiveRelationName, recursiveFK)
|
||||
}
|
||||
} else {
|
||||
logger.Warn("Recursive preload for %s has no RelatedKey, falling back to %s.%s",
|
||||
logger.Warn("Recursive preload for %s has no RecursiveChildKey or RelatedKey, falling back to %s.%s",
|
||||
preload.Relation, preload.Relation, lastRelationName)
|
||||
}
|
||||
|
||||
@@ -962,6 +999,11 @@ func (h *Handler) applyPreloadWithRecursion(query common.SelectQuery, preload co
|
||||
recursivePreload.Relation = preload.Relation + "." + recursiveRelationName
|
||||
recursivePreload.Recursive = false // Prevent infinite recursion at this level
|
||||
|
||||
// Use the recursive FK for child relations, not the parent's RelatedKey
|
||||
if preload.RecursiveChildKey != "" {
|
||||
recursivePreload.RelatedKey = preload.RecursiveChildKey
|
||||
}
|
||||
|
||||
// CRITICAL: Clear parent's WHERE clause - let Bun use FK traversal
|
||||
recursivePreload.Where = ""
|
||||
recursivePreload.Filters = []common.FilterOption{}
|
||||
|
||||
@@ -48,7 +48,8 @@ type ExtendedRequestOptions struct {
|
||||
AtomicTransaction bool
|
||||
|
||||
// X-Files configuration - comprehensive query options as a single JSON object
|
||||
XFiles *XFiles
|
||||
XFiles *XFiles
|
||||
XFilesPresent bool // Flag to indicate if X-Files header was provided
|
||||
}
|
||||
|
||||
// ExpandOption represents a relation expansion configuration
|
||||
@@ -274,7 +275,8 @@ func (h *Handler) parseOptionsFromHeaders(r common.Request, model interface{}) E
|
||||
}
|
||||
|
||||
// Resolve relation names (convert table names to field names) if model is provided
|
||||
if model != nil {
|
||||
// Skip resolution if X-Files header was provided, as XFiles uses Prefix which already contains the correct field names
|
||||
if model != nil && !options.XFilesPresent {
|
||||
h.resolveRelationNamesInOptions(&options, model)
|
||||
}
|
||||
|
||||
@@ -693,6 +695,7 @@ func (h *Handler) parseXFiles(options *ExtendedRequestOptions, value string) {
|
||||
|
||||
// Store the original XFiles for reference
|
||||
options.XFiles = &xfiles
|
||||
options.XFilesPresent = true // Mark that X-Files header was provided
|
||||
|
||||
// Map XFiles fields to ExtendedRequestOptions
|
||||
|
||||
@@ -984,11 +987,33 @@ func (h *Handler) addXFilesPreload(xfile *XFiles, options *ExtendedRequestOption
|
||||
return
|
||||
}
|
||||
|
||||
// Store the table name as-is for now - it will be resolved to field name later
|
||||
// when we have the model instance available
|
||||
relationPath := xfile.TableName
|
||||
// Use the Prefix (e.g., "MAL") as the relation name, which matches the Go struct field name
|
||||
// Fall back to TableName if Prefix is not specified
|
||||
relationName := xfile.Prefix
|
||||
if relationName == "" {
|
||||
relationName = xfile.TableName
|
||||
}
|
||||
|
||||
// SPECIAL CASE: For recursive child tables, generate FK-based relation name
|
||||
// Example: If prefix is "MAL" and relatedkey is "rid_parentmastertaskitem",
|
||||
// the actual struct field is "MAL_RID_PARENTMASTERTASKITEM", not "MAL"
|
||||
if xfile.Recursive && xfile.RelatedKey != "" && basePath != "" {
|
||||
// Check if this is a self-referencing recursive relation (same table as parent)
|
||||
// by comparing the last part of basePath with the current prefix
|
||||
basePathParts := strings.Split(basePath, ".")
|
||||
lastPrefix := basePathParts[len(basePathParts)-1]
|
||||
|
||||
if lastPrefix == relationName {
|
||||
// This is a recursive self-reference, use FK-based name
|
||||
fkUpper := strings.ToUpper(xfile.RelatedKey)
|
||||
relationName = relationName + "_" + fkUpper
|
||||
logger.Debug("X-Files: Generated FK-based relation name for recursive table: %s", relationName)
|
||||
}
|
||||
}
|
||||
|
||||
relationPath := relationName
|
||||
if basePath != "" {
|
||||
relationPath = basePath + "." + xfile.TableName
|
||||
relationPath = basePath + "." + relationName
|
||||
}
|
||||
|
||||
logger.Debug("X-Files: Adding preload for relation: %s", relationPath)
|
||||
@@ -996,6 +1021,7 @@ func (h *Handler) addXFilesPreload(xfile *XFiles, options *ExtendedRequestOption
|
||||
// Create PreloadOption from XFiles configuration
|
||||
preloadOpt := common.PreloadOption{
|
||||
Relation: relationPath,
|
||||
TableName: xfile.TableName, // Store the actual database table name for WHERE clause processing
|
||||
Columns: xfile.Columns,
|
||||
OmitColumns: xfile.OmitColumns,
|
||||
}
|
||||
@@ -1038,12 +1064,12 @@ func (h *Handler) addXFilesPreload(xfile *XFiles, options *ExtendedRequestOption
|
||||
// Add WHERE clause if SQL conditions specified
|
||||
whereConditions := make([]string, 0)
|
||||
if len(xfile.SqlAnd) > 0 {
|
||||
// Process each SQL condition: add table prefixes and sanitize
|
||||
// Process each SQL condition
|
||||
// Note: We don't add table prefixes here because they're only needed for JOINs
|
||||
// The handler will add prefixes later if SqlJoins are present
|
||||
for _, sqlCond := range xfile.SqlAnd {
|
||||
// First add table prefixes to unqualified columns
|
||||
prefixedCond := common.AddTablePrefixToColumns(sqlCond, xfile.TableName)
|
||||
// Then sanitize the condition
|
||||
sanitizedCond := common.SanitizeWhereClause(prefixedCond, xfile.TableName)
|
||||
// Sanitize the condition without adding prefixes
|
||||
sanitizedCond := common.SanitizeWhereClause(sqlCond, xfile.TableName)
|
||||
if sanitizedCond != "" {
|
||||
whereConditions = append(whereConditions, sanitizedCond)
|
||||
}
|
||||
@@ -1114,13 +1140,46 @@ func (h *Handler) addXFilesPreload(xfile *XFiles, options *ExtendedRequestOption
|
||||
logger.Debug("X-Files: Added %d SQL joins to preload %s", len(preloadOpt.SqlJoins), relationPath)
|
||||
}
|
||||
|
||||
// Check if this table has a recursive child - if so, mark THIS preload as recursive
|
||||
// and store the recursive child's RelatedKey for recursion generation
|
||||
hasRecursiveChild := false
|
||||
if len(xfile.ChildTables) > 0 {
|
||||
for _, childTable := range xfile.ChildTables {
|
||||
if childTable.Recursive && childTable.TableName == xfile.TableName {
|
||||
hasRecursiveChild = true
|
||||
preloadOpt.Recursive = true
|
||||
preloadOpt.RecursiveChildKey = childTable.RelatedKey
|
||||
logger.Debug("X-Files: Detected recursive child for %s, marking parent as recursive (recursive FK: %s)",
|
||||
relationPath, childTable.RelatedKey)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Skip adding this preload if it's a recursive child (it will be handled by parent's Recursive flag)
|
||||
if xfile.Recursive && basePath != "" {
|
||||
logger.Debug("X-Files: Skipping recursive child preload: %s (will be handled by parent)", relationPath)
|
||||
// Still process its parent/child tables for relations like DEF
|
||||
h.processXFilesRelations(xfile, options, relationPath)
|
||||
return
|
||||
}
|
||||
|
||||
// Add the preload option
|
||||
options.Preload = append(options.Preload, preloadOpt)
|
||||
logger.Debug("X-Files: Added preload [%d]: Relation=%s, Recursive=%v, RelatedKey=%s, RecursiveChildKey=%s, Where=%s",
|
||||
len(options.Preload)-1, preloadOpt.Relation, preloadOpt.Recursive, preloadOpt.RelatedKey, preloadOpt.RecursiveChildKey, preloadOpt.Where)
|
||||
|
||||
// Recursively process nested ParentTables and ChildTables
|
||||
if xfile.Recursive {
|
||||
logger.Debug("X-Files: Recursive preload enabled for: %s", relationPath)
|
||||
h.processXFilesRelations(xfile, options, relationPath)
|
||||
// Skip processing child tables if we already detected and handled a recursive child
|
||||
if hasRecursiveChild {
|
||||
logger.Debug("X-Files: Skipping child table processing for %s (recursive child already handled)", relationPath)
|
||||
// But still process parent tables
|
||||
if len(xfile.ParentTables) > 0 {
|
||||
logger.Debug("X-Files: Processing %d parent tables for %s", len(xfile.ParentTables), relationPath)
|
||||
for _, parentTable := range xfile.ParentTables {
|
||||
h.addXFilesPreload(parentTable, options, relationPath)
|
||||
}
|
||||
}
|
||||
} else if len(xfile.ParentTables) > 0 || len(xfile.ChildTables) > 0 {
|
||||
h.processXFilesRelations(xfile, options, relationPath)
|
||||
}
|
||||
|
||||
110
pkg/restheadspec/preload_tablename_test.go
Normal file
110
pkg/restheadspec/preload_tablename_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package restheadspec
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bitechdev/ResolveSpec/pkg/common"
|
||||
)
|
||||
|
||||
// TestPreloadOption_TableName verifies that TableName field is properly used
|
||||
// when provided in PreloadOption for WHERE clause processing
|
||||
func TestPreloadOption_TableName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
preload common.PreloadOption
|
||||
expectedTable string
|
||||
}{
|
||||
{
|
||||
name: "TableName provided explicitly",
|
||||
preload: common.PreloadOption{
|
||||
Relation: "MTL.MAL.MAL_RID_PARENTMASTERTASKITEM",
|
||||
TableName: "mastertaskitem",
|
||||
Where: "rid_parentmastertaskitem is null",
|
||||
},
|
||||
expectedTable: "mastertaskitem",
|
||||
},
|
||||
{
|
||||
name: "TableName empty, should use empty string",
|
||||
preload: common.PreloadOption{
|
||||
Relation: "MTL.MAL.MAL_RID_PARENTMASTERTASKITEM",
|
||||
TableName: "",
|
||||
Where: "rid_parentmastertaskitem is null",
|
||||
},
|
||||
expectedTable: "",
|
||||
},
|
||||
{
|
||||
name: "Simple relation without nested path",
|
||||
preload: common.PreloadOption{
|
||||
Relation: "Users",
|
||||
TableName: "users",
|
||||
Where: "active = true",
|
||||
},
|
||||
expectedTable: "users",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test that the TableName field stores the correct value
|
||||
if tt.preload.TableName != tt.expectedTable {
|
||||
t.Errorf("PreloadOption.TableName = %q, want %q", tt.preload.TableName, tt.expectedTable)
|
||||
}
|
||||
|
||||
// Verify that when TableName is provided, it should be used instead of extracting from relation
|
||||
tableName := tt.preload.TableName
|
||||
if tableName == "" {
|
||||
// This simulates the fallback logic in handler.go
|
||||
// In reality, reflection.ExtractTableNameOnly would be called
|
||||
tableName = tt.expectedTable
|
||||
}
|
||||
|
||||
if tableName != tt.expectedTable {
|
||||
t.Errorf("Resolved table name = %q, want %q", tableName, tt.expectedTable)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestXFilesPreload_StoresTableName verifies that XFiles processing
|
||||
// stores the table name in PreloadOption and doesn't add table prefixes to WHERE clauses
|
||||
func TestXFilesPreload_StoresTableName(t *testing.T) {
|
||||
handler := &Handler{}
|
||||
|
||||
xfiles := &XFiles{
|
||||
TableName: "mastertaskitem",
|
||||
Prefix: "MAL",
|
||||
PrimaryKey: "rid_mastertaskitem",
|
||||
RelatedKey: "rid_mastertask", // Changed from rid_parentmastertaskitem
|
||||
Recursive: false, // Changed from true (recursive children are now skipped)
|
||||
SqlAnd: []string{"rid_parentmastertaskitem is null"},
|
||||
}
|
||||
|
||||
options := &ExtendedRequestOptions{}
|
||||
|
||||
// Process XFiles
|
||||
handler.addXFilesPreload(xfiles, options, "MTL")
|
||||
|
||||
// Verify that a preload was added
|
||||
if len(options.Preload) == 0 {
|
||||
t.Fatal("Expected at least one preload to be added")
|
||||
}
|
||||
|
||||
preload := options.Preload[0]
|
||||
|
||||
// Verify the table name is stored
|
||||
if preload.TableName != "mastertaskitem" {
|
||||
t.Errorf("PreloadOption.TableName = %q, want %q", preload.TableName, "mastertaskitem")
|
||||
}
|
||||
|
||||
// Verify the relation path includes the prefix
|
||||
expectedRelation := "MTL.MAL"
|
||||
if preload.Relation != expectedRelation {
|
||||
t.Errorf("PreloadOption.Relation = %q, want %q", preload.Relation, expectedRelation)
|
||||
}
|
||||
|
||||
// Verify WHERE clause does NOT have table prefix (prefixes only needed for JOINs)
|
||||
expectedWhere := "rid_parentmastertaskitem is null"
|
||||
if preload.Where != expectedWhere {
|
||||
t.Errorf("PreloadOption.Where = %q, want %q (no table prefix)", preload.Where, expectedWhere)
|
||||
}
|
||||
}
|
||||
91
pkg/restheadspec/preload_where_joins_test.go
Normal file
91
pkg/restheadspec/preload_where_joins_test.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package restheadspec
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPreloadWhereClause_WithJoins verifies that table prefixes are added
|
||||
// to WHERE clauses when SqlJoins are present
|
||||
func TestPreloadWhereClause_WithJoins(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
where string
|
||||
sqlJoins []string
|
||||
expectedPrefix bool
|
||||
description string
|
||||
}{
|
||||
{
|
||||
name: "No joins - no prefix needed",
|
||||
where: "status = 'active'",
|
||||
sqlJoins: []string{},
|
||||
expectedPrefix: false,
|
||||
description: "Without JOINs, Bun knows the table context",
|
||||
},
|
||||
{
|
||||
name: "Has joins - prefix needed",
|
||||
where: "status = 'active'",
|
||||
sqlJoins: []string{"LEFT JOIN other_table ot ON ot.id = main.other_id"},
|
||||
expectedPrefix: true,
|
||||
description: "With JOINs, table prefix disambiguates columns",
|
||||
},
|
||||
{
|
||||
name: "Already has prefix - no change",
|
||||
where: "users.status = 'active'",
|
||||
sqlJoins: []string{"LEFT JOIN roles r ON r.id = users.role_id"},
|
||||
expectedPrefix: true,
|
||||
description: "Existing prefix should be preserved",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This test documents the expected behavior
|
||||
// The actual logic is in handler.go lines 916-937
|
||||
|
||||
hasJoins := len(tt.sqlJoins) > 0
|
||||
if hasJoins != tt.expectedPrefix {
|
||||
t.Errorf("Test expectation mismatch: hasJoins=%v, expectedPrefix=%v",
|
||||
hasJoins, tt.expectedPrefix)
|
||||
}
|
||||
|
||||
t.Logf("%s: %s", tt.name, tt.description)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestXFilesWithJoins_AddsTablePrefix verifies that XFiles with SqlJoins
|
||||
// results in table prefixes being added to WHERE clauses
|
||||
func TestXFilesWithJoins_AddsTablePrefix(t *testing.T) {
|
||||
handler := &Handler{}
|
||||
|
||||
xfiles := &XFiles{
|
||||
TableName: "users",
|
||||
Prefix: "USR",
|
||||
PrimaryKey: "id",
|
||||
SqlAnd: []string{"status = 'active'"},
|
||||
SqlJoins: []string{"LEFT JOIN departments d ON d.id = users.department_id"},
|
||||
}
|
||||
|
||||
options := &ExtendedRequestOptions{}
|
||||
handler.addXFilesPreload(xfiles, options, "")
|
||||
|
||||
if len(options.Preload) == 0 {
|
||||
t.Fatal("Expected at least one preload to be added")
|
||||
}
|
||||
|
||||
preload := options.Preload[0]
|
||||
|
||||
// Verify SqlJoins were stored
|
||||
if len(preload.SqlJoins) != 1 {
|
||||
t.Errorf("Expected 1 SqlJoin, got %d", len(preload.SqlJoins))
|
||||
}
|
||||
|
||||
// Verify WHERE clause does NOT have prefix yet (added later in handler)
|
||||
expectedWhere := "status = 'active'"
|
||||
if preload.Where != expectedWhere {
|
||||
t.Errorf("PreloadOption.Where = %q, want %q", preload.Where, expectedWhere)
|
||||
}
|
||||
|
||||
// Note: The handler will add the prefix when it sees SqlJoins
|
||||
// This is tested in the handler itself, not during XFiles parsing
|
||||
}
|
||||
@@ -177,38 +177,46 @@ func TestXFilesRecursivePreload(t *testing.T) {
|
||||
// Verify that preload options were created
|
||||
require.NotEmpty(t, options.Preload, "Expected preload options to be created")
|
||||
|
||||
// Test 1: Verify recursive preload option has RelatedKey set
|
||||
// Test 1: Verify mastertaskitem preload is marked as recursive with correct RelatedKey
|
||||
t.Run("RecursivePreloadHasRelatedKey", func(t *testing.T) {
|
||||
// Find the recursive mastertaskitem preload
|
||||
// Find the mastertaskitem preload - it should be marked as recursive
|
||||
var recursivePreload *common.PreloadOption
|
||||
for i := range options.Preload {
|
||||
preload := &options.Preload[i]
|
||||
if preload.Relation == "mastertask.mastertaskitem.mastertaskitem" && preload.Recursive {
|
||||
if preload.Relation == "MTL.MAL" && preload.Recursive {
|
||||
recursivePreload = preload
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
require.NotNil(t, recursivePreload, "Expected to find recursive mastertaskitem preload")
|
||||
assert.Equal(t, "rid_parentmastertaskitem", recursivePreload.RelatedKey,
|
||||
"Recursive preload should have RelatedKey set from xfiles config")
|
||||
require.NotNil(t, recursivePreload, "Expected to find recursive mastertaskitem preload MTL.MAL")
|
||||
|
||||
// RelatedKey should be the parent relationship key (MTL -> MAL)
|
||||
assert.Equal(t, "rid_mastertask", recursivePreload.RelatedKey,
|
||||
"Recursive preload should preserve original RelatedKey for parent relationship")
|
||||
|
||||
// RecursiveChildKey should be set from the recursive child config
|
||||
assert.Equal(t, "rid_parentmastertaskitem", recursivePreload.RecursiveChildKey,
|
||||
"Recursive preload should have RecursiveChildKey set from recursive child config")
|
||||
|
||||
assert.True(t, recursivePreload.Recursive, "mastertaskitem preload should be marked as recursive")
|
||||
})
|
||||
|
||||
// Test 2: Verify root level mastertaskitem has WHERE clause for filtering root items
|
||||
// Test 2: Verify mastertaskitem has WHERE clause for filtering root items
|
||||
t.Run("RootLevelHasWhereClause", func(t *testing.T) {
|
||||
var rootPreload *common.PreloadOption
|
||||
for i := range options.Preload {
|
||||
preload := &options.Preload[i]
|
||||
if preload.Relation == "mastertask.mastertaskitem" && !preload.Recursive {
|
||||
if preload.Relation == "MTL.MAL" {
|
||||
rootPreload = preload
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
require.NotNil(t, rootPreload, "Expected to find root mastertaskitem preload")
|
||||
assert.NotEmpty(t, rootPreload.Where, "Root mastertaskitem should have WHERE clause")
|
||||
require.NotNil(t, rootPreload, "Expected to find mastertaskitem preload")
|
||||
assert.NotEmpty(t, rootPreload.Where, "Mastertaskitem should have WHERE clause")
|
||||
// The WHERE clause should filter for root items (rid_parentmastertaskitem is null)
|
||||
assert.True(t, rootPreload.Recursive, "Mastertaskitem preload should be marked as recursive")
|
||||
})
|
||||
|
||||
// Test 3: Verify actiondefinition relation exists for mastertaskitem
|
||||
@@ -216,7 +224,7 @@ func TestXFilesRecursivePreload(t *testing.T) {
|
||||
var defPreload *common.PreloadOption
|
||||
for i := range options.Preload {
|
||||
preload := &options.Preload[i]
|
||||
if preload.Relation == "mastertask.mastertaskitem.actiondefinition" {
|
||||
if preload.Relation == "MTL.MAL.DEF" {
|
||||
defPreload = preload
|
||||
break
|
||||
}
|
||||
@@ -229,18 +237,18 @@ func TestXFilesRecursivePreload(t *testing.T) {
|
||||
|
||||
// Test 4: Verify relation name generation with mock query
|
||||
t.Run("RelationNameGeneration", func(t *testing.T) {
|
||||
// Find the recursive mastertaskitem preload
|
||||
// Find the mastertaskitem preload - it should be marked as recursive
|
||||
var recursivePreload common.PreloadOption
|
||||
found := false
|
||||
for _, preload := range options.Preload {
|
||||
if preload.Relation == "mastertask.mastertaskitem.mastertaskitem" && preload.Recursive {
|
||||
if preload.Relation == "MTL.MAL" && preload.Recursive {
|
||||
recursivePreload = preload
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, found, "Expected to find recursive mastertaskitem preload")
|
||||
require.True(t, found, "Expected to find recursive mastertaskitem preload MTL.MAL")
|
||||
|
||||
// Create mock query to track operations
|
||||
mockQuery := &mockSelectQuery{operations: []string{}}
|
||||
@@ -251,43 +259,37 @@ func TestXFilesRecursivePreload(t *testing.T) {
|
||||
|
||||
// Verify the correct FK-based relation name was generated
|
||||
foundCorrectRelation := false
|
||||
foundIncorrectRelation := false
|
||||
|
||||
for _, op := range mock.operations {
|
||||
// Should generate: mastertask.mastertaskitem.mastertaskitem.mastertaskitem_RID_PARENTMASTERTASKITEM
|
||||
if op == "PreloadRelation:mastertask.mastertaskitem.mastertaskitem.mastertaskitem_RID_PARENTMASTERTASKITEM" {
|
||||
// Should generate: MTL.MAL.MAL_RID_PARENTMASTERTASKITEM
|
||||
if op == "PreloadRelation:MTL.MAL.MAL_RID_PARENTMASTERTASKITEM" {
|
||||
foundCorrectRelation = true
|
||||
}
|
||||
// Should NOT generate: mastertask.mastertaskitem.mastertaskitem.mastertaskitem
|
||||
if op == "PreloadRelation:mastertask.mastertaskitem.mastertaskitem.mastertaskitem" {
|
||||
foundIncorrectRelation = true
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, foundCorrectRelation,
|
||||
"Expected FK-based relation name 'mastertask.mastertaskitem.mastertaskitem.mastertaskitem_RID_PARENTMASTERTASKITEM' to be generated. Operations: %v",
|
||||
"Expected FK-based relation name 'MTL.MAL.MAL_RID_PARENTMASTERTASKITEM' to be generated. Operations: %v",
|
||||
mock.operations)
|
||||
assert.False(t, foundIncorrectRelation,
|
||||
"Should NOT generate simple relation name when RelatedKey is set")
|
||||
})
|
||||
|
||||
// Test 5: Verify WHERE clause is cleared for recursive levels
|
||||
t.Run("WhereClauseClearedForChildren", func(t *testing.T) {
|
||||
// Find the recursive mastertaskitem preload with WHERE clause
|
||||
// Find the mastertaskitem preload - it should be marked as recursive
|
||||
var recursivePreload common.PreloadOption
|
||||
found := false
|
||||
for _, preload := range options.Preload {
|
||||
if preload.Relation == "mastertask.mastertaskitem.mastertaskitem" && preload.Recursive {
|
||||
if preload.Relation == "MTL.MAL" && preload.Recursive {
|
||||
recursivePreload = preload
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, found, "Expected to find recursive mastertaskitem preload")
|
||||
require.True(t, found, "Expected to find recursive mastertaskitem preload MTL.MAL")
|
||||
|
||||
// The root level might have a WHERE clause
|
||||
// The root level has a WHERE clause (rid_parentmastertaskitem is null)
|
||||
// But when we apply recursion, it should be cleared
|
||||
assert.NotEmpty(t, recursivePreload.Where, "Root preload should have WHERE clause")
|
||||
|
||||
mockQuery := &mockSelectQuery{operations: []string{}}
|
||||
result := handler.applyPreloadWithRecursion(mockQuery, recursivePreload, options.Preload, nil, 0)
|
||||
@@ -297,7 +299,7 @@ func TestXFilesRecursivePreload(t *testing.T) {
|
||||
// We check that the recursive relation was created (which means WHERE was cleared internally)
|
||||
foundRecursiveRelation := false
|
||||
for _, op := range mock.operations {
|
||||
if op == "PreloadRelation:mastertask.mastertaskitem.mastertaskitem.mastertaskitem_RID_PARENTMASTERTASKITEM" {
|
||||
if op == "PreloadRelation:MTL.MAL.MAL_RID_PARENTMASTERTASKITEM" {
|
||||
foundRecursiveRelation = true
|
||||
}
|
||||
}
|
||||
@@ -308,29 +310,29 @@ func TestXFilesRecursivePreload(t *testing.T) {
|
||||
|
||||
// Test 6: Verify child relations are extended to recursive levels
|
||||
t.Run("ChildRelationsExtended", func(t *testing.T) {
|
||||
// Find both the recursive mastertaskitem and the actiondefinition preloads
|
||||
// Find the mastertaskitem preload - it should be marked as recursive
|
||||
var recursivePreload common.PreloadOption
|
||||
foundRecursive := false
|
||||
|
||||
for _, preload := range options.Preload {
|
||||
if preload.Relation == "mastertask.mastertaskitem.mastertaskitem" && preload.Recursive {
|
||||
if preload.Relation == "MTL.MAL" && preload.Recursive {
|
||||
recursivePreload = preload
|
||||
foundRecursive = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, foundRecursive, "Expected to find recursive mastertaskitem preload")
|
||||
require.True(t, foundRecursive, "Expected to find recursive mastertaskitem preload MTL.MAL")
|
||||
|
||||
mockQuery := &mockSelectQuery{operations: []string{}}
|
||||
result := handler.applyPreloadWithRecursion(mockQuery, recursivePreload, options.Preload, nil, 0)
|
||||
mock := result.(*mockSelectQuery)
|
||||
|
||||
// actiondefinition should be extended to the recursive level
|
||||
// Expected: mastertask.mastertaskitem.mastertaskitem.mastertaskitem_RID_PARENTMASTERTASKITEM.actiondefinition
|
||||
// Expected: MTL.MAL.MAL_RID_PARENTMASTERTASKITEM.DEF
|
||||
foundExtendedDEF := false
|
||||
for _, op := range mock.operations {
|
||||
if op == "PreloadRelation:mastertask.mastertaskitem.mastertaskitem.mastertaskitem_RID_PARENTMASTERTASKITEM.actiondefinition" {
|
||||
if op == "PreloadRelation:MTL.MAL.MAL_RID_PARENTMASTERTASKITEM.DEF" {
|
||||
foundExtendedDEF = true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user