package mapping import ( "encoding/json" "testing" "time" "github.com/moustachioed/dash/backend/dashapi" "github.com/moustachioed/dash/backend/database/models" "gorm.io/datatypes" ) func TestJournalApiToDbFullObject(t *testing.T) { mapper := NewMapperImpl() api := dashapi.JournalEntry{ Date: "2022-02-18", Thankful: []string{"ttest1", "ttest2", "ttest3"}, LookingForward: []string{"lftest1", "lftest2", "lftest3"}, BeenGreat: []string{"bgtest1", "bgtest2", "bgtest3"}, DoBetter: []string{"dbtest1", "dbtest2", "dbtest3"}, Journal: "jtest", } db := mapper.JournalApiToDb(api).(models.Journal) gotDate, _ := db.Date.Value() got := gotDate.(time.Time).Format("2006-01-02") wantDate := api.Date if got != wantDate { t.Errorf("Mapped string %s not equal want string %s", got, wantDate) } got = db.Thankful.String() want, _ := json.Marshal(api.Thankful) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.LookingForward.String() want, _ = json.Marshal(api.LookingForward) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.BeenGreat.String() want, _ = json.Marshal(api.BeenGreat) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.DoBetter.String() want, _ = json.Marshal(api.DoBetter) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.Journal wantstring := api.Journal if got != string(wantstring) { t.Errorf("Mapped string %s not equal want string %s", got, wantstring) } } func TestJournalApiToDbPartialObject(t *testing.T) { mapper := NewMapperImpl() api := dashapi.JournalEntry{ Date: "2022-02-18", Thankful: nil, LookingForward: []string{"lftest1", "lftest2", "lftest3"}, BeenGreat: nil, DoBetter: []string{"dbtest1", "dbtest2", "dbtest3"}, Journal: "", } db := mapper.JournalApiToDb(api).(models.Journal) gotDate, _ := db.Date.Value() got := gotDate.(time.Time).Format("2006-01-02") wantDate := api.Date if got != wantDate { t.Errorf("Mapped string %s not equal want string %s", got, wantDate) } got = db.Thankful.String() want, _ := json.Marshal(api.Thankful) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.LookingForward.String() want, _ = json.Marshal(api.LookingForward) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.BeenGreat.String() want, _ = json.Marshal(api.BeenGreat) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.DoBetter.String() want, _ = json.Marshal(api.DoBetter) if got != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got = db.Journal wantstring := api.Journal if got != string(wantstring) { t.Errorf("Mapped string %s not equal want string %s", got, wantstring) } } func TestJournalDbToApiFullObject(t *testing.T) { mapper := NewMapperImpl() date, _ := time.Parse("2006-01-02", "2022-02-18") thankful, _ := json.Marshal([]string{"ttest1", "ttest2", "ttest3"}) lookingForward, _ := json.Marshal([]string{"lftest1", "lftest2", "lftest3"}) beenGreat, _ := json.Marshal([]string{"bgtest1", "bgtest2", "bgtest3"}) doBetter, _ := json.Marshal([]string{"dbtest1", "dbtest2", "test3"}) journal := "jtest" db := models.Journal{ Date: datatypes.Date(date), Thankful: datatypes.JSON(thankful), LookingForward: datatypes.JSON(lookingForward), BeenGreat: datatypes.JSON(beenGreat), DoBetter: datatypes.JSON(doBetter), Journal: journal, } api := mapper.JournalDbToApi(db).(dashapi.JournalEntry) got, _ := json.Marshal(api.Date) wantDate := "\"2022-02-18\"" if string(got) != wantDate { t.Errorf("Mapped string %s not equal want string %s", got, wantDate) } got, _ = json.Marshal(api.Thankful) want := thankful if string(got) != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got, _ = json.Marshal(api.LookingForward) want = lookingForward if string(got) != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got, _ = json.Marshal(api.BeenGreat) want = beenGreat if string(got) != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got, _ = json.Marshal(api.DoBetter) want = doBetter if string(got) != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } gotJournal := api.Journal wantJournal := journal if gotJournal != wantJournal { t.Errorf("Mapped string %s not equal want string %s", gotJournal, wantJournal) } } func TestJournalDbToApiPartialObject(t *testing.T) { mapper := NewMapperImpl() date, _ := time.Parse("2006-01-02", "2022-02-18") var thankful []byte = nil lookingForward, _ := json.Marshal([]string{"lftest1", "lftest2", "lftest3"}) var beenGreat []byte = nil doBetter, _ := json.Marshal([]string{"dbtest1", "dbtest2", "test3"}) journal := "jtest" db := models.Journal{ Date: datatypes.Date(date), Thankful: datatypes.JSON(thankful), LookingForward: datatypes.JSON(lookingForward), BeenGreat: datatypes.JSON(beenGreat), DoBetter: datatypes.JSON(doBetter), Journal: journal, } api := mapper.JournalDbToApi(db).(dashapi.JournalEntry) got, _ := json.Marshal(api.Date) wantDate := "\"2022-02-18\"" if string(got) != wantDate { t.Errorf("Mapped string %s not equal want string %s", got, wantDate) } got, _ = json.Marshal(api.Thankful) wantString := "null" if string(got) != wantString { t.Errorf("Mapped string %s not equal want string %s", got, wantString) } got, _ = json.Marshal(api.LookingForward) want := lookingForward if string(got) != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } got, _ = json.Marshal(api.BeenGreat) wantString = "null" if string(got) != wantString { t.Errorf("Mapped string %s not equal want string %s", got, wantString) } got, _ = json.Marshal(api.DoBetter) want = doBetter if string(got) != string(want) { t.Errorf("Mapped string %s not equal want string %s", got, want) } gotJournal := api.Journal wantJournal := journal if gotJournal != wantJournal { t.Errorf("Mapped string %s not equal want string %s", gotJournal, wantJournal) } }