mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
test(integration): build the integration suites again
The mysql and postgres suites used a removed cache rebuild method, a map for string metadata, and a shadowed metadata package. The mysql suite now sets parseTime. JSON columns pick json on mysql and jsonb elsewhere, so the mysql schema creates.
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// MySQLV2IntegrationTestSuite embeds the V2 test suite with MySQL container
|
||||
@@ -26,8 +27,7 @@ func (s *MySQLV2IntegrationTestSuite) SetupSuite() {
|
||||
ctx := context.Background()
|
||||
|
||||
// Start MySQL container
|
||||
container, err := mysql.RunContainer(ctx,
|
||||
testcontainers.WithImage("mysql:8.0"),
|
||||
container, err := mysql.Run(ctx, "mysql:8.0",
|
||||
mysql.WithDatabase("testdb"),
|
||||
mysql.WithUsername("testuser"),
|
||||
mysql.WithPassword("testpass"),
|
||||
@@ -55,7 +55,7 @@ func (s *MySQLV2IntegrationTestSuite) SetupTest() {
|
||||
s.ctx = context.Background()
|
||||
|
||||
// Get connection string from container
|
||||
connStr, err := s.container.ConnectionString(s.ctx)
|
||||
connStr, err := s.container.ConnectionString(s.ctx, "parseTime=true", "loc=UTC")
|
||||
s.Require().NoError(err)
|
||||
|
||||
// Create GORM store with MySQL
|
||||
@@ -106,9 +106,9 @@ func (s *MySQLV2IntegrationTestSuite) TearDownTest() {
|
||||
}
|
||||
|
||||
// Rebuild registry cache
|
||||
s.store.rebuildRegistryCache()
|
||||
_ = s.store.loadRegistryCache()
|
||||
|
||||
s.store.Close()
|
||||
_ = s.store.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,8 +156,7 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_HighConcurrency() {
|
||||
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func() {
|
||||
err := s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0")
|
||||
s.NoError(err)
|
||||
s.NoError(s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0"))
|
||||
done <- true
|
||||
}()
|
||||
}
|
||||
@@ -175,11 +174,11 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_HighConcurrency() {
|
||||
|
||||
// Test_MySQLV2_JSON tests MySQL JSON functionality
|
||||
func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_JSON() {
|
||||
metadata := map[string]interface{}{
|
||||
meta := map[string]string{
|
||||
"author": "Test Author",
|
||||
"license": "MIT",
|
||||
"description": "Test package",
|
||||
"keywords": []interface{}{"test", "mysql", "json"},
|
||||
"keywords": "test,mysql,json",
|
||||
}
|
||||
|
||||
pkg := &metadata.Package{
|
||||
@@ -190,7 +189,7 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_JSON() {
|
||||
Size: 1000,
|
||||
CachedAt: time.Now(),
|
||||
LastAccessed: time.Now(),
|
||||
Metadata: metadata,
|
||||
Metadata: meta,
|
||||
}
|
||||
|
||||
err := s.store.SavePackage(s.ctx, pkg)
|
||||
@@ -262,7 +261,7 @@ func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_CharacterSet() {
|
||||
Size: 1000,
|
||||
CachedAt: time.Now(),
|
||||
LastAccessed: time.Now(),
|
||||
Metadata: map[string]interface{}{
|
||||
Metadata: map[string]string{
|
||||
"description": "Test with emoji 🎉 and Chinese 中文",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@ func (s *PostgresV2IntegrationTestSuite) SetupSuite() {
|
||||
ctx := context.Background()
|
||||
|
||||
// Start PostgreSQL container
|
||||
container, err := postgres.RunContainer(ctx,
|
||||
testcontainers.WithImage("postgres:16-alpine"),
|
||||
container, err := postgres.Run(ctx, "postgres:16-alpine",
|
||||
postgres.WithDatabase("testdb"),
|
||||
postgres.WithUsername("testuser"),
|
||||
postgres.WithPassword("testpass"),
|
||||
@@ -94,7 +93,7 @@ func (s *PostgresV2IntegrationTestSuite) TearDownTest() {
|
||||
s.store.db.Exec("TRUNCATE TABLE " + table + " CASCADE")
|
||||
}
|
||||
|
||||
s.store.Close()
|
||||
_ = s.store.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,8 +152,7 @@ func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_HighConcurrency() {
|
||||
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func() {
|
||||
err := s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0")
|
||||
s.NoError(err)
|
||||
s.NoError(s.store.UpdateDownloadCount(s.ctx, "npm", "concurrent-test", "1.0.0"))
|
||||
done <- true
|
||||
}()
|
||||
}
|
||||
@@ -172,11 +170,11 @@ func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_HighConcurrency() {
|
||||
|
||||
// Test_PostgresV2_JSONB tests PostgreSQL JSONB functionality
|
||||
func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_JSONB() {
|
||||
metadata := map[string]interface{}{
|
||||
meta := map[string]string{
|
||||
"author": "Test Author",
|
||||
"license": "MIT",
|
||||
"description": "Test package",
|
||||
"keywords": []interface{}{"test", "postgres", "jsonb"},
|
||||
"keywords": "test,postgres,jsonb",
|
||||
}
|
||||
|
||||
pkg := &metadata.Package{
|
||||
@@ -187,7 +185,7 @@ func (s *PostgresV2IntegrationTestSuite) Test_PostgresV2_JSONB() {
|
||||
Size: 1000,
|
||||
CachedAt: time.Now(),
|
||||
LastAccessed: time.Now(),
|
||||
Metadata: metadata,
|
||||
Metadata: meta,
|
||||
}
|
||||
|
||||
err := s.store.SavePackage(s.ctx, pkg)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
// BaseModel provides common fields for all models with audit trail
|
||||
@@ -48,6 +49,8 @@ type PackageModel struct {
|
||||
|
||||
Name string `gorm:"not null;size:255;index:idx_package_name;index:idx_package_registry_name_version,priority:2"`
|
||||
Version string `gorm:"not null;size:100;index:idx_package_registry_name_version,priority:3"`
|
||||
// ReleaseVersion is the version shown to users when Version is a cache key (PyPI file names).
|
||||
ReleaseVersion string `gorm:"not null;size:100;default:''"`
|
||||
|
||||
// Storage information
|
||||
StorageKey string `gorm:"not null;uniqueIndex:idx_package_storage_key;size:512"`
|
||||
@@ -94,7 +97,7 @@ func (p *PackageModel) BeforeCreate(tx *gorm.DB) error {
|
||||
// PackageMetadataModel stores structured package metadata (1:1 with packages)
|
||||
// Separated from main table to reduce row size and improve query performance
|
||||
type PackageMetadataModel struct {
|
||||
RawMetadata JSONBField `gorm:"type:jsonb"` // Full metadata as JSONB (PostgreSQL) or JSON
|
||||
RawMetadata JSONBField // Full metadata as JSONB (PostgreSQL) or JSON
|
||||
BaseModel
|
||||
Author string `gorm:"size:255;index:idx_metadata_author"`
|
||||
License string `gorm:"size:100;index:idx_metadata_license"`
|
||||
@@ -112,7 +115,7 @@ func (PackageMetadataModel) TableName() string {
|
||||
// ScanResultModel represents security scan results (optimized)
|
||||
type ScanResultModel struct {
|
||||
ScannedAt time.Time `gorm:"not null;index:idx_scan_scanned_at"`
|
||||
Details JSONBField `gorm:"type:jsonb"` // Scanner-specific details
|
||||
Details JSONBField // Scanner-specific details
|
||||
BaseModel
|
||||
|
||||
Scanner string `gorm:"not null;size:50;index:idx_scan_scanner;index:idx_scan_package_scanner,priority:2"`
|
||||
@@ -237,7 +240,7 @@ func (DownloadStatsHourlyModel) TableName() string {
|
||||
type DownloadStatsDailyModel struct {
|
||||
TimeBucket time.Time `gorm:"not null;index:idx_stats_daily_composite,priority:2"` // Truncated to day
|
||||
PackageID *int64 `gorm:"index:idx_stats_daily_package"` // NULL = all packages in registry
|
||||
TopUserAgents JSONBField `gorm:"type:jsonb"` // Top 10 user agents
|
||||
TopUserAgents JSONBField // Top 10 user agents
|
||||
|
||||
BaseModel
|
||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||
@@ -254,7 +257,7 @@ func (DownloadStatsDailyModel) TableName() string {
|
||||
// AuditLogModel tracks all important changes (optional, for compliance)
|
||||
type AuditLogModel struct {
|
||||
Timestamp time.Time `gorm:"not null;index:idx_audit_timestamp"`
|
||||
Changes JSONBField `gorm:"type:jsonb"` // Before/after values
|
||||
Changes JSONBField // Before/after values
|
||||
EntityType string `gorm:"not null;size:50;index:idx_audit_entity_type"` // package, bypass, registry
|
||||
Action string `gorm:"not null;size:20;index:idx_audit_action"` // create, update, delete
|
||||
Username string `gorm:"not null;size:255;index:idx_audit_username"`
|
||||
@@ -293,6 +296,16 @@ func (APIKeyModel) TableName() string {
|
||||
// JSONBField is a custom type for JSONB (PostgreSQL) / JSON (MySQL/SQLite)
|
||||
type JSONBField map[string]interface{}
|
||||
|
||||
func (JSONBField) GormDataType() string { return "json" }
|
||||
|
||||
// GormDBDataType picks the column type per dialect: MySQL has no jsonb.
|
||||
func (JSONBField) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
|
||||
if db.Name() == "mysql" {
|
||||
return "json"
|
||||
}
|
||||
return "jsonb"
|
||||
}
|
||||
|
||||
func (j JSONBField) Value() (driver.Value, error) {
|
||||
if j == nil {
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user