Skip to content

Commit

Permalink
Clean up PR dotnet#23091
Browse files Browse the repository at this point in the history
  • Loading branch information
bricelam committed Dec 8, 2020
1 parent 8e07a9c commit fc43d5a
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 88 deletions.
7 changes: 1 addition & 6 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ public override string ConnectionString

_connectionString = value ?? string.Empty;
_connectionOptions = null;

if (ConnectionOptions.DefaultTimeout.HasValue)
{
DefaultTimeout = ConnectionOptions.DefaultTimeout.Value;
}
}
}

Expand Down Expand Up @@ -135,7 +130,7 @@ public override string DataSource
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public virtual int DefaultTimeout
{
get => _defaultTimeout ?? 30;
get => _defaultTimeout ?? ConnectionOptions.DefaultTimeout;
set => _defaultTimeout = value;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private enum Keywords
private string _password = string.Empty;
private bool? _foreignKeys;
private bool _recursiveTriggers;
private int? _defaultTimeout;
private int _defaultTimeout = 30;

static SqliteConnectionStringBuilder()
{
Expand Down Expand Up @@ -193,10 +193,10 @@ public bool RecursiveTriggers
}

/// <summary>
/// Gets or sets a value indicating the Default Connection Timeout.
/// Gets or sets the default <see cref="SqliteConnection.DefaultTimeout" /> value.
/// </summary>
/// <value>The Default Connection Timeout in seconds.</value>
public int? DefaultTimeout
/// <value>The default <see cref="SqliteConnection.DefaultTimeout" /> value.</value>
public int DefaultTimeout
{
get => _defaultTimeout;
set => base[DefaultTimeoutKeyword] = _defaultTimeout = value;
Expand Down Expand Up @@ -436,7 +436,7 @@ private void Reset(Keywords index)
return;

case Keywords.DefaultTimeout:
_defaultTimeout = null;
_defaultTimeout = 30;
return;

default:
Expand Down
21 changes: 0 additions & 21 deletions test/EFCore.Sqlite.Tests/Storage/SqliteRelationalConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,5 @@ public void Sets_DefaultTimeout_when_connection()
Assert.Same(originalConnection, connection);
Assert.Equal(42, originalConnection.DefaultTimeout);
}

[Fact]
public void Sets_DefaultTimeout_when_connection_overrides_connection_string()
{
var originalConnection = new SqliteConnection("Data Source=:memory:;Default Timeout=50") { DefaultTimeout = 21 };
Assert.Equal(21, originalConnection.DefaultTimeout);

var services = SqliteTestHelpers.Instance.CreateContextServices(
new DbContextOptionsBuilder()
.UseSqlite(originalConnection, x => x.CommandTimeout(42))
.Options);

Assert.Equal(42, originalConnection.DefaultTimeout);
}

[Fact]
public void Sets_DefaultTimeout_when_connection_string()
{
var originalConnection = new SqliteConnection("Data Source=:memory:;Default Timeout=50");
Assert.Equal(50, originalConnection.DefaultTimeout);
}
}
}
50 changes: 48 additions & 2 deletions test/Microsoft.Data.Sqlite.Tests/SqliteCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void Ctor_sets_values()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.DefaultTimeout = 1;
connection.Open();

using (var transaction = connection.BeginTransaction())
Expand All @@ -29,7 +28,6 @@ public void Ctor_sets_values()

Assert.Equal("SELECT 1;", command.CommandText);
Assert.Same(connection, command.Connection);
Assert.Equal(1, command.CommandTimeout);
Assert.Same(transaction, command.Transaction);
}
}
Expand Down Expand Up @@ -75,6 +73,54 @@ public void CommandText_throws_when_set_when_open_reader()
}
}

[Fact]
public void CommandTimeout_works()
{
var command = new SqliteCommand
{
Connection = new SqliteConnection("Command Timeout=1")
{
DefaultTimeout = 2
},
CommandTimeout = 3
};

Assert.Equal(3, command.CommandTimeout);
}

[Fact]
public void CommandTimeout_defaults_to_connection()
{
var command = new SqliteCommand
{
Connection = new SqliteConnection("Default Timeout=1")
{
DefaultTimeout = 2
}
};

Assert.Equal(2, command.CommandTimeout);
}

[Fact]
public void CommandTimeout_defaults_to_connection_string()
{
var command = new SqliteCommand
{
Connection = new SqliteConnection("Default Timeout=1")
};

Assert.Equal(1, command.CommandTimeout);
}

[Fact]
public void CommandTimeout_defaults_to_30()
{
var command = new SqliteCommand();

Assert.Equal(30, command.CommandTimeout);
}

