A lot more to go, but this is the core. Need to think about how to test the queue handlers.
141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
package domain64
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
|
|
"github.com/jpillora/go-tld"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func setup() *sql.DB {
|
|
ctx := context.Background()
|
|
|
|
// db, err := sql.Open("sqlite", "sqlc/test.sqlite")
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
// TODO
|
|
panic(err)
|
|
}
|
|
|
|
// create tables
|
|
if _, err := db.ExecContext(ctx, ddl); err != nil {
|
|
panic(err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func TestNewDomain64Map(t *testing.T) {
|
|
db := setup()
|
|
M, err := NewDomain64Map()
|
|
M.Setup(db)
|
|
if err != nil {
|
|
// TODO
|
|
t.Error(err)
|
|
}
|
|
if M.DB == nil {
|
|
t.Error("DB should not be nil")
|
|
}
|
|
}
|
|
|
|
func TestURLToRFQDN(t *testing.T) {
|
|
M, _ := NewDomain64Map()
|
|
// M.Setup(db)
|
|
simple, _ := tld.Parse("https://jadud.com/")
|
|
rfqdn := M.URLToRFQDN(simple)
|
|
if rfqdn != "com.jadud/" {
|
|
t.Errorf("Expected `com.jadud/`, got %s", rfqdn)
|
|
}
|
|
}
|
|
|
|
func TestURLToDomain64(t *testing.T) {
|
|
db := setup()
|
|
M, _ := NewDomain64Map()
|
|
M.Setup(db)
|
|
simple, _ := tld.Parse("https://jadud.com/")
|
|
d64, _ := M.URLToDomain64(simple)
|
|
if d64.TLD != 1 {
|
|
t.Errorf("expected TLD == 1, got %d", d64.TLD)
|
|
}
|
|
|
|
if d64.Domain != 1 {
|
|
t.Errorf("expected domain == 1, got %d", d64.Domain)
|
|
}
|
|
}
|
|
|
|
func TestURLToDomain64_02(t *testing.T) {
|
|
db := setup()
|
|
M, _ := NewDomain64Map()
|
|
M.Setup(db)
|
|
simple1, _ := tld.Parse("https://jadud.com/")
|
|
simple2, _ := tld.Parse("https://another.com/")
|
|
d64_1, _ := M.URLToDomain64(simple1)
|
|
d64_2, _ := M.URLToDomain64(simple2)
|
|
|
|
if d64_1.TLD != 1 {
|
|
t.Errorf("expected TLD == 1, got %d", d64_1.TLD)
|
|
}
|
|
|
|
if d64_1.Domain != 1 {
|
|
t.Errorf("expected domain == 1, got %d", d64_1.Domain)
|
|
}
|
|
|
|
if d64_2.TLD != 1 {
|
|
t.Errorf("expected TLD == 1, got %d", d64_2.TLD)
|
|
}
|
|
|
|
if d64_2.Domain != 2 {
|
|
t.Errorf("expected domain == 2, got %d", d64_2.Domain)
|
|
}
|
|
|
|
}
|
|
|
|
func TestURLToDomain64_03(t *testing.T) {
|
|
db := setup()
|
|
M, _ := NewDomain64Map()
|
|
M.Setup(db)
|
|
var tests = []struct {
|
|
url string
|
|
tld int64
|
|
domain int64
|
|
subdomain int64
|
|
path int64
|
|
d64 int64
|
|
}{
|
|
{"https://jadud.com/", 1, 1, 0, 0, 0x0100000100000000},
|
|
{"https://research.jadud.com/", 1, 1, 1, 0, 0x0100000101000000},
|
|
{"https://teaching.jadud.com/", 1, 1, 2, 0, 0x0100000102000000},
|
|
{"https://teaching.jadud.com/classes", 1, 1, 2, 1, 0x0100000102000001},
|
|
{"https://teaching.jadud.com/other-classes", 1, 1, 2, 2, 0x0100000102000002},
|
|
{"https://research.jadud.com/papers", 1, 1, 1, 1, 0x0100000101000001},
|
|
{"https://research.jadud.com/experiments", 1, 1, 1, 2, 0x0100000101000002},
|
|
{"https://teaching.another.com/classes", 1, 2, 1, 1, 0x0100000201000001},
|
|
{"https://teaching.jadud.org/classes", 2, 1, 1, 1, 0x0200000101000001},
|
|
// The ordering here matters; if we see a "bare" domain after first seeing the
|
|
// subdomain, I expect the numbering to come out right. That is, subdomain <- 0 and
|
|
// path <- 0. That is because of "" and "/" checking on subdomain and path, respectively.
|
|
{"https://jadud.org/", 2, 1, 0, 0, 0x0200000100000000},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
parsed, _ := tld.Parse(tt.url)
|
|
d64, _ := M.URLToDomain64(parsed)
|
|
if d64.TLD != tt.tld {
|
|
t.Errorf("%s TLD expected %d given %d", tt.url, tt.tld, d64.TLD)
|
|
}
|
|
if d64.Domain != tt.domain {
|
|
t.Errorf("%s Domain expected %d given %d", tt.url, tt.domain, d64.Domain)
|
|
}
|
|
if d64.Subdomain != tt.subdomain {
|
|
t.Errorf("%s Subdomain expected %d given %d", tt.url, tt.subdomain, d64.Subdomain)
|
|
}
|
|
if d64.Path != tt.path {
|
|
t.Errorf("%s Path expected %d given %d", tt.url, tt.path, d64.Path)
|
|
}
|
|
if d64.ToInt64() != tt.d64 {
|
|
t.Errorf("%s int64 value expected %d given %d", tt.url, tt.d64, d64.ToInt64())
|
|
}
|
|
}
|
|
}
|