Enha: sqlx instead of pgx
This commit is contained in:
		
							
								
								
									
										177
									
								
								repos/actions_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										177
									
								
								repos/actions_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,177 @@ | ||||
| package repos | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"gralias/models" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/jmoiron/sqlx" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	_ "github.com/mattn/go-sqlite3" | ||||
| ) | ||||
|  | ||||
| func setupActionsTestDB(t *testing.T) (*sqlx.DB, func()) { | ||||
| 	db, err := sqlx.Connect("sqlite3", ":memory:") | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	schema := ` | ||||
| 	CREATE TABLE IF NOT EXISTS actions ( | ||||
| 		room_id TEXT, | ||||
| 		actor TEXT, | ||||
| 		actor_color TEXT, | ||||
| 		action_type TEXT, | ||||
| 		word TEXT, | ||||
| 		word_color TEXT, | ||||
| 		number_associated TEXT, | ||||
| 		created_at INTEGER | ||||
| 	); | ||||
| 	` | ||||
| 	_, err = db.Exec(schema) | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	return db, func() { | ||||
| 		db.Close() | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestActionsRepo_CreateAction(t *testing.T) { | ||||
| 	db, teardown := setupActionsTestDB(t) | ||||
| 	defer teardown() | ||||
|  | ||||
| 	repo := &RepoProvider{DB: db} | ||||
|  | ||||
| 	roomID := "test_room_actions_1" | ||||
| 	action := &models.Action{ | ||||
| 		Actor:      "player1", | ||||
| 		ActorColor: "blue", | ||||
| 		Action:     "gave_clue", | ||||
| 		Word:       "apple", | ||||
| 		WordColor:  "red", | ||||
| 		Number:     "3", | ||||
| 		CreatedAt:  time.Now(), | ||||
| 	} | ||||
|  | ||||
| 	err := repo.CreateAction(context.Background(), roomID, action) | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	var retrievedAction models.Action | ||||
| 	err = db.Get(&retrievedAction, "SELECT * FROM actions WHERE room_id = ? AND actor = ?", roomID, action.Actor) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Equal(t, action.Word, retrievedAction.Word) | ||||
| } | ||||
|  | ||||
| func TestActionsRepo_ListActions(t *testing.T) { | ||||
| 	db, teardown := setupActionsTestDB(t) | ||||
| 	defer teardown() | ||||
|  | ||||
| 	repo := &RepoProvider{DB: db} | ||||
|  | ||||
| 	roomID := "test_room_actions_2" | ||||
| 	action1 := &models.Action{ | ||||
| 		Actor:      "player1", | ||||
| 		ActorColor: "blue", | ||||
| 		Action:     "gave_clue", | ||||
| 		Word:       "apple", | ||||
| 		WordColor:  "red", | ||||
| 		Number:     "3", | ||||
| 		CreatedAt:  time.Now().Add(-2 * time.Second), | ||||
| 	} | ||||
| 	action2 := &models.Action{ | ||||
| 		Actor:      "player2", | ||||
| 		ActorColor: "red", | ||||
| 		Action:     "guessed", | ||||
| 		Word:       "banana", | ||||
| 		WordColor:  "blue", | ||||
| 		Number:     "0", | ||||
| 		CreatedAt:  time.Now().Add(-1 * time.Second), | ||||
| 	} | ||||
|  | ||||
| 	_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt.UnixNano()) | ||||
| 	assert.NoError(t, err) | ||||
| 	_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt.UnixNano()) | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	actions, err := repo.ListActions(context.Background(), roomID) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Len(t, actions, 2) | ||||
| 	assert.Equal(t, action1.Word, actions[0].Word) | ||||
| 	assert.Equal(t, action2.Word, actions[1].Word) | ||||
| } | ||||
|  | ||||
| func TestActionsRepo_GetLastClue(t *testing.T) { | ||||
| 	db, teardown := setupActionsTestDB(t) | ||||
| 	defer teardown() | ||||
|  | ||||
| 	repo := &RepoProvider{DB: db} | ||||
|  | ||||
| 	roomID := "test_room_actions_3" | ||||
| 	action1 := &models.Action{ | ||||
| 		Actor:      "player1", | ||||
| 		ActorColor: "blue", | ||||
| 		Action:     "gave_clue", | ||||
| 		Word:       "apple", | ||||
| 		WordColor:  "red", | ||||
| 		Number:     "3", | ||||
| 		CreatedAt:  time.Now().Add(-3 * time.Second), | ||||
| 	} | ||||
| 	action2 := &models.Action{ | ||||
| 		Actor:      "player2", | ||||
| 		ActorColor: "red", | ||||
| 		Action:     "gave_clue", | ||||
| 		Word:       "banana", | ||||
| 		WordColor:  "blue", | ||||
| 		Number:     "2", | ||||
| 		CreatedAt:  time.Now().Add(-2 * time.Second), | ||||
| 	} | ||||
| 	// Non-clue action | ||||
| 	action3 := &models.Action{ | ||||
| 		Actor:      "player3", | ||||
| 		ActorColor: "blue", | ||||
| 		Action:     "guessed", | ||||
| 		Word:       "orange", | ||||
| 		WordColor:  "blue", | ||||
| 		Number:     "0", | ||||
| 		CreatedAt:  time.Now().Add(-1 * time.Second), | ||||
| 	} | ||||
|  | ||||
| 	_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt.UnixNano()) | ||||
| 	assert.NoError(t, err) | ||||
| 	_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt.UnixNano()) | ||||
| 	assert.NoError(t, err) | ||||
| 	_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action3.Actor, action3.ActorColor, action3.Action, action3.Word, action3.WordColor, action3.Number, action3.CreatedAt.UnixNano()) | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	lastClue, err := repo.GetLastClue(context.Background(), roomID) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.NotNil(t, lastClue) | ||||
| 	assert.Equal(t, action2.Word, lastClue.Word) | ||||
| } | ||||
|  | ||||
| func TestActionsRepo_DeleteActionsByRoomID(t *testing.T) { | ||||
| 	db, teardown := setupActionsTestDB(t) | ||||
| 	defer teardown() | ||||
|  | ||||
| 	repo := &RepoProvider{DB: db} | ||||
|  | ||||
| 	roomID := "test_room_actions_4" | ||||
| 	action1 := &models.Action{ | ||||
| 		Actor:      "player1", | ||||
| 		ActorColor: "blue", | ||||
| 		Action:     "gave_clue", | ||||
| 		Word:       "apple", | ||||
| 		WordColor:  "red", | ||||
| 		Number:     "3", | ||||
| 		CreatedAt:  time.Now(), | ||||
| 	} | ||||
| 	_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt.UnixNano()) | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	err = repo.DeleteActionsByRoomID(context.Background(), roomID) | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	var count int | ||||
| 	err = db.Get(&count, "SELECT COUNT(*) FROM actions WHERE room_id = ?", roomID) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Equal(t, 0, count) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder