Commit 355b31c2 authored by Max Richter's avatar Max Richter
Browse files

test(mongodb): write room repository tests

parent beb6b11d
package mongo
import (
"testing"
"time"
repos "git.coco.study/fvitt/good2go/database/mongo/repositories"
utils "git.coco.study/fvitt/good2go/database/mongo/utils"
)
func TestMain(t *testing.T) {
// Create a mongodb client
client, ctx := utils.CreateDBClient(20 * time.Second)
defer client.Disconnect(ctx)
// Connect to a specified database
db := client.Database(utils.GetDBName())
// Connect the repositories
repos.RoomRepository.ConnectTo(db.Collection("rooms"))
}
package mongo
import (
"testing"
"git.coco.study/fvitt/good2go/internal/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var repo *roomRepository
func TestMain(m *testing.M) {
// Mock the collection
repo = roomRepository{}.ConnectTo(&mongo.Collection{})
}
func Test_roomRepository_CreateRoom(t *testing.T) {
repo.CreateRoom(&model.Room{
Number: 100,
Capacity: 10,
})
room, err := repo.findByRoomNumber(100)
if err != nil {
t.Error(err)
}
if room.Number != 100 {
t.Error("Room was not created with the correct roomNumber")
}
}
func Test_roomRepository_UpdateRoom(t *testing.T) {
repo.UpdateRoom(&model.Room{
Number: 100,
Capacity: 22,
})
room, err := repo.findByRoomNumber(100)
if err != nil {
t.Error(err)
}
if room.Capacity != 22 {
t.Error("Room did not have the updated capacity")
}
}
func Test_roomRepository_GetRoom(t *testing.T) {
repo.CreateRoom(&model.Room{
Number: 420,
Capacity: 22,
})
_, err := repo.GetRoom(bson.M{
"number": 420,
})
if err != nil {
t.Error(err)
}
}
func Test_roomRepository_GetAllRooms(t *testing.T) {
rooms := repo.GetAllRooms()
t.Log(rooms)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment