Add test for geo model

This commit is contained in:
Daniel Carrillo 2021-12-01 16:57:41 +01:00
parent ac32dc240f
commit f7d0a679c9
Signed by: dcarrillo
GPG Key ID: E4CD5C09DAED6E16
2 changed files with 79 additions and 4 deletions

View File

@ -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
}

77
models/geo_test.go Normal file
View File

@ -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")))
}