git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/money/vat/vat_test.go (about)

     1  package vat
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestCheck(t *testing.T) {
     9  	r, err := CheckVAT("IE6388047V")
    10  	if err != nil {
    11  		t.Fatal(err)
    12  	}
    13  
    14  	if r.Valid == false {
    15  		t.Error("Is actually valid vat number")
    16  	}
    17  	if r.CountryCode != "IE" {
    18  		t.Error("Wrong country code")
    19  	}
    20  	if r.VATnumber != "6388047V" {
    21  		t.Error("Wrong vat number")
    22  	}
    23  	if r.Name != "GOOGLE IRELAND LIMITED" {
    24  		t.Error("Wrong name")
    25  	}
    26  	if r.Address != "3RD FLOOR, GORDON HOUSE, BARROW STREET, DUBLIN 4" {
    27  		t.Errorf("Wrong address: %s", r.Address)
    28  	}
    29  }
    30  
    31  func TestIsValidVAT(t *testing.T) {
    32  	var tests = []struct {
    33  		vatNumber string
    34  		isValid   bool
    35  	}{
    36  		{"IE6388047V", true},
    37  		{"", false},
    38  		{"I", false},
    39  		{"IE", false},
    40  		{"IE1", false},
    41  		{"LU 26375245", true}, // Amazon Europe Core Sarl
    42  	}
    43  
    44  	for _, tt := range tests {
    45  		v, err := IsValidVAT(tt.vatNumber)
    46  		if err == nil {
    47  			if tt.isValid != v {
    48  				t.Errorf("Expected %v for %v, got %v", tt.isValid, tt.vatNumber, v)
    49  			}
    50  		}
    51  	}
    52  }
    53  
    54  func TestGetEnvelope(t *testing.T) {
    55  	e, err := getEnvelope("IE6388047V") // Google Ireland
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if !strings.Contains(e, "IE") {
    60  		t.Error("Envelope is missing countryCode")
    61  	}
    62  	if !strings.Contains(e, "6388047V") {
    63  		t.Error("Envelope is missing vatNumber")
    64  	}
    65  
    66  	if _, err := getEnvelope(""); err == nil {
    67  		t.Error("Expected error for invalid vatNumber")
    68  	}
    69  	if _, err := getEnvelope("IE"); err == nil {
    70  		t.Error("Expected error for invalid vatNumber")
    71  	}
    72  	if _, err := getEnvelope("IE1"); err != nil {
    73  		t.Error("This should run the check, although it probably doesn't make sense")
    74  	}
    75  }
    76  
    77  func TestSanitizeVatNumber(t *testing.T) {
    78  	var tests = []struct {
    79  		in  string
    80  		out string
    81  	}{
    82  		{"IE 123", "IE123"},
    83  		{" IE 123 ", "IE123"},
    84  	}
    85  
    86  	for _, tt := range tests {
    87  		if tt.out != sanitizeVatNumber(tt.in) {
    88  			t.Error("sanitize failed for", tt.in)
    89  		}
    90  	}
    91  }