github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/jobqueue/jobs_test.go (about) 1 package jobqueue_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/onflow/flow-go/module" 9 "github.com/onflow/flow-go/module/jobqueue" 10 "github.com/onflow/flow-go/utils/unittest" 11 ) 12 13 func TestJobID(t *testing.T) { 14 fid := unittest.IdentifierFixture() 15 jobID := jobqueue.JobID(fid) 16 17 assert.IsType(t, module.JobID(""), jobID) 18 assert.Equal(t, fid.String(), string(jobID)) 19 } 20 21 func TestBlockJob(t *testing.T) { 22 block := unittest.BlockFixture() 23 job := jobqueue.BlockToJob(&block) 24 25 t.Run("job is correct type", func(t *testing.T) { 26 assert.IsType(t, &jobqueue.BlockJob{}, job, "job is not a block job") 27 }) 28 29 t.Run("job ID matches block ID", func(t *testing.T) { 30 jobID := jobqueue.JobID(block.ID()) 31 assert.Equal(t, job.ID(), jobID, "job ID is not the block ID") 32 }) 33 34 t.Run("job converts to block", func(t *testing.T) { 35 b, err := jobqueue.JobToBlock(job) 36 assert.NoError(t, err, "unexpected error converting notify job to block") 37 assert.Equal(t, block, *b, "converted block is not the same as the original block") 38 }) 39 40 t.Run("incorrect job type fails to convert to block", func(t *testing.T) { 41 e, err := jobqueue.JobToBlock(invalidJob{}) 42 assert.Error(t, err, "expected error converting invalidJob to block") 43 assert.Nil(t, e, "expected nil block") 44 }) 45 } 46 47 func TestBlockHeaderJob(t *testing.T) { 48 block := unittest.BlockHeaderFixture() 49 job := jobqueue.BlockHeaderToJob(block) 50 51 t.Run("job is correct type", func(t *testing.T) { 52 assert.IsType(t, &jobqueue.BlockHeaderJob{}, job, "job is not a block job") 53 }) 54 55 t.Run("job ID matches block ID", func(t *testing.T) { 56 jobID := jobqueue.JobID(block.ID()) 57 assert.Equal(t, job.ID(), jobID, "job ID is not the block ID") 58 }) 59 60 t.Run("job converts to header", func(t *testing.T) { 61 b, err := jobqueue.JobToBlockHeader(job) 62 assert.NoError(t, err, "unexpected error converting notify job to header") 63 assert.Equal(t, *block, *b, "converted header is not the same as the original header") 64 }) 65 66 t.Run("incorrect job type fails to convert to header", func(t *testing.T) { 67 e, err := jobqueue.JobToBlockHeader(invalidJob{}) 68 assert.Error(t, err, "expected error converting invalidJob to header") 69 assert.Nil(t, e, "expected nil header") 70 }) 71 } 72 73 type invalidJob struct{} 74 75 func (j invalidJob) ID() module.JobID { 76 return "invalid" 77 }