github.com/wit-ai/wit-go/v2@v2.0.2/intent_test.go (about) 1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 3 package witai 4 5 import ( 6 "net/http" 7 "net/http/httptest" 8 "reflect" 9 "testing" 10 ) 11 12 func TestGetIntents(t *testing.T) { 13 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 14 w.Write([]byte(`[ 15 { 16 "id": "2690212494559269", 17 "name": "buy_car" 18 }, 19 { 20 "id": "254954985556896", 21 "name": "get_weather" 22 }, 23 { 24 "id": "233273197778131", 25 "name": "make_call" 26 } 27 ]`)) 28 })) 29 defer testServer.Close() 30 31 c := NewClient(unitTestToken) 32 c.APIBase = testServer.URL 33 intents, _ := c.GetIntents() 34 35 wantIntents := []Intent{ 36 {ID: "2690212494559269", Name: "buy_car"}, 37 {ID: "254954985556896", Name: "get_weather"}, 38 {ID: "233273197778131", Name: "make_call"}, 39 } 40 41 if !reflect.DeepEqual(intents, wantIntents) { 42 t.Fatalf("expected\n\tintents: %v\n\tgot: %v", wantIntents, intents) 43 } 44 } 45 46 func TestCreateIntent(t *testing.T) { 47 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 48 w.Write([]byte(`{ 49 "id": "13989798788", 50 "name": "buy_flowers" 51 }`)) 52 })) 53 defer testServer.Close() 54 55 c := NewClient(unitTestToken) 56 c.APIBase = testServer.URL 57 intent, err := c.CreateIntent("buy_flowers") 58 59 wantIntent := &Intent{ 60 ID: "13989798788", 61 Name: "buy_flowers", 62 } 63 64 if err != nil { 65 t.Fatalf("nil error expected, got %v", err) 66 } 67 if !reflect.DeepEqual(wantIntent, intent) { 68 t.Fatalf("expected\n\tentity: %v\n\tgot: %v", wantIntent, intent) 69 } 70 } 71 72 func TestGetIntent(t *testing.T) { 73 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 74 w.Write([]byte(`{ 75 "id": "13989798788", 76 "name": "buy_flowers", 77 "entities": [{ 78 "id": "9078938883", 79 "name": "flower:flower" 80 },{ 81 "id": "11223229984", 82 "name": "wit$contact:contact" 83 }] 84 }`)) 85 })) 86 defer testServer.Close() 87 88 c := NewClient(unitTestToken) 89 c.APIBase = testServer.URL 90 intent, err := c.GetIntent("buy_flowers") 91 92 wantIntent := &Intent{ 93 ID: "13989798788", 94 Name: "buy_flowers", 95 Entities: []Entity{ 96 {ID: "9078938883", Name: "flower:flower"}, 97 {ID: "11223229984", Name: "wit$contact:contact"}, 98 }, 99 } 100 101 if err != nil { 102 t.Fatalf("nil error expected, got %v", err) 103 } 104 if !reflect.DeepEqual(wantIntent, intent) { 105 t.Fatalf("expected\n\tentity: %v\n\tgot: %v", wantIntent, intent) 106 } 107 } 108 109 func TestDeleteIntent(t *testing.T) { 110 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 111 w.Write([]byte(`{"deleted": "buy_flowers"}`)) 112 })) 113 defer testServer.Close() 114 115 c := NewClient(unitTestToken) 116 c.APIBase = testServer.URL 117 if err := c.DeleteIntent("buy_flowers"); err != nil { 118 t.Fatalf("nil error expected, got %v", err) 119 } 120 }