github.com/condensat/bank-core@v0.1.0/database/query/operationstatus_test.go (about)

     1  // Copyright 2020 Condensat Tech. All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  package query
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/condensat/bank-core/database/model"
    12  	"github.com/condensat/bank-core/database/query/tests"
    13  )
    14  
    15  func TestAddOrUpdateOperationStatus(t *testing.T) {
    16  	const databaseName = "TestAddOrUpdateOperationStatus"
    17  	t.Parallel()
    18  
    19  	db := tests.Setup(databaseName, OperationInfoModel())
    20  	defer tests.Teardown(db, databaseName)
    21  
    22  	open, err := AddOrUpdateOperationStatus(db, model.OperationStatus{OperationInfoID: 42, State: "open"})
    23  	if err != nil {
    24  		t.Errorf("Unable to create reference data")
    25  	}
    26  
    27  	close := updateOperationSate(open, "close")
    28  
    29  	type args struct {
    30  		operation model.OperationStatus
    31  	}
    32  	tests := []struct {
    33  		name    string
    34  		args    args
    35  		want    model.OperationStatus
    36  		wantErr bool
    37  	}{
    38  		{"default", args{}, model.OperationStatus{}, true},
    39  		{"invalidState", args{model.OperationStatus{OperationInfoID: 1, State: ""}}, model.OperationStatus{}, true},
    40  
    41  		{"valid", args{model.OperationStatus{OperationInfoID: 42, State: "close"}}, close, false},
    42  		{"update", args{updateOperationSate(open, "close")}, close, false},
    43  	}
    44  	for _, tt := range tests {
    45  		tt := tt // capture range variable
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			got, err := AddOrUpdateOperationStatus(db, tt.args.operation)
    48  			if (err != nil) != tt.wantErr {
    49  				t.Errorf("AddOrUpdateOperationStatus() error = %v, wantErr %v", err, tt.wantErr)
    50  				return
    51  			}
    52  			tt.want.LastUpdate = got.LastUpdate
    53  			if !reflect.DeepEqual(got, tt.want) {
    54  				t.Errorf("AddOrUpdateOperationStatus() = %v, want %v", got, tt.want)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestGetOperationStatus(t *testing.T) {
    61  	const databaseName = "TestGetOperationStatus"
    62  	t.Parallel()
    63  
    64  	db := tests.Setup(databaseName, OperationInfoModel())
    65  	defer tests.Teardown(db, databaseName)
    66  
    67  	const infoID = model.OperationInfoID(42)
    68  	ref1, _ := AddOrUpdateOperationStatus(db, model.OperationStatus{OperationInfoID: infoID, State: "state"})
    69  
    70  	type args struct {
    71  		operationInfoID model.OperationInfoID
    72  	}
    73  	tests := []struct {
    74  		name    string
    75  		args    args
    76  		want    model.OperationStatus
    77  		wantErr bool
    78  	}{
    79  		{"default", args{}, model.OperationStatus{}, true},
    80  		{"notExists", args{1337}, model.OperationStatus{}, true},
    81  
    82  		{"valid", args{ref1.OperationInfoID}, ref1, false},
    83  	}
    84  	for _, tt := range tests {
    85  		tt := tt // capture range variable
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			got, err := GetOperationStatus(db, tt.args.operationInfoID)
    88  			if (err != nil) != tt.wantErr {
    89  				t.Errorf("GetOperationStatus() error = %v, wantErr %v", err, tt.wantErr)
    90  				return
    91  			}
    92  			if !reflect.DeepEqual(got, tt.want) {
    93  				t.Errorf("GetOperationStatus() = %v, want %v", got, tt.want)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestFindActiveOperationInfo(t *testing.T) {
   100  	const databaseName = "TestFindActiveOperationInfo"
   101  	t.Parallel()
   102  
   103  	db := tests.Setup(databaseName, OperationInfoModel())
   104  	defer tests.Teardown(db, databaseName)
   105  
   106  	tests := []struct {
   107  		name    string
   108  		want    []model.OperationInfo
   109  		wantErr bool
   110  	}{
   111  		{"default", nil, false},
   112  	}
   113  	for _, tt := range tests {
   114  		tt := tt // capture range variable
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			got, err := FindActiveOperationInfo(db)
   117  			if (err != nil) != tt.wantErr {
   118  				t.Errorf("FindActiveOperationInfo() error = %v, wantErr %v", err, tt.wantErr)
   119  				return
   120  			}
   121  			if !reflect.DeepEqual(got, tt.want) {
   122  				t.Errorf("FindActiveOperationInfo() = %v, want %v", got, tt.want)
   123  			}
   124  		})
   125  	}
   126  }
   127  
   128  func TestFindActiveOperationStatus(t *testing.T) {
   129  	const databaseName = "TestFindActiveOperationStatus"
   130  	t.Parallel()
   131  
   132  	db := tests.Setup(databaseName, OperationInfoModel())
   133  	defer tests.Teardown(db, databaseName)
   134  
   135  	_, _ = AddOrUpdateOperationStatus(db, model.OperationStatus{OperationInfoID: 42, State: "settled", Accounted: "settled"})
   136  	active1, _ := AddOrUpdateOperationStatus(db, model.OperationStatus{OperationInfoID: 43, State: "state", Accounted: "active"})
   137  	active2, _ := AddOrUpdateOperationStatus(db, model.OperationStatus{OperationInfoID: 44, State: "settled", Accounted: "active"})
   138  	active3, _ := AddOrUpdateOperationStatus(db, model.OperationStatus{OperationInfoID: 45, State: "state", Accounted: "settled"})
   139  
   140  	tests := []struct {
   141  		name    string
   142  		want    []model.OperationStatus
   143  		wantErr bool
   144  	}{
   145  		{"active", []model.OperationStatus{active1, active2, active3}, false},
   146  	}
   147  	for _, tt := range tests {
   148  		tt := tt // capture range variable
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			got, err := FindActiveOperationStatus(db)
   151  			if (err != nil) != tt.wantErr {
   152  				t.Errorf("FindActiveOperationStatus() error = %v, wantErr %v", err, tt.wantErr)
   153  				return
   154  			}
   155  			if !reflect.DeepEqual(got, tt.want) {
   156  				t.Errorf("FindActiveOperationStatus() = %v, want %v", got, tt.want)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func updateOperationSate(operation model.OperationStatus, state string) model.OperationStatus {
   163  	result := operation
   164  	result.State = state
   165  	return result
   166  }