Skip to content

Commit dea2c67

Browse files
committed
tests(Spanner): Directly test ManagedSessionOptions
1 parent e9dc04f commit dea2c67

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"):
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Google.Cloud.Spanner.Common.V1;
16+
using NSubstitute;
17+
using System;
18+
using Xunit;
19+
20+
namespace Google.Cloud.Spanner.V1.Tests;
21+
22+
public class ManagedSessionOptionsTests
23+
{
24+
private static readonly DatabaseName SampleDatabaseName = new DatabaseName("project", "instance", "database");
25+
private readonly SpannerClient _mockClient = Substitute.For<SpannerClient>();
26+
27+
[Fact]
28+
public void Create_ValidatesParameters()
29+
{
30+
Assert.Throws<ArgumentNullException>(() => ManagedSessionOptions.Create(null, _mockClient));
31+
Assert.Throws<ArgumentNullException>(() => ManagedSessionOptions.Create(SampleDatabaseName, null));
32+
}
33+
34+
[Fact]
35+
public void Create_DefaultValues()
36+
{
37+
var options = ManagedSessionOptions.Create(SampleDatabaseName, _mockClient);
38+
Assert.Equal(SampleDatabaseName, options.DatabaseName);
39+
Assert.Same(_mockClient, options.Client);
40+
Assert.Null(options.DatabaseRole);
41+
Assert.Null(options.Timeout);
42+
Assert.Equal(TimeSpan.FromMinutes(1), options.EffectiveTimeout);
43+
}
44+
45+
[Fact]
46+
public void WithDatabaseRole()
47+
{
48+
var options = ManagedSessionOptions.Create(SampleDatabaseName, _mockClient)
49+
.WithTimeout(TimeSpan.FromSeconds(10));
50+
var optionsWithRole = options.WithDatabaseRole("new-role");
51+
52+
Assert.NotSame(options, optionsWithRole);
53+
Assert.Equal("new-role", optionsWithRole.DatabaseRole);
54+
// Verify other properties are preserved
55+
Assert.Equal(SampleDatabaseName, optionsWithRole.DatabaseName);
56+
Assert.Same(_mockClient, optionsWithRole.Client);
57+
Assert.Equal(TimeSpan.FromSeconds(10), optionsWithRole.Timeout);
58+
59+
// Verify original options are unchanged
60+
Assert.Null(options.DatabaseRole);
61+
Assert.Equal(TimeSpan.FromSeconds(10), options.Timeout);
62+
}
63+
64+
[Fact]
65+
public void WithTimeout_ValidatesParameters()
66+
{
67+
var options = ManagedSessionOptions.Create(SampleDatabaseName, _mockClient);
68+
Assert.Throws<ArgumentOutOfRangeException>(() => options.WithTimeout(TimeSpan.Zero));
69+
Assert.Throws<ArgumentOutOfRangeException>(() => options.WithTimeout(TimeSpan.FromSeconds(-1)));
70+
}
71+
72+
[Fact]
73+
public void WithTimeout()
74+
{
75+
var options = ManagedSessionOptions.Create(SampleDatabaseName, _mockClient)
76+
.WithDatabaseRole("role");
77+
var timeout = TimeSpan.FromSeconds(30);
78+
var optionsWithTimeout = options.WithTimeout(timeout);
79+
80+
Assert.NotSame(options, optionsWithTimeout);
81+
Assert.Equal(timeout, optionsWithTimeout.Timeout);
82+
Assert.Equal(timeout, optionsWithTimeout.EffectiveTimeout);
83+
// Verify other properties are preserved
84+
Assert.Equal(SampleDatabaseName, optionsWithTimeout.DatabaseName);
85+
Assert.Same(_mockClient, optionsWithTimeout.Client);
86+
Assert.Equal("role", optionsWithTimeout.DatabaseRole);
87+
88+
// Verify original options are unchanged
89+
Assert.Null(options.Timeout);
90+
Assert.Equal("role", options.DatabaseRole);
91+
}
92+
93+
[Fact]
94+
public void WithTimeout_Null()
95+
{
96+
var options = ManagedSessionOptions.Create(SampleDatabaseName, _mockClient)
97+
.WithDatabaseRole("role")
98+
.WithTimeout(TimeSpan.FromSeconds(30));
99+
var optionsWithNullTimeout = options.WithTimeout(null);
100+
101+
Assert.NotSame(options, optionsWithNullTimeout);
102+
Assert.Null(optionsWithNullTimeout.Timeout);
103+
Assert.Equal(TimeSpan.FromMinutes(1), optionsWithNullTimeout.EffectiveTimeout);
104+
// Verify other properties are preserved
105+
Assert.Equal(SampleDatabaseName, optionsWithNullTimeout.DatabaseName);
106+
Assert.Same(_mockClient, optionsWithNullTimeout.Client);
107+
Assert.Equal("role", optionsWithNullTimeout.DatabaseRole);
108+
109+
// Verify original options are unchanged
110+
Assert.Equal(TimeSpan.FromSeconds(30), options.Timeout);
111+
Assert.Equal("role", options.DatabaseRole);
112+
}
113+
}

0 commit comments

Comments
 (0)