Ejemplo n.º 1
0
func TestNewWorkItemStateForPharos(t *testing.T) {
	workItemState := testutil.MakeWorkItemState()
	pharosItem := models.NewWorkItemStateForPharos(workItemState)
	assert.Equal(t, workItemState.Id, pharosItem.Id)
	assert.Equal(t, workItemState.WorkItemId, pharosItem.WorkItemId)
	assert.Equal(t, workItemState.Action, pharosItem.Action)
	assert.Equal(t, workItemState.State, pharosItem.State)
}
Ejemplo n.º 2
0
func TestWorkItemStateSave(t *testing.T) {
	testServer := httptest.NewServer(http.HandlerFunc(workItemStateSaveHandler))
	defer testServer.Close()

	client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key")
	if err != nil {
		t.Error(err)
		return
	}

	// ---------------------------------------------
	// First, test create...
	// ---------------------------------------------
	obj := testutil.MakeWorkItemState()
	obj.Id = 0
	response := client.WorkItemStateSave(obj)

	// Check the request URL and method
	assert.Equal(t, "POST", response.Request.Method)
	assert.Equal(t, "/api/v2/item_state/", response.Request.URL.Opaque)

	// Basic sanity check on response values
	assert.Nil(t, response.Error)

	obj = response.WorkItemState()
	assert.EqualValues(t, "WorkItemState", response.ObjectType())
	if obj == nil {
		t.Errorf("WorkItemState should not be nil")
	}
	assert.NotEqual(t, 0, obj.Id)
	assert.NotEqual(t, 0, obj.WorkItemId)
	assert.Equal(t, "Ingest", obj.Action)
	assert.Equal(t, `{"key1":"value1","key2":"value2"}`, obj.State)
	assert.False(t, obj.CreatedAt.IsZero())
	assert.False(t, obj.UpdatedAt.IsZero())

	// Make sure the client returns the SAVED object,
	// not the unsaved one we sent.
	assert.NotEqual(t, 0, obj.Id)

	// ---------------------------------------------
	// Now test with an update...
	// ---------------------------------------------
	obj = testutil.MakeWorkItemState()
	origModTime := obj.UpdatedAt
	origId := obj.Id
	origWorkItemId := obj.WorkItemId

	// Change this
	obj.State = `{"key3":"value3"}`
	response = client.WorkItemStateSave(obj)

	// Check the request URL and method
	expectedUrl := fmt.Sprintf("/api/v2/item_state/%d/", obj.Id)
	assert.Equal(t, "PUT", response.Request.Method)
	assert.Equal(t, expectedUrl, response.Request.URL.Opaque)

	// Basic sanity check on response values
	assert.Nil(t, response.Error)

	obj = response.WorkItemState()
	assert.EqualValues(t, "WorkItemState", response.ObjectType())
	if obj == nil {
		t.Errorf("WorkItemState should not be nil")
	}
	assert.Equal(t, origId, obj.Id)
	assert.Equal(t, origWorkItemId, obj.WorkItemId)
	assert.Equal(t, `{"key3":"value3"}`, obj.State)
	assert.NotEqual(t, origModTime, obj.UpdatedAt)
}
Ejemplo n.º 3
0
func workItemStateGetHandler(w http.ResponseWriter, r *http.Request) {
	obj := testutil.MakeWorkItemState()
	objJson, _ := json.Marshal(obj)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintln(w, string(objJson))
}