github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/tests/errors_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/ShoshinNikita/budget-manager/internal/web/api/models"
     9  )
    10  
    11  func TestBadRequests(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	RunTest(t, TestCases{
    15  		{Name: "init", Fn: testErrors_InitData},
    16  		{Name: "get", Fn: testErrors_GetRequests},
    17  		{Name: "add", Fn: testErrors_AddRequests},
    18  		{Name: "edit", Fn: testErrors_EditRequests},
    19  		{Name: "remove", Fn: testErrors_RemoveRequests},
    20  	})
    21  }
    22  
    23  func testErrors_InitData(t *testing.T, host string) {
    24  	for _, req := range []RequestCreated{
    25  		{POST, SpendTypesPath, models.AddSpendTypeReq{Name: "#1"}},
    26  		{POST, SpendTypesPath, models.AddSpendTypeReq{Name: "#2", ParentID: 1}},
    27  		{POST, SpendTypesPath, models.AddSpendTypeReq{Name: "For monthly payments"}},
    28  		{POST, SpendTypesPath, models.AddSpendTypeReq{Name: "For spends"}},
    29  		//
    30  		{POST, IncomesPath, models.AddIncomeReq{MonthID: 1, Title: "title", Income: 100}},
    31  		//
    32  		{POST, MonthlyPaymentsPath, models.AddMonthlyPaymentReq{MonthID: 1, Title: "title", TypeID: 3, Cost: 100}},
    33  		//
    34  		{POST, SpendsPath, models.AddSpendReq{DayID: 1, Title: "title", TypeID: 4, Cost: 100}},
    35  	} {
    36  		req.Send(t, host, nil)
    37  	}
    38  }
    39  
    40  func testErrors_GetRequests(t *testing.T, host string) {
    41  	for _, tt := range []struct {
    42  		path Path
    43  		req  interface{}
    44  		err  string
    45  		code int
    46  	}{
    47  		{SearchSpendsPath, models.SearchSpendsReq{MinCost: 10, MaxCost: 5}, "min_cost can't be greater than max_cost", http.StatusBadRequest},
    48  		{MonthsPath, models.GetMonthByDateReq{Year: 2020, Month: time.January}, "such Month doesn't exist", http.StatusNotFound},
    49  		{MonthsPath, models.GetMonthByDateReq{Year: 2020, Month: -1}, "invalid month", http.StatusBadRequest},
    50  		{MonthsPath, models.GetMonthByDateReq{Year: 2020, Month: 0}, "invalid month", http.StatusBadRequest},
    51  		{MonthsPath, models.GetMonthByDateReq{Year: 2020, Month: 13}, "invalid month", http.StatusBadRequest},
    52  	} {
    53  		Request{GET, tt.path, tt.req, tt.code, tt.err}.Send(t, host, nil)
    54  	}
    55  }
    56  
    57  func testErrors_AddRequests(t *testing.T, host string) {
    58  	for _, tt := range []struct {
    59  		path Path
    60  		req  interface{}
    61  		err  string
    62  	}{
    63  		{IncomesPath, models.AddIncomeReq{MonthID: 0, Title: "1", Income: 1}, "month_id can't be empty or zero"},
    64  		{IncomesPath, models.AddIncomeReq{MonthID: 1, Title: "", Income: 1}, "title can't be empty"},
    65  		{IncomesPath, models.AddIncomeReq{MonthID: 1, Title: "", Income: 1}, "title can't be empty"},
    66  		{IncomesPath, models.AddIncomeReq{MonthID: 1, Title: "1", Income: 0}, "income must be greater than zero"},
    67  		{IncomesPath, models.AddIncomeReq{MonthID: 1, Title: "1", Income: -1}, "income must be greater than zero"},
    68  		//
    69  		{MonthlyPaymentsPath, models.AddMonthlyPaymentReq{MonthID: 0, Title: "1", Cost: 1}, "month_id can't be empty or zero"},
    70  		{MonthlyPaymentsPath, models.AddMonthlyPaymentReq{MonthID: 1, Title: "	", Cost: 1}, "title can't be empty"},
    71  		{MonthlyPaymentsPath, models.AddMonthlyPaymentReq{MonthID: 1, Title: "1", Cost: 0}, "cost must be greater than zero"},
    72  		{MonthlyPaymentsPath, models.AddMonthlyPaymentReq{MonthID: 1, Title: "1", Cost: -1}, "cost must be greater than zero"},
    73  		{MonthlyPaymentsPath, models.AddMonthlyPaymentReq{MonthID: 1, Title: "1", Cost: 1, TypeID: 10}, "such Spend Type doesn't exist"},
    74  		//
    75  		{SpendsPath, models.AddSpendReq{DayID: 0, Title: "1", Cost: 1}, "day_id can't be empty or zero"},
    76  		{SpendsPath, models.AddSpendReq{DayID: 1, Title: "           ", Cost: 1}, "title can't be empty"},
    77  		{SpendsPath, models.AddSpendReq{DayID: 1, Title: "1", Cost: -1}, "cost must be greater or equal to zero"},
    78  		{SpendsPath, models.AddSpendReq{DayID: 1, Title: "1", Cost: 1, TypeID: 10}, "such Spend Type doesn't exist"},
    79  		//
    80  		{SpendTypesPath, models.AddSpendTypeReq{Name: "   "}, "name can't be empty"},
    81  	} {
    82  		Request{POST, tt.path, tt.req, http.StatusBadRequest, tt.err}.Send(t, host, nil)
    83  	}
    84  }
    85  
    86  func testErrors_EditRequests(t *testing.T, host string) {
    87  	// Not Found
    88  	for _, tt := range []struct {
    89  		path Path
    90  		req  interface{}
    91  		err  string
    92  	}{
    93  		{IncomesPath, models.EditIncomeReq{ID: 10, Title: ptrStr("new")}, "such Income doesn't exist"},
    94  		{MonthlyPaymentsPath, models.EditMonthlyPaymentReq{ID: 10, Title: ptrStr("new")}, "such Monthly Payment doesn't exist"},
    95  		{SpendsPath, models.EditSpendReq{ID: 10, Title: ptrStr("new")}, "such Spend doesn't exist"},
    96  		{SpendTypesPath, models.EditSpendTypeReq{ID: 10, Name: ptrStr("new")}, "such Spend Type doesn't exist"},
    97  	} {
    98  		Request{PUT, tt.path, tt.req, http.StatusNotFound, tt.err}.Send(t, host, nil)
    99  	}
   100  
   101  	// Bad Requests
   102  	for _, tt := range []struct {
   103  		path Path
   104  		req  interface{}
   105  		err  string
   106  	}{
   107  		{IncomesPath, models.EditIncomeReq{ID: 1, Title: ptrStr("")}, "title can't be empty"},
   108  		{IncomesPath, models.EditIncomeReq{ID: 1, Title: ptrStr("          ")}, "title can't be empty"},
   109  		{IncomesPath, models.EditIncomeReq{ID: 1, Income: ptrFloat(0)}, "income must be greater than zero"},
   110  		{IncomesPath, models.EditIncomeReq{ID: 1, Income: ptrFloat(-11)}, "income must be greater than zero"},
   111  		//
   112  		{MonthlyPaymentsPath, models.EditMonthlyPaymentReq{ID: 1, Title: ptrStr("   ")}, "title can't be empty"},
   113  		{MonthlyPaymentsPath, models.EditMonthlyPaymentReq{ID: 1, Cost: ptrFloat(0)}, "cost must be greater than zero"},
   114  		{MonthlyPaymentsPath, models.EditMonthlyPaymentReq{ID: 1, Cost: ptrFloat(-11)}, "cost must be greater than zero"},
   115  		{MonthlyPaymentsPath, models.EditMonthlyPaymentReq{ID: 1, TypeID: ptrUint(10)}, "such Spend Type doesn't exist"},
   116  		//
   117  		{SpendsPath, models.EditSpendReq{ID: 1, Title: ptrStr("	")}, "title can't be empty"},
   118  		{SpendsPath, models.EditSpendReq{ID: 1, Cost: ptrFloat(-10)}, "cost must be greater or equal to zero"},
   119  		{SpendsPath, models.EditSpendReq{ID: 1, TypeID: ptrUint(10)}, "such Spend Type doesn't exist"},
   120  		//
   121  		{SpendTypesPath, models.EditSpendTypeReq{ID: 1, Name: ptrStr("     ")}, "name can't be empty"},
   122  		{SpendTypesPath, models.EditSpendTypeReq{ID: 1, ParentID: ptrUint(10)}, "check for a cycle failed: invalid Spend Type"},
   123  		{SpendTypesPath, models.EditSpendTypeReq{ID: 1, ParentID: ptrUint(2)}, "Spend Type with new parent type will have a cycle"},
   124  	} {
   125  		Request{PUT, tt.path, tt.req, http.StatusBadRequest, tt.err}.Send(t, host, nil)
   126  	}
   127  }
   128  
   129  func testErrors_RemoveRequests(t *testing.T, host string) {
   130  	for _, tt := range []struct {
   131  		path   Path
   132  		req    interface{}
   133  		status int
   134  		err    string
   135  	}{
   136  		// Not Found
   137  		{IncomesPath, models.RemoveIncomeReq{ID: 10}, http.StatusNotFound, "such Income doesn't exist"},
   138  		{MonthlyPaymentsPath, models.RemoveMonthlyPaymentReq{ID: 10}, http.StatusNotFound, "such Monthly Payment doesn't exist"},
   139  		{SpendsPath, models.RemoveSpendReq{ID: 10}, http.StatusNotFound, "such Spend doesn't exist"},
   140  		{SpendTypesPath, models.RemoveSpendTypeReq{ID: 10}, http.StatusNotFound, "such Spend Type doesn't exist"},
   141  		// Bad Request
   142  		{SpendTypesPath, models.RemoveSpendTypeReq{ID: 3}, http.StatusBadRequest, "Spend Type is used by Monthly Payment or Spend"},
   143  		{SpendTypesPath, models.RemoveSpendTypeReq{ID: 4}, http.StatusBadRequest, "Spend Type is used by Monthly Payment or Spend"},
   144  	} {
   145  		Request{DELETE, tt.path, tt.req, tt.status, tt.err}.Send(t, host, nil)
   146  	}
   147  }