package system

import "testing"

func TestMigrationsCreateOnlySystemSchema(t *testing.T) {
	db, err := openWithDriver("sqlite", "file:"+t.Name()+"?mode=memory&cache=shared")
	if err != nil {
		t.Fatal(err)
	}
	if err = migrateAll(db); err != nil {
		t.Fatal(err)
	}
	for _, table := range []string{"sys_users", "sys_base_menus", "sys_apis", "sys_departments", "sys_announcements"} {
		if !db.Migrator().HasTable(table) {
			t.Fatalf("system migration did not create %s", table)
		}
	}
	var users, menus, apis int64
	if err = db.Model(&userPO{}).Count(&users).Error; err != nil {
		t.Fatal(err)
	}
	if err = db.Model(&menuPO{}).Count(&menus).Error; err != nil {
		t.Fatal(err)
	}
	if err = db.Model(&apiPO{}).Count(&apis).Error; err != nil {
		t.Fatal(err)
	}
	if users != 0 || menus != 0 || apis != 0 {
		t.Fatalf("schema migration seeded business data: users=%d menus=%d apis=%d", users, menus, apis)
	}
}
