1/*
2Copyright 2017 Google LLC
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package spanner
18
19import (
20	"strings"
21	"testing"
22)
23
24// Test validDatabaseName()
25func TestValidDatabaseName(t *testing.T) {
26	validDbURI := "projects/spanner-cloud-test/instances/foo/databases/foodb"
27	invalidDbUris := []string{
28		// Completely wrong DB URI.
29		"foobarDB",
30		// Project ID contains "/".
31		"projects/spanner-cloud/test/instances/foo/databases/foodb",
32		// No instance ID.
33		"projects/spanner-cloud-test/instances//databases/foodb",
34	}
35	if err := validDatabaseName(validDbURI); err != nil {
36		t.Errorf("validateDatabaseName(%q) = %v, want nil", validDbURI, err)
37	}
38	for _, d := range invalidDbUris {
39		if err, wantErr := validDatabaseName(d), "should conform to pattern"; !strings.Contains(err.Error(), wantErr) {
40			t.Errorf("validateDatabaseName(%q) = %q, want error pattern %q", validDbURI, err, wantErr)
41		}
42	}
43}
44
45func TestReadOnlyTransactionClose(t *testing.T) {
46	// Closing a ReadOnlyTransaction shouldn't panic.
47	c := &Client{}
48	tx := c.ReadOnlyTransaction()
49	tx.Close()
50}
51