eintopf.info@v0.13.16/service/event/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 event_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"github.com/google/go-cmp/cmp/cmpopts"
    25  
    26  	"eintopf.info/service/dbmigration"
    27  	"eintopf.info/service/event"
    28  	"eintopf.info/service/event/testutil"
    29  	"eintopf.info/test"
    30  )
    31  
    32  func TestSqlStore(t *testing.T) {
    33  	testutil.TestStore(t, func() (event.Storer, func(), error) {
    34  		db, cleanup, err := test.CreateSqliteTestDB(t.Name())
    35  		if err != nil {
    36  			return nil, cleanup, err
    37  		}
    38  
    39  		migrationStore, err := dbmigration.NewSqlStore(db)
    40  		if err != nil {
    41  			return nil, cleanup, fmt.Errorf("failed to create migration store: %s", err)
    42  		}
    43  		migrationService := dbmigration.NewService(migrationStore)
    44  
    45  		store, err := event.NewSqlStore(db, migrationService)
    46  		if err != nil {
    47  			return nil, cleanup, err
    48  		}
    49  		return store, cleanup, nil
    50  	})
    51  }
    52  
    53  func TestSqlStoreMigrationsMoveOrganizerToNewTable(t *testing.T) {
    54  	db, cleanup, err := test.CreateSqliteTestDB(t.Name())
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	t.Cleanup(cleanup)
    59  
    60  	// Create old table with organizer and organizer2 fields
    61  	_, err = db.Exec(`
    62          CREATE TABLE IF NOT EXISTS events (
    63              id varchar(32) NOT NULL PRIMARY KEY UNIQUE,
    64              published boolean DEFAULT FALSE,
    65              deactivated boolean DEFAULT FALSE,
    66              canceled boolean DEFAULT FALSE,
    67              parent varchar(64) DEFAULT "",
    68              parentListed boolean DEFAULT FALSE,
    69              name varchar(64) NOT NULL,
    70              organizer varchar(64),
    71              organizer2 varchar(64),
    72              location varchar(64),
    73              location2 varchar(64),
    74              description varchar(512),
    75              start timestamp,
    76              end timestamp NULL default NULL,
    77              image varchar(128)
    78          );
    79      `)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	_, err = db.Exec(`
    85          INSERT INTO events ( id, published, deactivated, canceled, parent, parentListed, name, organizer, organizer2, location, location2, description, start, end, image)
    86          VALUES ( "id-1", true, false, false, "", false, "event-1", "organizer-1", "organizer-2", "location-1", "location-2", "some description", datetime('now'), datetime('now'), "")
    87      `)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	_, err = db.Exec(`
    92          INSERT INTO events ( id, published, deactivated, canceled, parent, parentListed, name, organizer, organizer2, location, location2, description, start, end, image)
    93          VALUES ( "id-2", true, false, false, "", false, "event-2", "organizer-3", "organizer-4", "location-3", "location-4", "some description 2", datetime('now'), datetime('now'), "")
    94      `)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	migrationStore, err := dbmigration.NewSqlStore(db)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	migrationService := dbmigration.NewService(migrationStore)
   103  	store, err := event.NewSqlStore(db, migrationService)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// RunMigrations again, to see that they are idempotent
   109  	_, err = event.NewSqlStore(db, migrationService)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	events, _, err := store.Find(context.Background(), nil)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	if diff := cmp.Diff([]*event.Event{
   119  
   120  		{
   121  			ID:          "id-1",
   122  			Published:   true,
   123  			Name:        "event-1",
   124  			Organizers:  []string{"organizer-1", "organizer-2"},
   125  			Involved:    []event.Involved{},
   126  			Location:    "location-1",
   127  			Location2:   "location-2",
   128  			Description: "some description",
   129  			Tags:        []string{},
   130  			OwnedBy:     []string{},
   131  		},
   132  		{
   133  			ID:          "id-2",
   134  			Published:   true,
   135  			Name:        "event-2",
   136  			Organizers:  []string{"organizer-3", "organizer-4"},
   137  			Involved:    []event.Involved{},
   138  			Location:    "location-3",
   139  			Location2:   "location-4",
   140  			Description: "some description 2",
   141  			Tags:        []string{},
   142  			OwnedBy:     []string{},
   143  		},
   144  	}, events, cmpopts.IgnoreFields(event.Event{}, "Start", "End")); diff != "" {
   145  		t.Errorf("events mismatch (-want +got):\n%s", diff)
   146  	}
   147  }