Ejemplo n.º 1
0
func Test_todoapp_api_GetTasks(t *testing.T) {
	m := setupMartini()

	response := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "http://*****:*****@Computer Level:5 due:2014-02-17"`)
	Contain(t, body, `"Original": "(A) 2012-01-30 @Phone Call Mom @Call +Family"`)
	Contain(t, body, `"Todo": "Turn off TV"`)
	Contain(t, body, `"Id": 4`)

	var tasksFromApi todo.TaskList
	if err := json.Unmarshal([]byte(body), &tasksFromApi); err != nil {
		t.Fatal(err)
	}

	config, err := readConfigurationFile(configFile)
	if err != nil {
		t.Fatal(err)
	}
	tasksFromFile, err := todo.LoadFromFilename(config.TodoTxtFilename)
	if err != nil {
		t.Fatal(err)
	}

	Expect(t, tasksFromApi.String(), tasksFromFile.String())
}
Ejemplo n.º 2
0
func Test_todoapp_api_GetTask(t *testing.T) {
	m := setupMartini()

	// ---------------------------------------------------------------------------
	// get task
	response := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "http://*****:*****@Go +go-todotxt"`)

	var taskFromApi todo.Task
	if err := json.Unmarshal([]byte(body), &taskFromApi); err != nil {
		t.Fatal(err)
	}

	config, err := readConfigurationFile(configFile)
	if err != nil {
		t.Fatal(err)
	}
	tasksFromFile, err := todo.LoadFromFilename(config.TodoTxtFilename)
	if err != nil {
		t.Fatal(err)
	}
	taskFromFile, err := tasksFromFile.GetTask(4)
	if err != nil {
		t.Fatal(err)
	}

	Expect(t, taskFromApi.String(), taskFromFile.String())

	// ---------------------------------------------------------------------------
	// try to get non-existing task
	response = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "http://localhost:4005/api/task/11", nil)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusNotFound)
}
Ejemplo n.º 3
0
func Test_todoapp_api_DeleteTask(t *testing.T) {
	m := setupMartini()

	config, err := readConfigurationFile(configFile)
	if err != nil {
		t.Fatal(err)
	}
	tasksFromFile, err := todo.LoadFromFilename(config.TodoTxtFilename)
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 10)

	if _, err := tasksFromFile.GetTask(5); err != nil {
		t.Fatal(err)
	}

	// ---------------------------------------------------------------------------
	// delete existing task
	response := httptest.NewRecorder()
	req, err := http.NewRequest("DELETE", "http://localhost:4005/api/task/5", nil)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusNoContent)

	body := response.Body.String()
	Expect(t, body, `"{}"`)

	if err := tasksFromFile.LoadFromFilename(config.TodoTxtFilename); err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 9)

	// ---------------------------------------------------------------------------
	// try to delete non-existing task
	response = httptest.NewRecorder()
	req, err = http.NewRequest("DELETE", "http://localhost:4005/api/task/15", nil)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusNotFound)
}
Ejemplo n.º 4
0
// Add todotxt.TaskList to martini context
func TaskList() martini.Handler {
	return func(c martini.Context, r render.Render, config *Config) {
		if err := checkAndCreateFile(config.TodoTxtFilename); err != nil {
			r.HTML(http.StatusInternalServerError, "500", err)
			return
		}

		tasks, err := todo.LoadFromFilename(config.TodoTxtFilename)
		if err != nil {
			r.HTML(http.StatusInternalServerError, "500", err)
			return
		}

		c.Map(tasks)
		c.Next()
	}
}
Ejemplo n.º 5
0
func Test_todoapp_api_DeleteTasks(t *testing.T) {
	m := setupMartini()

	config, err := readConfigurationFile(configFile)
	if err != nil {
		t.Fatal(err)
	}
	tasksFromFile, err := todo.LoadFromFilename(config.TodoTxtFilename)
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 9)

	if _, err := tasksFromFile.GetTask(5); err != nil {
		t.Fatal(err)
	}

	// ---------------------------------------------------------------------------
	// delete completed task / clear tasklist
	response := httptest.NewRecorder()
	req, err := http.NewRequest("DELETE", "http://*****:*****@GroceryStore",`)
	Contain(t, body, `"Id": 2`)
	Contain(t, body, `"Id": 5`)
	NotContain(t, body, `"Id": 0`)

	if err := tasksFromFile.LoadFromFilename(config.TodoTxtFilename); err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 6)

	// ---------------------------------------------------------------------------
	// try to delete completed task / clear tasklist, where there are only open tasks left
	response = httptest.NewRecorder()
	req, err = http.NewRequest("DELETE", "http://localhost:4005/api/tasks", nil)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusOK)

	body = response.Body.String()
	Contain(t, body, `Call dry cleaner @Home due:2014-02-19`)
	Contain(t, body, `"Id": 3`)
	Contain(t, body, `"Id": 6`)
	NotContain(t, body, `"Id": 0`)

	if err := tasksFromFile.LoadFromFilename(config.TodoTxtFilename); err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 6)
}
Ejemplo n.º 6
0
func Test_todoapp_api_PutTask(t *testing.T) {
	m := setupMartini()

	config, err := readConfigurationFile(configFile)
	if err != nil {
		t.Fatal(err)
	}
	tasksFromFile, err := todo.LoadFromFilename(config.TodoTxtFilename)
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 10)

	task, err := tasksFromFile.GetTask(6)
	if err != nil {
		t.Fatal(err)
	}

	Expect(t, task.Completed, false)
	task.Complete()
	Expect(t, task.Completed, true)

	data, err := json.MarshalIndent(task, "", "  ")
	if err != nil {
		t.Fatal(err)
	}
	var formBuffer bytes.Buffer
	formBuffer.Write(data)

	// ---------------------------------------------------------------------------
	// update existing task
	response := httptest.NewRecorder()
	req, err := http.NewRequest("PUT", "http://localhost:4005/api/task/6", &formBuffer)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusOK)
	Expect(t, response.Body.String(), string(data))

	if err := tasksFromFile.LoadFromFilename(config.TodoTxtFilename); err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 10)

	taskFromFile, err := tasksFromFile.GetTask(6)
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, taskFromFile.String(), task.String())
	Expect(t, taskFromFile.Completed, true)

	// ---------------------------------------------------------------------------
	// try to update non-existing task
	formBuffer.Reset()
	formBuffer.Write(data)
	response = httptest.NewRecorder()
	req, err = http.NewRequest("PUT", "http://localhost:4005/api/task/17", &formBuffer)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusNotFound)
}
Ejemplo n.º 7
0
func Test_todoapp_api_PostTask(t *testing.T) {
	m := setupMartini()

	config, err := readConfigurationFile(configFile)
	if err != nil {
		t.Fatal(err)
	}
	tasksFromFile, err := todo.LoadFromFilename(config.TodoTxtFilename)
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 9)

	task, err := todo.ParseTask("(F) Call dry cleaner @Home due:2014-02-19")
	if err != nil {
		t.Fatal(err)
	}

	data, err := json.MarshalIndent(task, "", "  ")
	if err != nil {
		t.Fatal(err)
	}
	var formBuffer bytes.Buffer
	formBuffer.Write(data)

	// ---------------------------------------------------------------------------
	// create new task
	response := httptest.NewRecorder()
	req, err := http.NewRequest("POST", "http://localhost:4005/api/task", &formBuffer)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusCreated)

	body := response.Body.String()
	Contain(t, body, task.Todo)
	Contain(t, body, task.Priority)
	Contain(t, body, task.Contexts[0])
	Contain(t, body, task.DueDate.Format(todo.DateLayout))

	if err := tasksFromFile.LoadFromFilename(config.TodoTxtFilename); err != nil {
		t.Fatal(err)
	}
	Expect(t, len(tasksFromFile), 10)

	taskFromFile, err := tasksFromFile.GetTask(10)
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, taskFromFile.Priority, "F")
	Expect(t, taskFromFile.Todo, "Call dry cleaner")
	Expect(t, taskFromFile.Contexts, []string{"Home"})
	expectedTime, err := time.Parse(todo.DateLayout, "2014-02-19")
	if err != nil {
		t.Fatal(err)
	}
	Expect(t, taskFromFile.DueDate, expectedTime)

	// ---------------------------------------------------------------------------
	// try to create invalid task
	formBuffer.Reset()
	response = httptest.NewRecorder()
	req, err = http.NewRequest("POST", "http://localhost:4005/api/task", &formBuffer)
	if err != nil {
		t.Fatal(err)
	}

	m.ServeHTTP(response, req)
	Expect(t, response.Code, http.StatusBadRequest)
}