Skip to content

Commit 8f3f20d

Browse files
authored
Workflow context getters (#39)
- Add GetWorkflowID() which takes a context and, if a DBOS one, returns the workflowID (otherwise an error) - Stop exporting some stuff - operation -> step rename - add missing `dbos_test.go` file - perform DBOS migrations in an isolated schema migration table
1 parent 1d134da commit 8f3f20d

File tree

5 files changed

+269
-165
lines changed

5 files changed

+269
-165
lines changed

dbos/dbos.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ type Config struct {
7171
AdminServer bool
7272
}
7373

74-
// ProcessConfig merges configuration from two sources in order of precedence:
74+
// processConfig merges configuration from two sources in order of precedence:
7575
// 1. programmatic configuration
7676
// 2. environment variables
7777
// Finally, it applies default values if needed.
78-
func ProcessConfig(inputConfig *Config) (*Config, error) {
78+
func processConfig(inputConfig *Config) (*Config, error) {
7979
// First check required fields
8080
if len(inputConfig.DatabaseURL) == 0 {
8181
return nil, fmt.Errorf("missing required config field: databaseURL")
@@ -133,7 +133,7 @@ func Initialize(inputConfig Config) error {
133133
}
134134

135135
// Load & process the configuration
136-
config, err := ProcessConfig(&inputConfig)
136+
config, err := processConfig(&inputConfig)
137137
if err != nil {
138138
return newInitializationError(err.Error())
139139
}

dbos/dbos_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package dbos
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"maps"
7+
"testing"
8+
)
9+
10+
func TestConfigValidationErrorTypes(t *testing.T) {
11+
databaseURL := getDatabaseURL(t)
12+
13+
t.Run("FailsWithoutAppName", func(t *testing.T) {
14+
config := Config{
15+
DatabaseURL: databaseURL,
16+
}
17+
18+
err := Initialize(config)
19+
if err == nil {
20+
t.Fatal("expected error when app name is missing, but got none")
21+
}
22+
23+
dbosErr, ok := err.(*DBOSError)
24+
if !ok {
25+
t.Fatalf("expected DBOSError, got %T", err)
26+
}
27+
28+
if dbosErr.Code != InitializationError {
29+
t.Fatalf("expected InitializationError code, got %v", dbosErr.Code)
30+
}
31+
32+
expectedMsg := "Error initializing DBOS Transact: missing required config field: appName"
33+
if dbosErr.Message != expectedMsg {
34+
t.Fatalf("expected error message '%s', got '%s'", expectedMsg, dbosErr.Message)
35+
}
36+
})
37+
38+
t.Run("FailsWithoutDatabaseURL", func(t *testing.T) {
39+
config := Config{
40+
AppName: "test-app",
41+
}
42+
43+
err := Initialize(config)
44+
if err == nil {
45+
t.Fatal("expected error when database URL is missing, but got none")
46+
}
47+
48+
dbosErr, ok := err.(*DBOSError)
49+
if !ok {
50+
t.Fatalf("expected DBOSError, got %T", err)
51+
}
52+
53+
if dbosErr.Code != InitializationError {
54+
t.Fatalf("expected InitializationError code, got %v", dbosErr.Code)
55+
}
56+
57+
expectedMsg := "Error initializing DBOS Transact: missing required config field: databaseURL"
58+
if dbosErr.Message != expectedMsg {
59+
t.Fatalf("expected error message '%s', got '%s'", expectedMsg, dbosErr.Message)
60+
}
61+
})
62+
}
63+
func TestAppVersion(t *testing.T) {
64+
if _, err := hex.DecodeString(_APP_VERSION); err != nil {
65+
t.Fatalf("APP_VERSION is not a valid hex string: %v", err)
66+
}
67+
68+
// Save the original registry content
69+
originalRegistry := make(map[string]workflowRegistryEntry)
70+
maps.Copy(originalRegistry, registry)
71+
72+
// Restore the registry after the test
73+
defer func() {
74+
registry = originalRegistry
75+
}()
76+
77+
// Replace the registry and verify the hash is different
78+
registry = make(map[string]workflowRegistryEntry)
79+
80+
WithWorkflow(func(ctx context.Context, input string) (string, error) {
81+
return "new-registry-workflow-" + input, nil
82+
})
83+
hash2 := computeApplicationVersion()
84+
if _APP_VERSION == hash2 {
85+
t.Fatalf("APP_VERSION hash did not change after replacing registry")
86+
}
87+
}

0 commit comments

Comments
 (0)