[Fact]
public void Connection_can_be_unset()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ namespace Microsoft.Data.Sqlite
{
public class SqliteConnectionStringBuilderTest
{
[Fact]
public void Ctor_parses_options()
[Theory]
[InlineData("Data Source")]
[InlineData("Filename")]
[InlineData("DataSource")]
public void Ctor_parses_DataSource(string keyword)
{
var builder = new SqliteConnectionStringBuilder("Data Source=test.db");
var builder = new SqliteConnectionStringBuilder($"{keyword}=test.db");

Assert.Equal("test.db", builder.DataSource);
}
Expand All @@ -33,6 +36,40 @@ public void Ctor_parses_mode()
Assert.Equal(SqliteOpenMode.Memory, builder.Mode);
}

[Fact]
public void Ctor_parses_Password()
{
var builder = new SqliteConnectionStringBuilder("Password=key");

Assert.Equal("key", builder.Password);
}

[Fact]
public void Ctor_parses_ForeignKeys()
{
var builder = new SqliteConnectionStringBuilder("Foreign Keys=True");

Assert.True(builder.ForeignKeys);
}

[Fact]
public void Ctor_parses_RecursiveTriggers()
{
var builder = new SqliteConnectionStringBuilder("Recursive Triggers=True");

Assert.True(builder.RecursiveTriggers);
}

[Theory]
[InlineData("Default Timeout")]
[InlineData("Command Timeout")]
public void Ctor_parses_DefaultTimeout(string keyword)
{
var builder = new SqliteConnectionStringBuilder($"{keyword}=1");

Assert.Equal(1, builder.DefaultTimeout);
}

[Fact]
public void ConnectionString_defaults_to_empty()
{
Expand Down Expand Up @@ -111,6 +148,12 @@ public void Password_defaults_to_empty()
Assert.Empty(new SqliteConnectionStringBuilder().Password);
}

[Fact]
public void DefaultTimeout_defaults_to_30()
{
Assert.Equal(30, new SqliteConnectionStringBuilder().DefaultTimeout);
}

[Fact]
public void Keys_works()
{
Expand Down Expand Up @@ -232,32 +275,11 @@ public void Item_throws_when_cannot_convert_to_bool_on_set(object value)
Assert.ThrowsAny<FormatException>(() => builder["Foreign Keys"] = value);
}

[Theory]
[InlineData("250", 250)]
public void Item_converts_to_int_on_set(object value, int? expected)
{
var builder = new SqliteConnectionStringBuilder();

builder["Default Timeout"] = value;

Assert.Equal(expected, builder["Default Timeout"]);
}

[Theory]
[InlineData("test")]
[InlineData("Unknown")]
public void Item_throws_when_cannot_convert_to_int_on_set(object value)
{
var builder = new SqliteConnectionStringBuilder();

Assert.ThrowsAny<FormatException>(() => builder["Default Timeout"] = value);
}

[Fact]
public void Clear_resets_everything()
{
var builder = new SqliteConnectionStringBuilder(
"Data Source=test.db;Mode=Memory;Cache=Shared;Password=test;Foreign Keys=True;Recursive Triggers=True");
"Data Source=test.db;Mode=Memory;Cache=Shared;Password=test;Foreign Keys=True;Recursive Triggers=True;Default Timeout=1");

builder.Clear();

Expand All @@ -267,6 +289,7 @@ public void Clear_resets_everything()
Assert.Empty(builder.Password);
Assert.Null(builder.ForeignKeys);
Assert.False(builder.RecursiveTriggers);
Assert.Equal(30, builder.DefaultTimeout);
}

[Fact]
Expand Down Expand Up @@ -348,11 +371,12 @@ public void ToString_builds_string()
Mode = SqliteOpenMode.Memory,
Password = "test",
ForeignKeys = true,
RecursiveTriggers = true
RecursiveTriggers = true,
DefaultTimeout = 1
};

Assert.Equal(
"Data Source=test.db;Mode=Memory;Cache=Shared;Password=test;Foreign Keys=True;Recursive Triggers=True",
"Data Source=test.db;Mode=Memory;Cache=Shared;Password=test;Foreign Keys=True;Recursive Triggers=True;Default Timeout=1",
builder.ToString());
}

Expand Down
40 changes: 13 additions & 27 deletions test/Microsoft.Data.Sqlite.Tests/SqliteConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,24 @@ public void DefaultTimeout_defaults_to_30()
}

[Fact]
public void DefaultTimeout_works()
public void DefaultTimeout_defaults_to_connection_string()
{
var connection = new SqliteConnection();
connection.DefaultTimeout = 1;
var connection = new SqliteConnection("Default Timeout=1");

Assert.Equal(1, connection.DefaultTimeout);
}

[Fact]
public void DefaultTimeout_works()
{
var connection = new SqliteConnection("Default Timeout=1")
{
DefaultTimeout = 2
};

Assert.Equal(2, connection.DefaultTimeout);
}

[Fact]
public void ServerVersion_returns_value()
{
Expand Down Expand Up @@ -470,28 +480,6 @@ public void Open_works_when_uri()
}
}

[Fact]
public void Open_works_when_default_timeout()
{
using (var connection = new SqliteConnection("Data Source=:memory:;Default Timeout=100"))
{
connection.Open();

Assert.Equal(100, connection.DefaultTimeout);
}
}

[Fact]
public void Open_works_when_command_timeout()
{
using (var connection = new SqliteConnection("Data Source=:memory:;Command Timeout=100"))
{
connection.Open();

Assert.Equal(100, connection.DefaultTimeout);
}
}

[Fact]
public void Close_works()
{
Expand Down Expand Up @@ -570,7 +558,6 @@ public void CreateCommand_returns_command()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.DefaultTimeout = 1;
connection.Open();

using (var transaction = connection.BeginTransaction())
Expand All @@ -579,7 +566,6 @@ public void CreateCommand_returns_command()

Assert.NotNull(command);
Assert.Same(connection, command.Connection);
Assert.Equal(1, command.CommandTimeout);
Assert.Same(transaction, command.Transaction);
}
}
Expand Down

0 comments on commit fc43d5a

Please sign in to comment.