ast_expr_calculator_test.go 3.7 KB
package astexpr

import (
	"github.com/stretchr/testify/assert"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"testing"
)

var table = &domain.DataTable{
	Fields: []*domain.Field{
		{
			Name:    "业绩",
			SQLName: "业绩",
		},
		{
			Name:    "绩效",
			SQLName: "绩效",
		},
		{
			Name:    "月份",
			SQLName: "月份",
		},
	},
	Data: [][]string{
		{
			"10000", "0.90", "2月",
		},
		{
			"20000", "0.95", "3月",
		},
		{
			"20000", "0.95", "3月",
		},
	},
}

var table1 = &domain.DataTable{
	Fields: []*domain.Field{
		{
			Name:    "产品",
			SQLName: "产品",
		},
		{
			Name:    "季度",
			SQLName: "季度",
		},
		{
			Name:    "数量",
			SQLName: "数量",
		},
	},
	Data: [][]string{
		{
			"苹果", "第一季度", "800",
		},
		{
			"香蕉", "第一季度", "906",
		},
		{
			"西瓜", "第一季度", "968",
		},
		{
			"菠萝", "第一季度", "227",
		},
		{
			"芒果", "第一季度", "612",
		},
		{
			"苹果", "第二季度", "530",
		},
		{
			"香蕉", "第二季度", "950",
		},
		{
			"西瓜", "第二季度", "533",
		},
		{
			"菠萝", "第二季度", "642",
		},
		{
			"芒果", "第二季度", "489",
		},
	},
}

func TestSumCalculator(t *testing.T) {
	inputs := []struct {
		expr string
		want []string
	}{
		{
			expr: `sum(1000+销售明细.业绩*销售明细.绩效)`,
			want: []string{"50000"},
		},
		{
			expr: `sum(销售明细.业绩)`,
			want: []string{"50000"},
		},
		{
			expr: `sum(销售明细.业绩/10)`,
			want: []string{"5000"},
		},
		{
			expr: `sum(10000,20000,20000)`,
			want: []string{"50000"},
		},
	}
	for _, expr := range inputs {
		calc, err := NewCalculator(expr.expr)
		if err != nil {
			t.Fatal(err)
		}
		calc.SetDataTable(table)
		got, err := calc.ExprASTResult(calc.ExprAST)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, expr.want, got.data)
	}
}

func TestSumIfCalculator(t *testing.T) {
	inputs := []struct {
		expr string
		want []string
	}{
		{
			expr: `sum(sumifs(销售明细.业绩,销售明细.月份,"3月"))`,
			want: []string{"40000"},
		},
		{
			expr: `sum(sumifs(销售明细.业绩,销售明细.月份,"3月",销售明细.业绩,"<25000"))`,
			want: []string{"40000"},
		},
		{
			expr: `sum(sumifs(销售明细.业绩,销售明细.月份,"3*"))`,
			want: []string{"40000"},
		},
		{
			expr: `sum(sumifs(销售明细.业绩,销售明细.月份,"*月"))`,
			want: []string{"50000"},
		},
		{
			expr: `sumifs(销售明细.业绩,销售明细.月份,销售明细.月份)`,
			want: []string{"10000", "40000"},
		},
		{
			expr: `sum(sumifs(销售明细.业绩,销售明细.月份,销售明细.月份))`,
			want: []string{"50000"},
		},
	}
	for _, expr := range inputs {
		calc, err := NewCalculator(expr.expr)
		if err != nil {
			t.Fatal(err)
		}
		calc.SetDataTable(table)
		got, err := calc.ExprASTResult(calc.ExprAST)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, expr.want, got.data)
	}
}

func TestCountIfCalculator(t *testing.T) {
	inputs := []struct {
		expr string
		want []string
	}{
		{
			expr: `countifs(水果季度.产品,"苹果")`,
			want: []string{"2"},
		},
		{
			expr: `countifs(水果季度.产品,"*果")`,
			want: []string{"4"},
		},
		{
			expr: `countifs(水果季度.季度,"第一季度",水果季度.数量,">600")`,
			want: []string{"4"},
		},
		{
			expr: `countifs(水果季度.产品,"*果",水果季度.数量,">600")`,
			want: []string{"2"},
		},
	}
	for _, expr := range inputs {
		calc, err := NewCalculator(expr.expr)
		if err != nil {
			t.Fatal(err)
		}
		calc.SetDataTable(table1)
		got, err := calc.ExprASTResult(calc.ExprAST)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, expr.want, got.data)
	}
}