test(server): add tests for trainingDate resources
This commit is contained in:
		
							parent
							
								
									d62a942b41
								
							
						
					
					
						commit
						e2fc77f767
					
				
					 1 changed files with 247 additions and 0 deletions
				
			
		|  | @ -4,6 +4,7 @@ import ( | |||
| 	"bytes" | ||||
| 	"encoding/json" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	"github.com/oapi-codegen/runtime/types" | ||||
| 	"github.com/shopspring/decimal" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"gitlab.mareshq.com/hq/yggdrasil/internal/money" | ||||
|  | @ -12,6 +13,7 @@ import ( | |||
| 	"net/http/httptest" | ||||
| 	"net/url" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| func doGet(t *testing.T, app *fiber.App, rawURL string) (*http.Response, error) { | ||||
|  | @ -235,4 +237,249 @@ func TestServer(t *testing.T) { | |||
| 		assert.Equal(t, http.StatusOK, lrr.StatusCode) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("Add training date", func(t *testing.T) { | ||||
| 		tr := &training.Training{ | ||||
| 			Name:        "Testing Training for training date", | ||||
| 			Description: "This is a test training for training date", | ||||
| 			Days:        1, | ||||
| 			Pricing: []training.TrainingPrice{ | ||||
| 				{ | ||||
| 					Amount:   decimal.NewFromInt(200), | ||||
| 					Currency: "EUR", | ||||
| 					Type:     training.OpenTrainingPrice, | ||||
| 				}, | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		_ = handlers.trainingRepository.Create(tr) | ||||
| 
 | ||||
| 		date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) | ||||
| 		startTime := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) | ||||
| 
 | ||||
| 		newTrainingDate := NewTrainingDate{ | ||||
| 			Address:   "Test Address 123, NYC", | ||||
| 			Capacity:  12, | ||||
| 			Date:      types.Date{Time: date}, | ||||
| 			Days:      tr.Days, | ||||
| 			IsOnline:  false, | ||||
| 			Location:  "NYC", | ||||
| 			StartTime: startTime.Format("15:04"), | ||||
| 			Price: Price{ | ||||
| 				Amount:   "200", | ||||
| 				Currency: "EUR", | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		rr, err := doPost(t, app, "/v1/trainings/"+tr.ID.String()+"/dates", newTrainingDate) | ||||
| 
 | ||||
| 		assert.Equal(t, http.StatusCreated, rr.StatusCode) | ||||
| 
 | ||||
| 		var resultTrainingDate TrainingDate | ||||
| 		err = json.NewDecoder(rr.Body).Decode(&resultTrainingDate) | ||||
| 		assert.NoError(t, err, "error unmarshalling response") | ||||
| 		assert.Equal(t, newTrainingDate.Address, resultTrainingDate.Address) | ||||
| 		assert.Equal(t, newTrainingDate.Location, resultTrainingDate.Location) | ||||
| 		assert.Equal(t, newTrainingDate.Capacity, resultTrainingDate.Capacity) | ||||
| 		assert.Equal(t, newTrainingDate.Days, resultTrainingDate.Days) | ||||
| 		assert.Equal(t, newTrainingDate.StartTime, resultTrainingDate.StartTime) | ||||
| 		assert.Equal(t, newTrainingDate.IsOnline, resultTrainingDate.IsOnline) | ||||
| 		assert.Equal(t, newTrainingDate.Price, resultTrainingDate.Price) | ||||
| 		assert.Equal(t, newTrainingDate.Date, resultTrainingDate.Date) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("Training not found", func(t *testing.T) { | ||||
| 		tr := &training.Training{ | ||||
| 			Name:        "Testing Training for training date", | ||||
| 			Description: "This is a test training for training date", | ||||
| 			Days:        1, | ||||
| 			Pricing: []training.TrainingPrice{ | ||||
| 				{ | ||||
| 					Amount:   decimal.NewFromInt(200), | ||||
| 					Currency: "EUR", | ||||
| 					Type:     training.OpenTrainingPrice, | ||||
| 				}, | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		_ = handlers.trainingRepository.Create(tr) | ||||
| 
 | ||||
| 		rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/97b0e954-14f3-4908-98fa-271f505056d3") | ||||
| 		assert.Equal(t, http.StatusNotFound, rr.StatusCode) | ||||
| 
 | ||||
| 		var trainingError NotFoundError | ||||
| 		err := json.NewDecoder(rr.Body).Decode(&trainingError) | ||||
| 		assert.NoError(t, err, "error getting response", err) | ||||
| 		assert.Equal(t, http.StatusNotFound, trainingError.Status) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("List all training dates", func(t *testing.T) { | ||||
| 		tr := &training.Training{ | ||||
| 			Name:        "Testing Training for training date", | ||||
| 			Description: "This is a test training for training date", | ||||
| 			Days:        1, | ||||
| 			Pricing: []training.TrainingPrice{ | ||||
| 				{ | ||||
| 					Amount:   decimal.NewFromInt(200), | ||||
| 					Currency: "EUR", | ||||
| 					Type:     training.OpenTrainingPrice, | ||||
| 				}, | ||||
| 			}, | ||||
| 		} | ||||
| 		_ = handlers.trainingRepository.Create(tr) | ||||
| 
 | ||||
| 		date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) | ||||
| 
 | ||||
| 		td := &training.TrainingDate{ | ||||
| 			Address:   "Test Address 123, NYC", | ||||
| 			Capacity:  12, | ||||
| 			Date:      date, | ||||
| 			Days:      tr.Days, | ||||
| 			IsOnline:  false, | ||||
| 			Location:  "NYC", | ||||
| 			StartTime: date, | ||||
| 			Price: money.Price{ | ||||
| 				Amount:   decimal.NewFromInt(200), | ||||
| 				Currency: "EUR", | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		trainingCount := 5 | ||||
| 
 | ||||
| 		for range trainingCount { | ||||
| 			_ = handlers.trainingDateRepository.Create(tr.ID, td) | ||||
| 		} | ||||
| 
 | ||||
| 		rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates") | ||||
| 		assert.Equal(t, http.StatusOK, rr.StatusCode) | ||||
| 
 | ||||
| 		var trainingDates []TrainingDate | ||||
| 		err := json.NewDecoder(rr.Body).Decode(&trainingDates) | ||||
| 		assert.NoError(t, err, "error unmarshalling response") | ||||
| 		assert.Equal(t, len(trainingDates), trainingCount) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("Update training date", func(t *testing.T) { | ||||
| 		tr := &training.Training{ | ||||
| 			Name:        "Testing Training for training date", | ||||
| 			Description: "This is a test training for training date", | ||||
| 			Days:        1, | ||||
| 			Pricing: []training.TrainingPrice{ | ||||
| 				{ | ||||
| 					Amount:   decimal.NewFromInt(200), | ||||
| 					Currency: "EUR", | ||||
| 					Type:     training.OpenTrainingPrice, | ||||
| 				}, | ||||
| 			}, | ||||
| 		} | ||||
| 		_ = handlers.trainingRepository.Create(tr) | ||||
| 
 | ||||
| 		date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) | ||||
| 
 | ||||
| 		td := &training.TrainingDate{ | ||||
| 			Address:   "Test Address 123, NYC", | ||||
| 			Capacity:  12, | ||||
| 			Date:      date, | ||||
| 			Days:      tr.Days, | ||||
| 			IsOnline:  false, | ||||
| 			Location:  "NYC", | ||||
| 			StartTime: date, | ||||
| 			Price: money.Price{ | ||||
| 				Amount:   decimal.NewFromInt(200), | ||||
| 				Currency: "EUR", | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		_ = handlers.trainingDateRepository.Create(tr.ID, td) | ||||
| 
 | ||||
| 		updTd := NewTrainingDate{ | ||||
| 			Address:   "Updated Address 123, NYC", | ||||
| 			Capacity:  8, | ||||
| 			Date:      types.Date{Time: date}, | ||||
| 			Days:      tr.Days, | ||||
| 			IsOnline:  true, | ||||
| 			Location:  "NYC", | ||||
| 			StartTime: date.Format("15:04"), | ||||
| 			Price: Price{ | ||||
| 				Amount:   "200", | ||||
| 				Currency: "EUR", | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		rr, _ := doPut(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String(), updTd) | ||||
| 		assert.Equal(t, http.StatusOK, rr.StatusCode) | ||||
| 
 | ||||
| 		var tdrr TrainingDate | ||||
| 		err := json.NewDecoder(rr.Body).Decode(&tdrr) | ||||
| 		assert.NoError(t, err, "error unmarshalling response") | ||||
| 		assert.Equal(t, updTd.Address, tdrr.Address) | ||||
| 		assert.Equal(t, updTd.Capacity, tdrr.Capacity) | ||||
| 		assert.Equal(t, updTd.Date, tdrr.Date) | ||||
| 		assert.Equal(t, updTd.Days, tdrr.Days) | ||||
| 		assert.Equal(t, updTd.IsOnline, tdrr.IsOnline) | ||||
| 		assert.Equal(t, updTd.Location, tdrr.Location) | ||||
| 		assert.Equal(t, updTd.StartTime, tdrr.StartTime) | ||||
| 		assert.Equal(t, updTd.Price, tdrr.Price) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("Delete training date", func(t *testing.T) { | ||||
| 		tr := &training.Training{ | ||||
| 			Name:        "Testing Training for training date", | ||||
| 			Description: "This is a test training for training date", | ||||
| 			Days:        1, | ||||
| 			Pricing: []training.TrainingPrice{ | ||||
| 				{ | ||||
| 					Amount:   decimal.NewFromInt(200), | ||||
| 					Currency: "EUR", | ||||
| 					Type:     training.OpenTrainingPrice, | ||||
| 				}, | ||||
| 			}, | ||||
| 		} | ||||
| 		_ = handlers.trainingRepository.Create(tr) | ||||
| 
 | ||||
| 		date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) | ||||
| 
 | ||||
| 		td := &training.TrainingDate{ | ||||
| 			Address:   "Test Address 123, NYC", | ||||
| 			Capacity:  12, | ||||
| 			Date:      date, | ||||
| 			Days:      tr.Days, | ||||
| 			IsOnline:  false, | ||||
| 			Location:  "NYC", | ||||
| 			StartTime: date, | ||||
| 			Price: money.Price{ | ||||
| 				Amount:   decimal.NewFromInt(200), | ||||
| 				Currency: "EUR", | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		_ = handlers.trainingDateRepository.Create(tr.ID, td) | ||||
| 
 | ||||
| 		rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()) | ||||
| 		assert.Equal(t, http.StatusOK, rr.StatusCode) | ||||
| 
 | ||||
| 		var tdrr TrainingDate | ||||
| 		err := json.NewDecoder(rr.Body).Decode(&tdrr) | ||||
| 		assert.NoError(t, err, "error unmarshalling response") | ||||
| 		assert.Equal(t, td.ID, tdrr.Id) | ||||
| 
 | ||||
| 		drr, _ := doDelete(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()) | ||||
| 		assert.Equal(t, http.StatusNoContent, drr.StatusCode) | ||||
| 
 | ||||
| 		rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()) | ||||
| 		assert.Equal(t, http.StatusNotFound, rr.StatusCode) | ||||
| 
 | ||||
| 		var trainingError NotFoundError | ||||
| 		err = json.NewDecoder(rr.Body).Decode(&trainingError) | ||||
| 		assert.NoError(t, err, "error getting response", err) | ||||
| 		assert.Equal(t, http.StatusNotFound, trainingError.Status) | ||||
| 
 | ||||
| 		lrr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates") | ||||
| 		assert.Equal(t, http.StatusOK, lrr.StatusCode) | ||||
| 
 | ||||
| 		var trainingDates []TrainingDate | ||||
| 		err = json.NewDecoder(lrr.Body).Decode(&trainingDates) | ||||
| 		assert.NoError(t, err, "error unmarshalling response") | ||||
| 		assert.Equal(t, len(trainingDates), 0) | ||||
| 	}) | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Reference in a new issue