eintopf.info@v0.13.16/service/action/action_test.go (about)

     1  // Copyright (C) 2024 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 action_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"eintopf.info/service/action"
    24  	"eintopf.info/service/auth"
    25  )
    26  
    27  func TestAction(t *testing.T) {
    28  	store := action.NewMemoryStore()
    29  	service := action.NewService(store, nil)
    30  
    31  	t.Run("Sucessull Call", func(t *testing.T) {
    32  		i := 0
    33  		service.Register("foo", func(ctx context.Context) error {
    34  			i++
    35  			return nil
    36  		}, "")
    37  
    38  		err := service.Call(auth.ContextWithID(context.Background(), "1312"), "foo")
    39  		if err != nil {
    40  			t.Error(err)
    41  		}
    42  		if i != 1 {
    43  			t.Errorf("i should be 1, got %d", i)
    44  		}
    45  
    46  		a, err := service.Store().FindByID(context.Background(), "foo")
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  		if a.Calls != 1 {
    51  			t.Errorf("Calls should be 1, got %d", a.Calls)
    52  		}
    53  		if a.Error != "" {
    54  			t.Errorf("Error should be empty, got '%s'", a.Error)
    55  		}
    56  		if a.LastCalledBy != "id:1312" {
    57  			t.Errorf("LastCalledBy should be '1312', got %s", a.LastCalledBy)
    58  		}
    59  	})
    60  
    61  	t.Run("Error Call", func(t *testing.T) {
    62  		service.Register("bar", func(ctx context.Context) error {
    63  			return fmt.Errorf("oh no")
    64  		}, "")
    65  
    66  		err := service.Call(auth.ContextWithID(context.Background(), "1312"), "bar")
    67  		if err == nil {
    68  			t.Error("should return an error")
    69  		}
    70  
    71  		a, err := service.Store().FindByID(context.Background(), "bar")
    72  		if err != nil {
    73  			t.Fatal(err)
    74  		}
    75  		if a.Calls != 1 {
    76  			t.Errorf("Calls should be 1, got %d", a.Calls)
    77  		}
    78  		if a.Error != "oh no" {
    79  			t.Errorf("Error should be 'oh no', got '%s'", a.Error)
    80  		}
    81  		if a.LastCalledBy != "id:1312" {
    82  			t.Errorf("LastCalledBy should be '1312', got %s", a.LastCalledBy)
    83  		}
    84  	})
    85  }