eintopf.info@v0.13.16/service/revent/store_sql_test.go (about) 1 // Copyright (C) 2022 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package revent_test 17 18 import ( 19 "context" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/google/go-cmp/cmp/cmpopts" 24 25 "eintopf.info/service/dbmigration" 26 "eintopf.info/service/revent" 27 "eintopf.info/test" 28 ) 29 30 func TestSqlStoreMigrationsMoveOrganizerToNewTable(t *testing.T) { 31 db, cleanup, err := test.CreateSqliteTestDB(t.Name()) 32 if err != nil { 33 t.Fatal(err) 34 } 35 t.Cleanup(cleanup) 36 37 // Create old table with organizer and organizer2 fields 38 _, err = db.Exec(` 39 CREATE TABLE IF NOT EXISTS repeatingEvents ( 40 id varchar(32) NOT NULL PRIMARY KEY UNIQUE, 41 deactivated boolean DEFAULT FALSE, 42 canceled boolean DEFAULT FALSE, 43 name varchar(64) NOT NULL, 44 organizer varchar(64), 45 organizer2 varchar(64), 46 location varchar(64), 47 location2 varchar(64), 48 description varchar(512), 49 start timestamp, 50 end timestamp NULL default NULL, 51 image varchar(128) 52 ); 53 `) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 _, err = db.Exec(` 59 INSERT INTO repeatingEvents ( id, deactivated, canceled, name, organizer, organizer2, location, location2, description, start, end, image) 60 VALUES ( "id-1", false, false, "repeatingEvent-1", "organizer-1", "organizer-2", "location-1", "location-2", "some description", datetime('now'), datetime('now'), "") 61 `) 62 if err != nil { 63 t.Fatal(err) 64 } 65 _, err = db.Exec(` 66 INSERT INTO repeatingEvents ( id, deactivated, canceled, name, organizer, organizer2, location, location2, description, start, end, image) 67 VALUES ( "id-2", false, false, "repeatingEvent-2", "organizer-3", "organizer-4", "location-3", "location-4", "some description 2", datetime('now'), datetime('now'), "") 68 `) 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 migrationStore, err := dbmigration.NewSqlStore(db) 74 if err != nil { 75 t.Fatal(err) 76 } 77 migrationService := dbmigration.NewService(migrationStore) 78 79 store, err := revent.NewSqlStore(db, migrationService) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 // RunMigrations again, to see that they are idempotent 85 _, err = revent.NewSqlStore(db, migrationService) 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 events, _, err := store.Find(context.Background(), nil) 91 if err != nil { 92 t.Fatal(err) 93 } 94 if diff := cmp.Diff([]*revent.RepeatingEvent{ 95 { 96 ID: "id-1", 97 Name: "repeatingEvent-1", 98 Organizers: []string{"organizer-1", "organizer-2"}, 99 Location: "location-1", 100 Location2: "location-2", 101 Description: "some description", 102 Tags: []string{}, 103 OwnedBy: []string{}, 104 Intervals: []revent.Interval{}, 105 }, 106 { 107 ID: "id-2", 108 Name: "repeatingEvent-2", 109 Organizers: []string{"organizer-3", "organizer-4"}, 110 Location: "location-3", 111 Location2: "location-4", 112 Description: "some description 2", 113 Tags: []string{}, 114 OwnedBy: []string{}, 115 Intervals: []revent.Interval{}, 116 }, 117 }, events, cmpopts.IgnoreFields(revent.RepeatingEvent{}, "Start", "End")); diff != "" { 118 t.Errorf("events mismatch (-want +got):\n%s", diff) 119 } 120 }