diff --git a/.env b/.env index e69de29..734e151 100644 --- a/.env +++ b/.env @@ -0,0 +1,4 @@ +DB_USER=mx +DB_NAME=crud_db +DB_HOST=localhost +DB_PORT=5432 \ No newline at end of file diff --git a/.github/workflows/go-build.yml b/.github/workflows/go-build.yml deleted file mode 100644 index ebfd0dc..0000000 --- a/.github/workflows/go-build.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Go Build Check - -on: - push: - branches: - - "master" - - "develop" - pull_request: - branches: - - "master" - - "develop" - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout Code - uses: actions/checkout@v4 - - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version: 1.23.4 # Adjust the version as needed - - - name: Install Dependencies - run: go mod tidy - - - name: Build Project - run: go build -v ./... diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml new file mode 100644 index 0000000..0f99312 --- /dev/null +++ b/.github/workflows/workflow.yml @@ -0,0 +1,55 @@ +name: Go Build and Test with Database + +on: + push: + branches: + - "master" + - "develop" + pull_request: + branches: + - "master" + - "develop" + +jobs: + test: + runs-on: ubuntu-latest + services: + postgres: + image: postgres:15 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: testdb + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U postgres -d testdb" + --health-interval 5s + --health-timeout 5s + --health-retries 5 + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: 1.23.4 + + - name: Install Dependencies + run: go mod tidy + + - name: Wait for PostgreSQL to Start + run: sleep 10 # Ensure PostgreSQL is fully initialized + + - name: Create .env file for testing + run: | + echo "DB_USER=postgres" > .env + echo "DB_PASSWORD=postgres" >> .env + echo "DB_NAME=testdb" >> .env + echo "DB_HOST=localhost" >> .env + echo "DB_PORT=5432" >> .env + + - name: Run Tests with Database + run: go test -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/models/user_test.go b/models/user_test.go index 8d53db9..5d15fc4 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -2,17 +2,40 @@ package models import ( "database/sql" + "fmt" + "log" + "os" + "strings" "testing" "user_crud/config" + "github.com/joho/godotenv" _ "github.com/lib/pq" // PostgreSQL driver ) func setupTestDB() { - // Initialize the test database connection - var err error - config.DB, err = sql.Open("postgres", "host=127.0.0.1 port=5432 user=mx dbname=crud_db sslmode=disable") + currentDir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + dir := strings.Replace(currentDir, "/models", "", 1) + + // Load environment variables + err = godotenv.Load(dir + "/.env") + if err != nil { + log.Fatal("Error loading .env file: ", err) + } + dbUser := os.Getenv("DB_USER") + dbName := os.Getenv("DB_NAME") + dbPassword := os.Getenv("DB_PASSWORD") + dbHost := os.Getenv("DB_HOST") + dbPort := os.Getenv("DB_PORT") + + dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", + dbHost, dbPort, dbUser, dbPassword, dbName) + fmt.Printf("%+v", dsn) + config.DB, err = sql.Open("postgres", dsn) if err != nil { panic(err) } @@ -26,6 +49,7 @@ func setupTestDB() { age INT ); `) + // fmt.Println(r.RowsAffected()) if err != nil { panic(err) } @@ -52,6 +76,12 @@ func TestCreateUser(t *testing.T) { if err != nil || count != 1 { t.Errorf("User was not inserted into the database") } + + // Delete user + err = DeleteUser(user.ID) + if err != nil { + t.Errorf("Failed to delete user: %v", err) + } } func TestGetAllUsers(t *testing.T) { @@ -69,6 +99,14 @@ func TestGetAllUsers(t *testing.T) { if len(users) < 2 { t.Errorf("Expected at least 2 users, got %d", len(users)) } + + for _, user := range users { + // Delete user + err = DeleteUser(user.ID) + if err != nil { + t.Errorf("Failed to delete user: %v", err) + } + } } func TestGetUserByID(t *testing.T) { @@ -87,6 +125,11 @@ func TestGetUserByID(t *testing.T) { if user.Email != "david@example.com" { t.Errorf("Expected email 'david@example.com', got %s", user.Email) } + // Delete user + err = DeleteUser(id) + if err != nil { + t.Errorf("Failed to delete user: %v", err) + } } func TestUpdateUser(t *testing.T) { @@ -112,6 +155,12 @@ func TestUpdateUser(t *testing.T) { if name != "Eve Adams" || age != 30 { t.Errorf("User was not updated correctly: got name=%s, age=%d", name, age) } + + // Delete user + err = DeleteUser(id) + if err != nil { + t.Errorf("Failed to delete user: %v", err) + } } func TestDeleteUser(t *testing.T) {