go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/aip/orderby_generator_test.go (about)

     1  // Copyright 2022 The LUCI Authors.
     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 aip
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestOrderByClause(t *testing.T) {
    25  	Convey("OrderByClause", t, func() {
    26  		table := NewTable().WithColumns(
    27  			NewColumn().WithFieldPath("foo").WithDatabaseName("db_foo").Sortable().Build(),
    28  			NewColumn().WithFieldPath("bar").WithDatabaseName("db_bar").Sortable().Build(),
    29  			NewColumn().WithFieldPath("baz").WithDatabaseName("db_baz").Sortable().Build(),
    30  			NewColumn().WithFieldPath("unsortable").WithDatabaseName("unsortable").Build(),
    31  		).Build()
    32  
    33  		Convey("Empty order by", func() {
    34  			result, err := table.OrderByClause([]OrderBy{})
    35  			So(err, ShouldBeNil)
    36  			So(result, ShouldEqual, "")
    37  		})
    38  		Convey("Single order by", func() {
    39  			result, err := table.OrderByClause([]OrderBy{
    40  				{
    41  					FieldPath: NewFieldPath("foo"),
    42  				},
    43  			})
    44  			So(err, ShouldBeNil)
    45  			So(result, ShouldEqual, "ORDER BY db_foo\n")
    46  		})
    47  		Convey("Multiple order by", func() {
    48  			result, err := table.OrderByClause([]OrderBy{
    49  				{
    50  					FieldPath:  NewFieldPath("foo"),
    51  					Descending: true,
    52  				},
    53  				{
    54  					FieldPath: NewFieldPath("bar"),
    55  				},
    56  				{
    57  					FieldPath:  NewFieldPath("baz"),
    58  					Descending: true,
    59  				},
    60  			})
    61  			So(err, ShouldBeNil)
    62  			So(result, ShouldEqual, "ORDER BY db_foo DESC, db_bar, db_baz DESC\n")
    63  		})
    64  		Convey("Unsortable field in order by", func() {
    65  			_, err := table.OrderByClause([]OrderBy{
    66  				{
    67  					FieldPath:  NewFieldPath("unsortable"),
    68  					Descending: true,
    69  				},
    70  			})
    71  			So(err, ShouldErrLike, `no sortable field named "unsortable", valid fields are foo, bar, baz`)
    72  		})
    73  		Convey("Repeated field in order by", func() {
    74  			_, err := table.OrderByClause([]OrderBy{
    75  				{
    76  					FieldPath: NewFieldPath("foo"),
    77  				},
    78  				{
    79  					FieldPath: NewFieldPath("foo"),
    80  				},
    81  			})
    82  			So(err, ShouldErrLike, `field appears in order_by multiple times: "foo"`)
    83  		})
    84  	})
    85  }
    86  
    87  func TestMergeWithDefaultOrder(t *testing.T) {
    88  	Convey("MergeWithDefaultOrder", t, func() {
    89  		defaultOrder := []OrderBy{
    90  			{
    91  				FieldPath:  NewFieldPath("foo"),
    92  				Descending: true,
    93  			}, {
    94  				FieldPath: NewFieldPath("bar"),
    95  			}, {
    96  				FieldPath:  NewFieldPath("baz"),
    97  				Descending: true,
    98  			},
    99  		}
   100  		Convey("Empty order", func() {
   101  			result := MergeWithDefaultOrder(defaultOrder, nil)
   102  			So(result, ShouldResemble, defaultOrder)
   103  		})
   104  		Convey("Non-empty order", func() {
   105  			order := []OrderBy{
   106  				{
   107  					FieldPath:  NewFieldPath("other"),
   108  					Descending: true,
   109  				},
   110  				{
   111  					FieldPath: NewFieldPath("baz"),
   112  				},
   113  			}
   114  			result := MergeWithDefaultOrder(defaultOrder, order)
   115  			So(result, ShouldResemble, []OrderBy{
   116  				{
   117  					FieldPath:  NewFieldPath("other"),
   118  					Descending: true,
   119  				},
   120  				{
   121  					FieldPath: NewFieldPath("baz"),
   122  				},
   123  				{
   124  					FieldPath:  NewFieldPath("foo"),
   125  					Descending: true,
   126  				}, {
   127  					FieldPath: NewFieldPath("bar"),
   128  				},
   129  			})
   130  		})
   131  	})
   132  }