// TestUpdateSiteStatus tests updating the up/down status of the site.
func TestUpdateSiteStatus(t *testing.T) {
	db, err := database.InitializeTestDB("")
	if err != nil {
		t.Fatal("Failed to create database:", err)
	}
	defer db.Close()

	// First create a site to update status.
	s := database.Site{SiteID: 1, Name: "Test", IsActive: true, URL: "http://www.google.com", PingIntervalSeconds: 60, TimeoutSeconds: 30}
	err = s.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create new site:", err)
	}

	// Update the status of the site to down
	err = s.UpdateSiteStatus(db, false)
	if err != nil {
		t.Fatal("Failed to update site status:", err)
	}

	//Get the saved site
	var updatedSite database.Site
	err = updatedSite.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve updated site:", err)
	}

	if updatedSite.IsSiteUp != false {
		t.Errorf("Site status should be down.")
	}

	// Update the status of the site to up
	err = s.UpdateSiteStatus(db, true)
	if err != nil {
		t.Fatal("Failed to update site status:", err)
	}

	// Update the first ping time of the site.
	firstPingTime := time.Date(2015, time.November, 10, 23, 22, 22, 00, time.UTC)
	err = s.UpdateSiteFirstPing(db, firstPingTime)
	if err != nil {
		t.Fatal("Failed to update first ping time:", err)
	}

	err = updatedSite.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve updated site:", err)
	}

	if updatedSite.IsSiteUp != true {
		t.Errorf("Site status should be up.")
	}

	if updatedSite.FirstPing != firstPingTime {
		t.Errorf("Site first ping time %s does not match input %s.", updatedSite.FirstPing, firstPingTime)
	}

}