Example #1
0
func TestJsonify(t *testing.T) {
	req, _ := http.NewRequest("GET", "/path/to/handler", nil)
	resp := mctest.NewMockTestResponse(t)

	data := &struct {
		X string
		Y string
	}{"foo", "bar"}
	Jsonify(func(w http.ResponseWriter, r *http.Request) (interface{}, error) {
		return data, nil
	})(resp, req)
	err := json.Unmarshal(resp.Bytes(), data)
	if err != nil {
		t.Fatalf("Error unmarshaling data %s", err)
	}
	if data.X != "foo" || data.Y != "bar" {
		t.Fatalf("Expected X to be 'foo' but is '%s' and Y to be 'bar' but is '%s'", data.X, data.Y)
	}
	resp = mctest.NewMockTestResponse(t)
	Jsonify(func(w http.ResponseWriter, r *http.Request) (interface{}, error) {
		return nil, errors.New("Test error")
	})(resp, req)
	resp.AssertCode(http.StatusInternalServerError)
	resp = mctest.NewMockTestResponse(t)
	Jsonify(func(w http.ResponseWriter, r *http.Request) (interface{}, error) {
		return nil, nil
	})(resp, req)
	resp.AssertCode(http.StatusOK)
}
Example #2
0
// Contrived example but yeah this is the idea
func TestTxHandler(t *testing.T) {
	db := CurrentDB()
	setupDB(db)
	db.Exec(fmt.Sprintf("DELETE FROM %s;", simpleTestTableName))
	if tableSize() != 0 {
		t.Fatalf("Expected 0 rows in table but has %d", tableSize())
	}
	req, _ := http.NewRequest("GET", "/path/to/handler", nil)
	resp := mctest.NewMockTestResponse(t)

	TxHandler(func(tx *sql.Tx, w http.ResponseWriter, r *http.Request) error {
		log.Printf("Inserting row ....")
		_, err := tx.Exec(fmt.Sprintf("INSERT INTO %s VALUES(1, 1);", simpleTestTableName))
		if err != nil {
			t.Errorf("Failed to exec %s", err)
		}
		return err
	})(resp, req)
	if tableSize() != 1 {
		t.Errorf("Expected 1 rows in table but has %d", tableSize())
	}
	TxHandler(func(tx *sql.Tx, w http.ResponseWriter, r *http.Request) error {
		_, err := tx.Exec(fmt.Sprintf("INSERT INTO %s VALUES(1, 1);", simpleTestTableName))
		if err != nil {
			t.Errorf("Failed to exec %s", err)
		}
		return errors.New("Failed expect automagic rollback please")
	})(resp, req)
	// Should be same number of rows
	if tableSize() != 1 {
		t.Errorf("Expected 1 rows in table but has %d", tableSize())
	}

}
Example #3
0
func TestJSONWrap(t *testing.T) {
	req, _ := http.NewRequest("GET", "/path/to/handler", nil)
	resp := mctest.NewMockTestResponse(t)
	JSONWrap(simpleHandler)(resp, req)
	h := resp.Header().Get("Content-Type")
	if h == "" {
		t.Fatalf("Exepcted runtime header to be set but was not")
	}
	if h != "application/json" {
		t.Fatalf("Expected header to be 'application/json' however is %s", h)
	}
}
Example #4
0
func TestMultiTxHandler(t *testing.T) {
	y := 1970
	year := fmt.Sprintf("%d", y)
	createSQL := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s(year INT, x INT);", testMultiTable)
	createTestDB(t, year, createSQL, testMultiTable)
	//defer cleanUpYears([]int{y})
	req, _ := http.NewRequest("GET", fmt.Sprintf("/api/name/%s/Epic", year), nil)
	resp := mctest.NewMockTestResponse(t)
	MultiTxHandler(func(tx *sql.Tx, w http.ResponseWriter, r *http.Request) error {
		log.Printf("Calling insert now...")
		sql := fmt.Sprintf("INSERT INTO %s VALUES(%d, 1);", testMultiTable, y)
		_, err := tx.Exec(sql)
		if err != nil {
			t.Fatalf("Insert failure error: %s \n%s", err, sql)
		}
		return err
	})(resp, req)
	s, err := yearTableSize(year, testMultiTable)
	if err != nil {
		t.Fatal(err)
	}
	if s != 1 {
		t.Fatalf("Expected 1 row in table but has %d", s)
	}
	MultiTxHandler(func(tx *sql.Tx, w http.ResponseWriter, r *http.Request) error {
		_, err := tx.Exec(fmt.Sprintf("INSERT INTO %s VALUES(1, 1);", testMultiTable))
		if err != nil {
			t.Errorf("Failed to exec %s", err)
		}
		return errors.New("Failed expect automagic rollback please")
	})(resp, req)
	// Should be same number of rows
	s, err = yearTableSize(year, testMultiTable)
	if err != nil {
		t.Fatal(err)
	}
	if s != 1 {
		t.Fatalf("Expected 1 row in table but has %d", s)
	}
}