Ejemplo n.º 1
0
func TestInsertClassifier(t *testing.T) {

	testServer := fakehttp.NewHTTPServerWithPort(NextPort())
	testServer.Start()

	// response when go-couch tries to see that the server is up
	testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)

	// response when go-couch check is db exists
	testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)

	// insert succeeds
	testServer.Response(200, jsonHeaders(), `{"id": "classifier", "rev": "bar", "ok": true}`)

	configuration := NewDefaultConfiguration()
	configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)

	classifier := NewClassifier(*configuration)
	classifier.SpecificationUrl = "http://s3.com/proto.txt"
	classifier.TrainingJobID = "123"

	err := classifier.Insert()

	assert.True(t, err == nil)
	assert.Equals(t, "classifier", classifier.Id)
	assert.Equals(t, "bar", classifier.Revision)

}
Ejemplo n.º 2
0
func TestUpdateProcessingState(t *testing.T) {

	testServer := fakehttp.NewHTTPServerWithPort(NextPort())
	testServer.Start()

	// response when go-couch tries to see that the server is up
	testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)

	// response when go-couch check is db exists
	testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)

	// first update returns 409
	testServer.Response(409, jsonHeaders(), "")

	// response to GET to refresh
	testServer.Response(200, jsonHeaders(), `{"_id": "training_job", "_rev": "bar", "processing-state": "pending"}`)

	// second update succeeds
	testServer.Response(200, jsonHeaders(), `{"id": "training_job", "rev": "bar"}`)

	configuration := NewDefaultConfiguration()
	configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)

	trainingJob := NewTrainingJob(*configuration)
	trainingJob.ElasticThoughtDoc.Id = "training_job"
	trainingJob.ElasticThoughtDoc.Revision = "rev"

	trainingJob.ProcessingState = Pending

	ok, err := trainingJob.UpdateProcessingState(Processing)
	assert.True(t, err == nil)
	assert.True(t, ok)

}
Ejemplo n.º 3
0
func TestUpdateClassifyJobProcessingState(t *testing.T) {

	testServer := fakehttp.NewHTTPServerWithPort(NextPort())
	testServer.Start()

	// response when go-couch tries to see that the server is up
	testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)

	// response when go-couch check is db exists
	testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)

	// first update returns 409
	testServer.Response(409, jsonHeaders(), "")

	// response to GET to refresh
	testServer.Response(200, jsonHeaders(), `{"_id": "classify_job", "_rev": "rev2", "processing-state": "pending"}`)

	// second update succeeds
	testServer.Response(200, jsonHeaders(), `{"id": "classify_job", "rev": "rev3"}`)

	configuration := NewDefaultConfiguration()
	configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)

	classifyJob := NewClassifyJob(*configuration)
	classifyJob.Id = "classify_job"
	classifyJob.Revision = "rev1"
	classifyJob.ClassifierID = "123"
	classifyJob.ProcessingState = Pending

	updated, err := classifyJob.UpdateProcessingState(Processing)
	assert.True(t, updated)
	assert.True(t, err == nil)
	assert.Equals(t, classifyJob.ProcessingState, Processing)

}
Ejemplo n.º 4
0
func TestSetSpecificationUrl(t *testing.T) {

	testServer := fakehttp.NewHTTPServerWithPort(NextPort())
	testServer.Start()

	// response when go-couch tries to see that the server is up
	testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)

	// response when go-couch check is db exists
	testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)

	// update succeeds
	testServer.Response(200, jsonHeaders(), `{"id": "classifier", "rev": "bar"}`)

	configuration := NewDefaultConfiguration()
	configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)

	classifier := NewClassifier(*configuration)
	classifier.Id = "classifier"
	classifier.Revision = "foo"

	err := classifier.SetSpecificationUrl("whatever")
	assert.True(t, err == nil)

	// make assertions about outgoing request
	for _, savedReq := range testServer.SavedRequests {

		path := savedReq.Request.URL.Path

		if strings.HasSuffix(path, "db/classifier") {
			var requestDictionary map[string]interface{}
			err := json.Unmarshal(savedReq.Data, &requestDictionary)
			assert.True(t, err == nil)
			assert.Equals(t, requestDictionary["_id"], "classifier")
			assert.Equals(t, requestDictionary["_rev"], "foo")
			assert.Equals(t, requestDictionary["specification-url"], "whatever")

		}

	}

}
Ejemplo n.º 5
0
func TestFindAllUnits(t *testing.T) {

	FLEET_API_ENDPOINT = "http://localhost:5977"

	mockFleetApi := fakehttp.NewHTTPServerWithPort(5977)
	mockFleetApi.Start()

	assetName := "data-test/fleet_api_units.json"
	mockResponse, err := Asset(assetName)
	assert.True(t, err == nil)
	mockFleetApi.Response(200, jsonHeaders(), string(mockResponse))

	c := CouchbaseFleet{}
	allUnits, err := c.findAllFleetUnits()
	if err != nil {
		log.Printf("err: %v", err)
	}
	assert.True(t, err == nil)

	assert.Equals(t, len(allUnits), 20)

}
Ejemplo n.º 6
0
func TestUpdateModelUrl(t *testing.T) {

	docId := "training_job"
	expectedTrainedModelUrl := fmt.Sprintf("%v%v/trained.caffemodel", CBFS_URI_PREFIX, docId)

	testServer := fakehttp.NewHTTPServerWithPort(NextPort())
	testServer.Start()

	// response when go-couch tries to see that the server is up
	testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)

	// response when go-couch check is db exists
	testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)

	// first update returns 409
	testServer.Response(409, jsonHeaders(), "")

	// response to GET to refresh
	testServer.Response(200, jsonHeaders(), `{"_id": "training_job", "_rev": "bar"}`)

	// second update succeeds
	testServer.Response(200, jsonHeaders(), `{"id": "training_job", "rev": "bar"}`)

	configuration := NewDefaultConfiguration()
	configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)

	trainingJob := NewTrainingJob(*configuration)
	trainingJob.ElasticThoughtDoc.Id = docId
	trainingJob.ElasticThoughtDoc.Revision = "rev"

	trainingJob.ProcessingState = Pending

	err := trainingJob.updateCaffeModelUrl()

	assert.True(t, err == nil)
	assert.Equals(t, trainingJob.TrainedModelUrl, expectedTrainedModelUrl)

}
func TestDecodeImageUrl(t *testing.T) {

	port := 8080
	fakeDecodedOcr := "fake ocr"
	sourceServer := fakehttp.NewHTTPServerWithPort(port)
	sourceServer.Start()
	headers := map[string]string{"Content-Type": "text/plain"}
	sourceServer.Response(200, headers, fakeDecodedOcr)

	openOcrUrl := fmt.Sprintf("http://localhost:%d", port)

	ocrRequest := OcrRequest{
		ImgUrl:     "http://fake.io/a.png",
		EngineType: ENGINE_TESSERACT,
	}

	openOcrClient := NewHttpClient(openOcrUrl)

	ocrDecoded, err := openOcrClient.DecodeImageUrl(ocrRequest)
	assert.True(t, err == nil)
	assert.Equals(t, ocrDecoded, fakeDecodedOcr)

}
Ejemplo n.º 8
0
func TestRandomNode(t *testing.T) {

	testServer := fakehttp.NewHTTPServerWithPort(8484)
	testServer.Start()

	jsonContentType := map[string]string{"Content-Type": "application/json"}

	// when cbfs tries to query list of nodes, return empty value
	testServer.Response(200, jsonContentType, `{}`)

	c, err := New("http://localhost:8484/")
	if err != nil {
		t.Fatalf("Error parsing thing: %v", err)
	}

	_, _, err = c.RandomNode()
	log.Printf("err: %v", err)
	if err == nil {
		// since we don't have any nodes, RandomNode() should
		// return an error
		t.Fatalf("Expected error calling randomnode: %v", err)
	}

}