Example #1
0
func TestNewSub(t *testing.T) {
	testhelpers.CleanupDB()

	lobby := testhelpers.CreateLobby()
	lobby.Save()

	player := testhelpers.CreatePlayer()
	player.Save()

	tperr := lobby.AddPlayer(player, 0, "red", "scout1")
	assert.Nil(t, tperr)

	sub, err := models.NewSub(lobby.ID, player.SteamId)
	assert.Nil(t, err)

	db.DB.Save(sub)

	subs, err := models.GetPlayerSubs(player.SteamId)
	assert.Nil(t, err)
	assert.Equal(t, len(subs), 1)
	assert.Equal(t, subs[0].LobbyID, lobby.ID)

	player2 := testhelpers.CreatePlayer()
	player2.Save()
	tperr = lobby.AddPlayer(player2, 0, "red", "scout1")
	assert.Nil(t, tperr)

	err = db.DB.Where("lobby_id = ? AND steam_id = ?", lobby.ID, player.SteamId).First(sub).Error
	assert.Nil(t, err)
	assert.True(t, sub.Filled)
}
Example #2
0
func TestNewChatMessage(t *testing.T) {
	testhelpers.CleanupDB()

	lobby := testhelpers.CreateLobby()
	lobby.Save()

	player := testhelpers.CreatePlayer()
	player.Save()

	for i := 0; i < 3; i++ {
		time.Sleep(time.Second)

		message := models.NewChatMessage(strconv.Itoa(i), 0, player)
		assert.NotNil(t, message)

		err := db.DB.Save(message).Error
		assert.Nil(t, err)
	}

	messages, err := models.GetMessages(player, 0)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 3)

	for i := 1; i < len(messages); i++ {
		assert.True(t, messages[i].CreatedAt.Unix() > messages[i-1].CreatedAt.Unix())
	}

	for i := 0; i < 3; i++ {
		time.Sleep(time.Second)

		message := models.NewChatMessage(strconv.Itoa(i), 1, player)
		assert.NotNil(t, message)

		err := db.DB.Save(message).Error
		assert.Nil(t, err)
	}

	messages, err = models.GetMessages(player, 1)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 3)

	for i := 1; i < len(messages); i++ {
		assert.True(t, messages[i].CreatedAt.Unix() > messages[i-1].CreatedAt.Unix())
	}

	messages, err = models.GetAllMessages(player)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 6)
}