Beispiel #1
0
// loadRelationships adds relationships to run tests.
func loadRelationships(context interface{}, db *db.DB) error {
	rels, err := relationshipfix.Get()
	if err != nil {
		return err
	}

	if err := relationshipfix.Add(context, db, rels[0:2]); err != nil {
		return err
	}

	return nil
}
// setup initializes for each indivdual test.
func setup(t *testing.T) ([]relationship.Relationship, *db.DB) {
	tests.ResetLog()

	rels, err := relationshipfix.Get()
	if err != nil {
		t.Fatalf("%s\tShould load relationship records from file : %v", tests.Failed, err)
	}
	t.Logf("%s\tShould load relationship records from file.", tests.Success)

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}

	return rels, db
}
// TestUpsertRelationship tests the insert and update of a relationship.
func TestUpsertRelationship(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to insert and then update a relationship.")
	{
		//----------------------------------------------------------------------
		// Get the fixture.

		rels, err := relationshipfix.Get()
		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)

		relStrData, err := json.Marshal(&rels[2])
		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 Relationship.

		url := "/v1/relationship"
		r := httptest.NewRequest("PUT", url, bytes.NewBuffer(relStrData))
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

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

		//----------------------------------------------------------------------
		// Retrieve the Relationship.

		url = "/v1/relationship/" + relPrefix + "flagged"
		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 != 200 {
				t.Fatalf("\t%s\tShould be able to retrieve the relationship : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the relationship.", tests.Success)

			var rel relationship.Relationship
			if err := json.Unmarshal(w.Body.Bytes(), &rel); 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 !reflect.DeepEqual(rels[2], rel) {
				t.Logf("\t%+v", rels[2])
				t.Logf("\t%+v", rel)
				t.Fatalf("\t%s\tShould be able to get back the same relationship.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same relationship.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Update the Relationship.

		rels[2].InString = "blocked"

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

		url = "/v1/relationship"
		r = httptest.NewRequest("PUT", url, bytes.NewBuffer(relStrData))
		w = httptest.NewRecorder()

		a.ServeHTTP(w, r)

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

		//----------------------------------------------------------------------
		// Retrieve the Relationship.

		url = "/v1/relationship/" + relPrefix + "flagged"
		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 != 200 {
				t.Fatalf("\t%s\tShould be able to retrieve the relationship : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the relationship.", tests.Success)

			var relUpdated relationship.Relationship
			if err := json.Unmarshal(w.Body.Bytes(), &relUpdated); 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 relUpdated.InString != "blocked" {
				t.Log(w.Body.String())
				t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
			}
			t.Logf("\t%s\tShould get the expected result.", tests.Success)
		}
	}
}