-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserializer_test.go
80 lines (64 loc) · 1.89 KB
/
serializer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package jorb
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestJsonSerializer_SaveLoad(t *testing.T) {
t.Parallel()
// Create a test run
run := NewRun[MyOverallContext, MyJobContext]("test", MyOverallContext{Name: "overall"})
// Add 10 jobs with random data
for i := 0; i < 10; i++ {
job := MyJobContext{Count: 0, Name: fmt.Sprintf("job-%d", i)}
run.AddJob(job)
}
// Create a JsonSerializer with a temporary file
tempFile := filepath.Join(t.TempDir(), "test.json")
serializer := &JsonSerializer[MyOverallContext, MyJobContext]{File: tempFile}
// Serialize the run
err := serializer.Serialize(*run)
require.NoError(t, err)
require.FileExists(t, tempFile)
actualRun, err := serializer.Deserialize()
require.NoError(t, err)
// Check that the run is the same
assert.True(t, run.Equal(actualRun))
}
func Test_SerializeWithError(t *testing.T) {
t.Parallel()
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
r := NewRun[MyOverallContext, MyJobContext]("test", MyOverallContext{Name: "overall"})
r.UpdateJob(Job[MyJobContext]{
C: MyJobContext{Count: 0, Name: "job-0"},
StateErrors: map[string][]string{
"key": []string{
"e1", "e2",
},
},
})
tempFile := filepath.Join(tempDir, "test.json")
serializer := &JsonSerializer[MyOverallContext, MyJobContext]{File: tempFile}
err = serializer.Serialize(*r)
require.NoError(t, err)
actualRun, err := serializer.Deserialize()
require.NoError(t, err)
// Nill out the last updates so we don't have to do assert near
for k, _ := range r.Jobs {
j := r.Jobs[k]
j.LastUpdate = nil
r.Jobs[k] = j
j = actualRun.Jobs[k]
j.LastUpdate = nil
actualRun.Jobs[k] = j
}
assert.True(t, r.Equal(actualRun))
}