|
15 | 15 | package civil
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "database/sql" |
| 19 | + "database/sql/driver" |
18 | 20 | "encoding/json"
|
19 | 21 | "testing"
|
20 | 22 | "time"
|
@@ -705,3 +707,105 @@ func TestUnmarshalJSON(t *testing.T) {
|
705 | 707 | }
|
706 | 708 | }
|
707 | 709 | }
|
| 710 | + |
| 711 | +func TestValuer(t *testing.T) { |
| 712 | + for _, test := range []struct { |
| 713 | + data driver.Valuer |
| 714 | + want interface{} |
| 715 | + }{ |
| 716 | + {&Date{1987, 4, 15}, `1987-04-15`}, |
| 717 | + {&Time{18, 54, 2, 0}, `18:54:02`}, |
| 718 | + {&DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}, `1987-04-15T18:54:02`}, |
| 719 | + } { |
| 720 | + got, err := test.data.Value() |
| 721 | + if err != nil { |
| 722 | + t.Fatalf("%s: %v", test.data, err) |
| 723 | + } |
| 724 | + if !cmp.Equal(got, test.want) { |
| 725 | + t.Errorf("%s: got %#v, want %#v", test.data, test.data, test.want) |
| 726 | + } |
| 727 | + } |
| 728 | +} |
| 729 | + |
| 730 | +func TestScanner(t *testing.T) { |
| 731 | + var d Date |
| 732 | + var tm Time |
| 733 | + var dt DateTime |
| 734 | + for _, test := range []struct { |
| 735 | + data interface{} |
| 736 | + ptr sql.Scanner |
| 737 | + want interface{} |
| 738 | + }{ |
| 739 | + // time input |
| 740 | + {time.Date(1987, 4, 15, 18, 54, 2, 0, time.UTC), &d, &Date{1987, 4, 15}}, |
| 741 | + {time.Date(1987, 4, 15, 18, 54, 2, 0, time.UTC), &tm, &Time{18, 54, 2, 0}}, |
| 742 | + {time.Date(1987, 4, 15, 18, 54, 2, 0, time.UTC), &dt, &DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}}, |
| 743 | + |
| 744 | + // *time input |
| 745 | + {toPtr(time.Date(1987, 4, 15, 18, 54, 2, 0, time.UTC)), &d, &Date{1987, 4, 15}}, |
| 746 | + {toPtr(time.Date(1987, 4, 15, 18, 54, 2, 0, time.UTC)), &tm, &Time{18, 54, 2, 0}}, |
| 747 | + {toPtr(time.Date(1987, 4, 15, 18, 54, 2, 0, time.UTC)), &dt, &DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}}, |
| 748 | + |
| 749 | + // string input |
| 750 | + {`1987-04-15`, &d, &Date{1987, 4, 15}}, |
| 751 | + {`18:54:02`, &tm, &Time{18, 54, 2, 0}}, |
| 752 | + {`1987-04-15T18:54:02`, &dt, &DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}}, |
| 753 | + |
| 754 | + // *string input |
| 755 | + {toPtr(`1987-04-15`), &d, &Date{1987, 4, 15}}, |
| 756 | + {toPtr(`18:54:02`), &tm, &Time{18, 54, 2, 0}}, |
| 757 | + {toPtr(`1987-04-15T18:54:02`), &dt, &DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}}, |
| 758 | + |
| 759 | + // []byte input |
| 760 | + {[]byte(`1987-04-15`), &d, &Date{1987, 4, 15}}, |
| 761 | + {[]byte(`18:54:02`), &tm, &Time{18, 54, 2, 0}}, |
| 762 | + {[]byte(`1987-04-15T18:54:02`), &dt, &DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}}, |
| 763 | + |
| 764 | + // *[]byte input |
| 765 | + {toPtr([]byte(`1987-04-15`)), &d, &Date{1987, 4, 15}}, |
| 766 | + {toPtr([]byte(`18:54:02`)), &tm, &Time{18, 54, 2, 0}}, |
| 767 | + {toPtr([]byte(`1987-04-15T18:54:02`)), &dt, &DateTime{Date{1987, 4, 15}, Time{18, 54, 2, 0}}}, |
| 768 | + } { |
| 769 | + if err := test.ptr.Scan(test.data); err != nil { |
| 770 | + t.Fatalf("%s: %v", test.data, err) |
| 771 | + } |
| 772 | + if !cmp.Equal(test.ptr, test.want) { |
| 773 | + t.Errorf("%s: got %#v, want %#v", test.data, test.ptr, test.want) |
| 774 | + } |
| 775 | + } |
| 776 | + |
| 777 | + // expected test failures |
| 778 | + for _, test := range []struct { |
| 779 | + data interface{} |
| 780 | + ptr sql.Scanner |
| 781 | + want string |
| 782 | + }{ |
| 783 | + // int64 input |
| 784 | + {int64(12345), &d, "unsupported scan type for Date: int64"}, |
| 785 | + {int64(12345), &tm, "unsupported scan type for Time: int64"}, |
| 786 | + {int64(12345), &dt, "unsupported scan type for DateTime: int64"}, |
| 787 | + |
| 788 | + // float64 input |
| 789 | + {float64(0.9876), &d, "unsupported scan type for Date: float64"}, |
| 790 | + {float64(0.9876), &tm, "unsupported scan type for Time: float64"}, |
| 791 | + {float64(0.9876), &dt, "unsupported scan type for DateTime: float64"}, |
| 792 | + |
| 793 | + // bool input |
| 794 | + {true, &d, "unsupported scan type for Date: bool"}, |
| 795 | + {true, &tm, "unsupported scan type for Time: bool"}, |
| 796 | + {true, &dt, "unsupported scan type for DateTime: bool"}, |
| 797 | + } { |
| 798 | + err := test.ptr.Scan(test.data) |
| 799 | + if err == nil { |
| 800 | + t.Errorf("%q, got nil, want error", test.data) |
| 801 | + continue |
| 802 | + } |
| 803 | + if err.Error() != test.want { |
| 804 | + t.Errorf("%v: got %s, want %s", test.data, err, test.want) |
| 805 | + } |
| 806 | + } |
| 807 | +} |
| 808 | + |
| 809 | +func toPtr[V any](v V) *V { |
| 810 | + return &v |
| 811 | +} |
0 commit comments