github.com/wit-ai/wit-go/v2@v2.0.2/integration_test.go (about)

     1  // +build integration
     2  // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
     3  
     4  package witai
     5  
     6  import (
     7  	"net/http"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  var (
    15  	integrationEntity = Entity{
    16  		ID:  "integration_entity_id",
    17  		Doc: "integration_entity_doc",
    18  	}
    19  	integrationApp = App{
    20  		Name:        "integration_app_id",
    21  		Private:     false,
    22  		Description: "integration_app_desc",
    23  		Lang:        "en",
    24  	}
    25  	integrationEntityUpdateFields = Entity{
    26  		ID:      "integration_entity_id",
    27  		Lookups: []string{"keywords"},
    28  		Doc:     "integration_entity_doc_updated",
    29  	}
    30  )
    31  
    32  func TestIntegrationInvalidToken(t *testing.T) {
    33  	c := NewClient("invalid_token")
    34  	_, err := c.GetEntity(integrationEntity.ID)
    35  	if err == nil {
    36  		t.Fatalf("expected error, got: nil")
    37  	}
    38  }
    39  
    40  func TestIntegrationGetUnknownEntity(t *testing.T) {
    41  	c := getIntegrationClient()
    42  	_, err := c.GetEntity("unknown_id")
    43  	if err == nil {
    44  		t.Fatalf("expected error, got: nil")
    45  	}
    46  }
    47  
    48  func TestIntegrationDeleteUnknownEntity(t *testing.T) {
    49  	c := getIntegrationClient()
    50  	err := c.DeleteEntity("unknown_id")
    51  	if err == nil {
    52  		t.Fatalf("expected error, got: nil")
    53  	}
    54  }
    55  
    56  func TestIntegrationUnknownEntity(t *testing.T) {
    57  	c := getIntegrationClient()
    58  	_, err := c.GetEntity("unknown_id")
    59  	if err == nil {
    60  		t.Fatalf("expected error, got: nil")
    61  	}
    62  }
    63  
    64  func TestIntegrationCreateEntity(t *testing.T) {
    65  	c := getIntegrationClient()
    66  
    67  	// just to make sure we don't create duplicates
    68  	c.DeleteEntity(integrationEntity.ID)
    69  
    70  	// create entity
    71  	entity, err := c.CreateEntity(integrationEntity)
    72  	if err != nil {
    73  		t.Fatalf("expected nil error got: %v", err)
    74  	}
    75  	if entity == nil {
    76  		t.Fatalf("expected non nil entity")
    77  	}
    78  	if entity.Lang != "en" {
    79  		t.Fatalf("expected lang=en, got: %s", entity.Lang)
    80  	}
    81  
    82  	// create may take some time
    83  	time.Sleep(2 * time.Second)
    84  }
    85  
    86  func TestIntegrationUpdateEntity(t *testing.T) {
    87  	c := getIntegrationClient()
    88  
    89  	// update entity
    90  	err := c.UpdateEntity(integrationEntity.ID, integrationEntityUpdateFields)
    91  	if err != nil {
    92  		t.Fatalf("expected nil error, got %v", err)
    93  	}
    94  
    95  	time.Sleep(time.Second)
    96  }
    97  
    98  func TestIntegrationAddEntityValue(t *testing.T) {
    99  	c := getIntegrationClient()
   100  
   101  	var err error
   102  
   103  	// add entity value 1
   104  	if _, err = c.AddEntityValue(integrationEntity.ID, EntityValue{
   105  		Value:       "London",
   106  		Expressions: []string{"London"},
   107  	}); err != nil {
   108  		t.Fatalf("expected nil error, got %v", err)
   109  	}
   110  	// add entity value 2
   111  	if _, err = c.AddEntityValue(integrationEntity.ID, EntityValue{
   112  		Value:       "Ho Chi Minh City",
   113  		Expressions: []string{"Ho Chi Minh City"},
   114  	}); err != nil {
   115  		t.Fatalf("expected nil error, got %v", err)
   116  	}
   117  	// add entity value expression
   118  	if _, err = c.AddEntityValueExpression(integrationEntity.ID, "Ho Chi Minh City", "HoChiMinh"); err != nil {
   119  		t.Fatalf("expected nil error, got %v", err)
   120  	}
   121  	if _, err = c.AddEntityValueExpression(integrationEntity.ID, "Ho Chi Minh City", "hochiminhcity"); err != nil {
   122  		t.Fatalf("expected nil error, got %v", err)
   123  	}
   124  	if err = c.DeleteEntityValueExpression(integrationEntity.ID, "Ho Chi Minh City", "HoChiMinh"); err != nil {
   125  		t.Fatalf("expected nil error, got %v", err)
   126  	}
   127  
   128  	// delete entity value 1
   129  	if err = c.DeleteEntityValue(integrationEntity.ID, "Ho Chi Minh City"); err != nil {
   130  		t.Fatalf("expected nil error, got %v", err)
   131  	}
   132  }
   133  
   134  func TestIntegrationGetEntity(t *testing.T) {
   135  	c := getIntegrationClient()
   136  
   137  	// check entity
   138  	e, err := c.GetEntity(integrationEntity.ID)
   139  	if err != nil {
   140  		t.Fatalf("expected nil error, got %v", err)
   141  	}
   142  
   143  	if len(e.Values) != 1 {
   144  		t.Fatalf("expected 1 value, got %v", e.Values)
   145  	}
   146  
   147  	if e.Doc != integrationEntityUpdateFields.Doc {
   148  		t.Fatalf("expected doc=%s, got %s", integrationEntityUpdateFields.Doc, e.Doc)
   149  	}
   150  
   151  	entities, err := c.GetEntities()
   152  	if err != nil {
   153  		t.Fatalf("expected nil error, got %v", err)
   154  	}
   155  	if len(entities) == 0 {
   156  		t.Fatalf("expected >0 entities, got %v", entities)
   157  	}
   158  
   159  	err = c.DeleteEntity(integrationEntity.ID)
   160  	if err != nil {
   161  		t.Fatalf("expected nil error got err=%v", err)
   162  	}
   163  }
   164  
   165  func TestIntegrationSamples(t *testing.T) {
   166  	c := getIntegrationClient()
   167  
   168  	// cleanup
   169  	c.DeleteSamples([]Sample{
   170  		{
   171  			Text: "I want to fly to SFO",
   172  		},
   173  	})
   174  
   175  	// Deletion takes time
   176  	time.Sleep(time.Second * 5)
   177  
   178  	// samples test
   179  	_, validateErr := c.ValidateSamples([]Sample{
   180  		{
   181  			Text: "I want to fly to SFO",
   182  			Entities: []SampleEntity{
   183  				{
   184  					Entity: "wit$location",
   185  					Value:  "SFO",
   186  					Start:  17,
   187  					End:    20,
   188  				},
   189  			},
   190  		},
   191  	})
   192  
   193  	if validateErr != nil {
   194  		t.Fatalf("expected nil error, got %v", validateErr)
   195  	}
   196  
   197  	// Training takes time
   198  	time.Sleep(time.Second * 20)
   199  
   200  	// get samples
   201  	samples, samplesErr := c.GetSamples(1, 0)
   202  	if samplesErr != nil {
   203  		t.Fatalf("expected nil error, got %v", samplesErr)
   204  	}
   205  	if len(samples) != 1 {
   206  		t.Fatalf("expected 1 sample, got %v", samples)
   207  	}
   208  
   209  	// delete samples
   210  	_, delSamplesErr := c.DeleteSamples([]Sample{
   211  		{
   212  			Text: "I want to fly to SFO",
   213  		},
   214  	})
   215  	if delSamplesErr != nil {
   216  		t.Fatalf("expected nil error, got %v", delSamplesErr)
   217  	}
   218  }
   219  
   220  func TestIntegrationExport(t *testing.T) {
   221  	c := getIntegrationClient()
   222  
   223  	uri, _ := c.Export()
   224  
   225  	if !strings.Contains(uri, "fbcdn.net") {
   226  		t.Fatalf("uri should contain fbcdn.net, got: %s", uri)
   227  	}
   228  }
   229  
   230  func getIntegrationClient() *Client {
   231  	c := NewClient(os.Getenv("WITAI_INTEGRATION_TOKEN"))
   232  	c.SetHTTPClient(&http.Client{
   233  		Timeout: time.Second * 20,
   234  	})
   235  	return c
   236  }