// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"database/sql/driver"
	"fmt"
	"math"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testcollection"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testfile"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testsummary"
)

// TestSummaryQuery is the builder for querying TestSummary entities.
type TestSummaryQuery struct {
	config
	ctx                *QueryContext
	order              []testsummary.OrderOption
	inters             []Interceptor
	predicates         []predicate.TestSummary
	withTestCollection *TestCollectionQuery
	withPassed         *TestFileQuery
	withFailed         *TestFileQuery
	withFKs            bool
	modifiers          []func(*sql.Selector)
	loadTotal          []func(context.Context, []*TestSummary) error
	withNamedPassed    map[string]*TestFileQuery
	withNamedFailed    map[string]*TestFileQuery
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Where adds a new predicate for the TestSummaryQuery builder.
func (tsq *TestSummaryQuery) Where(ps ...predicate.TestSummary) *TestSummaryQuery {
	tsq.predicates = append(tsq.predicates, ps...)
	return tsq
}

// Limit the number of records to be returned by this query.
func (tsq *TestSummaryQuery) Limit(limit int) *TestSummaryQuery {
	tsq.ctx.Limit = &limit
	return tsq
}

// Offset to start from.
func (tsq *TestSummaryQuery) Offset(offset int) *TestSummaryQuery {
	tsq.ctx.Offset = &offset
	return tsq
}

// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (tsq *TestSummaryQuery) Unique(unique bool) *TestSummaryQuery {
	tsq.ctx.Unique = &unique
	return tsq
}

// Order specifies how the records should be ordered.
func (tsq *TestSummaryQuery) Order(o ...testsummary.OrderOption) *TestSummaryQuery {
	tsq.order = append(tsq.order, o...)
	return tsq
}

