Skip to content

Commit 20e2f50

Browse files
author
Marc Sallin
committed
#27: Added UniqueConstraint and tests for it.
1 parent 3843108 commit 20e2f50

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

SQLite.CodeFirst.Test/SQLite.CodeFirst.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="Statement\ColumnConstraint\MaxLengthConstraint.cs" />
8181
<Compile Include="Statement\ColumnConstraint\ColumnConstraintCollectionTest.cs" />
8282
<Compile Include="Statement\ColumnConstraint\NotNullConstraintTest.cs" />
83+
<Compile Include="Statement\ColumnConstraint\UniqueConstraintTest.cs" />
8384
<Compile Include="Statement\ColumnStatementCollectionTest.cs" />
8485
<Compile Include="Statement\PrimaryKeyStatementTest.cs" />
8586
<Compile Include="Statement\CreateDatabaseStatementTest.cs" />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using SQLite.CodeFirst.Statement.ColumnConstraint;
3+
4+
namespace SQLite.CodeFirst.Test.Statement.ColumnConstraint
5+
{
6+
[TestClass]
7+
public class UniqueConstraintTest
8+
{
9+
[TestMethod]
10+
public void CreateStatement_StatementIsCorrect_NoConstraint()
11+
{
12+
var uniqueConstraint = new UniqueConstraint();
13+
uniqueConstraint.OnConflict = OnConflictAction.None;
14+
string output = uniqueConstraint.CreateStatement();
15+
Assert.AreEqual(output, "UNIQUE");
16+
}
17+
18+
[TestMethod]
19+
public void CreateStatement_StatementIsCorrect_WithConstraint()
20+
{
21+
var uniqueConstraint = new UniqueConstraint();
22+
uniqueConstraint.OnConflict = OnConflictAction.Rollback;
23+
string output = uniqueConstraint.CreateStatement();
24+
Assert.AreEqual(output, "UNIQUE ON CONFLICT ROLLBACK");
25+
}
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Text;
2+
3+
namespace SQLite.CodeFirst.Statement.ColumnConstraint
4+
{
5+
internal class UniqueConstraint : IColumnConstraint
6+
{
7+
private const string Template = "UNIQUE {conflict-clause}";
8+
9+
public OnConflictAction OnConflict { get; set; }
10+
11+
public string CreateStatement()
12+
{
13+
var sb = new StringBuilder(Template);
14+
15+
sb.Replace("{conflict-clause}", OnConflict != OnConflictAction.None ? "ON CONFLICT " + OnConflict.ToString().ToUpperInvariant() : string.Empty);
16+
17+
return sb.ToString().Trim();
18+
}
19+
}
20+
}

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Internal\Builder\NameCreators\IndexNameCreator.cs" />
9090
<Compile Include="Internal\Builder\NameCreators\SpecialChars.cs" />
9191
<Compile Include="IDatabaseCreator.cs" />
92+
<Compile Include="Internal\Statement\ColumnConstraint\UniqueConstraint.cs" />
9293
<Compile Include="Internal\Utility\HashCreator.cs" />
9394
<Compile Include="Internal\Utility\HistoryEntityTypeValidator.cs" />
9495
<Compile Include="ISqliteSqlGenerator.cs" />

0 commit comments

Comments
 (0)