github.com/woocoos/entco@v0.0.0-20240411071658-1e7b23d4df15/ecx/route_drvier_test.go (about)

     1  package ecx
     2  
     3  import (
     4  	"context"
     5  	"entgo.io/ent/dialect"
     6  	"entgo.io/ent/dialect/sql"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/tsingsun/woocoo/pkg/conf"
     9  	"github.com/tsingsun/woocoo/pkg/store/sqlx"
    10  	"github.com/woocoos/casbin-ent-adapter/ent"
    11  	"github.com/woocoos/entco/pkg/identity"
    12  	"testing"
    13  
    14  	_ "github.com/mattn/go-sqlite3"
    15  )
    16  
    17  func TestNewRouteDriver(t *testing.T) {
    18  	cfg := conf.NewFromStringMap(map[string]any{
    19  		"store": map[string]any{
    20  			"portal": map[string]any{
    21  				"driverName": "sqlite3",
    22  				"dsn":        ":memory:",
    23  				"multiInstances": map[string]any{
    24  					"test.com": map[string]any{
    25  						"driverName": "sqlite3",
    26  						"dsn":        ":memory:",
    27  					},
    28  					"test.cn": map[string]any{
    29  						"driverName": "sqlite3",
    30  						"dsn":        ":memory:",
    31  					},
    32  				},
    33  			},
    34  		},
    35  	})
    36  	type args struct {
    37  		cfg *conf.Configuration
    38  	}
    39  	tests := []struct {
    40  		name  string
    41  		args  args
    42  		check func(driver *RouteDriver)
    43  	}{
    44  		{
    45  			name: "multi",
    46  			args: args{cfg: cfg.Sub("store.portal")},
    47  			check: func(driver *RouteDriver) {
    48  				assert.Equal(t, 2, len(driver.dbRules))
    49  			},
    50  		},
    51  		{
    52  			name: "from-bytes",
    53  			args: args{
    54  				cfg: conf.NewFromBytes([]byte(`
    55  store:
    56    portal:
    57      driverName: sqlite3
    58      dsn: ":memory:"
    59      multiInstances:
    60        test.com:
    61          driverName: sqlite3
    62          dsn: ":memory:"
    63  `)).Sub("store.portal"),
    64  			},
    65  			check: func(driver *RouteDriver) {
    66  				assert.Equal(t, 1, len(driver.dbRules))
    67  			},
    68  		},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			got := NewRouteDriver(tt.args.cfg)
    73  			tt.check(got)
    74  		})
    75  	}
    76  }
    77  
    78  func TestMultiInstances(t *testing.T) {
    79  	pcfg := conf.NewFromStringMap(map[string]any{
    80  		"store": map[string]any{
    81  			"portal": map[string]any{
    82  				"driverName": "sqlite3",
    83  				"dsn":        "file:cashbin?mode=memory&cache=shared&_fk=1",
    84  				"multiInstances": map[string]any{
    85  					"1": map[string]any{
    86  						"driverName": "sqlite3",
    87  						"dsn":        "file:cashbin?mode=memory&cache=shared&_fk=1",
    88  					},
    89  				},
    90  			},
    91  		},
    92  	}).Sub("store.portal")
    93  	var pd dialect.Driver
    94  	if pcfg.IsSet("multiInstances") {
    95  		pd = NewRouteDriver(pcfg)
    96  	} else {
    97  		pd = sql.OpenDB(pcfg.String("driverName"), sqlx.NewSqlDB(pcfg))
    98  	}
    99  	client := ent.NewClient(ent.Driver(pd))
   100  	ctx := identity.WithTenantID(context.Background(), 1)
   101  	err := client.Schema.Create(ctx)
   102  	assert.NoError(t, err)
   103  }