feat: add automated db tests and update env examples
This commit is contained in:
parent
fc4e3df02d
commit
0e5c93ec6c
5 changed files with 6158 additions and 0 deletions
BIN
backend/api
Executable file
BIN
backend/api
Executable file
Binary file not shown.
54
backend/internal/repository/postgres/postgres_test.go
Normal file
54
backend/internal/repository/postgres/postgres_test.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/saveinmed/backend-go/internal/config"
|
||||
)
|
||||
|
||||
func TestDatabaseConnection(t *testing.T) {
|
||||
if os.Getenv("SKIP_DB_TEST") != "" {
|
||||
t.Skip("Skipping database tests")
|
||||
}
|
||||
|
||||
// Simple .env loader for testing purposes
|
||||
if content, err := os.ReadFile("../../../.env"); err == nil {
|
||||
for _, line := range strings.Split(string(content), "\n") {
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) == 2 {
|
||||
os.Setenv(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
|
||||
// Create a context with timeout
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
db, err := sqlx.ConnectContext(ctx, "pgx", cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
t.Fatalf("Failed to ping database: %v", err)
|
||||
}
|
||||
|
||||
var result int
|
||||
if err := db.QueryRowContext(ctx, "SELECT 1").Scan(&result); err != nil {
|
||||
t.Fatalf("Failed to execute query: %v", err)
|
||||
}
|
||||
|
||||
if result != 1 {
|
||||
t.Errorf("Expected 1, got %d", result)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
DATABASE_URL=postgresql://user:password@host:port/dbname?schema=public
|
||||
JWT_SECRET=secret-key
|
||||
PORT=3000
|
||||
API_URL=http://localhost:3000
|
||||
|
|
|
|||
6072
backoffice/package-lock.json
generated
Normal file
6072
backoffice/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
31
backoffice/test/app.e2e-spec.ts
Normal file
31
backoffice/test/app.e2e-spec.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
||||
import * as request from 'supertest';
|
||||
import { AppModule } from './../src/app.module';
|
||||
|
||||
describe('AppController (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
beforeEach(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication(
|
||||
new FastifyAdapter(),
|
||||
);
|
||||
await app.init();
|
||||
await app.getHttpAdapter().getInstance().ready();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
// Since we don't have a root route, we can try to hit docs or a known route if any.
|
||||
// Or check if the app initializes correctly.
|
||||
it('should initialize', () => {
|
||||
expect(app).toBeDefined();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue