summaryrefslogtreecommitdiffstats
path: root/app/job_test.go
blob: 8f068901aa74f4ca649929061dbdf514694e35c4 (plain)
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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
	"testing"

	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/store"
)

func TestGetJob(t *testing.T) {
	Setup()

	status := &model.Job{
		Id:     model.NewId(),
		Status: model.NewId(),
	}
	if result := <-Srv.Store.Job().Save(status); result.Err != nil {
		t.Fatal(result.Err)
	}

	defer Srv.Store.Job().Delete(status.Id)

	if received, err := GetJob(status.Id); err != nil {
		t.Fatal(err)
	} else if received.Id != status.Id || received.Status != status.Status {
		t.Fatal("inccorrect job status received")
	}
}

func TestGetJobByType(t *testing.T) {
	Setup()

	jobType := model.NewId()

	statuses := []*model.Job{
		{
			Id:      model.NewId(),
			Type:    jobType,
			CreateAt: 1000,
		},
		{
			Id:      model.NewId(),
			Type:    jobType,
			CreateAt: 999,
		},
		{
			Id:      model.NewId(),
			Type:    jobType,
			CreateAt: 1001,
		},
	}

	for _, status := range statuses {
		store.Must(Srv.Store.Job().Save(status))
		defer Srv.Store.Job().Delete(status.Id)
	}

	if received, err := GetJobsByType(jobType, 0, 2); err != nil {
		t.Fatal(err)
	} else if len(received) != 2 {
		t.Fatal("received wrong number of statuses")
	} else if received[0].Id != statuses[2].Id {
		t.Fatal("should've received newest job first")
	} else if received[1].Id != statuses[0].Id {
		t.Fatal("should've received second newest job second")
	}

	if received, err := GetJobsByType(jobType, 2, 2); err != nil {
		t.Fatal(err)
	} else if len(received) != 1 {
		t.Fatal("received wrong number of statuses")
	} else if received[0].Id != statuses[1].Id {
		t.Fatal("should've received oldest job last")
	}
}