From ef33facb45ac319f914b35ad4601317ddcb0f526 Mon Sep 17 00:00:00 2001 From: Anton Berezhnyi Date: Wed, 20 Sep 2023 14:56:02 +0300 Subject: [PATCH] Make test cases --- CommonEventData.go | 8 ++- CommonEventData_test.go | 23 +++++++ LessonCreateEvent.go | 2 +- LessonCreateEvent_test.go | 20 +++--- LessonDeletedEvent.go | 2 +- LessonDeletedEvent_test.go | 20 ++++++ LessonEditEvent.go | 4 +- LessonEditEvent_test.go | 58 +++++++++++++++++ Message_test.go | 127 ++++++++++++++++++++++++++++++++++--- ScoreEditEvent.go | 2 +- ScoreEditEvent_test.go | 43 +++++++++++++ 11 files changed, 281 insertions(+), 28 deletions(-) create mode 100644 CommonEventData_test.go create mode 100644 LessonDeletedEvent_test.go create mode 100644 LessonEditEvent_test.go create mode 100644 ScoreEditEvent_test.go diff --git a/CommonEventData.go b/CommonEventData.go index 93c1511..c24f811 100644 --- a/CommonEventData.go +++ b/CommonEventData.go @@ -29,10 +29,12 @@ func parseUint(s string) uint { return uint(value) } -func EventToMessage(event interface{}) (message Message) { +func EventToMessage(event interface{}) *Message { tmpForm := map[string]interface{}{} - message.Form = Form{} + message := &Message{ + Form: Form{}, + } _ = mapstructure.Decode(event, &tmpForm) for key, value := range tmpForm { @@ -51,5 +53,5 @@ func EventToMessage(event interface{}) (message Message) { } } - return + return message } diff --git a/CommonEventData_test.go b/CommonEventData_test.go new file mode 100644 index 0000000..10d8e9c --- /dev/null +++ b/CommonEventData_test.go @@ -0,0 +1,23 @@ +package dekanat_events + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +var testCommonEventDataEvent = CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "999999", + DisciplineId: "193000", + Semester: "0", +} + +func TestCommonEventData_GetDisciplineId(t *testing.T) { + assert.Equal(t, uint(193000), testCommonEventDataEvent.GetDisciplineId()) +} + +func TestCommonEventData_GetLessonId(t *testing.T) { + assert.Equal(t, uint(999999), testCommonEventDataEvent.GetLessonId()) +} diff --git a/LessonCreateEvent.go b/LessonCreateEvent.go index 3205d79..68eacea 100644 --- a/LessonCreateEvent.go +++ b/LessonCreateEvent.go @@ -19,7 +19,7 @@ func createLessonCreateEvent(form Form, eventData *CommonEventData) (event Lesso return } -func (event *LessonCreateEvent) ToMessage() Message { +func (event *LessonCreateEvent) ToMessage() *Message { message := EventToMessage(event) message.Form["n"] = strconv.Itoa(LessonFormActionNumber) message.Form["action"] = LessonInsertFormAction diff --git a/LessonCreateEvent_test.go b/LessonCreateEvent_test.go index 110633b..ab5382b 100644 --- a/LessonCreateEvent_test.go +++ b/LessonCreateEvent_test.go @@ -5,18 +5,18 @@ import ( ) func TestLessonCreateEvent_ToMessage(t *testing.T) { - receipt := "asdads" - - createEvent := LessonCreateEvent{ - TypeId: "211", + originalEvent := LessonCreateEvent{ CommonEventData: CommonEventData{ - ReceiptHandle: &receipt, - LessonId: "4989", + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "0", + DisciplineId: "193000", + Semester: "0", }, + TypeId: "1", + Date: "23.12.2022", + TeacherId: "9999", } - createEvent.ToMessage() - - // fmt.Printf("%+v", message) - + ExecuteTestMessageToEvent(t, originalEvent, originalEvent.ToMessage()) } diff --git a/LessonDeletedEvent.go b/LessonDeletedEvent.go index 62ffc5e..00562bf 100644 --- a/LessonDeletedEvent.go +++ b/LessonDeletedEvent.go @@ -16,7 +16,7 @@ func createLessonDeleteEvent(form Form, eventData *CommonEventData) (event Lesso return } -func (event *LessonDeletedEvent) ToMessage() Message { +func (event *LessonDeletedEvent) ToMessage() *Message { message := EventToMessage(event) message.Form["n"] = strconv.Itoa(LessonDeleteFormActionNumber) message.Form["action"] = LessonDeleteFormAction diff --git a/LessonDeletedEvent_test.go b/LessonDeletedEvent_test.go new file mode 100644 index 0000000..e99a6fd --- /dev/null +++ b/LessonDeletedEvent_test.go @@ -0,0 +1,20 @@ +package dekanat_events + +import "testing" + +func TestLessonDeletedEvent_ToMessage(t *testing.T) { + t.Run("TestLessonEditEvent_ToMessage", func(t *testing.T) { + originalEvent := LessonDeletedEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "999999", + DisciplineId: "193000", + Semester: "0", + }, + } + + ExecuteTestMessageToEvent(t, originalEvent, originalEvent.ToMessage()) + }) +} diff --git a/LessonEditEvent.go b/LessonEditEvent.go index 75b6107..02de9b2 100644 --- a/LessonEditEvent.go +++ b/LessonEditEvent.go @@ -14,7 +14,7 @@ type LessonEditEvent struct { TypeId string `mapstructure:"tzn"` Date string `mapstructure:"date_z"` TeacherId string `mapstructure:"teacher"` - IsDeleted bool + IsDeleted bool `mapstructure:"-"` } func createLessonEditEvent(form Form, eventData *CommonEventData) (event LessonEditEvent, err error) { @@ -23,7 +23,7 @@ func createLessonEditEvent(form Form, eventData *CommonEventData) (event LessonE return } -func (event *LessonEditEvent) ToMessage() Message { +func (event *LessonEditEvent) ToMessage() *Message { message := EventToMessage(event) message.Form["n"] = strconv.Itoa(LessonEditEventActionNumber) message.Form["action"] = LessonEditFormAction diff --git a/LessonEditEvent_test.go b/LessonEditEvent_test.go new file mode 100644 index 0000000..9a7bfb2 --- /dev/null +++ b/LessonEditEvent_test.go @@ -0,0 +1,58 @@ +package dekanat_events + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +var testLessonEditEvent = LessonEditEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "999999", + DisciplineId: "193000", + Semester: "0", + }, + TypeId: "12", + Date: "12.12.2022", + TeacherId: "9999", + IsDeleted: false, +} + +func TestLessonEditEvent_ToMessage(t *testing.T) { + t.Run("TestLessonEditEvent_ToMessage", func(t *testing.T) { + originalEvent := LessonEditEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "999999", + DisciplineId: "193000", + Semester: "0", + }, + TypeId: "1", + Date: "12.12.2022", + TeacherId: "9999", + IsDeleted: false, + } + + ExecuteTestMessageToEvent(t, originalEvent, originalEvent.ToMessage()) + }) +} + +func TestLessonEditEvent_GetTypeId(t *testing.T) { + assert.Equal(t, uint8(12), testLessonEditEvent.GetTypeId()) +} + +func TestLessonEditEvent_GetDate(t *testing.T) { + t.Run("not empty date", func(t *testing.T) { + expectedDate := time.Date(2022, 12, 12, 0, 0, 0, 0, time.Local) + assert.Equal(t, expectedDate, testLessonEditEvent.GetDate()) + }) + + t.Run("empty date", func(t *testing.T) { + assert.True(t, (&LessonEditEvent{}).GetDate().IsZero()) + }) +} diff --git a/Message_test.go b/Message_test.go index 1ebb9dc..8d49e93 100644 --- a/Message_test.go +++ b/Message_test.go @@ -79,44 +79,124 @@ const ScoreEditEventFormJSON = `{ "AddEstim":"0" }` -func TestCreateMessageAndToForm(t *testing.T) { +var testReceiptHandle = "ReceiptHandle" + +func TestMakeEventFromMessageJson(t *testing.T) { t.Run("Fetch LessonCreateEvent", func(t *testing.T) { - actualEvent := tranformMessageJsonToEvent(t, LessonCreateEventFormJSON) + expectedEvent := LessonCreateEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "0", + DisciplineId: "193000", + Semester: "0", + }, + TypeId: "1", + Date: "23.12.2022", + TeacherId: "9999", + } + + actualEvent := transformMessageJsonToEvent(t, LessonCreateEventFormJSON) assert.IsType(t, LessonCreateEvent{}, actualEvent) - event := actualEvent.(LessonCreateEvent) + assert.Equal(t, expectedEvent, actualEvent) + event := actualEvent.(LessonCreateEvent) assert.Equal(t, uint(0), event.GetLessonId()) assert.Equal(t, uint(193000), event.GetDisciplineId()) assert.Equal(t, "0", event.Semester) }) t.Run("Fetch LessonEditEvent", func(t *testing.T) { - actualEvent := tranformMessageJsonToEvent(t, LessonEditEventFormJSON) + expectedEvent := LessonEditEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "999999", + DisciplineId: "193000", + Semester: "0", + }, + TypeId: "1", + Date: "12.12.2022", + TeacherId: "9999", + IsDeleted: false, + } + + actualEvent := transformMessageJsonToEvent(t, LessonEditEventFormJSON) assert.IsType(t, LessonEditEvent{}, actualEvent) - event := actualEvent.(LessonEditEvent) + assert.Equal(t, expectedEvent, actualEvent) + event := actualEvent.(LessonEditEvent) assert.Equal(t, uint(999999), event.GetLessonId()) assert.Equal(t, uint(193000), event.GetDisciplineId()) assert.Equal(t, "0", event.Semester) }) t.Run("Fetch LessonDeletedEvent", func(t *testing.T) { - actualEvent := tranformMessageJsonToEvent(t, LessonDeletedEventFormJSON) + expectedEvent := LessonDeletedEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "00AB0000-0000-0000-0000-000CD0000AA0", + LessonId: "999999", + DisciplineId: "193000", + Semester: "0", + }, + } + + actualEvent := transformMessageJsonToEvent(t, LessonDeletedEventFormJSON) assert.IsType(t, LessonDeletedEvent{}, actualEvent) + assert.Equal(t, expectedEvent, actualEvent) event := actualEvent.(LessonDeletedEvent) assert.Equal(t, uint(999999), event.GetLessonId()) assert.Equal(t, uint(193000), event.GetDisciplineId()) assert.Equal(t, "0", event.Semester) + + fmt.Printf("%#v\n", event) }) t.Run("Fetch ScoreEditEvent", func(t *testing.T) { - actualEvent := tranformMessageJsonToEvent(t, ScoreEditEventFormJSON) + expectedEvent := ScoreEditEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "99FED80A-2E33-40CB-9CEF-01E25B5AA66B", + LessonId: "999999", + DisciplineId: "188619", + Semester: "0", + }, + Date: "18.12.2022", + Scores: map[int]map[uint8]string{ + 110030: { + 1: "", + }, + 110043: { + 2: "", + }, + 110044: { + 1: "-11", + 2: "", + }, + 110054: { + 1: "нб/нп", + 2: "", + }, + 110055: { + 1: "", + 2: "", + }, + }, + } + + actualEvent := transformMessageJsonToEvent(t, ScoreEditEventFormJSON) assert.IsType(t, ScoreEditEvent{}, actualEvent) + assert.Equal(t, expectedEvent, actualEvent) event := actualEvent.(ScoreEditEvent) assert.Equal(t, uint(999999), event.GetLessonId()) @@ -128,6 +208,20 @@ func TestCreateMessageAndToForm(t *testing.T) { assert.Equal(t, "", event.Scores[110054][2]) }) + t.Run("Empty Form", func(t *testing.T) { + messageJson := fmt.Sprintf(EventMessageJSON, "{}") + receiptHandle := "ReceiptHandle" + + message, err := CreateMessage(&messageJson, &receiptHandle) + assert.False(t, message.HasValidForm()) + assert.NoError(t, err) + + event, err := message.ToEvent() + + assert.Error(t, err) + assert.Nil(t, event) + }) + t.Run("Wrong form", func(t *testing.T) { eventFormJSON := strings.Replace(ScoreEditEventFormJSON, `"st`, `"__`, -1) @@ -135,6 +229,7 @@ func TestCreateMessageAndToForm(t *testing.T) { receiptHandle := "ReceiptHandle" message, err := CreateMessage(&messageJson, &receiptHandle) + assert.True(t, message.HasValidForm()) assert.NoError(t, err) event, err := message.ToEvent() @@ -149,6 +244,7 @@ func TestCreateMessageAndToForm(t *testing.T) { message, err := CreateMessage(&messageJson, &receiptHandle) assert.Error(t, err) + assert.False(t, message.HasValidForm()) assert.NotNil(t, message) assert.Equal(t, "ReceiptHandle", *message.ReceiptHandle) @@ -157,11 +253,10 @@ func TestCreateMessageAndToForm(t *testing.T) { }) } -func tranformMessageJsonToEvent(t *testing.T, formJSON string) interface{} { +func transformMessageJsonToEvent(t *testing.T, formJSON string) interface{} { messageJson := fmt.Sprintf(EventMessageJSON, formJSON) - receiptHandle := "ReceiptHandle" - message, err := CreateMessage(&messageJson, &receiptHandle) + message, err := CreateMessage(&messageJson, &testReceiptHandle) assert.NoError(t, err) assert.True(t, message.HasValidForm()) @@ -170,3 +265,15 @@ func tranformMessageJsonToEvent(t *testing.T, formJSON string) interface{} { return event } + +func ExecuteTestMessageToEvent(t *testing.T, originalEvent interface{}, message *Message) { + json := message.ToJson() + + message, err := CreateMessage(json, nil) + assert.NoError(t, err) + + parsedEvent, err := message.ToEvent() + assert.NoError(t, err) + assert.IsType(t, originalEvent, parsedEvent) + assert.Equal(t, originalEvent, parsedEvent) +} diff --git a/ScoreEditEvent.go b/ScoreEditEvent.go index 9b1cd32..f21c66c 100644 --- a/ScoreEditEvent.go +++ b/ScoreEditEvent.go @@ -52,7 +52,7 @@ func createScoreEditEvent(form Form, eventData *CommonEventData) (event ScoreEdi return } -func (event *ScoreEditEvent) ToMessage() Message { +func (event *ScoreEditEvent) ToMessage() *Message { message := EventToMessage(event) message.Form["n"] = strconv.Itoa(ScoreEditFormActionNumber) diff --git a/ScoreEditEvent_test.go b/ScoreEditEvent_test.go new file mode 100644 index 0000000..414bee0 --- /dev/null +++ b/ScoreEditEvent_test.go @@ -0,0 +1,43 @@ +package dekanat_events + +import ( + "testing" +) + +func TestScoreEditEvent_ToMessage(t *testing.T) { + t.Run("TestLessonEditEvent_ToMessage", func(t *testing.T) { + originalEvent := ScoreEditEvent{ + CommonEventData: CommonEventData{ + ReceiptHandle: &testReceiptHandle, + Timestamp: 1673000000, + SessionId: "99FED80A-2E33-40CB-9CEF-01E25B5AA66B", + LessonId: "999999", + DisciplineId: "188619", + Semester: "0", + }, + Date: "18.12.2022", + Scores: map[int]map[uint8]string{ + 110030: { + 1: "", + }, + 110043: { + 2: "", + }, + 110044: { + 1: "-11", + 2: "", + }, + 110054: { + 1: "нб/нп", + 2: "", + }, + 110055: { + 1: "", + 2: "", + }, + }, + } + + ExecuteTestMessageToEvent(t, originalEvent, originalEvent.ToMessage()) + }) +}