github.com/CiscoM31/godata@v1.0.10/expand_parser_test.go (about)

     1  package godata
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  )
     7  
     8  func TestTrivialExpand(t *testing.T) {
     9  	input := "Products/Categories"
    10  	ctx := context.Background()
    11  	output, err := ParseExpandString(ctx, input)
    12  
    13  	if err != nil {
    14  		t.Error(err)
    15  		return
    16  	}
    17  
    18  	if output.ExpandItems[0].Path[0].Value != "Products" {
    19  		t.Error("First path item is not 'Products'")
    20  		return
    21  	}
    22  	if output.ExpandItems[0].Path[1].Value != "Categories" {
    23  		t.Error("Second path item is not 'Categories'")
    24  		return
    25  	}
    26  }
    27  
    28  func TestSimpleExpand(t *testing.T) {
    29  	input := "Products($filter=DiscontinuedDate eq null)"
    30  	ctx := context.Background()
    31  	output, err := ParseExpandString(ctx, input)
    32  
    33  	if err != nil {
    34  		t.Error(err)
    35  		return
    36  	}
    37  
    38  	if output.ExpandItems[0].Path[0].Value != "Products" {
    39  		t.Error("First path item is not 'Products'")
    40  		return
    41  	}
    42  
    43  	if output.ExpandItems[0].Filter == nil {
    44  		t.Error("Filter not parsed")
    45  		return
    46  	}
    47  
    48  	if output.ExpandItems[0].Filter.Tree == nil {
    49  		t.Error("Filter tree is null")
    50  		return
    51  	}
    52  
    53  	if output.ExpandItems[0].Filter.Tree.Token.Value != "eq" {
    54  		t.Error("Filter not parsed correctly")
    55  		return
    56  	}
    57  }
    58  
    59  func TestExpandNestedCommas(t *testing.T) {
    60  	input := "DirectReports($select=FirstName,LastName;$levels=4)"
    61  	ctx := context.Background()
    62  	output, err := ParseExpandString(ctx, input)
    63  
    64  	if err != nil {
    65  		t.Error(err)
    66  		return
    67  	}
    68  
    69  	if output.ExpandItems[0].Path[0].Value != "DirectReports" {
    70  		t.Error("First path item is not 'DirectReports'")
    71  		return
    72  	}
    73  
    74  	if output.ExpandItems[0].Select.SelectItems[0].Segments[0].Value != "FirstName" {
    75  		actual := output.ExpandItems[0].Select.SelectItems[0].Segments[0]
    76  		t.Error("First select segment is '" + actual.Value + "', expected 'FirstName'")
    77  		return
    78  	}
    79  
    80  	if output.ExpandItems[0].Select.SelectItems[1].Segments[0].Value != "LastName" {
    81  		actual := output.ExpandItems[0].Select.SelectItems[1].Segments[0]
    82  		t.Error("First select segment is '" + actual.Value + "', expected 'LastName'")
    83  		return
    84  	}
    85  
    86  	if output.ExpandItems[0].Levels != 4 {
    87  		t.Error("Levels does not equal 4")
    88  		return
    89  	}
    90  
    91  }
    92  
    93  func TestExpandNestedParens(t *testing.T) {
    94  	input := "Products($filter=not (DiscontinuedDate eq null))"
    95  	ctx := context.Background()
    96  	output, err := ParseExpandString(ctx, input)
    97  
    98  	if err != nil {
    99  		t.Error(err)
   100  		return
   101  	}
   102  
   103  	if output.ExpandItems[0].Path[0].Value != "Products" {
   104  		t.Error("First path item is not 'Products'")
   105  		return
   106  	}
   107  
   108  	if output.ExpandItems[0].Filter == nil {
   109  		t.Error("Filter not parsed")
   110  		return
   111  	}
   112  
   113  	if output.ExpandItems[0].Filter.Tree == nil {
   114  		t.Error("Filter tree is null")
   115  		return
   116  	}
   117  
   118  	if output.ExpandItems[0].Filter.Tree.Token.Value != "not" {
   119  		actual := output.ExpandItems[0].Filter.Tree.Token.Value
   120  		t.Error("Root filter value is '" + actual + "', expected 'not'")
   121  		return
   122  	}
   123  }
   124  
   125  func TestExpandNegativeCases(t *testing.T) {
   126  	testcases := []string{
   127  		"Products,",        // Extraneous comma
   128  		"Products//Data",   // Double slash
   129  		"/Products",        // Extraneous leading slash
   130  		"Products/",        // Extraneous trailing slash
   131  		"Orders,/Products", // Extraneous leading slash
   132  		"Products/,Orders", // Extraneous trailing slash
   133  	}
   134  
   135  	for _, testcase := range testcases {
   136  		t.Logf("Expand: %s", testcase)
   137  		ctx := context.Background()
   138  		output, err := ParseExpandString(ctx, testcase)
   139  
   140  		if err == nil {
   141  			t.Error("Expected parsing to return error.")
   142  			return
   143  		}
   144  		if output != nil {
   145  			t.Error("Expected parsing to return nil output.")
   146  			return
   147  		}
   148  	}
   149  }