package db_test

import (
	"testing"
	"gitlab.computing.dcu.ie/murphg62/2023-ca400-murphg62-byrnm257/src/backend/pkg/database"
	"os"
)

func TestDbConnect(t *testing.T){

	host := "postgres"
	port := 5432
	user := os.Getenv("POSTGRES_USER")
	password := os.Getenv("POSTGRES_PASSWORD")
	dbname := os.Getenv("POSTGRES_DB")


	DB, err := database.ConnectDB(host, port, user, password, dbname);

	if err != nil {
		t.Errorf("Failed to connect to database: %v", err)
	}

	err = DB.Ping()
	if err != nil {
		t.Errorf("Failed to ping the database: %v", err)
	}
}

func TestDbDisconnect(t *testing.T){

	host := "postgres"
	port := 5432
	user := os.Getenv("POSTGRES_USER")
	password := os.Getenv("POSTGRES_PASSWORD")
	dbname := os.Getenv("POSTGRES_DB")

	DB, err := database.ConnectDB(host, port, user, password, dbname);
	if err!= nil {
		t.Errorf("Failed to connect to database: %v", err)
	}

	err = database.CloseDB(DB)
	if err != nil {
		t.Errorf("Failed to disconnect from the database: %v", err)
	}
}
