github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/aggregate_test.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package eorm
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestAggregate(t *testing.T) {
    25  	db := memoryDB()
    26  	testCases := []CommonTestCase{
    27  		{
    28  			name:    "avg",
    29  			builder: NewSelector[TestModel](db).Select(Avg("Age")),
    30  			wantSql: "SELECT AVG(`age`) FROM `test_model`;",
    31  		},
    32  		{
    33  			name:    "max",
    34  			builder: NewSelector[TestModel](db).Select(Max("Age")),
    35  			wantSql: "SELECT MAX(`age`) FROM `test_model`;",
    36  		},
    37  		{
    38  			name:    "min",
    39  			builder: NewSelector[TestModel](db).Select(Min("Age").As("min_age")),
    40  			wantSql: "SELECT MIN(`age`) AS `min_age` FROM `test_model`;",
    41  		},
    42  		{
    43  			name:    "sum",
    44  			builder: NewSelector[TestModel](db).Select(Sum("Age")),
    45  			wantSql: "SELECT SUM(`age`) FROM `test_model`;",
    46  		},
    47  		{
    48  			name:    "count",
    49  			builder: NewSelector[TestModel](db).Select(Count("Age")),
    50  			wantSql: "SELECT COUNT(`age`) FROM `test_model`;",
    51  		},
    52  		{
    53  			name:    "count distinct",
    54  			builder: NewSelector[TestModel](db).Select(CountDistinct("FirstName")),
    55  			wantSql: "SELECT COUNT(DISTINCT `first_name`) FROM `test_model`;",
    56  		},
    57  		{
    58  			name:    "avg distinct",
    59  			builder: NewSelector[TestModel](db).Select(AvgDistinct("FirstName")),
    60  			wantSql: "SELECT AVG(DISTINCT `first_name`) FROM `test_model`;",
    61  		},
    62  		{
    63  			name:    "SUM distinct",
    64  			builder: NewSelector[TestModel](db).Select(SumDistinct("FirstName")),
    65  			wantSql: "SELECT SUM(DISTINCT `first_name`) FROM `test_model`;",
    66  		},
    67  	}
    68  
    69  	for _, tc := range testCases {
    70  		c := tc
    71  		t.Run(c.name, func(t *testing.T) {
    72  			query, err := c.builder.Build()
    73  			assert.Equal(t, c.wantErr, err)
    74  			if err != nil {
    75  				return
    76  			}
    77  			assert.Equal(t, c.wantSql, query.SQL)
    78  			assert.Equal(t, c.wantArgs, query.Args)
    79  		})
    80  	}
    81  }
    82  
    83  func ExampleAggregate_As() {
    84  	db := memoryDB()
    85  	query, _ := NewSelector[TestModel](db).Select(Avg("Age").As("avg_age")).Build()
    86  	fmt.Println(query.SQL)
    87  	// Output: SELECT AVG(`age`) AS `avg_age` FROM `test_model`;
    88  }
    89  
    90  func ExampleAvg() {
    91  	db := memoryDB()
    92  	query, _ := NewSelector[TestModel](db).Select(Avg("Age").As("avg_age")).Build()
    93  	fmt.Println(query.SQL)
    94  	// Output: SELECT AVG(`age`) AS `avg_age` FROM `test_model`;
    95  }
    96  
    97  func ExampleCount() {
    98  	db := memoryDB()
    99  	query, _ := NewSelector[TestModel](db).Select(Count("Age")).Build()
   100  	fmt.Println(query.SQL)
   101  	// Output: SELECT COUNT(`age`) FROM `test_model`;
   102  }
   103  
   104  func ExampleMax() {
   105  	db := memoryDB()
   106  	query, _ := NewSelector[TestModel](db).Select(Max("Age")).Build()
   107  	fmt.Println(query.SQL)
   108  	// Output: SELECT MAX(`age`) FROM `test_model`;
   109  }
   110  
   111  func ExampleMin() {
   112  	db := memoryDB()
   113  	query, _ := NewSelector[TestModel](db).Select(Min("Age")).Build()
   114  	fmt.Println(query.SQL)
   115  	// Output: SELECT MIN(`age`) FROM `test_model`;
   116  }
   117  
   118  func ExampleSum() {
   119  	db := memoryDB()
   120  	query, _ := NewSelector[TestModel](db).Select(Sum("Age")).Build()
   121  	fmt.Println(query.SQL)
   122  	// Output: SELECT SUM(`age`) FROM `test_model`;
   123  }