eintopf.info@v0.13.16/service/event/operator_test.go (about)

     1  // Copyright (C) 2022 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package event_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/petergtz/pegomock"
    23  
    24  	"eintopf.info/service/auth"
    25  	"eintopf.info/service/event"
    26  	"eintopf.info/test"
    27  )
    28  
    29  func TestOperatorCreate(t *testing.T) {
    30  	ctx := auth.ContextWithID(context.Background(), "foo")
    31  	pegomock.RegisterMockTestingT(t)
    32  	eventService := event.NewMemoryStore()
    33  	queueService := test.NewTestQueue()
    34  	operator := event.NewOperator(eventService, queueService)
    35  
    36  	_, err := operator.Create(ctx, &event.NewEvent{Name: "foo"})
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	queueService.WasCalled(t, event.NewCreateOperation(
    41  		&event.Event{ID: "0", Name: "foo"},
    42  		"foo",
    43  	))
    44  
    45  	eventService.Create(context.Background(), &event.NewEvent{})
    46  	_, err = operator.Create(ctx, &event.NewEvent{Name: "foo", Parent: "id:1"})
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	queueService.WasCalled(t, event.NewCreateOperation(
    51  		&event.Event{ID: "2", Name: "foo", Parent: "id:1"},
    52  		"foo",
    53  	))
    54  	queueService.WasCalled(t, event.NewCreateOperation(
    55  		&event.Event{ID: "1"},
    56  		"foo",
    57  	))
    58  }
    59  
    60  func TestOperatorUpdate(t *testing.T) {
    61  	ctx := auth.ContextWithID(context.Background(), "foo")
    62  	eventService := event.NewMemoryStore()
    63  	queueService := test.NewTestQueue()
    64  	operator := event.NewOperator(eventService, queueService)
    65  
    66  	eventService.Create(context.Background(), &event.NewEvent{})
    67  	event1 := &event.Event{ID: "0"}
    68  	_, err := operator.Update(ctx, event1)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	queueService.WasCalled(t, event.NewUpdateOperation(
    73  		event1,
    74  		"foo",
    75  	))
    76  
    77  	eventService.Create(context.Background(), &event.NewEvent{Name: "foo"})
    78  	_, err = operator.Update(ctx, &event.Event{ID: "0", Parent: "id:1"})
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	queueService.WasCalled(t, event.NewUpdateOperation(
    83  		&event.Event{ID: "0", Parent: "id:1"},
    84  		"foo",
    85  	))
    86  	queueService.WasCalled(t, event.NewUpdateOperation(
    87  		&event.Event{ID: "1", Name: "foo"},
    88  		"foo",
    89  	))
    90  }
    91  
    92  func TestOperatorDelete(t *testing.T) {
    93  	ctx := auth.ContextWithID(context.Background(), "foo")
    94  	pegomock.RegisterMockTestingT(t)
    95  	eventService := event.NewMemoryStore()
    96  	queueService := test.NewTestQueue()
    97  	operator := event.NewOperator(eventService, queueService)
    98  
    99  	eventService.Create(context.Background(), &event.NewEvent{})
   100  	err := operator.Delete(ctx, "0")
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	queueService.WasCalled(t, event.NewDeleteOperation(
   105  		"0",
   106  		"foo",
   107  	))
   108  
   109  	eventService.Create(context.Background(), &event.NewEvent{})
   110  	eventService.Create(context.Background(), &event.NewEvent{Parent: "id:0"})
   111  	err = operator.Delete(ctx, "1")
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	queueService.WasCalled(t, event.NewDeleteOperation(
   116  		"1",
   117  		"foo",
   118  	))
   119  	queueService.WasCalled(t, event.NewUpdateOperation(
   120  		&event.Event{ID: "0"},
   121  		"foo",
   122  	))
   123  }