diff --git a/models/geo.go b/models/geo.go index a9049ec..cd82ef7 100644 --- a/models/geo.go +++ b/models/geo.go @@ -73,8 +73,7 @@ func CloseDBs() { // LookUp an IP and get city data func (record *GeoRecord) LookUp(ip net.IP) error { - err := db.city.Lookup(ip, record) - if err != nil { + if err := db.city.Lookup(ip, record); err != nil { return err } @@ -83,8 +82,7 @@ func (record *GeoRecord) LookUp(ip net.IP) error { // LookUp an IP and get ASN data func (record *ASNRecord) LookUp(ip net.IP) error { - err := db.asn.Lookup(ip, record) - if err != nil { + if err := db.asn.Lookup(ip, record); err != nil { return err } diff --git a/models/geo_test.go b/models/geo_test.go new file mode 100644 index 0000000..8b85456 --- /dev/null +++ b/models/geo_test.go @@ -0,0 +1,77 @@ +package models + +import ( + "net" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestModels(t *testing.T) { + expectedCity := &GeoRecord{ + Country: struct { + ISOCode string "maxminddb:\"iso_code\"" + Names map[string]string "maxminddb:\"names\"" + }{ + ISOCode: "GB", + Names: map[string]string{ + "de": "Vereinigtes Königreich", + "en": "United Kingdom", + "es": "Reino Unido", + "fr": "Royaume-Uni", + "ja": "イギリス", + "pt-BR": "Reino Unido", + "ru": "Великобритания", + "zh-CN": "英国", + }, + }, + City: struct { + Names map[string]string "maxminddb:\"names\"" + }{ + Names: map[string]string{ + "de": "London", + "en": "London", + "es": "Londres", + "fr": "Londres", + "ja": "ロンドン", + "pt-BR": "Londres", + "ru": "Лондон", + }, + }, + Location: struct { + Latitude float64 "maxminddb:\"latitude\"" + Longitude float64 "maxminddb:\"longitude\"" + TimeZone string "maxminddb:\"time_zone\"" + }{ + Latitude: 51.5142, + Longitude: -0.0931, + TimeZone: "Europe/London", + }, + Postal: struct { + Code string "maxminddb:\"code\"" + }{ + Code: "", + }, + } + + expectedASN := &ASNRecord{ + AutonomousSystemNumber: 12552, + AutonomousSystemOrganization: "IP-Only", + } + + Setup("../test/GeoIP2-City-Test.mmdb", "../test/GeoLite2-ASN-Test.mmdb") + defer CloseDBs() + + assert.NotNil(t, db.asn) + assert.NotNil(t, db.city) + + cityRecord := &GeoRecord{} + assert.Nil(t, cityRecord.LookUp(net.ParseIP("81.2.69.192"))) + assert.Equal(t, expectedCity, cityRecord) + assert.Error(t, cityRecord.LookUp(net.ParseIP("error"))) + + asnRecord := &ASNRecord{} + assert.Nil(t, asnRecord.LookUp(net.ParseIP("82.99.17.64"))) + assert.Equal(t, expectedASN, asnRecord) + assert.Error(t, asnRecord.LookUp(net.ParseIP("error"))) +}