package dialectsfcrm

import (
	"testing"

	soql "github.com/nexuscrm/dialect-sf-crm-soql"
)

func TestQuerySpecFromParsedSOQL_MapsParentAggregateGroupBy(t *testing.T) {
	parsed, err := soql.ParseSOQL(`SELECT Account.Industry, COUNT(Id) total FROM Opportunity WHERE Account.Industry = 'Technology' GROUP BY Account.Industry HAVING Account.Industry != null ORDER BY Account.Industry ASC`)
	if err != nil {
		t.Fatalf("ParseSOQL: %v", err)
	}

	spec := querySpecFromParsedSOQL(parsed)
	if spec.GroupBy != "Account.Industry" {
		t.Fatalf("GroupBy = %q, want Account.Industry", spec.GroupBy)
	}
	if got := predicateExpression(t, spec.Filter); got != "Account.Industry == 'Technology'" {
		t.Fatalf("Filter = %q, want parent relationship predicate", got)
	}
	if got := predicateExpression(t, spec.Having); got != "Account.Industry != null" {
		t.Fatalf("Having = %q, want parent relationship predicate", got)
	}
	if spec.SortField != "Account.Industry" || spec.SortDirection != "ASC" {
		t.Fatalf("SortField/Direction = %q/%q, want Account.Industry/ASC", spec.SortField, spec.SortDirection)
	}
	if len(spec.OrderBy) != 1 || spec.OrderBy[0].Field != "Account.Industry" || spec.OrderBy[0].Direction != "ASC" {
		t.Fatalf("OrderBy = %#v, want parent relationship sort", spec.OrderBy)
	}
}
