Newer
Older
Name: "huevos",
Description: "huevos frescos",
Deadline: time.Now().Add(24 * time.Hour),
Products: []db.OrderProduct{
{
ProductCode: testProduct.Code,
Price: testProduct.Price,
},
},
}
func TestOrderAddList(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
resp := tapi.do("GET", "/order", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Wrong number of orders", len(orders), orders)
}
if orders[0].Name != testOrder.Name {
t.Error("Wrong name:", orders[0].Name)
}
if len(orders[0].Products) != 1 {
t.Fatal("Wrong number of products", len(orders[0].Products), orders[0].Products)
}
if orders[0].Products[0].Price != testProduct.Price {
t.Error("Wrong product price:", orders[0].Products[0].Price)
}
}
func TestOrderActive(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
resp := tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Wrong number of orders", len(orders), orders)
}
if orders[0].Name != testOrder.Name {
t.Error("Wrong name:", orders[0].Name)
}
}
func TestOrderDelete(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
order := testOrder
order.Deadline = time.Now().Add(-24 * time.Hour)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
var orders []db.Order
resp = tapi.do("GET", "/order", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
purchase := []db.OrderPurchase{
{
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
},
}
resp = tapi.doAdmin("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order purchase:", resp.Status)
}
if len(orders) != 1 {
t.Error("Deactivated wrong orders:", orders)
}
resp = tapi.doOrder("DELETE", fmt.Sprintf("/order/%d", orders[0].ID), nil, nil)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't delete order:", resp.Status)
}
resp = tapi.do("GET", "/order", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(orders) != 0 {
t.Error("The order didn't get removed", orders)
}
var transactions []db.Transaction
resp = tapi.doAdmin("GET", "/transaction", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(transactions) != 0 {
t.Error("The transactions didn't get removed", transactions)
}
var member db.Member
resp = tapi.doAdmin("GET", fmt.Sprintf("/member/%d", testMember.Num), nil, &member)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if member.Balance != testMember.Balance {
t.Error("Wrong balance on test member:", member.Balance)
}
resp = tapi.doAdmin("GET", fmt.Sprintf("/member/%d", testMemberAdmin.Num), nil, &member)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if member.Balance != testMemberAdmin.Balance {
t.Error("Wrong balance on admin member:", member.Balance)
}
}
func TestOrderPurchase(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestOrder()
resp := tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
resp = tapi.do("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", len(orders), orders)
}
if transactions[0].Total != -total {
t.Fatal("Wrong total", transactions[0].Total)
}
resp = tapi.do("GET", "/member/me", nil, &member)
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the member:", resp.Status)
}
if member.Balance != testMember.Balance-total {
t.Error("Wrong product balance:", member.Balance)
}
}
func TestOrderNoDeactivation(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
order := testOrder
now := time.Now()
order.Deadline = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Didn't find my new order")
}
if len(orders) != 0 {
t.Error("Deactivated some orders:", orders)
}
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Didn't find my new order after deactivation")
}
}
func TestOrderDeactivation(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
order := testOrder
order.Deadline = time.Now().Add(-24 * time.Hour)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Didn't find my new order")
}
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
resp = tapi.doAdmin("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
dbPath := path.Join(tapi.testPath, "test.db")
if err != nil {
t.Fatal("Can't initialize the db:", err)
}
orders = database.DeactivateOrders()
if len(orders) != 1 {
t.Error("Deactivated wrong orders:", orders)
}
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(orders) != 0 {
t.Fatal("I found some orders")
}
resp = tapi.doOrder("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", len(orders), orders)
}
if transactions[0].Type != "refund" {
t.Fatal("Should be a refund", transactions[0].Type)
}
if transactions[0].Total != total {
t.Fatal("Wrong total:", transactions[0].Total)
}
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't member:", resp.Status)
}
if member.Balance != testMemberOrder.Balance+total {
t.Fatal("Wrong member balance:", member.Balance, testMemberOrder.Balance, total)
func TestGetOrder(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
resp := tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Didn't find my new order")
}
id := strconv.Itoa(int(orders[0].ID))
var body OrderGetResponse
resp = tapi.do("GET", "/order/"+id, nil, &body)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get order:", resp.Status)
}
if body.Transaction != nil {
t.Error("Unexpected transaction", body.Transaction)
}
if body.Order.Name != testOrder.Name {
t.Error("Wrong name:", body.Order.Name)
}
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
resp = tapi.do("GET", "/order/"+id, nil, &body)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get order:", resp.Status)
}
if body.Transaction == nil {
t.Fatal("There is no transaction")
}
if body.Transaction.Type != "order" {
t.Error("Wrong transaction type", body.Transaction.Type)
}
}
func TestUpdateOrderPurchase(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestOrder()
var orders []db.Order
resp := tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
purchase := []db.OrderPurchase{
{
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
},
}
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
purchase[0].Amount = 2
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
var transactions []db.Transaction
resp = tapi.do("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", len(orders), orders)
}
total := 2 * testProduct.Price
if transactions[0].Total != -total {
t.Fatal("Wrong total", transactions[0].Total)
}
if transactions[0].OrderPurchase[0].Amount != 2 {
t.Fatal("Wrong amount", transactions[0].OrderPurchase)
}
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(orders[0].Transactions[0].OrderPurchase) != 1 {
t.Fatal("Wrong number of product purchases:", orders[0].Transactions[0].OrderPurchase)
}
var member db.Member
resp = tapi.do("GET", "/member/me", nil, &member)
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the member:", resp.Status)
}
if member.Balance != testMember.Balance-total {
t.Error("Wrong product balance:", member.Balance)
}
}
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
func TestOrderUpdate(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestOrder()
var orders []db.Order
resp := tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
purchase := []db.OrderPurchase{
{
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
},
}
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
order := testOrder
order.Products[0].Price = 1000
resp = tapi.doOrder("PUT", fmt.Sprintf("/order/%d", orders[0].ID), order, nil)
if resp.StatusCode != http.StatusAccepted {
tapi.t.Fatal("Can't update order:", resp.Status)
}
var transactions []db.Transaction
resp = tapi.do("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", len(orders), orders)
}
total := 3 * order.Products[0].Price
if transactions[0].Total != -total {
t.Error("Wrong total", transactions[0].Total)
}
var member db.Member
resp = tapi.do("GET", "/member/me", nil, &member)
if resp.StatusCode != http.StatusOK {
t.Error("Can't find the member:", resp.Status)
}
if member.Balance != testMember.Balance-total {
t.Error("Wrong product balance:", member.Balance)
}
}
func TestOrderUpdateProduct(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestOrder()
var orders []db.Order
resp := tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
purchase := []db.OrderPurchase{
{
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
},
}
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
testProduct2 := db.Product{
Code: 123,
Name: "Huevos",
Price: 120,
Stock: 15,
}
resp = tapi.doAdmin("POST", "/product", testProduct2, nil)
if resp.StatusCode != http.StatusCreated {
tapi.t.Fatal("Can't create product:", resp.Status)
}
order := testOrder
order.Products[0].Price = testProduct2.Price
order.Products[0].ProductCode = testProduct2.Code
resp = tapi.doOrder("PUT", fmt.Sprintf("/order/%d", orders[0].ID), order, nil)
if resp.StatusCode != http.StatusAccepted {
tapi.t.Fatal("Can't update order:", resp.Status)
}
var orderResponse OrderGetResponse
resp = tapi.do("GET", fmt.Sprintf("/order/%d", orders[0].ID), nil, &orderResponse)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get order:", resp.Status)
}
if len(orderResponse.Order.Products) != 1 {
t.Fatal("Wrong len of products:", orderResponse.Order.Products)
}
if orderResponse.Order.Products[0].ProductCode != testProduct2.Code {
t.Fatal("Wrong product code:", orderResponse.Order.Products)
}
if orderResponse.Order.Products[0].Price != testProduct2.Price {
t.Fatal("Wrong product price:", orderResponse.Order.Products)
}
var transactions []db.Transaction
resp = tapi.do("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", len(orders), orders)
}
if transactions[0].Total != 0 {
t.Error("Wrong total", transactions[0].Total)
}
if len(transactions[0].OrderPurchase) != 0 {
t.Error("Wrong purchases", transactions[0].OrderPurchase)
}
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
}
func TestOrderUpdateReactivate(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestProducts()
order := testOrder
now := time.Now()
order.Deadline = time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
resp := tapi.doOrder("POST", "/order", order, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
var orders []db.Order
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
purchase := []db.OrderPurchase{
{
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
},
}
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
orders = tapi.deactivateOrders()
if len(orders) != 1 {
t.Error("Deactivated none orders:", orders)
}
order.Deadline = testOrder.Deadline
resp = tapi.doOrder("PUT", fmt.Sprintf("/order/%d", orders[0].ID), order, nil)
if resp.StatusCode != http.StatusAccepted {
tapi.t.Fatal("Can't update order:", resp.Status)
}
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
if len(orders) != 1 {
t.Error("The order is not being reactivated", orders)
}
var transactions []db.Transaction
resp = tapi.doOrder("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 0 {
t.Fatal("Wrong number of transactions", transactions)
}
}
func TestOrderUpdateDeactivated(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestProducts()
order := testOrder
now := time.Now()
order.Deadline = time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
resp := tapi.doOrder("POST", "/order", order, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
var orders []db.Order
resp = tapi.do("GET", "/order/active", nil, &orders)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders:", resp.Status)
}
purchase := []db.OrderPurchase{
{
OrderProductID: orders[0].Products[0].ID,
Amount: 3,
},
}
resp = tapi.do("POST", fmt.Sprintf("/order/%d/purchase", orders[0].ID), purchase, nil)
if resp.StatusCode != http.StatusCreated {
t.Fatal("Can't create order:", resp.Status)
}
orders = tapi.deactivateOrders()
if len(orders) != 1 {
t.Error("Deactivated none orders:", orders)
}
order.Products[0].Price = 1000
resp = tapi.doOrder("PUT", fmt.Sprintf("/order/%d", orders[0].ID), order, nil)
if resp.StatusCode != http.StatusAccepted {
tapi.t.Fatal("Can't update order:", resp.Status)
}
total := 3 * order.Products[0].Price
var transactions []db.Transaction
resp = tapi.doOrder("GET", "/transaction/mine", nil, &transactions)
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get transactions:", resp.Status)
}
if len(transactions) != 1 {
t.Fatal("Wrong number of transactions", transactions)
}
if transactions[0].Total != total {
t.Fatal("Wrong updated total", transactions[0].Total, total)
}
}
func TestOrderPicks(t *testing.T) {
tapi := newTestAPI(t)
defer tapi.close()
tapi.addTestMember()
tapi.addTestProducts()
tapi.addTestOrder()
testOrderOld := testOrder
testOrderOld.Deadline = time.Now().Add(-24 * time.Hour)
if resp.StatusCode != http.StatusCreated {
tapi.t.Fatal("Can't create old order:", resp.Status)
}
var orders []db.Order
if resp.StatusCode != http.StatusOK {
t.Fatal("Can't get orders picks:", resp.Status)
}
if len(orders) != 1 {
t.Fatal("Wrong number of orders", len(orders), orders)
}
if orders[0].Name != testOrder.Name {
t.Error("Wrong name:", orders[0].Name)
}
if orders[0].Deadline.Day() != testOrder.Deadline.Day() {
t.Fatal("Wrong deadline", orders[0].Deadline, "!=", testOrder.Deadline)
}
}
if resp.StatusCode != http.StatusCreated {
tapi.t.Fatal("Can't create order:", resp.Status)
}
}
func (tapi *testAPI) deactivateOrders() []db.Order {
dbPath := path.Join(tapi.testPath, "test.db")
database, err := db.Init(dbPath)
if err != nil {
tapi.t.Fatal("Can't initialize the db:", err)
}
return database.DeactivateOrders()
}