File: dev_test.go

package info (click to toggle)
golang-ariga-atlas 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,676 kB
  • sloc: javascript: 592; sql: 404; makefile: 10
file content (59 lines) | stat: -rw-r--r-- 1,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package sqlx

import (
	"context"
	"strings"
	"testing"

	"ariga.io/atlas/sql/migrate"
	"ariga.io/atlas/sql/schema"

	"github.com/stretchr/testify/require"
)

func TestDriver_NormalizeRealm(t *testing.T) {
	var (
		drv = &mockDriver{
			realm: schema.NewRealm(schema.New("test").SetCharset("utf8mb4")),
		}
		dev = &DevDriver{
			Driver:     drv,
			MaxNameLen: 64,
		}
	)
	normal, err := dev.NormalizeRealm(context.Background(), schema.NewRealm(schema.New("test")))
	require.NoError(t, err)
	require.Equal(t, normal, drv.realm)

	require.Len(t, drv.schemas, 1)
	require.True(t, strings.HasPrefix(drv.schemas[0], "atlas_dev_test_"))

	require.Len(t, drv.changes, 2, "expect 2 calls (create and drop)")
	require.Len(t, drv.changes[0], 1)
	require.Equal(t, &schema.AddSchema{S: schema.New(drv.schemas[0])}, drv.changes[0][0])
	require.Len(t, drv.changes[1], 1)
	require.Equal(t, &schema.DropSchema{S: schema.New(drv.schemas[0]), Extra: []schema.Clause{&schema.IfExists{}}}, drv.changes[1][0])
}

type mockDriver struct {
	migrate.Driver
	// Inspect.
	schemas []string
	realm   *schema.Realm
	// Apply.
	changes [][]schema.Change
}

func (m *mockDriver) InspectRealm(_ context.Context, opts *schema.InspectRealmOption) (*schema.Realm, error) {
	m.schemas = append(m.schemas, opts.Schemas...)
	return m.realm, nil
}

func (m *mockDriver) ApplyChanges(_ context.Context, changes []schema.Change, _ ...migrate.PlanOption) error {
	m.changes = append(m.changes, changes)
	return nil
}