Serge Bazanski | b456c18 | 2020-11-16 22:46:15 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
| 8 | ) |
| 9 | |
| 10 | // Fake test data for test in this file. |
| 11 | var ( |
| 12 | itemInternet1 = &pb.Item{ |
| 13 | Title: "Dostęp do Internetu - Umowa FOOBAR/10 - Opłata Abonentowa 2020/08", |
| 14 | Count: 1, |
| 15 | UnitPrice: 4200, |
| 16 | Vat: 23000, |
| 17 | } |
| 18 | itemInternet2 = &pb.Item{ |
| 19 | Title: "Dostęp do Internetu - Umowa FOOBAR/10 - Opłata Abonentowa 2020/09", |
| 20 | Count: 1, |
| 21 | UnitPrice: 4200, |
| 22 | Vat: 23000, |
| 23 | } |
| 24 | itemHardware = &pb.Item{ |
| 25 | Title: "Thinkpad x230, i7, 16GB RAM, Refurbished", |
| 26 | Count: 1, |
| 27 | UnitPrice: 10000, |
| 28 | Vat: 23000, |
| 29 | GtuCode: []pb.GTUCode{pb.GTUCode_GTU_05}, |
| 30 | } |
| 31 | billing1 = []string{ |
| 32 | "Wykop Sp. z o. o.", |
| 33 | "Zakręt 8", |
| 34 | "60-351 Poznań", |
| 35 | } |
| 36 | billing2 = []string{ |
| 37 | "TEH Adam Karolczak", |
| 38 | "Zgoda 18/2", |
| 39 | "95-200 Pabianice", |
| 40 | } |
| 41 | vatID1 = "PL8086133742" |
| 42 | vatID2 = "DE133742429" |
| 43 | iban = "PL 59 1090 2402 9746 7956 2256 2375" |
| 44 | swift = "WLPPZLPAXXX" |
| 45 | ) |
| 46 | |
| 47 | func TestCalculate(t *testing.T) { |
| 48 | now := time.Now() |
| 49 | for _, te := range []struct { |
| 50 | description string |
| 51 | data *pb.InvoiceData |
| 52 | want *pb.Invoice |
| 53 | }{ |
| 54 | { |
| 55 | description: "Invoice without JPK_V7 codes", |
| 56 | data: &pb.InvoiceData{ |
| 57 | Item: []*pb.Item{itemInternet1, itemInternet2}, |
| 58 | InvoicerBilling: billing1, |
| 59 | CustomerBilling: billing2, |
| 60 | InvoicerVatId: vatID1, |
| 61 | CustomerVatId: vatID2, |
| 62 | Date: now.UnixNano(), |
| 63 | DaysDue: 21, |
| 64 | Iban: iban, |
| 65 | Swift: swift, |
| 66 | Unit: "PLN", |
| 67 | }, |
| 68 | want: &pb.Invoice{ |
| 69 | TotalNet: 8400, |
| 70 | Total: 10332, |
| 71 | Unit: "PLN", |
| 72 | }, |
| 73 | }, |
| 74 | { |
| 75 | description: "Invoice with JPK_V7 codes", |
| 76 | data: &pb.InvoiceData{ |
| 77 | // Repeated item with GTU code GTU_5, to ensure result doesn't |
| 78 | // have repeated codes. |
| 79 | Item: []*pb.Item{itemInternet1, itemHardware, itemHardware}, |
| 80 | InvoicerBilling: billing1, |
| 81 | CustomerBilling: billing2, |
| 82 | InvoicerVatId: vatID1, |
| 83 | CustomerVatId: vatID2, |
| 84 | Date: now.UnixNano(), |
| 85 | DaysDue: 21, |
| 86 | Iban: iban, |
| 87 | Swift: swift, |
| 88 | Unit: "PLN", |
| 89 | }, |
| 90 | want: &pb.Invoice{ |
| 91 | TotalNet: 24200, |
| 92 | Total: 29766, |
| 93 | Unit: "PLN", |
| 94 | GtuCode: []pb.GTUCode{pb.GTUCode_GTU_05}, |
| 95 | }, |
| 96 | }, |
| 97 | } { |
| 98 | t.Run(te.description, func(t *testing.T) { |
| 99 | invoice := &pb.Invoice{ |
| 100 | Data: te.data, |
| 101 | Date: te.data.Date, |
| 102 | } |
| 103 | calculateInvoiceData(invoice) |
| 104 | if want, got := te.want.TotalNet, invoice.TotalNet; want != got { |
| 105 | t.Errorf("got TotalNet %d, wanted %d", got, want) |
| 106 | } |
| 107 | if want, got := te.want.Total, invoice.Total; want != got { |
| 108 | t.Errorf("got Total %d, wanted %d", got, want) |
| 109 | } |
| 110 | if want, got := te.want.Unit, invoice.Unit; want != got { |
| 111 | t.Errorf("got Unit %q, wanted %q", got, want) |
| 112 | } |
| 113 | due := time.Duration(int64(time.Hour*24) * te.data.DaysDue) |
| 114 | if want, got := now.Add(due).UnixNano(), invoice.DueDate; want != got { |
| 115 | t.Errorf("got DueDate %d, wanted %d", got, want) |
| 116 | } |
| 117 | if want, got := len(te.want.GtuCode), len(invoice.GtuCode); want != got { |
| 118 | t.Errorf("got %d GTU codes, wanted %d", got, want) |
| 119 | } else { |
| 120 | for i, want := range te.want.GtuCode { |
| 121 | got := invoice.GtuCode[i] |
| 122 | if want != got { |
| 123 | t.Errorf("GTU code %d: wanted %s, got %s", i, want.String(), got.String()) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |