示例#1
0
文件: robot.go 项目: bcho/read
func (r *robot) responsePublish(_ tgbot.Message) string {
	defer r.SetIdle()

	daysBefore := 7
	fmt.Sscanf(r.currCommanadArgument, "%d", &daysBefore)
	statsDuration := time.Duration(-daysBefore*24) * time.Hour
	statsSpan := timespan.New(time.Now(), statsDuration)
	things := r.articles.GetInPeriod(statsSpan)

	postUrl, err := r.publisher.Publish(statsSpan, things)
	if err != nil {
		return err.Error()
	}
	return postUrl
}
示例#2
0
文件: robot.go 项目: bcho/read
func (r *robot) responseStats(_ tgbot.Message) string {
	defer r.SetIdle()

	daysBefore := 7
	fmt.Sscanf(r.currCommanadArgument, "%d", &daysBefore)
	statsDuration := time.Duration(-daysBefore*24) * time.Hour
	statsSpan := timespan.New(time.Now(), statsDuration)

	things := r.articles.GetInPeriod(statsSpan)
	return fmt.Sprintf(
		"You read %d article(s) during %s ~ %s:\n\n%s",
		len(things),
		statsSpan.Start().Format("2006-01-02"),
		statsSpan.End().Format("2006-01-02"),
		strings.Join(things, "\n-------------------------------\n\n"),
	)
}
示例#3
0
文件: brain_test.go 项目: bcho/read
func TestForget(t *testing.T) {
	var (
		err             error
		retrievedThing  string
		retrievedThings []string
		present         bool
	)

	at := time.Now()
	key := "foo"
	thing := "bar"
	brain := NewBrain()
	err = brain.Remember(at, key, thing)
	if err != nil {
		t.Error(err)
	}

	retrievedThing, present = brain.Get(key)
	if !present {
		t.Errorf("expected true")
	}
	if retrievedThing != thing {
		t.Errorf("expected %s, got: %s", thing, retrievedThing)
	}

	err = brain.Forget(key)
	if err != nil {
		t.Error(err)
	}

	retrievedThing, present = brain.Get(key)
	if present {
		t.Errorf("expected false")
	}
	span := timespan.New(at, time.Duration(1)*time.Hour)
	retrievedThings = brain.GetInPeriod(span)
	if len(retrievedThings) != 0 {
		t.Errorf("expected 0 things, got: %v", retrievedThings)
	}
}
示例#4
0
文件: brain_test.go 项目: bcho/read
func TestGetInPeriod(t *testing.T) {
	var (
		err    error
		span   timespan.Span
		things []string
	)

	brain := NewBrain()
	err = brain.Remember(parseTime("2006-01-02", "2015-01-01"), "t1", "t1")
	if err != nil {
		t.Error(err)
	}
	err = brain.Remember(parseTime("2006-01-02", "2015-02-01"), "t2", "t2")
	if err != nil {
		t.Error(err)
	}
	err = brain.Remember(parseTime("2006-01-02", "2015-02-01"), "t3", "t3")
	if err != nil {
		t.Error(err)
	}
	err = brain.Remember(parseTime("2006-01-02", "2015-02-02"), "t4", "t4")
	if err != nil {
		t.Error(err)
	}

	// Cover everythings
	span = timespan.New(
		parseTime("2006-01-02", "2015-01-01"),
		time.Duration(365*24)*time.Hour,
	)
	things = brain.GetInPeriod(span)
	if len(things) != 4 {
		t.Errorf("expected 4 things, got: %v", things)
	}

	// Cover nothings (from left side)
	span = timespan.New(
		parseTime("2006-01-02", "2014-01-01"),
		time.Duration(365*24)*time.Hour,
	)
	things = brain.GetInPeriod(span)
	if len(things) != 0 {
		t.Errorf("expected 0 things, got: %v", things)
	}

	// Cover nothings (from right side)
	span = timespan.New(
		parseTime("2006-01-02", "2016-01-01"),
		time.Duration(365*24)*time.Hour,
	)
	things = brain.GetInPeriod(span)
	if len(things) != 0 {
		t.Errorf("expected 0 things, got: %v", things)
	}

	// Partial cover
	span = timespan.New(
		parseTime("2006-01-02", "2015-02-01"),
		time.Duration(1)*time.Hour,
	)
	things = brain.GetInPeriod(span)
	if len(things) != 2 {
		t.Errorf("expected 2 things, got: %v", things)
	}
	if things[0] != "t2" {
		t.Errorf("expected 't2', got: %s", things[0])
	}
	if things[1] != "t3" {
		t.Errorf("expected 't3', got: %s", things[1])
	}
}