geodb_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package geodb_test
  2. import (
  3. "testing"
  4. "imuslab.com/zoraxy/mod/geodb"
  5. )
  6. func TestResolveCountryCodeFromIP(t *testing.T) {
  7. // Create a new store
  8. store, err := geodb.NewGeoDb(nil)
  9. if err != nil {
  10. t.Errorf("error creating store: %v", err)
  11. return
  12. }
  13. /*
  14. result, err := store.Search("7.8.8.8")
  15. if err != nil {
  16. t.Error(err.Error())
  17. return
  18. }
  19. fmt.Println(">> ", result, err)
  20. return
  21. */
  22. // Test an IP address that should return a valid country code
  23. ip := "8.8.8.8"
  24. expected := "US"
  25. info, err := store.ResolveCountryCodeFromIP(ip)
  26. if err != nil {
  27. t.Errorf("error resolving country code for IP %s: %v", ip, err)
  28. return
  29. }
  30. if info.CountryIsoCode != expected {
  31. t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
  32. }
  33. // Test an IP address that should return an empty country code
  34. ip = "127.0.0.1"
  35. expected = ""
  36. info, err = store.ResolveCountryCodeFromIP(ip)
  37. if err != nil {
  38. t.Errorf("error resolving country code for IP %s: %v", ip, err)
  39. return
  40. }
  41. if info.CountryIsoCode != expected {
  42. t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
  43. }
  44. }