github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/automation/trigger/trigger_test.go (about)

     1  package trigger_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/qri-io/qri/automation/trigger"
     9  	"github.com/qri-io/qri/automation/workflow"
    10  	"github.com/qri-io/qri/profile"
    11  )
    12  
    13  func TestSet(t *testing.T) {
    14  	s := trigger.NewSet(trigger.RuntimeType, trigger.NewRuntimeTrigger)
    15  	if err := s.Add(&workflow.Workflow{OwnerID: profile.ID("ownerID")}); !errors.Is(err, trigger.ErrEmptyWorkflowID) {
    16  		t.Errorf("Add - source with no WorkflowID - expected error %q, got %q", trigger.ErrEmptyWorkflowID, err)
    17  	}
    18  	if err := s.Add(&workflow.Workflow{ID: "workflowID"}); !errors.Is(err, trigger.ErrEmptyOwnerID) {
    19  		t.Errorf("Add - source with no OwnerID - expected error %q, got %q", trigger.ErrEmptyOwnerID, err)
    20  	}
    21  	if err := s.Add(&workflow.Workflow{OwnerID: profile.ID("ownerID"), ID: "workflowID"}); err != nil {
    22  		t.Errorf("Add - unexpected error: %s", err)
    23  	}
    24  	if diff := cmp.Diff(map[profile.ID]map[string][]trigger.Trigger{}, s.Active()); diff != "" {
    25  		t.Errorf("active triggers mismatch (-want +got):\n%s", diff)
    26  	}
    27  	alice := profile.ID("alice")
    28  	sourceA := &workflow.Workflow{
    29  		Active:  true,
    30  		OwnerID: alice,
    31  		ID:      "workflow1",
    32  		Triggers: []map[string]interface{}{
    33  			{
    34  				"id":     "trigger1",
    35  				"active": true,
    36  				"type":   trigger.RuntimeType,
    37  			},
    38  			{
    39  				"id":     "trigger_not_active",
    40  				"active": false,
    41  				"type":   trigger.RuntimeType,
    42  			},
    43  		},
    44  	}
    45  	sourceB := &workflow.Workflow{
    46  		Active:  true,
    47  		OwnerID: alice,
    48  		ID:      "workflow2",
    49  	}
    50  	sourceC := &workflow.Workflow{
    51  		Active:  true,
    52  		OwnerID: alice,
    53  		ID:      "workflow3",
    54  		Triggers: []map[string]interface{}{
    55  			{
    56  				"id":     "trigger2",
    57  				"active": true,
    58  				"type":   trigger.RuntimeType,
    59  			},
    60  			{
    61  				"id":     "trigger3",
    62  				"active": true,
    63  				"type":   trigger.RuntimeType,
    64  			},
    65  		},
    66  	}
    67  	bob := profile.ID("bob")
    68  	sourceD := &workflow.Workflow{
    69  		Active:  true,
    70  		OwnerID: bob,
    71  		ID:      "workflow4",
    72  		Triggers: []map[string]interface{}{
    73  			{
    74  				"id":     "trigger4",
    75  				"active": true,
    76  				"type":   trigger.RuntimeType,
    77  			},
    78  		},
    79  	}
    80  	sourceE := &workflow.Workflow{
    81  		Active:  false,
    82  		OwnerID: bob,
    83  		ID:      "workflow5",
    84  		Triggers: []map[string]interface{}{
    85  			{
    86  				"id":     "trigger_not_active_workflow",
    87  				"active": true,
    88  				"type":   trigger.RuntimeType,
    89  			},
    90  		},
    91  	}
    92  
    93  	prevNewID := trigger.NewID
    94  	defer func() {
    95  		trigger.NewID = prevNewID
    96  	}()
    97  	triggerIDs := []string{
    98  		"trigger1",
    99  		"trigger2",
   100  		"trigger3",
   101  		"trigger4",
   102  	}
   103  	triggerIDIndex := 0
   104  	trigger.NewID = func() string {
   105  		if triggerIDIndex >= len(triggerIDs) {
   106  			t.Fatal("more triggers created than ids allocated")
   107  		}
   108  		id := triggerIDs[triggerIDIndex]
   109  		triggerIDIndex++
   110  		return id
   111  	}
   112  	triggers := []trigger.Trigger{}
   113  	for i := 0; i < len(triggerIDs); i++ {
   114  		trig := trigger.NewEmptyRuntimeTrigger()
   115  		trig.SetActive(true)
   116  		triggers = append(triggers, trig)
   117  	}
   118  
   119  	expected := map[profile.ID]map[string][]trigger.Trigger{
   120  		alice: {
   121  			"workflow1": triggers[:1],
   122  			"workflow3": triggers[1:3],
   123  		},
   124  		bob: {
   125  			"workflow4": triggers[3:],
   126  		},
   127  	}
   128  	if err := s.Add(sourceA, sourceB, sourceC, sourceD, sourceE); err != nil {
   129  		t.Fatalf("Add unexpected error: %s", err)
   130  	}
   131  	if diff := cmp.Diff(expected, s.Active(), cmp.AllowUnexported(trigger.RuntimeTrigger{})); diff != "" {
   132  		t.Errorf("active triggers mismatch (-want +got):\n%s", diff)
   133  	}
   134  
   135  	if exists := s.Exists(sourceD); !exists {
   136  		t.Fatal("Exists error, expected source D to exist")
   137  	}
   138  
   139  	// removing the triggers for an ownerID that has no more workflows
   140  	// should remove the entry in the store
   141  	sourceD.Triggers = []map[string]interface{}{}
   142  	delete(expected, bob)
   143  	if err := s.Add(sourceD); err != nil {
   144  		t.Fatalf("Add unexpected error: %s", err)
   145  	}
   146  	if diff := cmp.Diff(expected, s.Active(), cmp.AllowUnexported(trigger.RuntimeTrigger{})); diff != "" {
   147  		t.Errorf("active triggers mismatch (-want +got):\n%s", diff)
   148  	}
   149  
   150  	if exists := s.Exists(sourceD); exists {
   151  		t.Fatal("Exists error, expected source D to NOT exists")
   152  	}
   153  
   154  	sourceA.Triggers = []map[string]interface{}{
   155  		sourceA.Triggers[0],
   156  		sourceC.Triggers[0],
   157  		sourceC.Triggers[1],
   158  	}
   159  	expected[alice]["workflow1"] = triggers[:3]
   160  	if err := s.Add(sourceA); err != nil {
   161  		t.Fatalf("Add unexpected error: %s", err)
   162  	}
   163  	if diff := cmp.Diff(expected, s.Active(), cmp.AllowUnexported(trigger.RuntimeTrigger{})); diff != "" {
   164  		t.Errorf("active triggers mismatch (-want +got):\n%s", diff)
   165  	}
   166  
   167  	sourceA.Triggers = sourceA.Triggers[:1]
   168  	if exists := s.Exists(sourceA); exists {
   169  		t.Fatal("Exists error, expected source A (with incorrect triggers) to NOT exist")
   170  	}
   171  
   172  	sourceC.Triggers = []map[string]interface{}{}
   173  	delete(expected[alice], "workflow3")
   174  	if err := s.Add(sourceC); err != nil {
   175  		t.Fatalf("Add unexpected error: %s", err)
   176  	}
   177  	if diff := cmp.Diff(expected, s.Active(), cmp.AllowUnexported(trigger.RuntimeTrigger{})); diff != "" {
   178  		t.Errorf("active triggers mismatch (-want +got):\n%s", diff)
   179  	}
   180  
   181  	sourceA.Triggers = []map[string]interface{}{}
   182  	delete(expected, alice)
   183  	if err := s.Add(sourceA); err != nil {
   184  		t.Fatalf("Add unexpected error: %s", err)
   185  	}
   186  	if diff := cmp.Diff(expected, s.Active(), cmp.AllowUnexported(trigger.RuntimeTrigger{})); diff != "" {
   187  		t.Errorf("active triggers mismatch (-want +got):\n%s", diff)
   188  	}
   189  }