Read misc.territory, timezone, useragent and car as tables, and state the data rules behind them #23

Merged
lilleman merged 14 commits from misc-territory into main 2026-09-19 01:40:34 +02:00
8 changed files with 98 additions and 68 deletions
Showing only changes of commit e9d8603611 - Show all commits
+2 -2
View File
@@ -352,8 +352,8 @@ func TestRunSelectsATableRow(t *testing.T) {
if code, _, errb := runOut("--no-shipped-data", "-d", dir, "[Skåne län]"); code != 2 || !strings.Contains(errb, "JSON") { if code, _, errb := runOut("--no-shipped-data", "-d", dir, "[Skåne län]"); code != 2 || !strings.Contains(errb, "JSON") {
t.Fatalf("[Skåne län] = %d, stderr=%q, want misuse: a leading bracket that is no JSON names nothing", code, errb) t.Fatalf("[Skåne län] = %d, stderr=%q, want misuse: a leading bracket that is no JSON names nothing", code, errb)
} }
if code, out, _ := runOut("--seed", "1", "--format", "csv", "misc.country[SE]"); code != 0 || !strings.HasPrefix(out, "alpha2,") || !strings.Contains(out, "\nSE,SWE,") { if code, out, _ := runOut("--seed", "1", "--format", "csv", "misc.territory[SE]"); code != 0 || !strings.HasPrefix(out, "alpha2,") || !strings.Contains(out, "\nSE,SWE,") {
t.Fatalf("--format csv misc.country[SE] = %d, %q", code, out) t.Fatalf("--format csv misc.territory[SE] = %d, %q", code, out)
} }
} }
+18 -17
View File
@@ -95,23 +95,24 @@ func TestShippedMiscCategories(t *testing.T) {
func TestShippedMiscReferenceData(t *testing.T) { func TestShippedMiscReferenceData(t *testing.T) {
f := newGenerator(t, "data/misc", WithSeed(1)) f := newGenerator(t, "data/misc", WithSeed(1))
re := map[string]*regexp.Regexp{ re := map[string]*regexp.Regexp{
"currency": regexp.MustCompile(`^[A-Z]{3}$`), "currency": regexp.MustCompile(`^[A-Z]{3}$`),
"currency.name": regexp.MustCompile(`\p{L}`), "currency.name": regexp.MustCompile(`\p{L}`),
"currency.symbol": regexp.MustCompile(`^\S+$`), "currency.symbol": regexp.MustCompile(`^\S+$`),
"country": regexp.MustCompile(`\p{L}`), "territory": regexp.MustCompile(`\p{L}`),
"country.alpha2": regexp.MustCompile(`^[A-Z]{2}$`), "territory.alpha2": regexp.MustCompile(`^[A-Z]{2}$`),
"country.alpha3": regexp.MustCompile(`^[A-Z]{3}$`), "territory.alpha3": regexp.MustCompile(`^[A-Z]{3}$`),
"language": regexp.MustCompile(`\p{L}`), "territory.country": regexp.MustCompile(`^[A-Z]{2}$`),
"language.code": regexp.MustCompile(`^[a-z]{2}$`), "language": regexp.MustCompile(`\p{L}`),
"language.code3": regexp.MustCompile(`^[a-z]{3}$`), "language.code": regexp.MustCompile(`^[a-z]{2}$`),
"timezone": regexp.MustCompile(`^([A-Za-z_]+(/[A-Za-z_]+)+|UTC)$`), "language.code3": regexp.MustCompile(`^[a-z]{3}$`),
"mimetype": regexp.MustCompile(`^[a-z]+/[a-z0-9.+-]+$`), "timezone": regexp.MustCompile(`^([A-Za-z_]+(/[A-Za-z_]+)+|UTC)$`),
"mimetype.ext": regexp.MustCompile(`^\.[a-z0-9_-]+$`), "mimetype": regexp.MustCompile(`^[a-z]+/[a-z0-9.+-]+$`),
"httpstatus": regexp.MustCompile(`^[1-5]\d{2} \S.*$`), "mimetype.ext": regexp.MustCompile(`^\.[a-z0-9_-]+$`),
"httpstatus.code": regexp.MustCompile(`^[1-5]\d{2}$`), "httpstatus": regexp.MustCompile(`^[1-5]\d{2} \S.*$`),
"useragent": regexp.MustCompile(`^Mozilla/5\.0 .+`), "httpstatus.code": regexp.MustCompile(`^[1-5]\d{2}$`),
"car": regexp.MustCompile(`^\S.* \S`), "useragent": regexp.MustCompile(`^Mozilla/5\.0 .+`),
"car.maker": regexp.MustCompile(`^\S`), "car": regexp.MustCompile(`^\S.* \S`),
"car.maker": regexp.MustCompile(`^\S`),
} }
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
for p, rx := range re { for p, rx := range re {
+3 -3
View File
@@ -108,9 +108,9 @@ func TestFakeTemplateErrors(t *testing.T) {
{`name: {/no.such.path}`, "no entry"}, {`name: {/no.such.path}`, "no entry"},
{`name: {..nope}`, "write {/nope}"}, {`name: {..nope}`, "write {/nope}"},
{`{"format":"x"}`, "is a string"}, {`{"format":"x"}`, "is a string"},
{`{/misc.country} {/misc.country.alpha2}`, "renders a level that {/misc.country.alpha2} reads a path into; name the fields you want instead, or draw them apart with a drawGroup"}, {`{/misc.territory} {/misc.territory.alpha2}`, "renders a level that {/misc.territory.alpha2} reads a path into; name the fields you want instead, or draw them apart with a drawGroup"},
{`{"format":"{/misc.country.alpha2} {x}","x":"{/misc.country}"}`, "reads a path into"}, {`{"format":"{/misc.territory.alpha2} {x}","x":"{/misc.territory}"}`, "reads a path into"},
{`{"format":"{/misc.country.alpha2}","drawGroup":"g"}`, "nothing can reference"}, {`{"format":"{/misc.territory.alpha2}","drawGroup":"g"}`, "nothing can reference"},
} { } {
_, err := f.FakeTemplate(c.input) _, err := f.FakeTemplate(c.input)
if err == nil || !strings.Contains(err.Error(), c.want) { if err == nil || !strings.Contains(err.Error(), c.want) {
+1 -1
View File
@@ -154,7 +154,7 @@ func TestListedPathsAllRender(t *testing.T) {
} }
} }
} }
for _, p := range []string{"misc.car.maker", "misc.country.alpha2", "misc.country.numeric", "misc.currency.symbol", "misc.httpstatus.code", "misc.mimetype.ext"} { for _, p := range []string{"misc.car.maker", "misc.territory.alpha2", "misc.territory.numeric", "misc.currency.symbol", "misc.httpstatus.code", "misc.mimetype.ext"} {
if !slices.Contains(paths, p) { if !slices.Contains(paths, p) {
t.Errorf("List() omits %q, which the README advertises and Fake renders", p) t.Errorf("List() omits %q, which the README advertises and Fake renders", p)
} }
+74 -45
View File
@@ -644,17 +644,17 @@ func TestTableRowsAreAlternatives(t *testing.T) {
func TestTableOverridesByLayering(t *testing.T) { func TestTableOverridesByLayering(t *testing.T) {
mine := writeFiles(t, map[string]string{ mine := writeFiles(t, map[string]string{
"misc/country.json": `{"format":"{name}","rows":"country.tsv","key":"alpha2"}`, "misc/territory.json": `{"format":"{name}","rows":"territory.tsv","key":"alpha2"}`,
"misc/country.tsv": "alpha2\tname\nXX\tNowhere\nYY\tElsewhere\n", "misc/territory.tsv": "alpha2\tname\nXX\tNowhere\nYY\tElsewhere\n",
}) })
f, err := New(WithDataPath(mine), WithSeed(1)) f, err := New(WithDataPath(mine), WithSeed(1))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if v := fake(t, f, "misc.country[XX]"); v != "Nowhere" { if v := fake(t, f, "misc.territory[XX]"); v != "Nowhere" {
t.Fatalf("misc.country[XX] = %q, want the layered table", v) t.Fatalf("misc.territory[XX] = %q, want the layered table", v)
} }
if paths := f.List(); slices.Contains(paths, "misc.country.alpha3") { if paths := f.List(); slices.Contains(paths, "misc.territory.alpha3") {
t.Fatal("List() still offers the shipped table's columns under an overridden category") t.Fatal("List() still offers the shipped table's columns under an overridden category")
} }
} }
@@ -662,50 +662,55 @@ func TestTableOverridesByLayering(t *testing.T) {
func TestShippedTables(t *testing.T) { func TestShippedTables(t *testing.T) {
f := newGenerator(t, "data/misc", WithSeed(1)) f := newGenerator(t, "data/misc", WithSeed(1))
for path, want := range map[string]string{ for path, want := range map[string]string{
"country[SE]": "Sweden", "territory[SE]": "Sweden",
"country[Sweden].alpha3": "SWE", "territory[Sweden].alpha3": "SWE",
"country[SE].numeric": "752", "territory[SE].numeric": "752",
"country[SE].tld": ".se", "territory[SE].tld": ".se",
"country[SE].calling-code": "46", "territory[SE].calling-code": "46",
"country[SE].capital": "Stockholm", "territory[SE].capital": "Stockholm",
"country[SE].currency": "SEK", "territory[SE].currency": "SEK",
"country[SE].flag": "🇸🇪", "territory[SE].flag": "🇸🇪",
"currency[SEK].name": "Swedish Krona", "territory[SE].country": "SE",
"currency[SEK].symbol": "kr", "territory[GL]": "Greenland",
"currency[SEK].numeric": "752", "territory[GL].country": "DK",
"currency[SEK].decimals": "2", "territory[Åland Islands].country": "FI",
"currency[Euro].code": "EUR", "currency[SEK].name": "Swedish Krona",
"language[sv]": "Swedish", "currency[SEK].symbol": "kr",
"language[sv].code3": "swe", "currency[SEK].numeric": "752",
"language[Swedish].code": "sv", "currency[SEK].decimals": "2",
"language[nl]": "Dutch", "currency[Euro].code": "EUR",
"httpstatus[200]": "200 OK", "language[sv]": "Swedish",
"httpstatus[404]": "404 Not Found", "language[sv].code3": "swe",
"httpstatus[404].reason": "Not Found", "language[Swedish].code": "sv",
"httpstatus[451].reason": "Unavailable For Legal Reasons", "language[nl]": "Dutch",
"httpstatus[500].reason": "Internal Server Error", "httpstatus[200]": "200 OK",
"mimetype[application/json].ext": ".json", "httpstatus[404]": "404 Not Found",
"mimetype[text/markdown].ext": ".md", "httpstatus[404].reason": "Not Found",
"mimetype[.jpg]": "image/jpeg", "httpstatus[451].reason": "Unavailable For Legal Reasons",
"mimetype[.mov]": "video/quicktime", "httpstatus[500].reason": "Internal Server Error",
"mimetype[.mp3]": "audio/mpeg", "mimetype[application/json].ext": ".json",
"mimetype[.mp4]": "video/mp4", "mimetype[text/markdown].ext": ".md",
"mimetype[.ogg]": "audio/ogg", "mimetype[.jpg]": "image/jpeg",
"mimetype[.mov]": "video/quicktime",
"mimetype[.mp3]": "audio/mpeg",
"mimetype[.mp4]": "video/mp4",
"mimetype[.ogg]": "audio/ogg",
} { } {
if got := fake(t, f, path); got != want { if got := fake(t, f, path); got != want {
t.Errorf("Fake(%q) = %q, want %q", path, got, want) t.Errorf("Fake(%q) = %q, want %q", path, got, want)
} }
} }
re := map[string]*regexp.Regexp{ re := map[string]*regexp.Regexp{
"country.numeric": regexp.MustCompile(`^\d{3}$`), "territory.numeric": regexp.MustCompile(`^\d{3}$`),
"country.tld": regexp.MustCompile(`^\.[a-z]{2}$`), "territory.tld": regexp.MustCompile(`^\.[a-z]{2}$`),
"country.calling-code": regexp.MustCompile(`^\d{1,4}(-\d{3})?$`), "territory.calling-code": regexp.MustCompile(`^\d{1,4}(-\d{3})?$`),
"country.capital": regexp.MustCompile(`\p{L}`), "territory.capital": regexp.MustCompile(`\p{L}`),
"country.currency": regexp.MustCompile(`^[A-Z]{3}$`), "territory.country": regexp.MustCompile(`^[A-Z]{2}$`),
"country.flag": regexp.MustCompile(`^[\x{1F1E6}-\x{1F1FF}]{2}$`), "territory.currency": regexp.MustCompile(`^[A-Z]{3}$`),
"country.languages": regexp.MustCompile(`^[a-z]{2,3}(-[A-Z]{2})?(,[a-z]{2,3}(-[A-Z]{2})?)*$`), "territory.flag": regexp.MustCompile(`^[\x{1F1E6}-\x{1F1FF}]{2}$`),
"currency.numeric": regexp.MustCompile(`^\d{3}$`), "territory.languages": regexp.MustCompile(`^[a-z]{2,3}(-[A-Z]{2})?(,[a-z]{2,3}(-[A-Z]{2})?)*$`),
"currency.decimals": regexp.MustCompile(`^[0-4]$`), "currency.numeric": regexp.MustCompile(`^\d{3}$`),
"currency.decimals": regexp.MustCompile(`^[0-4]$`),
} }
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
for p, rx := range re { for p, rx := range re {
@@ -716,10 +721,34 @@ func TestShippedTables(t *testing.T) {
} }
count := map[string]bool{} count := map[string]bool{}
for i := 0; i < 5000; i++ { for i := 0; i < 5000; i++ {
count[fake(t, f, "country.alpha2")] = true count[fake(t, f, "territory.alpha2")] = true
} }
if len(count) < 200 { if len(count) < 200 {
t.Fatalf("country draws %d distinct rows in 5000, want the full register", len(count)) t.Fatalf("territory draws %d distinct rows in 5000, want the full register", len(count))
}
}
// TestEveryTerritoryNamesAShippedCountry proves what no parent can: the sovereign a
// territory names is a row of the same table, which a link would make a cycle.
func TestEveryTerritoryNamesAShippedCountry(t *testing.T) {
data, err := os.ReadFile(filepath.Join("data", "misc", "territory.tsv"))
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSuffix(string(data), "\n"), "\n")
head := strings.Split(lines[0], "\t")
alpha2, country := slices.Index(head, "alpha2"), slices.Index(head, "country")
if alpha2 < 0 || country < 0 {
t.Fatalf("territory.tsv columns %v, want alpha2 and country", head)
}
keys := map[string]bool{}
for _, line := range lines[1:] {
keys[strings.Split(line, "\t")[alpha2]] = true
}
for i, line := range lines[1:] {
if c := strings.Split(line, "\t")[country]; !keys[c] {
t.Errorf("territory.tsv line %d: country %q is no alpha2 of the table", i+2, c)
}
} }
} }