Пример #1
0
func TestRoundHourDown(t *testing.T) {
	timeVal := getStartTime()
	numChanges := 100

	// Different intervals that will be randomly added to the value
	intervals := []time.Duration{
		time.Millisecond,
		time.Second,
		time.Minute,
		time.Hour,
	}

	for i := 0; i < numChanges; i++ {
		roundedTime := RoundHourDown(timeVal)

		if roundedTime.Minute() != 0 {
			t.Fatalf(fmt.Sprintf("Time %v should have been rounded down to the nearst hour", timeVal))
		}

		if roundedTime.Hour() != timeVal.Hour() {
			t.Fatalf(fmt.Sprintf("Times do not have matching hours. Non-rounded: %v, rounded: %v", timeVal, roundedTime))
		}

		// adjust the time val
		interval := intervals[lib.RandomInt(len(intervals)-1)]
		randomVal := lib.RandomInt(200)

		timeVal = timeVal.Add(time.Duration(randomVal) * interval)
	}
}
Пример #2
0
func TestSortQuickStats(t *testing.T) {
	numQuickStats := 20
	quickStats := make([]*m.QuickStats, 0, numQuickStats)
	for i := 0; i < numQuickStats; i++ {
		quickStat := &m.QuickStats{}
		quickStat.Visits = lib.RandomInt(100)
		quickStats = append(quickStats, quickStat)
	}

	sorted := SortQuickStats(quickStats)

	if !confirmQuickStatsSort(sorted) {
		t.Fatalf("Sort failed: %v", sorted)
	}
}
Пример #3
0
func TestSaveTimeInterval(t *testing.T) {
	t.Skip("No Mongo tests allowed")
	mongoUri := os.Getenv("MONGO_URI")
	if mongoUri == "" {
		t.Fatalf("No MONGO_URI env variable set")
	}

	session := lib.DBConnect(mongoUri)
	defer lib.DBClose(session)

	// Save a bunch of articles
	numArticles := 20
	articles := make([]*m.Article, 0, numArticles)
	for i := 0; i < numArticles; i++ {
		article := &m.Article{}
		article.ArticleId = i + 1
		articles = append(articles, article)
	}
	/*err := a.SaveArticles(mongoUri, articles)
	if err != nil {
		t.Fatalf("%v", err)
	}*/

	// Calculate a bunch of the time intervals
	topPages := make([]*mc.TopArticle, 0, numArticles)
	visits := map[int]int{}
	for i := 0; i < numArticles; i++ {
		article := &mc.TopArticle{}
		articleId := i + 1
		numVisits := lib.RandomInt(500)

		article.ArticleId = articleId
		article.Visits = numVisits

		visits[articleId] = numVisits
		topPages = append(topPages, article)
	}
	CalculateTimeInterval(topPages, session)

	// Now check the articles saved and make sure they updated the visits
	savedArticles := make([]*m.Article, 0, numArticles)
	articleCol := session.DB("").C("Article")
	articleCol.Find(bson.M{
		"article_id": bson.M{
			"$gte": 1,
			"$lte": 20,
		},
	}).All(&savedArticles)

	if len(savedArticles) != numArticles {
		t.Fatalf("Failed to get the right number of articles from the DB")
	}

	// Verify the visits match up
	for _, article := range savedArticles {
		articleId := article.ArticleId
		if len(article.Visits) != 1 {
			t.Fatalf("Should be exactly one visit in the array, there are %d", len(article.Visits))
		}

		numVisits, ok := visits[articleId]
		if !ok {
			t.Fatalf("Failed to find visits in map for articleId=%d", articleId)
		}

		if numVisits != article.Visits[0].Max {
			t.Fatalf("Article %d: Expected: %d visits. Actual: %d visits", articleId, numVisits, article.Visits[0].Max)
		}
	}
}