Skip to content

Commit

Permalink
Make test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Sep 20, 2023
1 parent 7c1eb64 commit ef33fac
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 28 deletions.
8 changes: 5 additions & 3 deletions CommonEventData.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -51,5 +53,5 @@ func EventToMessage(event interface{}) (message Message) {
}
}

return
return message
}
23 changes: 23 additions & 0 deletions CommonEventData_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
2 changes: 1 addition & 1 deletion LessonCreateEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions LessonCreateEvent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion LessonDeletedEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions LessonDeletedEvent_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
4 changes: 2 additions & 2 deletions LessonEditEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions LessonEditEvent_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
127 changes: 117 additions & 10 deletions Message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -128,13 +208,28 @@ 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)

messageJson := fmt.Sprintf(EventMessageJSON, eventFormJSON)
receiptHandle := "ReceiptHandle"

message, err := CreateMessage(&messageJson, &receiptHandle)
assert.True(t, message.HasValidForm())
assert.NoError(t, err)

event, err := message.ToEvent()
Expand All @@ -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)

Expand All @@ -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())
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion ScoreEditEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading

0 comments on commit ef33fac

Please sign in to comment.