github.com/Shahlojon/wallet@v1.3.1-0.20201026124859-dcfb12a6a993/pkg/wallet/service_test.go (about)

     1  package wallet
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"testing"
     7  	"log"
     8  	"github.com/Shahlojon/wallet/pkg/types"
     9  	"github.com/google/uuid"
    10  )
    11  
    12  func TestService_RegisterAccount_unsuccess(t *testing.T) {
    13  	vc := Service{}
    14  
    15  	accounts := []types.Account{
    16  		{ID: 1, Phone: "+992000000001", Balance: 2_000_00},
    17  		{ID: 2, Phone: "+992000000002", Balance: 3_000_00},
    18  		{ID: 3, Phone: "+992000000003", Balance: 4_000_00},
    19  		{ID: 4, Phone: "+992000000004", Balance: 5_000_00},
    20  		{ID: 5, Phone: "+992000000005", Balance: 6_000_00},
    21  		{ID: 6, Phone: "+992000000006", Balance: 7_000_00},
    22  	}
    23  	result, err := vc.RegisterAccount("+992000000007")
    24  	for _, account := range accounts {
    25  		if account.Phone == result.Phone {
    26  			t.Errorf("invalid result, expected: %v, actual: %v", err, result)
    27  			break
    28  		}
    29  	}
    30  }
    31  
    32  func TestService_FindAccoundById_Method_NotFound(t *testing.T) {
    33  	svc := Service{}
    34  	svc.RegisterAccount("+9920000001")
    35  
    36  	account, err := svc.FindAccountByID(3)
    37  	if err == nil {
    38  		t.Errorf("\ngot > %v \nwant > nil", account)
    39  	}
    40  }
    41  
    42  func TestService_FindPaymentByID_success(t *testing.T) {
    43  	//создаем сервис
    44  	s := newTestServiceUser()
    45  	_, payments, err := s.addAccountUser(defaultTestAccountUser)
    46  	if err != nil {
    47  		t.Error(err)
    48  		return
    49  	}
    50  
    51  	//пробуем найти платеж
    52  	payment := payments[0]
    53  	got, err := s.FindPaymentByID(payment.ID)
    54  	if err != nil {
    55  		t.Errorf("FindPaymentByID(): error = %v", err)
    56  	}
    57  
    58  	if !reflect.DeepEqual(payment, got) {
    59  		t.Errorf("FindPaymentByID(): wrong payment returned = %v", err)
    60  	}
    61  }
    62  
    63  func TestService_FindPaymentByID_fail(t *testing.T) {
    64  	//создаем сервис
    65  	s := newTestServiceUser()
    66  	_, _, err := s.addAccountUser(defaultTestAccountUser)
    67  	if err != nil {
    68  		t.Error(err)
    69  		return
    70  	}
    71  
    72  	//пробуем найти несуществующий платеж
    73  	_, err = s.FindPaymentByID(uuid.New().String())
    74  	if err == nil {
    75  		t.Error("FindPaymentByID: must return error, returned nil")
    76  		return
    77  	}
    78  
    79  	if err != ErrPaymentNotFound {
    80  		t.Errorf("FindPaymentByID(): must return ErrPaymnetNotFound, returned = %v", err)
    81  		return
    82  	}
    83  }
    84  
    85  func TestService_Reject_fail(t *testing.T) {
    86  	svc := Service{}
    87  	svc.RegisterAccount("+9920000001")
    88  
    89  	account, err := svc.FindAccountByID(1)
    90  	if err != nil {
    91  		t.Errorf("\ngot > %v \nwant > nil", err)
    92  	}
    93  
    94  	err = svc.Deposit(account.ID, 1000_00)
    95  	if err != nil {
    96  		t.Errorf("\ngot > %v \nwant > nil", err)
    97  	}
    98  
    99  	payment, err := svc.Pay(account.ID, 100_00, "auto")
   100  	if err != nil {
   101  		t.Errorf("\ngot > %v \nwant > nil", err)
   102  	}
   103  
   104  	pay, err := svc.FindPaymentByID(payment.ID)
   105  	if err != nil {
   106  		t.Errorf("\ngot > %v \nwant > nil", err)
   107  	}
   108  
   109  	editPayID := pay.ID + "l"
   110  	err = svc.Reject(editPayID)
   111  	if err == nil {
   112  		t.Errorf("\ngot > %v \nwant > nil", err)
   113  	}
   114  }
   115  
   116  func TestService_Reject_success(t *testing.T) {
   117  	//создаем сервис
   118  	s := newTestServiceUser()
   119  	_, payments, err := s.addAccountUser(defaultTestAccountUser)
   120  	if err != nil {
   121  		t.Error(err)
   122  		return
   123  	}
   124  
   125  	//пробуем отменить платёж
   126  	payment := payments[0]
   127  	err = s.Reject(payment.ID)
   128  	if err != nil {
   129  		t.Errorf("Reject(): error = %v", err)
   130  		return
   131  	}
   132  
   133  	savedPayment, err := s.FindPaymentByID(payment.ID)
   134  	if err != nil {
   135  		t.Errorf("Reject(): status didn't changed, paymnet = %v", savedPayment)
   136  		return
   137  	}
   138  	savedAccount, err := s.FindAccountByID(payment.AccountID)
   139  	if err != nil {
   140  		t.Errorf("Reject(): can't find account by id, error = %v", err)
   141  		return
   142  	}
   143  	if savedAccount.Balance != defaultTestAccountUser.balance {
   144  		t.Errorf("Reject(): balance didn't changed, account = %v", savedAccount)
   145  		return
   146  	}
   147  }
   148  
   149  
   150  func TestService_Repeat_success_user(t *testing.T) {
   151  	//создаем сервис
   152  	s := newTestServiceUser()
   153  	s.RegisterAccount("+9922000000")
   154  	account, err :=s.FindAccountByID(1)
   155  	if err != nil {
   156  		t.Error(err)
   157  		return
   158  	}
   159  	//пополняем баланс
   160  	err = s.Deposit(account.ID, 1000_00)
   161  	if err != nil {
   162  		t.Errorf("\ngot > %v \nwant > nil", err)
   163  	}
   164  	//pay
   165  	payment, err := s.Pay(account.ID, 100_00, "auto")
   166  	if err != nil {
   167  		t.Errorf("\ngot > %v \nwant > nil", err)
   168  	}
   169  
   170  	pay, err := s.FindPaymentByID(payment.ID)
   171  	if err != nil {
   172  		t.Errorf("\ngot > %v \nwant > nil", err)
   173  	}
   174  
   175  	pay, err = s.Repeat(pay.ID)
   176  	if err != nil {
   177  		t.Errorf("Repeat(): can't payment for an account(%v), error(%v)",pay.ID, err)
   178  	}
   179  }
   180  
   181  func TestService_Export_success_user(t *testing.T) {
   182  	var svc Service
   183  
   184  	svc.RegisterAccount("+992000000001")
   185  	svc.RegisterAccount("+992000000002")
   186  	svc.RegisterAccount("+992000000003")
   187  
   188  	err := svc.ExportToFile("export.txt")
   189  	if err != nil {
   190  		t.Errorf("method ExportToFile returned not nil error, err => %v", err)
   191  	}
   192  
   193  }
   194  
   195  func TestService_Import_success_user(t *testing.T) {
   196  	var svc Service
   197  
   198  
   199  	err := svc.ImportFromFile("export.txt")
   200  	
   201  	if err != nil {
   202  		t.Errorf("method ExportToFile returned not nil error, err => %v", err)
   203  	}
   204  
   205  }
   206  
   207  
   208  
   209  func TestService_ExportImport_success_user(t *testing.T) {
   210  	var svc Service
   211  
   212  	svc.RegisterAccount("+992000000001")
   213  	svc.RegisterAccount("+992000000002")
   214  	svc.RegisterAccount("+992000000003")
   215  	svc.RegisterAccount("+992000000004")
   216  
   217  	wd,_:= os.Getwd()
   218  	err := svc.Export(wd)
   219  	if err != nil {
   220  		t.Errorf("method Export returned not nil error, err => %v", err)
   221  	}
   222  	err = svc.Import(wd)
   223  	if err != nil {
   224  		t.Errorf("method Import returned not nil error, err => %v", err)
   225  	}
   226  }
   227  
   228  
   229  func TestService_ExportHistory_success_user(t *testing.T) {
   230  	var svc Service
   231  
   232  	account, err := svc.RegisterAccount("+992000000001")
   233  
   234  	if err != nil {
   235  		t.Errorf("method RegisterAccount returned not nil error, account => %v", account)
   236  	}
   237  
   238  	err = svc.Deposit(account.ID, 100_00)
   239  	if err != nil {
   240  		t.Errorf("method Deposit returned not nil error, error => %v", err)
   241  	}
   242  
   243  	_, err = svc.Pay(account.ID, 1, "Cafe")
   244  	_, err = svc.Pay(account.ID, 2, "Cafe")
   245  	_, err = svc.Pay(account.ID, 3, "Cafe")
   246  	_, err = svc.Pay(account.ID, 4, "Cafe")
   247  	_, err = svc.Pay(account.ID, 5, "Cafe")
   248  	_, err = svc.Pay(account.ID, 6, "Cafe")
   249  	_, err = svc.Pay(account.ID, 7, "Cafe")
   250  	_, err = svc.Pay(account.ID, 8, "Cafe")
   251  	_, err = svc.Pay(account.ID, 9, "Cafe")
   252  	_, err = svc.Pay(account.ID, 10, "Cafe")
   253  	_, err = svc.Pay(account.ID, 11, "Cafe")
   254  	if err != nil {
   255  		t.Errorf("method Pay returned not nil error, err => %v", err)
   256  	}
   257  
   258  	payments, err := svc.ExportAccountHistory(account.ID)
   259  
   260  	if err != nil {
   261  		t.Errorf("method ExportAccountHistory returned not nil error, err => %v", err)
   262  	}
   263  	err = svc.HistoryToFiles(payments, "data", 4)
   264  
   265  	if err != nil {
   266  		t.Errorf("method HistoryToFiles returned not nil error, err => %v", err)
   267  	}
   268  
   269  }
   270  
   271  func TestService_ExportHistory(t *testing.T) {
   272  	svc := &Service{}
   273  
   274  	account, err := svc.RegisterAccount("+992000000001")
   275  	if err != nil {
   276  	}
   277  
   278  	err = svc.Deposit(account.ID, 100_00)
   279  	if err != nil {
   280  	}
   281  
   282  	svc.Pay(account.ID, 10_00, "auto")
   283  
   284  	payment, err := svc.ExportAccountHistory(1)
   285  	if err != nil {
   286  		t.Error(err)
   287  	}
   288  	err = svc.HistoryToFiles(payment, "data", 2)
   289  	if err != nil {
   290  		t.Error(err)
   291  	}
   292  }
   293  func TestService_FavoritePayment_success_user(t *testing.T) {
   294  	//создаем сервис
   295  	var s Service
   296  
   297  	account, err := s.RegisterAccount("+9922000000")
   298  	if err != nil {
   299  		t.Errorf("method RegisterAccount return not nil error, account=>%v", account)
   300  		return
   301  	}
   302  	//пополняем баланс
   303  	err = s.Deposit(account.ID, 1000_00)
   304  	if err != nil {
   305  		t.Errorf("method Deposit return not nil error, error=>%v", err)
   306  	}
   307  	//pay
   308  	payment, err := s.Pay(account.ID, 100_00, "auto")
   309  	if err != nil {
   310  		t.Errorf("method Pay return not nil error, account=>%v", account)
   311  	}
   312  	//edit favorite
   313  	favorite, err := s.FavoritePayment(payment.ID, "auto")
   314  	if err != nil {
   315  		t.Errorf("method FavoritePayment returned not nil error, favorite=>%v", favorite)
   316  	}
   317  
   318  	paymentFavorite, err := s.PayFromFavorite(favorite.ID)
   319  	if err != nil {
   320  		t.Errorf("method PayFromFavorite returned nil, paymentFavorite(%v)", paymentFavorite)
   321  	}
   322  }
   323  
   324  func BenchmarkSumPayment(b *testing.B){
   325  	var svc Service
   326  
   327  	account, err := svc.RegisterAccount("+992000000001")
   328  
   329  	if err != nil {
   330  		b.Errorf("method RegisterAccount returned not nil error, account => %v", account)
   331  	}
   332  
   333  	err = svc.Deposit(account.ID, 100_00)
   334  	if err != nil {
   335  		b.Errorf("method Deposit returned not nil error, error => %v", err)
   336  	}
   337  
   338  	_, err = svc.Pay(account.ID, 1, "Cafe")
   339  	_, err = svc.Pay(account.ID, 2, "Cafe")
   340  	_, err = svc.Pay(account.ID, 3, "Cafe")
   341  	_, err = svc.Pay(account.ID, 4, "Cafe")
   342  	_, err = svc.Pay(account.ID, 5, "Cafe")
   343  	_, err = svc.Pay(account.ID, 6, "Cafe")
   344  	_, err = svc.Pay(account.ID, 7, "Cafe")
   345  	_, err = svc.Pay(account.ID, 8, "Cafe")
   346  	_, err = svc.Pay(account.ID, 9, "Cafe")
   347  	_, err = svc.Pay(account.ID, 10, "Cafe")
   348  	_, err = svc.Pay(account.ID, 11, "Cafe")
   349  	if err != nil {
   350  		b.Errorf("method Pay returned not nil error, err => %v", err)
   351  	}
   352  
   353  	want := types.Money(66)
   354  
   355  	got := svc.SumPayments(2)
   356  	if want != got{
   357  		b.Errorf(" error, want => %v got => %v", want, got)
   358  	}
   359  }
   360  
   361  func BenchmarkFilterPayments(b *testing.B) {
   362  	svc := &Service{}
   363  
   364  	account, err := svc.RegisterAccount("+992000000000")
   365  	account1, err := svc.RegisterAccount("+992000000001")
   366  	account2, err := svc.RegisterAccount("+992000000002")
   367  	account3, err := svc.RegisterAccount("+992000000003")
   368  	account4, err := svc.RegisterAccount("+992000000004")
   369  	acc, err := svc.RegisterAccount("+992000000005")
   370  	if err != nil {
   371  	}
   372  	svc.Deposit(acc.ID, 100)
   373  	err = svc.Deposit(account.ID, 100_00)
   374  	if err != nil {
   375  	}
   376  
   377  	svc.Pay(account.ID, 10_00, "auto")
   378  	svc.Pay(account.ID, 10_00, "auto")
   379  	svc.Pay(account1.ID, 10_00, "auto")
   380  	svc.Pay(account2.ID, 10_00, "auto")
   381  	svc.Pay(account1.ID, 10_00, "auto")
   382  	svc.Pay(account1.ID, 10_00, "auto")
   383  	svc.Pay(account3.ID, 10_00, "auto")
   384  	svc.Pay(account4.ID, 10_00, "auto")
   385  	svc.Pay(account1.ID, 10_00, "auto")
   386  	svc.Pay(account3.ID, 10_00, "auto")
   387  	svc.Pay(account2.ID, 10_00, "auto")
   388  	svc.Pay(account4.ID, 10_00, "auto")
   389  	svc.Pay(account4.ID, 10_00, "auto")
   390  	svc.Pay(account4.ID, 10_00, "auto")
   391  
   392  	a, err := svc.FilterPayments(account.ID, 5)
   393  	if err != nil {
   394  		b.Error(err)
   395  	}
   396  	log.Println(len(a))
   397  }
   398  
   399  func BenchmarkService_FilterPaymentsByFn(b *testing.B) {
   400  	svc := &Service{}
   401  	filter := func(payment types.Payment) bool {
   402  		for _, value := range svc.payments {
   403  			if payment.ID == value.ID {
   404  				return true
   405  			}
   406  		}
   407  		return false
   408  	}
   409  	account, err := svc.RegisterAccount("+992000000000")
   410  	account1, err := svc.RegisterAccount("+992000000001")
   411  	account2, err := svc.RegisterAccount("+992000000002")
   412  	account3, err := svc.RegisterAccount("+992000000003")
   413  	account4, err := svc.RegisterAccount("+992000000004")
   414  	acc, err := svc.RegisterAccount("+992000000005")
   415  	if err != nil {
   416  	}
   417  	svc.Deposit(acc.ID, 100)
   418  	err = svc.Deposit(account.ID, 100_00)
   419  	if err != nil {
   420  	}
   421  
   422  	svc.Pay(account.ID, 10_00, "auto")
   423  	svc.Pay(account.ID, 10_00, "auto")
   424  	svc.Pay(account1.ID, 10_00, "auto")
   425  	svc.Pay(account2.ID, 10_00, "auto")
   426  	svc.Pay(account1.ID, 10_00, "auto")
   427  	svc.Pay(account1.ID, 10_00, "auto")
   428  	svc.Pay(account3.ID, 10_00, "auto")
   429  	svc.Pay(account4.ID, 10_00, "auto")
   430  	svc.Pay(account1.ID, 10_00, "auto")
   431  	svc.Pay(account3.ID, 10_00, "auto")
   432  	svc.Pay(account2.ID, 10_00, "auto")
   433  	svc.Pay(account4.ID, 10_00, "auto")
   434  	svc.Pay(account4.ID, 10_00, "auto")
   435  	svc.Pay(account4.ID, 10_00, "auto")
   436  	a, err := svc.FilterPaymentsByFn(filter, 4)
   437  	if err != nil {
   438  		b.Error(err)
   439  	}
   440  	log.Println(a)
   441  }
   442  
   443  func BenchmarkSumPaymentsWithProgress_user(b *testing.B) {
   444  	var svc Service
   445  
   446  	account, err := svc.RegisterAccount("+992000000001")
   447  
   448  	if err != nil {
   449  		b.Errorf("method RegisterAccount returned not nil error, account => %v", account)
   450  	}
   451  
   452  	err = svc.Deposit(account.ID, 10000000_0000000)
   453  	if err != nil {
   454  		b.Errorf("method Deposit returned not nil error, error => %v", err)
   455  	}
   456  
   457  	// for i := 0; i < 10000; i++ {
   458  	// 	svc.Pay(account.ID, types.Money(i), "Cafe")
   459  	// } 
   460  
   461  	// ch := svc.SumPaymentsWithProgress()
   462  
   463  	
   464  
   465  	sum :=svc.SumPayments(2)
   466  	//  s, ok := <-ch
   467  	total := types.Money(0)
   468  	for i := range svc.SumPaymentsWithProgress() {
   469  		total += i.Result
   470  	}
   471  	if sum != total{
   472  		b.Error("Errorrrorororo")
   473  	}
   474  	// if !ok {
   475  	// 	b.Errorf(" method SumPaymentsWithProgress ok not closed => %v", ok)
   476  	// }
   477  
   478  	// log.Println("=======>>>>>",s) 
   479  }
   480  
   481  func TestService_SumPayments(b *testing.T) {
   482  	svc := &Service{}
   483  
   484  	account, err := svc.RegisterAccount("+992000000001")
   485  	if err != nil {
   486  	}
   487  
   488  	err = svc.Deposit(account.ID, 100_00)
   489  	if err != nil {
   490  	}
   491  
   492  	svc.Pay(account.ID, 10_00, "auto")
   493  	svc.Pay(account.ID, 10_00, "auto")
   494  	svc.Pay(account.ID, 10_00, "auto")
   495  	svc.Pay(account.ID, 10_00, "auto")
   496  	svc.Pay(account.ID, 10_00, "auto")
   497  	svc.Pay(account.ID, 10_00, "auto")
   498  	svc.Pay(account.ID, 10_00, "auto")
   499  	want := types.Money(7000)
   500  
   501  	got := svc.SumPayments(5)
   502  	if want != got {
   503  		b.Errorf(" error, want => %v got => %v", want, got)
   504  	}
   505  }
   506