github.com/mithrandie/csvq@v1.18.1/lib/query/runtime_information_test.go (about)

     1  package query
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/mithrandie/csvq/lib/parser"
     9  	"github.com/mithrandie/csvq/lib/value"
    10  )
    11  
    12  var getRuntimeInformationTests = []struct {
    13  	Input  parser.RuntimeInformation
    14  	Expect value.Primary
    15  	Error  string
    16  }{
    17  	{
    18  		Input:  parser.RuntimeInformation{Name: "uncommitted"},
    19  		Expect: value.NewBoolean(true),
    20  	},
    21  	{
    22  		Input:  parser.RuntimeInformation{Name: "created"},
    23  		Expect: value.NewInteger(2),
    24  	},
    25  	{
    26  		Input:  parser.RuntimeInformation{Name: "updated"},
    27  		Expect: value.NewInteger(3),
    28  	},
    29  	{
    30  		Input:  parser.RuntimeInformation{Name: "updated_views"},
    31  		Expect: value.NewInteger(1),
    32  	},
    33  	{
    34  		Input:  parser.RuntimeInformation{Name: "loaded_tables"},
    35  		Expect: value.NewInteger(4),
    36  	},
    37  	{
    38  		Input:  parser.RuntimeInformation{Name: "working_directory"},
    39  		Expect: value.NewString(GetWD()),
    40  	},
    41  	{
    42  		Input:  parser.RuntimeInformation{Name: "version"},
    43  		Expect: value.NewString("v1.0.0"),
    44  	},
    45  	{
    46  		Input: parser.RuntimeInformation{Name: "invalid"},
    47  		Error: "@#INVALID is an unknown runtime information",
    48  	},
    49  }
    50  
    51  func TestGetRuntimeInformation(t *testing.T) {
    52  	defer func() {
    53  		_ = TestTx.CachedViews.Clean(TestTx.FileContainer)
    54  		TestTx.UncommittedViews.Clean()
    55  		initFlag(TestTx.Flags)
    56  	}()
    57  
    58  	TestTx.CachedViews = GenerateViewMap([]*View{
    59  		{FileInfo: &FileInfo{Path: "table1"}},
    60  		{FileInfo: &FileInfo{Path: "table2"}},
    61  		{FileInfo: &FileInfo{Path: "table3"}},
    62  		{FileInfo: &FileInfo{Path: "table4"}},
    63  	})
    64  	TestTx.UncommittedViews = UncommittedViews{
    65  		mtx: &sync.RWMutex{},
    66  		Created: map[string]*FileInfo{
    67  			"TABLE1": {},
    68  			"TABLE2": {},
    69  		},
    70  		Updated: map[string]*FileInfo{
    71  			"TABLE3": {},
    72  			"TABLE4": {},
    73  			"TABLE5": {},
    74  			"VIEW1":  {ViewType: ViewTypeTemporaryTable},
    75  		},
    76  	}
    77  
    78  	for _, v := range getRuntimeInformationTests {
    79  		result, err := GetRuntimeInformation(TestTx, v.Input)
    80  
    81  		if err != nil {
    82  			if v.Error == "" {
    83  				t.Errorf("unexpected error %q for %q", err.Error(), v.Input)
    84  			} else if v.Error != err.Error() {
    85  				t.Errorf("error %q, want error %q for %q", err.Error(), v.Error, v.Input)
    86  			}
    87  			continue
    88  		}
    89  		if v.Error != "" {
    90  			t.Errorf("no error, want error %q for %q", v.Error, v.Input)
    91  			continue
    92  		}
    93  
    94  		if !reflect.DeepEqual(result, v.Expect) {
    95  			t.Errorf("result = %#v, want %#v for %q", result, v.Expect, v.Input)
    96  		}
    97  	}
    98  }