github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/pkg/buffalotools/database_middeware_test.go (about)

     1  //go:build sqlite
     2  // +build sqlite
     3  
     4  package buffalotools_test
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/gobuffalo/buffalo"
    12  	"github.com/gobuffalo/httptest"
    13  	"github.com/gobuffalo/pop/v6"
    14  	"github.com/wawandco/ox/pkg/buffalotools"
    15  )
    16  
    17  var deets = []*pop.ConnectionDetails{
    18  	{Dialect: "sqlite3", URL: "sqlite://rodb.db?_busy_timeout=5000&_fk=true"},
    19  	{Dialect: "sqlite3", URL: "sqlite://tx.db?_busy_timeout=5000&_fk=true"},
    20  }
    21  
    22  func TestDatabase(t *testing.T) {
    23  	conns := map[string]*pop.Connection{}
    24  	for _, cc := range deets {
    25  		conn, err := pop.NewConnection(cc)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  
    30  		conns[conn.Dialect.Details().Database] = conn
    31  	}
    32  
    33  	for _, conn := range conns {
    34  		err := pop.CreateDB(conn)
    35  		if err != nil {
    36  			t.Fatal(err)
    37  		}
    38  
    39  		err = conn.Open()
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  	}
    44  
    45  	t.Cleanup(func() {
    46  		for _, v := range conns {
    47  			v.Close()
    48  			err := pop.DropDB(v)
    49  			if err != nil {
    50  				t.Fatal(err)
    51  			}
    52  		}
    53  	})
    54  
    55  	t.Run("Nil Connections", func(t *testing.T) {
    56  		app := buffalo.New(buffalo.Options{})
    57  
    58  		var conn *pop.Connection
    59  		hnd := func(c buffalo.Context) error {
    60  			conn = c.Value("tx").(*pop.Connection)
    61  
    62  			return nil
    63  		}
    64  
    65  		app.Use(buffalotools.DatabaseMiddleware(nil, nil))
    66  		app.GET("/", hnd)
    67  		app.POST("/", hnd)
    68  
    69  		tt := httptest.New(app)
    70  		r := tt.HTML("/").Get()
    71  
    72  		if r.Code != http.StatusInternalServerError {
    73  			t.Errorf("expected %d, got %d", http.StatusInternalServerError, r.Code)
    74  		}
    75  
    76  		if conn != nil {
    77  			t.Errorf("expected nil, got %v", conn)
    78  		}
    79  	})
    80  
    81  	t.Run("With one connection", func(t *testing.T) {
    82  		app := buffalo.New(buffalo.Options{})
    83  		app.Middleware.Clear()
    84  		app.Use(buffalotools.DatabaseMiddleware(conns["tx.db"], nil))
    85  
    86  		var conn *pop.Connection
    87  		hnd := func(c buffalo.Context) error {
    88  			conn = c.Value("tx").(*pop.Connection)
    89  
    90  			return nil
    91  		}
    92  
    93  		app.GET("/", hnd)
    94  		app.POST("/", hnd)
    95  
    96  		tt := httptest.New(app)
    97  		r := tt.HTML("/").Get()
    98  
    99  		if r.Code != http.StatusOK {
   100  			t.Errorf("expected %d, got %d", http.StatusOK, r.Code)
   101  		}
   102  
   103  		if "sqlite3" != conn.Dialect.Name() {
   104  			t.Errorf("expected %s, got %s", "sqlite3", conn.Dialect)
   105  		}
   106  
   107  		r = tt.HTML("/").Post(nil)
   108  
   109  		if r.Code != http.StatusOK {
   110  			t.Errorf("expected %d, got %d", http.StatusOK, r.Code)
   111  		}
   112  
   113  		if "sqlite3" != conn.Dialect.Name() {
   114  			t.Errorf("expected %s, got %s", "sqlite3", conn.Dialect)
   115  		}
   116  	})
   117  
   118  	t.Run("With rodb connection", func(t *testing.T) {
   119  
   120  		app := buffalo.New(buffalo.Options{})
   121  		app.Middleware.Clear()
   122  		app.Use(buffalotools.DatabaseMiddleware(conns["tx.db"], conns["rodb.db"]))
   123  
   124  		var conn *pop.Connection
   125  		hnd := func(c buffalo.Context) error {
   126  			conn = c.Value("tx").(*pop.Connection)
   127  
   128  			return nil
   129  		}
   130  
   131  		app.GET("/", hnd)
   132  		app.POST("/", hnd)
   133  
   134  		tt := httptest.New(app)
   135  		r := tt.HTML("/").Get()
   136  
   137  		if r.Code != http.StatusOK {
   138  			t.Errorf("expected %d, got %d", http.StatusOK, r.Code)
   139  		}
   140  
   141  		if "sqlite3" != conn.Dialect.Name() {
   142  			t.Errorf("expected %s, got %s", "sqlite3", conn.Dialect)
   143  		}
   144  
   145  		if !strings.Contains(conn.Dialect.URL(), "rodb.db") {
   146  			t.Errorf("expected %s to contain rodb.db but it did not", conn.Dialect.URL())
   147  		}
   148  
   149  		r = tt.HTML("/").Post(nil)
   150  
   151  		if r.Code != http.StatusOK {
   152  			t.Errorf("expected %d, got %d", http.StatusOK, r.Code)
   153  		}
   154  
   155  		if !strings.Contains(conn.Dialect.URL(), "tx.db") {
   156  			t.Errorf("expected %s to contain tx.db but it did not", conn.Dialect.URL())
   157  		}
   158  	})
   159  
   160  }