test: add self-contained integration environment
Separate schema migrations from environment-specific fixtures, run the full API workflow against a disposable PostgreSQL Testcontainer, and provide seeded development and UI test environments. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
6153017b62
commit
6969f79015
18 changed files with 772 additions and 269 deletions
9
backend/internal/database/seed/common.sql
Normal file
9
backend/internal/database/seed/common.sql
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
INSERT INTO traits (name, normalized) VALUES
|
||||
('Go', 'go'),
|
||||
('PostgreSQL', 'postgresql'),
|
||||
('Docker', 'docker'),
|
||||
('React', 'react'),
|
||||
('TypeScript', 'typescript'),
|
||||
('Kubernetes', 'kubernetes'),
|
||||
('Backend', 'backend')
|
||||
ON CONFLICT (normalized) DO NOTHING;
|
||||
87
backend/internal/database/seed/development.sql
Normal file
87
backend/internal/database/seed/development.sql
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
INSERT INTO users (email, password_hash, role)
|
||||
VALUES ('interviewer@go-guess.local', crypt('admin123', gen_salt('bf')), 'interviewer')
|
||||
ON CONFLICT (email) DO NOTHING;
|
||||
|
||||
INSERT INTO job_postings (title, description, seniority, position, status, duration_minutes)
|
||||
SELECT 'Senior Go Backend Engineer', 'Build reliable APIs for passenger-facing railway systems.',
|
||||
'Senior', 'Backend Engineer', 'published', 60
|
||||
WHERE NOT EXISTS (SELECT 1 FROM job_postings WHERE title = 'Senior Go Backend Engineer');
|
||||
|
||||
INSERT INTO job_postings (title, description, seniority, position, status, duration_minutes)
|
||||
SELECT 'Full-stack Product Engineer', 'Deliver accessible operational tools from database to browser.',
|
||||
'Medior', 'Full-stack Engineer', 'published', 45
|
||||
WHERE NOT EXISTS (SELECT 1 FROM job_postings WHERE title = 'Full-stack Product Engineer');
|
||||
|
||||
INSERT INTO questions (text, type, reference_answer)
|
||||
SELECT 'How do you design graceful shutdown for an HTTP service?', 'open',
|
||||
'Stop accepting work, cancel dependent operations, drain active requests, and close resources within a deadline.'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM questions WHERE text = 'How do you design graceful shutdown for an HTTP service?');
|
||||
|
||||
INSERT INTO questions (text, type)
|
||||
SELECT 'Which construct protects shared data across goroutines?', 'radio'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM questions WHERE text = 'Which construct protects shared data across goroutines?');
|
||||
|
||||
INSERT INTO questions (text, type, reference_answer, code_snippet)
|
||||
SELECT 'Review this component and identify unnecessary renders.', 'code_review',
|
||||
'Use a stable key and avoid recreating expensive derived values when the candidates input has not changed.',
|
||||
E'function CandidateList({ candidates }) {\n return candidates.map((candidate, index) => (\n <CandidateCard key={index} candidate={candidate} />\n ));\n}'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM questions WHERE text = 'Review this component and identify unnecessary renders.');
|
||||
|
||||
INSERT INTO question_options (question_id, text)
|
||||
SELECT q.id, option.text
|
||||
FROM questions q
|
||||
CROSS JOIN (VALUES ('sync.Mutex'), ('context.Context'), ('http.Client')) AS option(text)
|
||||
WHERE q.text = 'Which construct protects shared data across goroutines?'
|
||||
AND NOT EXISTS (SELECT 1 FROM question_options existing WHERE existing.question_id = q.id);
|
||||
|
||||
INSERT INTO job_posting_questions (job_posting_id, question_id)
|
||||
SELECT j.id, q.id
|
||||
FROM job_postings j
|
||||
JOIN questions q ON q.text IN (
|
||||
'How do you design graceful shutdown for an HTTP service?',
|
||||
'Which construct protects shared data across goroutines?'
|
||||
)
|
||||
WHERE j.title = 'Senior Go Backend Engineer'
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO job_posting_questions (job_posting_id, question_id)
|
||||
SELECT j.id, q.id
|
||||
FROM job_postings j
|
||||
JOIN questions q ON q.text = 'Review this component and identify unnecessary renders.'
|
||||
WHERE j.title = 'Full-stack Product Engineer'
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO job_posting_labels (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized IN ('backend', 'go')
|
||||
WHERE j.title = 'Senior Go Backend Engineer' ON CONFLICT DO NOTHING;
|
||||
INSERT INTO job_required_skills (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized IN ('go', 'postgresql', 'docker')
|
||||
WHERE j.title = 'Senior Go Backend Engineer' ON CONFLICT DO NOTHING;
|
||||
INSERT INTO job_additional_skills (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized = 'kubernetes'
|
||||
WHERE j.title = 'Senior Go Backend Engineer' ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO job_posting_labels (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized IN ('react', 'typescript')
|
||||
WHERE j.title = 'Full-stack Product Engineer' ON CONFLICT DO NOTHING;
|
||||
INSERT INTO job_required_skills (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized IN ('go', 'react', 'typescript')
|
||||
WHERE j.title = 'Full-stack Product Engineer' ON CONFLICT DO NOTHING;
|
||||
INSERT INTO job_additional_skills (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized = 'docker'
|
||||
WHERE j.title = 'Full-stack Product Engineer' ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO participants (first_name, last_name, birthday, email, contact_info, cv, cv_filename)
|
||||
VALUES
|
||||
('Ada', 'Lovelace', '1990-12-10', 'ada@example.com', '+32 470 00 00 01',
|
||||
convert_to('Senior backend engineer with Go, PostgreSQL and Docker experience.', 'UTF8'), 'ada-cv.txt'),
|
||||
('Grace', 'Hopper', '1988-12-09', 'grace@example.com', '+32 470 00 00 02',
|
||||
convert_to('Full-stack developer working with Go, React and TypeScript.', 'UTF8'), 'grace-cv.txt')
|
||||
ON CONFLICT (email) DO NOTHING;
|
||||
|
||||
INSERT INTO participant_traits (participant_id, trait_id)
|
||||
SELECT p.id, t.id FROM participants p JOIN traits t ON t.normalized IN ('go', 'postgresql', 'docker')
|
||||
WHERE p.email = 'ada@example.com' ON CONFLICT DO NOTHING;
|
||||
INSERT INTO participant_traits (participant_id, trait_id)
|
||||
SELECT p.id, t.id FROM participants p JOIN traits t ON t.normalized IN ('go', 'react', 'typescript')
|
||||
WHERE p.email = 'grace@example.com' ON CONFLICT DO NOTHING;
|
||||
38
backend/internal/database/seed/seed.go
Normal file
38
backend/internal/database/seed/seed.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package seed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//go:embed *.sql
|
||||
var files embed.FS
|
||||
|
||||
func Apply(ctx context.Context, db *sql.DB, environment string) error {
|
||||
if _, err := apply(ctx, db, "common.sql"); err != nil {
|
||||
return err
|
||||
}
|
||||
switch environment {
|
||||
case "", "production":
|
||||
return nil
|
||||
case "development", "test":
|
||||
_, err := apply(ctx, db, environment+".sql")
|
||||
return err
|
||||
default:
|
||||
return fmt.Errorf("unsupported APP_ENV %q", environment)
|
||||
}
|
||||
}
|
||||
|
||||
func apply(ctx context.Context, db *sql.DB, filename string) (sql.Result, error) {
|
||||
content, err := files.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read seed %s: %w", filename, err)
|
||||
}
|
||||
result, err := db.ExecContext(ctx, string(content))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("apply seed %s: %w", filename, err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
40
backend/internal/database/seed/test.sql
Normal file
40
backend/internal/database/seed/test.sql
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
INSERT INTO users (email, password_hash, role)
|
||||
VALUES ('integration@example.com', crypt('integration-password', gen_salt('bf')), 'interviewer')
|
||||
ON CONFLICT (email) DO NOTHING;
|
||||
|
||||
INSERT INTO job_postings (title, description, seniority, position, status, duration_minutes)
|
||||
SELECT 'Integration Backend Engineer', 'Deterministic job fixture for API and UI integration tests.',
|
||||
'Senior', 'Backend Engineer', 'published', 30
|
||||
WHERE NOT EXISTS (SELECT 1 FROM job_postings WHERE title = 'Integration Backend Engineer');
|
||||
|
||||
INSERT INTO questions (text, type, reference_answer)
|
||||
SELECT 'Explain graceful shutdown.', 'open',
|
||||
'Stop new work, cancel dependencies, drain requests, and close resources.'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM questions WHERE text = 'Explain graceful shutdown.');
|
||||
|
||||
INSERT INTO questions (text, type, reference_answer, code_snippet)
|
||||
SELECT 'Review the error handling.', 'code_review', 'Return or log the error instead of discarding it.',
|
||||
E'func save() error {\n persist()\n return nil\n}'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM questions WHERE text = 'Review the error handling.');
|
||||
|
||||
INSERT INTO job_posting_questions (job_posting_id, question_id)
|
||||
SELECT j.id, q.id FROM job_postings j
|
||||
JOIN questions q ON q.text IN ('Explain graceful shutdown.', 'Review the error handling.')
|
||||
WHERE j.title = 'Integration Backend Engineer'
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO job_posting_labels (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized = 'backend'
|
||||
WHERE j.title = 'Integration Backend Engineer' ON CONFLICT DO NOTHING;
|
||||
INSERT INTO job_required_skills (job_posting_id, trait_id)
|
||||
SELECT j.id, t.id FROM job_postings j JOIN traits t ON t.normalized IN ('go', 'postgresql')
|
||||
WHERE j.title = 'Integration Backend Engineer' ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO participants (first_name, last_name, birthday, email, contact_info, cv, cv_filename)
|
||||
VALUES ('Integration', 'Candidate', '1992-04-15', 'candidate.integration@example.com',
|
||||
'+32 470 00 00 99', convert_to('Backend engineer using Go and PostgreSQL.', 'UTF8'), 'integration-cv.txt')
|
||||
ON CONFLICT (email) DO NOTHING;
|
||||
|
||||
INSERT INTO participant_traits (participant_id, trait_id)
|
||||
SELECT p.id, t.id FROM participants p JOIN traits t ON t.normalized IN ('backend', 'go', 'postgresql')
|
||||
WHERE p.email = 'candidate.integration@example.com' ON CONFLICT DO NOTHING;
|
||||
Loading…
Add table
Add a link
Reference in a new issue