turash/bugulma/backend/internal/financial/risk_assessor_test.go
Damir Mukimov 000eab4740
Major repository reorganization and missing backend endpoints implementation
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools

Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
  * GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
  * GET /api/v1/users/me/organizations - User organizations
  * POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue

API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules

Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
2025-11-25 06:01:16 +01:00

233 lines
5.3 KiB
Go

package financial
import (
"testing"
)
func TestRiskAssessor_AssessRisk(t *testing.T) {
config := DefaultConfig()
ra := NewRiskAssessor(config)
data := &ResourceFlowData{
ResourceType: "heat",
DistanceKm: 50.0,
AnnualVolume: 500.0,
}
risk := ra.AssessRisk(data)
if risk.OverallRisk <= 0 || risk.OverallRisk > 1 {
t.Errorf("OverallRisk = %v, should be between 0 and 1", risk.OverallRisk)
}
if risk.RiskLevel == "" {
t.Error("RiskLevel should not be empty")
}
if risk.TechnicalRisk < 0 || risk.TechnicalRisk > 1 {
t.Errorf("TechnicalRisk = %v, should be between 0 and 1", risk.TechnicalRisk)
}
if risk.RegulatoryRisk < 0 || risk.RegulatoryRisk > 1 {
t.Errorf("RegulatoryRisk = %v, should be between 0 and 1", risk.RegulatoryRisk)
}
if risk.MarketRisk < 0 || risk.MarketRisk > 1 {
t.Errorf("MarketRisk = %v, should be between 0 and 1", risk.MarketRisk)
}
}
func TestRiskAssessor_CalculateTechnicalRisk(t *testing.T) {
config := DefaultConfig()
ra := NewRiskAssessor(config)
tests := []struct {
name string
distanceKm float64
resourceType string
expectedMin float64
expectedMax float64
}{
{
name: "Short distance heat",
distanceKm: 5.0,
resourceType: "heat",
expectedMin: 0.2, // Just resource type risk
expectedMax: 0.3,
},
{
name: "Long distance heat",
distanceKm: 200.0,
resourceType: "heat",
expectedMin: 0.4, // Distance + resource type risk
expectedMax: 0.6,
},
{
name: "Biowaste",
distanceKm: 5.0,
resourceType: "biowaste",
expectedMin: 0.3, // Resource type risk
expectedMax: 0.5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := ra.(*RiskAssessorImpl)
risk := impl.calculateTechnicalRisk(tt.distanceKm, tt.resourceType)
if risk < tt.expectedMin || risk > tt.expectedMax {
t.Errorf("calculateTechnicalRisk() = %v, expected between %v and %v",
risk, tt.expectedMin, tt.expectedMax)
}
})
}
}
func TestRiskAssessor_CalculateRegulatoryRisk(t *testing.T) {
config := DefaultConfig()
ra := NewRiskAssessor(config)
tests := []struct {
name string
resourceType string
expected float64
}{
{"Heat", "heat", 0.2},
{"Steam", "steam", 0.2},
{"Biowaste", "biowaste", 0.6},
{"CO2", "CO2", 0.4},
{"Water", "water", 0.4},
{"Unknown", "unknown", 0.3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := ra.(*RiskAssessorImpl)
risk := impl.calculateRegulatoryRisk(tt.resourceType)
if risk != tt.expected {
t.Errorf("calculateRegulatoryRisk(%s) = %v, want %v",
tt.resourceType, risk, tt.expected)
}
})
}
}
func TestRiskAssessor_CalculateMarketRisk(t *testing.T) {
config := DefaultConfig()
ra := NewRiskAssessor(config)
tests := []struct {
name string
annualVolume float64
expected float64
}{
{"Low volume", 50.0, 0.7},
{"Medium volume", 500.0, 0.4},
{"High volume", 5000.0, 0.1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := ra.(*RiskAssessorImpl)
risk := impl.calculateMarketRisk(tt.annualVolume)
if risk != tt.expected {
t.Errorf("calculateMarketRisk(%v) = %v, want %v",
tt.annualVolume, risk, tt.expected)
}
})
}
}
func TestRiskAssessor_CalculateRiskLevel(t *testing.T) {
config := DefaultConfig()
ra := NewRiskAssessor(config)
tests := []struct {
name string
overallRisk float64
expected string
}{
{"Low risk", 0.1, "low"},
{"Medium risk", 0.4, "medium"},
{"High risk", 0.7, "high"},
{"Critical risk", 0.9, "critical"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := ra.(*RiskAssessorImpl)
level := impl.calculateRiskLevel(tt.overallRisk)
if level != tt.expected {
t.Errorf("calculateRiskLevel(%v) = %v, want %v",
tt.overallRisk, level, tt.expected)
}
})
}
}
func TestRiskAssessor_IdentifyPrimaryRiskFactors(t *testing.T) {
config := DefaultConfig()
ra := NewRiskAssessor(config)
tests := []struct {
name string
data *ResourceFlowData
risk *RiskAssessment
expectedFactors []string
}{
{
name: "Long distance",
data: &ResourceFlowData{
ResourceType: "heat",
DistanceKm: 150.0,
AnnualVolume: 1000.0,
},
risk: &RiskAssessment{
TechnicalRisk: 0.5,
RegulatoryRisk: 0.2,
MarketRisk: 0.1,
},
expectedFactors: []string{"technical_failure"},
},
{
name: "Biowaste with low volume",
data: &ResourceFlowData{
ResourceType: "biowaste",
DistanceKm: 50.0,
AnnualVolume: 100.0,
},
risk: &RiskAssessment{
TechnicalRisk: 0.5,
RegulatoryRisk: 0.6,
MarketRisk: 0.7,
},
expectedFactors: []string{"technical_failure", "regulatory_changes", "market_volatility"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := ra.(*RiskAssessorImpl)
factors := impl.identifyPrimaryRiskFactors(tt.data, tt.risk)
// Check that expected factors are present
for _, expected := range tt.expectedFactors {
found := false
for _, factor := range factors {
if factor == expected {
found = true
break
}
}
if !found {
t.Errorf("identifyPrimaryRiskFactors() missing expected factor: %s", expected)
}
}
})
}
}