Пример #1
0
func TestDeleteEpisode(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	var count int = 4
	populateSampleEpisodes(t, "test 1", count)

	r, err := Delete(fmt.Sprintf("%s/api/episodes/%d", s.URL, 1))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if r.StatusCode != http.StatusOK {
		t.Errorf("expected %d status code, got %d", http.StatusOK, r.StatusCode)
	}

	var rm Episode
	err = json.NewDecoder(r.Body).Decode(&rm)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if 1 != rm.Id {
		t.Errorf("expected returned item id %d, got id %d", 1, rm.Id)
	}

	// Double check this doesn't exist in the database
	if episode, err := database.GetEpisode(1); err == nil {
		t.Errorf("expected error fetching deleted episode, got %v", episode)
	}
}
Пример #2
0
func TestGetEpisode(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	var count int = 4
	populateSampleEpisodes(t, "test 1", count)

	s1, err := database.GetEpisode(3)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := http.Get(fmt.Sprintf("%s/api/episodes/%d", s.URL, s1.Id))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var response Episode
	err = json.NewDecoder(r.Body).Decode(&response)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if s1.Id != response.Id {
		t.Errorf("expected id %d, got %d", s1.Id, response.Id)
	}

	if s1.Title != response.Title {
		t.Errorf("expected title %q, got %q", s1.Title, response.Title)
	}
}
Пример #3
0
// GET /api/episodes/:id
// Returns a single Episode identified by :id
func EpisodeShow(w http.ResponseWriter, r *http.Request) (interface{}, error) {
	idstr := getPathParam(r, "id")
	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		return nil, err
	}

	s, err := database.GetEpisode(id)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, notFound{}
		} else {
			return nil, err
		}
	}

	return s, nil
}
Пример #4
0
func TestCreateEpisode(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	episode := createTestEpisode()
	b, err := json.Marshal(episode)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	r, err := Put(s.URL+"/api/episodes", "application/json", strings.NewReader(fmt.Sprintf("%s", b)))
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	var response Episode
	err = json.NewDecoder(r.Body).Decode(&response)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	if response.Id == 0 {
		t.Errorf("expected non-zero episode Id, got %v", response.Id)
	}

	if response.Title != episode.Title {
		t.Errorf("expected matching titles, got %v", response)
	}

	// Check that it is actually in the database
	_, err = database.GetEpisode(response.Id)
	if err != nil {
		t.Errorf("Failed to find created episode in database: %v", err)
	}
}