Exemplo n.º 1
0
// TestInferId tests the inference of an item_id from type and source id.
func TestInferId(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	// Test the inference of item_id where source_id is present.
	d, err := itemfix.GetData("data.json")
	if err != nil {
		t.Fatalf("\t%s\tShould be able to load item data.json fixture: %v", tests.Failed, err)
	}

	// Create an item out of the data.
	it := item.Item{
		Type:    "test_type",
		Version: 1,
		Data:    d,
	}

	// Infer the id from the data.
	if err := it.InferIDFromData(); err != nil {
		t.Fatalf("\t%s\tShould be able to InferID from data containing field id: %v", tests.Failed, err)
	}

	// Check to ensure the id is as expected.
	if it.ID != fmt.Sprintf("%s_%v", it.Type, d["id"]) {
		t.Fatalf("\t%s\tShould infer item_id of form type + \"_\" + source_id: %v", tests.Failed, err)
	}

	// Test the inference of item_id where source_id is not present.
	d, err = itemfix.GetData("data_without_id.json")
	if err != nil {
		t.Fatalf("\t%s\tShould be able to load item data_without_id.json fixture: %v", tests.Failed, err)
	}

	// Create an item out of the data.
	it = item.Item{
		Type:    "test_type",
		Version: 1,
		Data:    d,
	}

	// Ensure that the id fails without the source_id in data.
	if err := it.InferIDFromData(); err == nil {
		t.Fatalf("\t%s\tShould not be able to InferID from data not containing field id: %v", tests.Failed, err)
	}

}
Exemplo n.º 2
0
// TestUpsertData tests the upsert of data into an item.
func TestUpsertData(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to insert items received as data.")
	{
		//----------------------------------------------------------------------
		// Get the fixture.

		dat, err := itemfix.GetData("data.json")
		if err != nil {
			t.Fatalf("\t%s\tShould be able to retrieve the fixture : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to retrieve the fixture.", tests.Success)

		itemStrData, err := json.Marshal(dat)
		if err != nil {
			t.Fatalf("\t%s\tShould be able to marshal the fixture : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to marshal the fixture.", tests.Success)

		//----------------------------------------------------------------------
		// Insert the Item.

		typ := "PTEST_comment"
		url := "/v1/data/" + typ
		r := httptest.NewRequest("POST", url, bytes.NewBuffer(itemStrData))
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to insert : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to insert the data : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to insert the data.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Retrieve the item.

		url = "/v1/item/" + fmt.Sprintf("%s_%v", typ, dat["id"])
		r = httptest.NewRequest("GET", url, nil)
		w = httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to get : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to retrieve the item : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the item.", tests.Success)

			var itemsBack []item.Item
			if err := json.Unmarshal(w.Body.Bytes(), &itemsBack); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if len(itemsBack) == 0 {
				t.Fatalf("\t%s\tShould be able to get back the same item.", tests.Failed)
			}

			if itemsBack[0].ID != fmt.Sprintf("%s_%v", typ, dat["id"]) || itemsBack[0].Type != typ {
				t.Fatalf("\t%s\tShould be able to get back the same item.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same item.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Delete the Item.

		url = "/v1/item/" + fmt.Sprintf("%s_%v", typ, dat["id"])
		r = httptest.NewRequest("DELETE", url, nil)
		w = httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to delete : %s", url)
		{
			if w.Code != 204 {
				t.Fatalf("\t%s\tShould be able to delete the item : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to delete the item.", tests.Success)
		}

	}
}