예제 #1
0
// loadPatterns adds patterns to run tests.
func loadPatterns(context interface{}, db *db.DB) error {
	ps, _, err := patternfix.Get()
	if err != nil {
		return err
	}

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

	return nil
}
예제 #2
0
// setup initializes for each indivdual test.
func setup(t *testing.T) ([]pattern.Pattern, *db.DB) {
	tests.ResetLog()

	patterns, _, err := patternfix.Get()
	if err != nil {
		t.Fatalf("%s\tShould load pattern records from the fixture file : %v", tests.Failed, err)
	}
	t.Logf("%s\tShould load pattern records from the fixture 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 patterns, db
}
예제 #3
0
// TestUpsertPattern tests the insert and update of a pattern.
func TestUpsertPattern(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

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

		ps, _, err := patternfix.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)

		pStrData, err := json.Marshal(&ps[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 Pattern.

		url := "/v1/pattern"
		r := httptest.NewRequest("PUT", url, bytes.NewBuffer(pStrData))
		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 pattern : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to insert the pattern.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Retrieve the Pattern.

		url = "/v1/pattern/" + patternPrefix + "asset"
		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 pattern : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the pattern.", tests.Success)

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

		//----------------------------------------------------------------------
		// Update the Pattern.

		ps[2].Type = patternPrefix + "article"

		pStrData, err = json.Marshal(ps[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/pattern"
		r = httptest.NewRequest("PUT", url, bytes.NewBuffer(pStrData))
		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 pattern : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to update the pattern.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Retrieve the Pattern.

		url = "/v1/pattern/" + patternPrefix + "article"
		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 pattern : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the pattern.", tests.Success)

			var pUpdated pattern.Pattern
			if err := json.Unmarshal(w.Body.Bytes(), &pUpdated); 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 pUpdated.Type != patternPrefix+"article" {
				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)
		}
	}
}