github.com/456vv/valexa@v1.0.2-0.20200706152242-1fb922d71ce5/EchoRequest_test.go (about)

     1  package valexa
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  	
     7  )
     8  
     9  func Test_EchoRequest_VerifyTimestamp(testingT *testing.T){
    10  	er := &EchoRequest{Request:EchoRequestRequest{}}
    11  	tests := []struct{
    12  		timestamp 	string
    13  		limit		int
    14  		result		bool
    15  		str			string
    16  	}{
    17  		//限制10s,设置了错误时间
    18  		{timestamp:"2017-12-01T09:00:10Z", limit:10, result: false, str:"111"},
    19  		//限制15s,设置了-14s
    20  		{timestamp:time.Now().UTC().Add(time.Second*(-14)).Format(time.RFC3339), limit:15, result: true, str:"222"},
    21  		//限制15s,设置了-15s
    22  		{timestamp:time.Now().UTC().Add(time.Second*(-15)).Format(time.RFC3339), limit:15, result: false, str:"333"},
    23  		//限制15s,设置了-16s
    24  		{timestamp:time.Now().UTC().Add(time.Second*(-16)).Format(time.RFC3339), limit:15, result: false, str:"444"},
    25  		//限制0s,设置了-16s
    26  		{timestamp:time.Now().UTC().Add(time.Second*(-16)).Format(time.RFC3339), limit:0, result: false, str:"555"},
    27  		//限制0s,设置了-1s
    28  		{timestamp:time.Now().UTC().Add(time.Second*(-1)).Format(time.RFC3339), limit:0, result: false, str:"666"},
    29  		//限制1s,设置了-0s
    30  		{timestamp:time.Now().UTC().Add(time.Second*(0)).Format(time.RFC3339), limit:1, result: true, str:"777"},
    31  	}
    32  	for _, test := range tests {
    33  		er.Request.Timestamp = test.timestamp
    34  		if er.VerifyTimestamp(test.limit) != test.result {
    35  			testingT.Fatal("err", test.str)
    36  		}
    37  	}
    38  }
    39  
    40  func Test_EchoRequest_GetApplicationID(testingT *testing.T){
    41  	er := &EchoRequest{}
    42  	if er.GetApplicationID() != "" {
    43  		testingT.Fatal("err")
    44  	}
    45  	er.Session.Application.ApplicationID="1234"
    46  	if er.GetApplicationID() != "1234" {
    47  		testingT.Fatal("err")
    48  	}
    49  }
    50  
    51  func Test_EchoRequest_GetSessionID(testingT *testing.T){
    52  	er := &EchoRequest{}
    53  	if er.GetSessionID() != "" {
    54  		testingT.Fatal("err")
    55  	}
    56  	er.Session.SessionID="1234"
    57  	if er.GetSessionID() != "1234" {
    58  		testingT.Fatal("err")
    59  	}
    60  }
    61  
    62  func Test_EchoRequest_GetUserID(testingT *testing.T){
    63  	er := &EchoRequest{}
    64  	if er.GetUserID() != "" {
    65  		testingT.Fatal("err")
    66  	}
    67  	er.Session.User.UserID="1234"
    68  	if er.GetUserID() != "1234" {
    69  		testingT.Fatal("err")
    70  	}
    71  }
    72  
    73  func Test_EchoRequest_GetAccessToken(testingT *testing.T){
    74  	er := &EchoRequest{}
    75  	if er.GetAccessToken() != "" {
    76  		testingT.Fatal("err")
    77  	}
    78  	er.Session.User.AccessToken="1234"
    79  	if er.GetAccessToken() != "1234" {
    80  		testingT.Fatal("err")
    81  	}
    82  }
    83  
    84  
    85  
    86  func Test_EchoRequest_GetRequestType(testingT *testing.T){
    87  	er := &EchoRequest{}
    88  	if er.GetRequestType() != "" {
    89  		testingT.Fatal("err")
    90  	}
    91  	er.Request.Type="1234"
    92  	if er.GetRequestType() != "1234" {
    93  		testingT.Fatal("err")
    94  	}
    95  }
    96  
    97  func Test_EchoRequest_GetIntentName(testingT *testing.T){
    98  	er := &EchoRequest{}
    99  	if name := er.GetIntentName(); name != "" {
   100  		testingT.Fatal("err")
   101  	}
   102  	
   103  	er.Request.Type="IntentRequest"
   104  	er.Request.Intent.Name="Iiem"
   105  	
   106  	if intent := er.GetIntentName(); intent != "Iiem" {
   107  		testingT.Fatal("err")
   108  	}
   109  }
   110  
   111  func Test_EchoRequest_GetSlots(testingT *testing.T){
   112  	er := &EchoRequest{}
   113  	er.Request.Type="IntentRequest"
   114  	if _, err := er.GetSlots(); err == nil {
   115  		testingT.Fatal("err")
   116  	}
   117  	
   118  	er.Request.Intent.Slots = make(map[string]*EchoRequestRequestIntentSlot)
   119  	if slots, err := er.GetSlots(); err != nil || slots == nil {
   120  		testingT.Fatal("err")
   121  	}
   122  	
   123  	er.Request.Intent.Slots = make(map[string]*EchoRequestRequestIntentSlot)
   124  	er.Request.Intent.Slots["Item"] = &EchoRequestRequestIntentSlot{
   125  		Name	: "A",
   126  		Value	: "B",
   127  	}
   128  	slots, err := er.GetSlots()
   129  	if err != nil || slots == nil {
   130  		testingT.Fatal("err")
   131  	}
   132  
   133  	if	slot, ok := slots["Item"]; !ok || slot.Name != "A" || slot.Value != "B" {
   134  		testingT.Fatal("err")
   135  	}
   136  }
   137  
   138  func Test_EchoRequest_GetSlotNames(testingT *testing.T){
   139  	er := &EchoRequest{}
   140  	names := er.GetSlotNames()
   141  	if len(names) > 0 {
   142  		testingT.Fatal("err")
   143  	}
   144  	er.Request.Intent.Slots = make(map[string]*EchoRequestRequestIntentSlot)
   145  	names = er.GetSlotNames()
   146  	if len(names) > 0 {
   147  		testingT.Fatal("err")
   148  	}
   149  	er.Request.Type="IntentRequest"
   150  	er.Request.Intent.Slots["Item"] = &EchoRequestRequestIntentSlot{
   151  		Name	: "A",
   152  		Value	: "B",
   153  	}
   154  	names = er.GetSlotNames()
   155  	if len(names) != 1 || names[0] != "Item" {
   156  		testingT.Fatal("err")
   157  	}
   158  }
   159  
   160  func Test_EchoRequest_GetSlotValue(t *testing.T){
   161  	er := &EchoRequest{}
   162  	val, err := er.GetSlotValue("Item")
   163  	if err == nil {
   164  		t.Fatal("err")
   165  	}
   166  	er.Request.Type="IntentRequest"
   167  	er.Request.Intent.Slots = make(map[string]*EchoRequestRequestIntentSlot)
   168  	er.Request.Intent.Slots["Item"] = &EchoRequestRequestIntentSlot{
   169  		Name	: "A",
   170  		Value	: "B",
   171  	}
   172  	val, err = er.GetSlotValue("Item")
   173  	if err != nil || val != "B"  {
   174  		t.Fatal("err")
   175  	}
   176  }
   177  
   178  func Test_EchoRequest_GetSlot_1(testingT *testing.T){
   179  	er := &EchoRequest{}
   180  	slot, err := er.GetSlot("Item")
   181  	if err == nil {
   182  		testingT.Fatal("err")
   183  	}
   184  	er.Request.Intent.Slots = nil
   185  	slot, err = er.GetSlot("Item")
   186  	if err == nil {
   187  		testingT.Fatal("err")
   188  	}
   189  	er.Request.Type="IntentRequest"
   190  	er.Request.Intent.Slots = make(map[string]*EchoRequestRequestIntentSlot)
   191  	er.Request.Intent.Slots["Item"] = &EchoRequestRequestIntentSlot{
   192  		Name	: "A",
   193  		Value	: "B",
   194  	}
   195  	slot, err = er.GetSlot("Item")
   196  	if err != nil || slot.Name != "A" || slot.Value != "B"{
   197  		testingT.Fatal("err")
   198  	}
   199  }