automigrate_test.go 8.84 KB
Newer Older
Amos Wenger's avatar
Amos Wenger committed
1 2 3 4
package hades_test

import (
	"context"
5
	"fmt"
Amos Wenger's avatar
Amos Wenger committed
6
	"testing"
7
	"time"
Amos Wenger's avatar
Amos Wenger committed
8

9 10
	"github.com/itchio/hades/sqliteutil2"

Amos Wenger's avatar
Amos Wenger committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	"crawshaw.io/sqlite"
	"github.com/go-xorm/builder"
	"github.com/itchio/hades"
	"github.com/stretchr/testify/assert"
)

func Test_AutoMigrate(t *testing.T) {
	dbpool, err := sqlite.Open("file:memory:?mode=memory", 0, 10)
	ordie(err)
	defer dbpool.Close()

	conn := dbpool.Get(context.Background().Done())
	defer dbpool.Put(conn)

	{
		type User struct {
			ID        int64
			FirstName string
		}

		models := []interface{}{&User{}}

		c, err := hades.NewContext(makeConsumer(t), models...)
		ordie(err)
		c.Log = true

		t.Logf("first migration")
		ordie(c.AutoMigrate(conn))

		pti, err := c.PragmaTableInfo(conn, "users")
		ordie(err)
		assert.EqualValues(t, "id", pti[0].Name)
		assert.EqualValues(t, "INTEGER", pti[0].Type)
		assert.True(t, pti[0].PrimaryKey)
		assert.True(t, pti[0].NotNull)

		assert.EqualValues(t, "first_name", pti[1].Name)
		assert.EqualValues(t, "TEXT", pti[1].Type)
		assert.False(t, pti[1].PrimaryKey)
		assert.False(t, pti[1].NotNull)

Amos Wenger's avatar
Amos Wenger committed
52
		ordie(c.Save(conn, &User{ID: 123, FirstName: "Joanna"}))
Amos Wenger's avatar
Amos Wenger committed
53
		u := &User{}
Amos Wenger's avatar
Amos Wenger committed
54 55 56
		foundUser, err := c.SelectOne(conn, u, builder.Eq{"id": 123})
		ordie(err)
		assert.True(t, foundUser)
Amos Wenger's avatar
Amos Wenger committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		assert.EqualValues(t, &User{ID: 123, FirstName: "Joanna"}, u)

		t.Logf("first migration (bis)")
		ordie(c.AutoMigrate(conn))
	}

	{
		type User struct {
			ID        int64
			FirstName string
			LastName  string
		}

		models := []interface{}{&User{}}

		c, err := hades.NewContext(makeConsumer(t), models...)
		ordie(err)
		c.Log = true

		t.Logf("second migration")
		ordie(c.AutoMigrate(conn))

		pti, err := c.PragmaTableInfo(conn, "users")
		ordie(err)
		assert.EqualValues(t, "id", pti[0].Name)
		assert.EqualValues(t, "INTEGER", pti[0].Type)
		assert.True(t, pti[0].PrimaryKey)
		assert.True(t, pti[0].NotNull)

		assert.EqualValues(t, "first_name", pti[1].Name)
		assert.EqualValues(t, "TEXT", pti[1].Type)
		assert.False(t, pti[1].PrimaryKey)
		assert.False(t, pti[1].NotNull)

		assert.EqualValues(t, "last_name", pti[2].Name)
		assert.EqualValues(t, "TEXT", pti[2].Type)
		assert.False(t, pti[2].PrimaryKey)
		assert.False(t, pti[2].NotNull)

		u := &User{}
Amos Wenger's avatar
Amos Wenger committed
97 98 99 100 101 102 103
		foundUser, err := c.SelectOne(conn, u, builder.Eq{"id": 83294})
		ordie(err)
		assert.False(t, foundUser)

		foundUser, err = c.SelectOne(conn, u, builder.Eq{"id": 123})
		ordie(err)
		assert.True(t, foundUser)
Amos Wenger's avatar
Amos Wenger committed
104 105 106 107 108 109 110
		assert.EqualValues(t, &User{ID: 123, FirstName: "Joanna", LastName: ""}, u)

		t.Logf("second migration (bis)")
		ordie(c.AutoMigrate(conn))
	}
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
func Test_AutoMigrateNoPK(t *testing.T) {
	dbpool, err := sqlite.Open("file:memory:?mode=memory", 0, 10)
	ordie(err)
	defer dbpool.Close()

	conn := dbpool.Get(context.Background().Done())
	defer dbpool.Put(conn)

	type Humanoid struct {
		Name string
	}

	models := []interface{}{&Humanoid{}}

	c, err := hades.NewContext(makeConsumer(t), models...)
	ordie(err)
	c.Log = true

	err = c.AutoMigrate(conn)
	assert.Error(t, err)
}

func Test_AutoMigrateAllValidTypes(t *testing.T) {
	dbpool, err := sqlite.Open("file:memory:?mode=memory", 0, 10)
	ordie(err)
	defer dbpool.Close()

	conn := dbpool.Get(context.Background().Done())
	defer dbpool.Put(conn)

	type Humanoid struct {
		ID        int64
		FirstName string
		Alive     bool
		HeartRate float64
		BornAt    time.Time
		Whatever  struct {
			Ohey        string
			ThisIsValid int64
		} `hades:"-"`
	}

	models := []interface{}{&Humanoid{}}

	c, err := hades.NewContext(makeConsumer(t), models...)
	ordie(err)
	c.Log = true

	ordie(c.AutoMigrate(conn))

	pti, err := c.PragmaTableInfo(conn, "humanoids")
	ordie(err)

	assert.EqualValues(t, 5, len(pti))

	assert.EqualValues(t, "id", pti[0].Name)
	assert.EqualValues(t, "INTEGER", pti[0].Type)
	assert.True(t, pti[0].PrimaryKey)
	assert.True(t, pti[0].NotNull)

	assert.EqualValues(t, "first_name", pti[1].Name)
	assert.EqualValues(t, "TEXT", pti[1].Type)
	assert.False(t, pti[1].PrimaryKey)
	assert.False(t, pti[1].NotNull)

	assert.EqualValues(t, "alive", pti[2].Name)
Amos Wenger's avatar
Amos Wenger committed
177
	assert.EqualValues(t, "BOOLEAN", pti[2].Type)
178 179 180 181 182 183 184 185 186 187 188 189
	assert.False(t, pti[2].PrimaryKey)
	assert.False(t, pti[2].NotNull)

	assert.EqualValues(t, "heart_rate", pti[3].Name)
	assert.EqualValues(t, "REAL", pti[3].Type)
	assert.False(t, pti[3].PrimaryKey)
	assert.False(t, pti[3].NotNull)

	assert.EqualValues(t, "born_at", pti[4].Name)
	assert.EqualValues(t, "DATETIME", pti[4].Type)
	assert.False(t, pti[4].PrimaryKey)
	assert.False(t, pti[4].NotNull)
Amos Wenger's avatar
Amos Wenger committed
190 191 192 193 194 195 196 197 198

	tim := time.Now()
	h1 := &Humanoid{
		ID:        12,
		Alive:     true,
		BornAt:    tim,
		FirstName: "Jeremy",
		HeartRate: 3.14,
	}
Amos Wenger's avatar
Amos Wenger committed
199
	ordie(c.Save(conn, h1))
Amos Wenger's avatar
Amos Wenger committed
200 201

	h2 := &Humanoid{}
Amos Wenger's avatar
Amos Wenger committed
202 203 204
	found, err := c.SelectOne(conn, h2, builder.Eq{"id": 12})
	ordie(err)
	assert.True(t, found)
Amos Wenger's avatar
Amos Wenger committed
205 206 207 208 209 210

	assert.EqualValues(t, h1.ID, h2.ID)
	assert.EqualValues(t, h1.Alive, h2.Alive)
	assert.EqualValues(t, h1.BornAt.Format(time.RFC3339Nano), h2.BornAt.Format(time.RFC3339Nano))
	assert.EqualValues(t, h1.FirstName, h2.FirstName)
	assert.EqualValues(t, h1.HeartRate, h2.HeartRate)
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
func Test_AutoMigrateSquash(t *testing.T) {
	dbpool, err := sqlite.Open("file:memory:?mode=memory", 0, 10)
	ordie(err)
	defer dbpool.Close()

	conn := dbpool.Get(context.Background().Done())
	defer dbpool.Put(conn)

	type AndroidTraits struct {
		Funny bool
		Wise  bool
		Fair  bool
	}

	type Android struct {
		ID     int64
		Title  string
		Traits AndroidTraits `hades:"squash"`
	}

	models := []interface{}{&Android{}}

	c, err := hades.NewContext(makeConsumer(t), models...)
	ordie(err)
	c.Log = true

	ordie(c.AutoMigrate(conn))
240
	defer c.ExecRaw(conn, "DROP TABLE androids", nil)
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

	pti, err := c.PragmaTableInfo(conn, "androids")
	ordie(err)

	assert.EqualValues(t, 5, len(pti))

	assert.EqualValues(t, "id", pti[0].Name)
	assert.EqualValues(t, "INTEGER", pti[0].Type)
	assert.True(t, pti[0].PrimaryKey)
	assert.True(t, pti[0].NotNull)

	assert.EqualValues(t, "title", pti[1].Name)
	assert.EqualValues(t, "TEXT", pti[1].Type)
	assert.False(t, pti[1].PrimaryKey)
	assert.False(t, pti[1].NotNull)

	assert.EqualValues(t, "funny", pti[2].Name)
	assert.EqualValues(t, "BOOLEAN", pti[2].Type)
	assert.False(t, pti[2].PrimaryKey)
	assert.False(t, pti[2].NotNull)

	assert.EqualValues(t, "wise", pti[3].Name)
	assert.EqualValues(t, "BOOLEAN", pti[3].Type)
	assert.False(t, pti[3].PrimaryKey)
	assert.False(t, pti[3].NotNull)

	assert.EqualValues(t, "fair", pti[4].Name)
	assert.EqualValues(t, "BOOLEAN", pti[4].Type)
	assert.False(t, pti[4].PrimaryKey)
	assert.False(t, pti[4].NotNull)
}

Amos Wenger's avatar
Amos Wenger committed
273 274
func ordie(err error) {
	if err != nil {
275
		panic(fmt.Sprintf("%+v", err))
Amos Wenger's avatar
Amos Wenger committed
276 277
	}
}
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

func Test_AutoMigratePreservesData(t *testing.T) {
	dbpool, err := sqlite.Open("file:memory:?mode=memory", 0, 10)
	ordie(err)
	defer dbpool.Close()

	conn := dbpool.Get(context.Background().Done())
	defer dbpool.Put(conn)

	defer sqliteutil2.Exec(conn, "DROP TABLE androids", nil)

	{
		type AndroidTraits struct {
			Funny string
			Wise  string
		}

		type Android struct {
			ID     int64
			Title  string
			Traits AndroidTraits `hades:"squash"`
		}

		models := []interface{}{&Android{}}

		c, err := hades.NewContext(makeConsumer(t), models...)
		ordie(err)
		c.Log = true

		{
			var migrateStats hades.AutoMigrateStats
			ordie(c.AutoMigrateEx(conn, &migrateStats))
			assert.EqualValues(t, 1, migrateStats.NumCreated)
			assert.EqualValues(t, 0, migrateStats.NumMigrated)
			assert.EqualValues(t, 0, migrateStats.NumCurrent)
		}

		refAndroid := &Android{
			ID:    123,
			Title: "tomo",
			Traits: AndroidTraits{
				Funny: "funny",
				Wise:  "wise",
			},
		}
		ordie(c.Save(conn, refAndroid))

		var a Android
		ok, err := c.SelectOne(conn, &a, builder.NewCond())
		ordie(err)
		assert.True(t, ok)
		assert.EqualValues(t, refAndroid, &a)
	}

	{
		type AndroidTraits struct {
			Funny string
			Loyal string
			Wise  string
		}

		type Android struct {
			ID     int64
			Title  string
			Traits AndroidTraits `hades:"squash"`
		}

		models := []interface{}{&Android{}}

		c, err := hades.NewContext(makeConsumer(t), models...)
		ordie(err)
		c.Log = true

		{
			var migrateStats hades.AutoMigrateStats
			ordie(c.AutoMigrateEx(conn, &migrateStats))
			assert.EqualValues(t, 0, migrateStats.NumCreated)
			assert.EqualValues(t, 1, migrateStats.NumMigrated)
			assert.EqualValues(t, 0, migrateStats.NumCurrent)
		}

		refAndroid := &Android{
			ID:    123,
			Title: "tomo",
			Traits: AndroidTraits{
				Funny: "funny",
				Wise:  "wise",
			},
		}

		var a Android
		ok, err := c.SelectOne(conn, &a, builder.NewCond())
		ordie(err)
		assert.True(t, ok)
		assert.EqualValues(t, refAndroid, &a)

		// migrate once more
		{
			var migrateStats hades.AutoMigrateStats
			ordie(c.AutoMigrateEx(conn, &migrateStats))
			assert.EqualValues(t, 0, migrateStats.NumCreated)
			assert.EqualValues(t, 0, migrateStats.NumMigrated)
			assert.EqualValues(t, 1, migrateStats.NumCurrent)
		}

		ok, err = c.SelectOne(conn, &a, builder.NewCond())
		ordie(err)
		assert.True(t, ok)
		assert.EqualValues(t, refAndroid, &a)
	}
}