Exemplo n.º 1
0
func TestKeyComplete(t *testing.T) {
	ctx, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}

	dueDate := time.Date(2016, 2, 29, 13, 0, 0, 0, time.UTC)
	k := writeTodoItem(ctx, "hello", dueDate, false, &testUser, false)
	switch (*k).(type) {
	case TodoID:
		k1 := (*k).(TodoID)
		k2 := datastore.Key(k1)
		assert(t, !k2.Incomplete(), "write returned an incomplete key")
	case E, TodoItem:
		t.Fatal(fmt.Sprintf("Expected write to return a todo ID, got something else: %s", *k))
	}

	defer done()
}
Exemplo n.º 2
0
// write a new item; read it back; modify the due date; check that we see the change
func TestMemcacheUpdate(t *testing.T) {
	ctx, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}
	dueDate := time.Date(2016, 2, 29, 13, 0, 0, 0, time.UTC)
	dueDate1 := time.Date(2016, 3, 12, 13, 0, 0, 0, time.UTC)

	id := writeTodoItem(ctx, "Brush my teeth", dueDate, false, &testUser, false)
	switch (*id).(type) {
	case TodoID:
		id1 := (*id).(TodoID)
		item := readTodoItem(ctx, id1)
		t.Log("item = ", *item)
		switch (*item).(type) {
		case TodoItem:
			int_id := datastore.Key(id1)
			updateTodoItem(ctx, testUser.Email, "Brush my teeth", dueDate1, false, int_id.IntID())
			cached_value, err := memcache.Get(ctx, int_id.String())
			if err != nil {
				t.Fatal("memcache.Get returned a weird result")
			}
			parsed_value := jsonToTodoItem(cached_value.Value)
			switch (*parsed_value).(type) {
			case TodoItem:
				cached_item := (*parsed_value).(TodoItem)
				assert(t,
					cached_item.DueDate == dueDate1,
					"cached item due date doesn't reflect update")
			default:
				t.Fatal("error encoding memcached item")
			}
		default:
			t.Fatal("readTodoItem returned a weird result: ", *item)
		}
	default:
		t.Fatal("writeTodoItem returned a weird result")
	}

	defer done()
}
Exemplo n.º 3
0
// write 1 todo item
// list todo items: should be 1 item, not complete
// update the first item
// list todo items: should be 1 item, complete
func TestListUpdateList(t *testing.T) {

	ctx, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}
	dueDate := time.Date(2016, 2, 29, 13, 0, 0, 0, time.UTC)

	id := writeTodoItem(ctx, "phone up my friend", dueDate, false, &testUser, false)
	switch (*id).(type) {
	case TodoID:
		id1 := (*id).(TodoID)
		k := datastore.Key(id1)
		listResults := listTodoItems(ctx, &testUser)
		switch (*listResults).(type) {
		case Matches:
			items := ([]Match)((*listResults).(Matches))
			assert(t, len(items) == 1, fmt.Sprintf("wrong number of todo items: expected 1, saw %d", len(items)))
			result := updateTodoItem(ctx, testUser.Email, "phone up my friend", dueDate, true, k.IntID())
			assert(t, *result == Ok{}, fmt.Sprintf("error updating item: %s", *result))
			_, err := memcache.Get(ctx, testUser.Email)
			assert(t, err == memcache.ErrCacheMiss, "user's todo list was still cached after updating an item")
			listResults1 := listTodoItems(ctx, &testUser)
			switch (*listResults1).(type) {
			case Matches:
				items1 := ([]Match)((*listResults1).(Matches))
				assert(t, len(items1) == 1, fmt.Sprintf("wrong number of todo items: expected 1, saw %d", len(items1)))
				assert(t, items1[0].Value.State == "completed", fmt.Sprintf("expected completed task, saw: %s", items1[0].Value.State))
			default:
				t.Fatal(fmt.Sprintf("weird result from second listTodoItems: %s", *listResults1))
			}
		default:
			t.Fatal("weird result from listTodoItems")
		}
	default:
		t.Fatal("weird result from writeTodoItem")
	}

	defer done()

}
Exemplo n.º 4
0
// Takes a todo item ID, returns a todo item
func readTodoItem(ctx context.Context, itemID TodoID) *MaybeError {
	// n.b. doesn't check the owner
	item := new(TodoItem)
	var err error
	var result = new(MaybeError)
	var key = new(datastore.Key)
	*key = datastore.Key(itemID)
	log("calling Get on: " + (*key).String())
	maybeCached := lookupCache(ctx, *key)
	if (*maybeCached != CacheMiss{}) {
		// item was cached, return it
		return maybeCached
	}

	if err = datastore.Get(ctx, key, item); err != nil {
		log("read failed: " + err.Error())
		*result = E(err.Error())
	} else {
		log("read succeeded with " + item.Description)
		*result = *item
	}
	updateCache(ctx, *key, *item)
	return (result)
}
Exemplo n.º 5
0
// read an item; check that the item is in the cache
func TestMemcacheRead(t *testing.T) {
	ctx, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}
	dueDate := time.Date(2016, 2, 29, 13, 0, 0, 0, time.UTC)

	id := writeTodoItem(ctx, "Brush my teeth", dueDate, false, &testUser, false)
	switch (*id).(type) {
	case TodoID:
		id1 := (*id).(TodoID)
		item := readTodoItem(ctx, id1)
		switch (*item).(type) {
		case TodoItem:
			read_item := (*item).(TodoItem)
			k := datastore.Key(id1)
			cache_item, err := memcache.Get(ctx, k.String())
			if err != nil {
				t.Fatal("memcache error")
			}
			cache_value := jsonToTodoItem(cache_item.Value)
			switch (*cache_value).(type) {
			case TodoItem:
				cached_item := (*cache_value).(TodoItem)
				assert(t, read_item == cached_item, "Cached todo item differs from the original item")
			default:
				t.Fatal("memcache.Get returned a weird result")
			}
		default:
			t.Fatal("readTodoItem returned a weird result")
		}
	default:
		t.Fatal("writeTodoItem returned a weird result")
	}
	defer done()
}