// QueryTestCollection chains the current query on the "test_collection" edge.
func (tsq *TestSummaryQuery) QueryTestCollection() *TestCollectionQuery {
	query := (&TestCollectionClient{config: tsq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := tsq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := tsq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(testsummary.Table, testsummary.FieldID, selector),
			sqlgraph.To(testcollection.Table, testcollection.FieldID),
			sqlgraph.Edge(sqlgraph.O2O, true, testsummary.TestCollectionTable, testsummary.TestCollectionColumn),
		)
		fromU = sqlgraph.SetNeighbors(tsq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// QueryPassed chains the current query on the "passed" edge.
func (tsq *TestSummaryQuery) QueryPassed() *TestFileQuery {
	query := (&TestFileClient{config: tsq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := tsq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := tsq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(testsummary.Table, testsummary.FieldID, selector),
			sqlgraph.To(testfile.Table, testfile.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, false, testsummary.PassedTable, testsummary.PassedColumn),
		)
		fromU = sqlgraph.SetNeighbors(tsq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// QueryFailed chains the current query on the "failed" edge.
func (tsq *TestSummaryQuery) QueryFailed() *TestFileQuery {
	query := (&TestFileClient{config: tsq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := tsq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := tsq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(testsummary.Table, testsummary.FieldID, selector),
			sqlgraph.To(testfile.Table, testfile.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, false, testsummary.FailedTable, testsummary.FailedColumn),
		)
		fromU = sqlgraph.SetNeighbors(tsq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// First returns the first TestSummary entity from the query.
// Returns a *NotFoundError when no TestSummary was found.
func (tsq *TestSummaryQuery) First(ctx context.Context) (*TestSummary, error) {
	nodes, err := tsq.Limit(1).All(setContextOp(ctx, tsq.ctx, ent.OpQueryFirst))
	if err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nil, &NotFoundError{testsummary.Label}
	}
	return nodes[0], nil
}

// FirstX is like First, but panics if an error occurs.
func (tsq *TestSummaryQuery) FirstX(ctx context.Context) *TestSummary {
	node, err := tsq.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return node
}

// FirstID returns the first TestSummary ID from the query.
// Returns a *NotFoundError when no TestSummary ID was found.
func (tsq *TestSummaryQuery) FirstID(ctx context.Context) (id int, err error) {
	var ids []int
	if ids, err = tsq.Limit(1).IDs(setContextOp(ctx, tsq.ctx, ent.OpQueryFirstID)); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{testsummary.Label}
		return
	}
	return ids[0], nil
}

// FirstIDX is like FirstID, but panics if an error occurs.
func (tsq *TestSummaryQuery) FirstIDX(ctx context.Context) int {
	id, err := tsq.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}

// Only returns a single TestSummary entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one TestSummary entity is found.
// Returns a *NotFoundError when no TestSummary entities are found.
func (tsq *TestSummaryQuery) Only(ctx context.Context) (*TestSummary, error) {
	nodes, err := tsq.Limit(2).All(setContextOp(ctx, tsq.ctx, ent.OpQueryOnly))
	if err != nil {
		return nil, err
	}
	switch len(nodes) {
	case 1:
		return nodes[0], nil
	case 0:
		return nil, &NotFoundError{testsummary.Label}
	default:
		return nil, &NotSingularError{testsummary.Label}
	}
}

// OnlyX is like Only, but panics if an error occurs.
func (tsq *TestSummaryQuery) OnlyX(ctx context.Context) *TestSummary {
	node, err := tsq.Only(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// OnlyID is like Only, but returns the only TestSummary ID in the query.
// Returns a *NotSingularError when more than one TestSummary ID is found.
// Returns a *NotFoundError when no entities are found.
func (tsq *TestSummaryQuery) OnlyID(ctx context.Context) (id int, err error) {
	var ids []int
	if ids, err = tsq.Limit(2).IDs(setContextOp(ctx, tsq.ctx, ent.OpQueryOnlyID)); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &NotFoundError{testsummary.Label}
	default:
		err = &NotSingularError{testsummary.Label}
	}
	return
}

// OnlyIDX is like OnlyID, but panics if an error occurs.
func (tsq *TestSummaryQuery) OnlyIDX(ctx context.Context) int {
	id, err := tsq.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// All executes the query and returns a list of TestSummaries.
func (tsq *TestSummaryQuery) All(ctx context.Context) ([]*TestSummary, error) {
	ctx = setContextOp(ctx, tsq.ctx, ent.OpQueryAll)
	if err := tsq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	qr := querierAll[[]*TestSummary, *TestSummaryQuery]()
	return withInterceptors[[]*TestSummary](ctx, tsq, qr, tsq.inters)
}

// AllX is like All, but panics if an error occurs.
func (tsq *TestSummaryQuery) AllX(ctx context.Context) []*TestSummary {
	nodes, err := tsq.All(ctx)
	if err != nil {
		panic(err)
	}
	return nodes
}

// IDs executes the query and returns a list of TestSummary IDs.
func (tsq *TestSummaryQuery) IDs(ctx context.Context) (ids []int, err error) {
	if tsq.ctx.Unique == nil && tsq.path != nil {
		tsq.Unique(true)
	}
	ctx = setContextOp(ctx, tsq.ctx, ent.OpQueryIDs)
	if err = tsq.Select(testsummary.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// IDsX is like IDs, but panics if an error occurs.
func (tsq *TestSummaryQuery) IDsX(ctx context.Context) []int {
	ids, err := tsq.IDs(ctx)
	if err != nil {
		panic(err)
	}
	return ids
}

// Count returns the count of the given query.
func (tsq *TestSummaryQuery) Count(ctx context.Context) (int, error) {
	ctx = setContextOp(ctx, tsq.ctx, ent.OpQueryCount)
	if err := tsq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return withInterceptors[int](ctx, tsq, querierCount[*TestSummaryQuery](), tsq.inters)
}

// CountX is like Count, but panics if an error occurs.
func (tsq *TestSummaryQuery) CountX(ctx context.Context) int {
	count, err := tsq.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}

// Exist returns true if the query has elements in the graph.
func (tsq *TestSummaryQuery) Exist(ctx context.Context) (bool, error) {
	ctx = setContextOp(ctx, tsq.ctx, ent.OpQueryExist)
	switch _, err := tsq.FirstID(ctx); {
	case IsNotFound(err):
		return false, nil
	case err != nil:
		return false, fmt.Errorf("ent: check existence: %w", err)
	default:
		return true, nil
	}
}

// ExistX is like Exist, but panics if an error occurs.
func (tsq *TestSummaryQuery) ExistX(ctx context.Context) bool {
	exist, err := tsq.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}

// Clone returns a duplicate of the TestSummaryQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (tsq *TestSummaryQuery) Clone() *TestSummaryQuery {
	if tsq == nil {
		return nil
	}
	return &TestSummaryQuery{
		config:             tsq.config,
		ctx:                tsq.ctx.Clone(),
		order:              append([]testsummary.OrderOption{}, tsq.order...),
		inters:             append([]Interceptor{}, tsq.inters...),
		predicates:         append([]predicate.TestSummary{}, tsq.predicates...),
		withTestCollection: tsq.withTestCollection.Clone(),
		withPassed:         tsq.withPassed.Clone(),
		withFailed:         tsq.withFailed.Clone(),
		// clone intermediate query.
		sql:  tsq.sql.Clone(),
		path: tsq.path,
	}
}

// WithTestCollection tells the query-builder to eager-load the nodes that are connected to
// the "test_collection" edge. The optional arguments are used to configure the query builder of the edge.
func (tsq *TestSummaryQuery) WithTestCollection(opts ...func(*TestCollectionQuery)) *TestSummaryQuery {
	query := (&TestCollectionClient{config: tsq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	tsq.withTestCollection = query
	return tsq
}

// WithPassed tells the query-builder to eager-load the nodes that are connected to
// the "passed" edge. The optional arguments are used to configure the query builder of the edge.
func (tsq *TestSummaryQuery) WithPassed(opts ...func(*TestFileQuery)) *TestSummaryQuery {
	query := (&TestFileClient{config: tsq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	tsq.withPassed = query
	return tsq
}

// WithFailed tells the query-builder to eager-load the nodes that are connected to
// the "failed" edge. The optional arguments are used to configure the query builder of the edge.
func (tsq *TestSummaryQuery) WithFailed(opts ...func(*TestFileQuery)) *TestSummaryQuery {
	query := (&TestFileClient{config: tsq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	tsq.withFailed = query
	return tsq
}

// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
//	var v []struct {
//		OverallStatus testsummary.OverallStatus `json:"overall_status,omitempty"`
//		Count int `json:"count,omitempty"`
//	}
//
//	client.TestSummary.Query().
//		GroupBy(testsummary.FieldOverallStatus).
//		Aggregate(ent.Count()).
//		Scan(ctx, &v)
func (tsq *TestSummaryQuery) GroupBy(field string, fields ...string) *TestSummaryGroupBy {
	tsq.ctx.Fields = append([]string{field}, fields...)
	grbuild := &TestSummaryGroupBy{build: tsq}
	grbuild.flds = &tsq.ctx.Fields
	grbuild.label = testsummary.Label
	grbuild.scan = grbuild.Scan
	return grbuild
}

// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
//	var v []struct {
//		OverallStatus testsummary.OverallStatus `json:"overall_status,omitempty"`
//	}
//
//	client.TestSummary.Query().
//		Select(testsummary.FieldOverallStatus).
//		Scan(ctx, &v)
func (tsq *TestSummaryQuery) Select(fields ...string) *TestSummarySelect {
	tsq.ctx.Fields = append(tsq.ctx.Fields, fields...)
	sbuild := &TestSummarySelect{TestSummaryQuery: tsq}
	sbuild.label = testsummary.Label
	sbuild.flds, sbuild.scan = &tsq.ctx.Fields, sbuild.Scan
	return sbuild
}

// Aggregate returns a TestSummarySelect configured with the given aggregations.
func (tsq *TestSummaryQuery) Aggregate(fns ...AggregateFunc) *TestSummarySelect {
	return tsq.Select().Aggregate(fns...)
}

func (tsq *TestSummaryQuery) prepareQuery(ctx context.Context) error {
	for _, inter := range tsq.inters {
		if inter == nil {
			return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
		}
		if trv, ok := inter.(Traverser); ok {
			if err := trv.Traverse(ctx, tsq); err != nil {
				return err
			}
		}
	}
	for _, f := range tsq.ctx.Fields {
		if !testsummary.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if tsq.path != nil {
		prev, err := tsq.path(ctx)
		if err != nil {
			return err
		}
		tsq.sql = prev
	}
	return nil
}

func (tsq *TestSummaryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*TestSummary, error) {
	var (
		nodes       = []*TestSummary{}
		withFKs     = tsq.withFKs
		_spec       = tsq.querySpec()
		loadedTypes = [3]bool{
			tsq.withTestCollection != nil,
			tsq.withPassed != nil,
			tsq.withFailed != nil,
		}
	)
	if tsq.withTestCollection != nil {
		withFKs = true
	}
	if withFKs {
		_spec.Node.Columns = append(_spec.Node.Columns, testsummary.ForeignKeys...)
	}
	_spec.ScanValues = func(columns []string) ([]any, error) {
		return (*TestSummary).scanValues(nil, columns)
	}
	_spec.Assign = func(columns []string, values []any) error {
		node := &TestSummary{config: tsq.config}
		nodes = append(nodes, node)
		node.Edges.loadedTypes = loadedTypes
		return node.assignValues(columns, values)
	}
	if len(tsq.modifiers) > 0 {
		_spec.Modifiers = tsq.modifiers
	}
	for i := range hooks {
		hooks[i](ctx, _spec)
	}
	if err := sqlgraph.QueryNodes(ctx, tsq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	if query := tsq.withTestCollection; query != nil {
		if err := tsq.loadTestCollection(ctx, query, nodes, nil,
			func(n *TestSummary, e *TestCollection) { n.Edges.TestCollection = e }); err != nil {
			return nil, err
		}
	}
	if query := tsq.withPassed; query != nil {
		if err := tsq.loadPassed(ctx, query, nodes,
			func(n *TestSummary) { n.Edges.Passed = []*TestFile{} },
			func(n *TestSummary, e *TestFile) { n.Edges.Passed = append(n.Edges.Passed, e) }); err != nil {
			return nil, err
		}
	}
	if query := tsq.withFailed; query != nil {
		if err := tsq.loadFailed(ctx, query, nodes,
			func(n *TestSummary) { n.Edges.Failed = []*TestFile{} },
			func(n *TestSummary, e *TestFile) { n.Edges.Failed = append(n.Edges.Failed, e) }); err != nil {
			return nil, err
		}
	}
	for name, query := range tsq.withNamedPassed {
		if err := tsq.loadPassed(ctx, query, nodes,
			func(n *TestSummary) { n.appendNamedPassed(name) },
			func(n *TestSummary, e *TestFile) { n.appendNamedPassed(name, e) }); err != nil {
			return nil, err
		}
	}
	for name, query := range tsq.withNamedFailed {
		if err := tsq.loadFailed(ctx, query, nodes,
			func(n *TestSummary) { n.appendNamedFailed(name) },
			func(n *TestSummary, e *TestFile) { n.appendNamedFailed(name, e) }); err != nil {
			return nil, err
		}
	}
	for i := range tsq.loadTotal {
		if err := tsq.loadTotal[i](ctx, nodes); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

func (tsq *TestSummaryQuery) loadTestCollection(ctx context.Context, query *TestCollectionQuery, nodes []*TestSummary, init func(*TestSummary), assign func(*TestSummary, *TestCollection)) error {
	ids := make([]int, 0, len(nodes))
	nodeids := make(map[int][]*TestSummary)
	for i := range nodes {
		if nodes[i].test_collection_test_summary == nil {
			continue
		}
		fk := *nodes[i].test_collection_test_summary
		if _, ok := nodeids[fk]; !ok {
			ids = append(ids, fk)
		}
		nodeids[fk] = append(nodeids[fk], nodes[i])
	}
	if len(ids) == 0 {
		return nil
	}
	query.Where(testcollection.IDIn(ids...))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		nodes, ok := nodeids[n.ID]
		if !ok {
			return fmt.Errorf(`unexpected foreign-key "test_collection_test_summary" returned %v`, n.ID)
		}
		for i := range nodes {
			assign(nodes[i], n)
		}
	}
	return nil
}
func (tsq *TestSummaryQuery) loadPassed(ctx context.Context, query *TestFileQuery, nodes []*TestSummary, init func(*TestSummary), assign func(*TestSummary, *TestFile)) error {
	fks := make([]driver.Value, 0, len(nodes))
	nodeids := make(map[int]*TestSummary)
	for i := range nodes {
		fks = append(fks, nodes[i].ID)
		nodeids[nodes[i].ID] = nodes[i]
		if init != nil {
			init(nodes[i])
		}
	}
	query.withFKs = true
	query.Where(predicate.TestFile(func(s *sql.Selector) {
		s.Where(sql.InValues(s.C(testsummary.PassedColumn), fks...))
	}))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		fk := n.test_summary_passed
		if fk == nil {
			return fmt.Errorf(`foreign-key "test_summary_passed" is nil for node %v`, n.ID)
		}
		node, ok := nodeids[*fk]
		if !ok {
			return fmt.Errorf(`unexpected referenced foreign-key "test_summary_passed" returned %v for node %v`, *fk, n.ID)
		}
		assign(node, n)
	}
	return nil
}
func (tsq *TestSummaryQuery) loadFailed(ctx context.Context, query *TestFileQuery, nodes []*TestSummary, init func(*TestSummary), assign func(*TestSummary, *TestFile)) error {
	fks := make([]driver.Value, 0, len(nodes))
	nodeids := make(map[int]*TestSummary)
	for i := range nodes {
		fks = append(fks, nodes[i].ID)
		nodeids[nodes[i].ID] = nodes[i]
		if init != nil {
			init(nodes[i])
		}
	}
	query.withFKs = true
	query.Where(predicate.TestFile(func(s *sql.Selector) {
		s.Where(sql.InValues(s.C(testsummary.FailedColumn), fks...))
	}))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		fk := n.test_summary_failed
		if fk == nil {
			return fmt.Errorf(`foreign-key "test_summary_failed" is nil for node %v`, n.ID)
		}
		node, ok := nodeids[*fk]
		if !ok {
			return fmt.Errorf(`unexpected referenced foreign-key "test_summary_failed" returned %v for node %v`, *fk, n.ID)
		}
		assign(node, n)
	}
	return nil
}

func (tsq *TestSummaryQuery) sqlCount(ctx context.Context) (int, error) {
	_spec := tsq.querySpec()
	if len(tsq.modifiers) > 0 {
		_spec.Modifiers = tsq.modifiers
	}
	_spec.Node.Columns = tsq.ctx.Fields
	if len(tsq.ctx.Fields) > 0 {
		_spec.Unique = tsq.ctx.Unique != nil && *tsq.ctx.Unique
	}
	return sqlgraph.CountNodes(ctx, tsq.driver, _spec)
}

func (tsq *TestSummaryQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := sqlgraph.NewQuerySpec(testsummary.Table, testsummary.Columns, sqlgraph.NewFieldSpec(testsummary.FieldID, field.TypeInt))
	_spec.From = tsq.sql
	if unique := tsq.ctx.Unique; unique != nil {
		_spec.Unique = *unique
	} else if tsq.path != nil {
		_spec.Unique = true
	}
	if fields := tsq.ctx.Fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, testsummary.FieldID)
		for i := range fields {
			if fields[i] != testsummary.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
	}
	if ps := tsq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := tsq.ctx.Limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := tsq.ctx.Offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := tsq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}

func (tsq *TestSummaryQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(tsq.driver.Dialect())
	t1 := builder.Table(testsummary.Table)
	columns := tsq.ctx.Fields
	if len(columns) == 0 {
		columns = testsummary.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if tsq.sql != nil {
		selector = tsq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	if tsq.ctx.Unique != nil && *tsq.ctx.Unique {
		selector.Distinct()
	}
	for _, p := range tsq.predicates {
		p(selector)
	}
	for _, p := range tsq.order {
		p(selector)
	}
	if offset := tsq.ctx.Offset; offset != nil {
		// limit is mandatory for offset clause. We start
		// with default value, and override it below if needed.
		selector.Offset(*offset).Limit(math.MaxInt32)
	}
	if limit := tsq.ctx.Limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}

// WithNamedPassed tells the query-builder to eager-load the nodes that are connected to the "passed"
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
func (tsq *TestSummaryQuery) WithNamedPassed(name string, opts ...func(*TestFileQuery)) *TestSummaryQuery {
	query := (&TestFileClient{config: tsq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	if tsq.withNamedPassed == nil {
		tsq.withNamedPassed = make(map[string]*TestFileQuery)
	}
	tsq.withNamedPassed[name] = query
	return tsq
}

// WithNamedFailed tells the query-builder to eager-load the nodes that are connected to the "failed"
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
func (tsq *TestSummaryQuery) WithNamedFailed(name string, opts ...func(*TestFileQuery)) *TestSummaryQuery {
	query := (&TestFileClient{config: tsq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	if tsq.withNamedFailed == nil {
		tsq.withNamedFailed = make(map[string]*TestFileQuery)
	}
	tsq.withNamedFailed[name] = query
	return tsq
}

// TestSummaryGroupBy is the group-by builder for TestSummary entities.
type TestSummaryGroupBy struct {
	selector
	build *TestSummaryQuery
}

// Aggregate adds the given aggregation functions to the group-by query.
func (tsgb *TestSummaryGroupBy) Aggregate(fns ...AggregateFunc) *TestSummaryGroupBy {
	tsgb.fns = append(tsgb.fns, fns...)
	return tsgb
}

// Scan applies the selector query and scans the result into the given value.
func (tsgb *TestSummaryGroupBy) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, tsgb.build.ctx, ent.OpQueryGroupBy)
	if err := tsgb.build.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*TestSummaryQuery, *TestSummaryGroupBy](ctx, tsgb.build, tsgb, tsgb.build.inters, v)
}

func (tsgb *TestSummaryGroupBy) sqlScan(ctx context.Context, root *TestSummaryQuery, v any) error {
	selector := root.sqlQuery(ctx).Select()
	aggregation := make([]string, 0, len(tsgb.fns))
	for _, fn := range tsgb.fns {
		aggregation = append(aggregation, fn(selector))
	}
	if len(selector.SelectedColumns()) == 0 {
		columns := make([]string, 0, len(*tsgb.flds)+len(tsgb.fns))
		for _, f := range *tsgb.flds {
			columns = append(columns, selector.C(f))
		}
		columns = append(columns, aggregation...)
		selector.Select(columns...)
	}
	selector.GroupBy(selector.Columns(*tsgb.flds...)...)
	if err := selector.Err(); err != nil {
		return err
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := tsgb.build.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

// TestSummarySelect is the builder for selecting fields of TestSummary entities.
type TestSummarySelect struct {
	*TestSummaryQuery
	selector
}

// Aggregate adds the given aggregation functions to the selector query.
func (tss *TestSummarySelect) Aggregate(fns ...AggregateFunc) *TestSummarySelect {
	tss.fns = append(tss.fns, fns...)
	return tss
}

// Scan applies the selector query and scans the result into the given value.
func (tss *TestSummarySelect) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, tss.ctx, ent.OpQuerySelect)
	if err := tss.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*TestSummaryQuery, *TestSummarySelect](ctx, tss.TestSummaryQuery, tss, tss.inters, v)
}

func (tss *TestSummarySelect) sqlScan(ctx context.Context, root *TestSummaryQuery, v any) error {
	selector := root.sqlQuery(ctx)
	aggregation := make([]string, 0, len(tss.fns))
	for _, fn := range tss.fns {
		aggregation = append(aggregation, fn(selector))
	}
	switch n := len(*tss.selector.flds); {
	case n == 0 && len(aggregation) > 0:
		selector.Select(aggregation...)
	case n != 0 && len(aggregation) > 0:
		selector.AppendSelect(aggregation...)
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := tss.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}
