package db

import (
	"testing"
	"time"
)

func TestPurgeHashrateSamplesBefore(t *testing.T) {
	d, err := New(t.TempDir())
	if err != nil {
		t.Fatal(err)
	}
	defer d.Close()

	_, err = d.Exec("INSERT INTO hashrate_samples (agent_id, hashrate, timestamp) VALUES ('a1', 100, ?)",
		time.Now().Add(-48*time.Hour))
	if err != nil {
		t.Fatal(err)
	}
	_, err = d.Exec("INSERT INTO hashrate_samples (agent_id, hashrate, timestamp) VALUES ('a1', 200, ?)",
		time.Now())
	if err != nil {
		t.Fatal(err)
	}

	n, err := d.PurgeHashrateSamplesBefore(time.Now().Add(-24 * time.Hour))
	if err != nil {
		t.Fatal(err)
	}
	if n != 1 {
		t.Fatalf("expected 1 purged, got %d", n)
	}
}
