github.com/nshntarora/pop@v0.1.2/model_test.go (about) 1 package pop 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/nshntarora/pop/testdata/models/a" 10 "github.com/nshntarora/pop/testdata/models/ac" 11 "github.com/nshntarora/pop/testdata/models/b" 12 "github.com/nshntarora/pop/testdata/models/bc" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func Test_Model_TableName(t *testing.T) { 18 for k, v := range []interface{}{ 19 User{}, 20 &User{}, 21 22 &Users{}, 23 Users{}, 24 25 []*User{}, 26 &[]*User{}, 27 28 []User{}, 29 &[]User{}, 30 } { 31 t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { 32 r := require.New(t) 33 m := Model{Value: v} 34 r.Equal("users", m.TableName()) 35 }) 36 } 37 } 38 39 type tn struct{} 40 41 func (tn) TableName() string { 42 return "this is my table name" 43 } 44 45 type tnc struct{} 46 47 func (tnc) TableName(ctx context.Context) string { 48 return ctx.Value("name").(string) 49 } 50 51 // A failing test case for #477 52 func Test_TableNameCache(t *testing.T) { 53 r := assert.New(t) 54 r.Equal("usera", (&Model{Value: a.User{}}).TableName()) 55 r.Equal("userb", (&Model{Value: b.User{}}).TableName()) 56 r.Equal("usera", (&Model{Value: []a.User{}}).TableName()) 57 r.Equal("userb", (&Model{Value: []b.User{}}).TableName()) 58 } 59 60 // A failing test case for #477 61 func Test_TableNameContextCache(t *testing.T) { 62 ctx := context.WithValue(context.Background(), "name", "context_table") 63 64 r := assert.New(t) 65 r.Equal("context_table_useras", (&Model{Value: ac.User{}, ctx: ctx}).TableName()) 66 r.Equal("context_table_userbs", (&Model{Value: bc.User{}, ctx: ctx}).TableName()) 67 r.Equal("context_table_useras", (&Model{Value: []ac.User{}, ctx: ctx}).TableName()) 68 r.Equal("context_table_userbs", (&Model{Value: []bc.User{}, ctx: ctx}).TableName()) 69 } 70 71 func Test_TableName(t *testing.T) { 72 r := require.New(t) 73 74 cases := []interface{}{ 75 tn{}, 76 &tn{}, 77 []tn{}, 78 &[]tn{}, 79 []*tn{}, 80 &[]*tn{}, 81 } 82 for _, tc := range cases { 83 m := Model{Value: tc} 84 r.Equal("this is my table name", m.TableName()) 85 } 86 } 87 88 func Test_TableNameContext(t *testing.T) { 89 r := require.New(t) 90 91 tn := "context_table_names" 92 ctx := context.WithValue(context.Background(), "name", tn) 93 94 cases := []interface{}{ 95 tnc{}, 96 []tnc{}, 97 } 98 for _, tc := range cases { 99 m := Model{Value: tc, ctx: ctx} 100 r.Equal(tn, m.TableName()) 101 } 102 } 103 104 type TimeTimestamp struct { 105 ID int `db:"id"` 106 CreatedAt time.Time `db:"created_at"` 107 UpdatedAt time.Time `db:"updated_at"` 108 } 109 110 type UnixTimestamp struct { 111 ID int `db:"id"` 112 CreatedAt int `db:"created_at"` 113 UpdatedAt int `db:"updated_at"` 114 } 115 116 func Test_Touch_Time_Timestamp(t *testing.T) { 117 r := require.New(t) 118 119 m := NewModel(&TimeTimestamp{}, context.Background()) 120 121 // Override time.Now() 122 t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") 123 124 m.setCreatedAt(t0) 125 m.setUpdatedAt(t0) 126 v := m.Value.(*TimeTimestamp) 127 r.Equal(t0, v.CreatedAt) 128 r.Equal(t0, v.UpdatedAt) 129 } 130 131 func Test_Touch_Time_Timestamp_With_Existing_Value(t *testing.T) { 132 r := require.New(t) 133 134 // Override time.Now() 135 t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") 136 137 createdAt := nowFunc().Add(-36 * time.Hour) 138 139 m := NewModel(&TimeTimestamp{CreatedAt: createdAt}, context.Background()) 140 m.setCreatedAt(t0) 141 m.setUpdatedAt(t0) 142 143 v := m.Value.(*TimeTimestamp) 144 r.Equal(createdAt, v.CreatedAt) 145 r.Equal(t0, v.UpdatedAt) 146 } 147 148 func Test_Touch_Unix_Timestamp(t *testing.T) { 149 r := require.New(t) 150 151 m := NewModel(&UnixTimestamp{}, context.Background()) 152 153 // Override time.Now() 154 t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") 155 156 m.setCreatedAt(t0) 157 m.setUpdatedAt(t0) 158 v := m.Value.(*UnixTimestamp) 159 r.Equal(int(t0.Unix()), v.CreatedAt) 160 r.Equal(int(t0.Unix()), v.UpdatedAt) 161 } 162 163 func Test_Touch_Unix_Timestamp_With_Existing_Value(t *testing.T) { 164 r := require.New(t) 165 166 // Override time.Now() 167 t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") 168 169 createdAt := int(time.Now().Add(-36 * time.Hour).Unix()) 170 171 m := NewModel(&UnixTimestamp{CreatedAt: createdAt}, context.Background()) 172 m.setCreatedAt(t0) 173 m.setUpdatedAt(t0) 174 175 v := m.Value.(*UnixTimestamp) 176 r.Equal(createdAt, v.CreatedAt) 177 r.Equal(int(t0.Unix()), v.UpdatedAt) 178 } 179 180 func Test_IDField(t *testing.T) { 181 r := require.New(t) 182 183 type testCustomID struct { 184 ID int `db:"custom_id"` 185 } 186 m := Model{Value: &testCustomID{ID: 1}} 187 r.Equal("custom_id", m.IDField()) 188 189 type testNormalID struct { 190 ID int 191 } 192 m = Model{Value: &testNormalID{ID: 1}} 193 r.Equal("id", m.IDField()) 194 } 195 196 type testPrefixID struct { 197 ID int `db:"custom_id"` 198 } 199 200 func (t testPrefixID) TableName() string { 201 return "foo.bar" 202 } 203 204 func Test_WhereID(t *testing.T) { 205 r := require.New(t) 206 m := Model{Value: &testPrefixID{ID: 1}} 207 208 r.Equal("foo_bar.custom_id = ?", m.WhereID()) 209 r.Equal("foo_bar.custom_id = :custom_id", m.WhereNamedID()) 210 211 type testNormalID struct { 212 ID int 213 } 214 m = Model{Value: &testNormalID{ID: 1}} 215 r.Equal("id", m.IDField()) 216 }