|
| 1 | +// +build !postgres9.6 |
| 2 | + |
| 3 | +package tests |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "github.com/tabbed/pqtype" |
| 11 | +) |
| 12 | + |
| 13 | +// https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-MACADDR8 |
| 14 | +func TestMacaddr8(t *testing.T) { |
| 15 | + for _, addr := range []struct { |
| 16 | + input string |
| 17 | + output string |
| 18 | + }{ |
| 19 | + { |
| 20 | + "08:00:2b:01:02:03:04:05", |
| 21 | + "08:00:2b:01:02:03:04:05", |
| 22 | + }, |
| 23 | + { |
| 24 | + "08-00-2b-01-02-03-04-05", |
| 25 | + "08:00:2b:01:02:03:04:05", |
| 26 | + }, |
| 27 | + { |
| 28 | + "08002b:0102030405", |
| 29 | + "08:00:2b:01:02:03:04:05", |
| 30 | + }, |
| 31 | + { |
| 32 | + "08002b-0102030405", |
| 33 | + "08:00:2b:01:02:03:04:05", |
| 34 | + }, |
| 35 | + { |
| 36 | + "0800.2b01.0203.0405", |
| 37 | + "08:00:2b:01:02:03:04:05", |
| 38 | + }, |
| 39 | + { |
| 40 | + "0800-2b01-0203-0405", |
| 41 | + "08:00:2b:01:02:03:04:05", |
| 42 | + }, |
| 43 | + { |
| 44 | + "08002b01:02030405", |
| 45 | + "08:00:2b:01:02:03:04:05", |
| 46 | + }, |
| 47 | + { |
| 48 | + "08002b0102030405", |
| 49 | + "08:00:2b:01:02:03:04:05", |
| 50 | + }, |
| 51 | + } { |
| 52 | + addr = addr |
| 53 | + t.Run(addr.input, func(t *testing.T) { |
| 54 | + var cidr pqtype.Macaddr |
| 55 | + if err := db.QueryRow(fmt.Sprintf(`SELECT '%s'::macaddr8`, addr.input)).Scan(&cidr); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + if diff := cmp.Diff(true, cidr.Valid); diff != "" { |
| 59 | + t.Errorf("valid mismatch (-want +got):\n%s", diff) |
| 60 | + } |
| 61 | + if diff := cmp.Diff(addr.output, cidr.Addr.String()); diff != "" { |
| 62 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | + t.Run("NULL", func(t *testing.T) { |
| 67 | + var cidr pqtype.Macaddr |
| 68 | + if err := db.QueryRow(fmt.Sprintf(`SELECT NULL::macaddr8`)).Scan(&cidr); err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + if diff := cmp.Diff(false, cidr.Valid); diff != "" { |
| 72 | + t.Errorf("valid mismatch (-want +got):\n%s", diff) |
| 73 | + } |
| 74 | + }) |
| 75 | +} |
0 commit comments