-
-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed locations where an assumption is made that a ForeignKey.Referen…
…ceDocumentType will not be null * Fixed several locations where an assumption is made that a ForeignKeyDefinition.ReferenceDocumentType will not be null * Fixed a typo * Fixes #1338
- Loading branch information
1 parent
0839ec6
commit 6579824
Showing
5 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/Marten.Testing/Bugs/bug_1338_Validate_Null_ForeignKeyDefinition_ReferenceDocumenType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Marten.Schema; | ||
using Marten.Storage; | ||
using Npgsql; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Marten.Testing.Bugs | ||
{ | ||
public class Bug_1338_Validate_Null_ForeignKeyDefinition_ReferenceDocumenType : IntegratedFixture | ||
{ | ||
[Fact] | ||
public void StorageFeatures_AllActiveFeatures_Should_Not_Throw_With_ExternalForeignKeyDefinitions() | ||
{ | ||
CreateExternalTableForTesting(); | ||
|
||
StoreOptions(_ => | ||
{ | ||
_.Schema.For<ClassWithExternalForeignKey>().ForeignKey(x => x.ForeignId, _.DatabaseSchemaName, "external_table", "id"); | ||
}); | ||
|
||
theStore.Storage.AllActiveFeatures(theStore.Tenancy.Default).All(x => x != null).ShouldBeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void UnitOfWork_GetTypeDependencies_Should_Not_Throw_With_ExternalForeignKeyDefinitions() | ||
{ | ||
CreateExternalTableForTesting(); | ||
|
||
StoreOptions(_ => | ||
{ | ||
_.Schema.For<ClassWithExternalForeignKey>().ForeignKey(x => x.ForeignId, _.DatabaseSchemaName, "external_table", "id"); | ||
}); | ||
|
||
// Inserting a new document will force a call to: | ||
// UnitOfWork.ApplyChanges() | ||
// UnitOfWork.buildChangeSet() | ||
// UnitOfWork.determineChanges() | ||
// UnitOfWork.shouldSort() | ||
// and finally, the function that we want to regression test" | ||
// UnitOfWork.GetTypeDependencies(ClassWithExternalForeignKey) | ||
using (var session = theStore.LightweightSession()) | ||
{ | ||
session.Insert(new ClassWithExternalForeignKey{Id = 1, ForeignId = 1}); | ||
session.SaveChanges(); | ||
} | ||
} | ||
|
||
private static void CreateExternalTableForTesting() | ||
{ | ||
const string dropSql = "DROP TABLE IF EXISTS public.external_table CASCADE;"; | ||
const string createSql = | ||
@"CREATE TABLE public.external_table ( | ||
id integer, | ||
CONSTRAINT ""external_table_pkey"" PRIMARY KEY (id) | ||
);"; | ||
const string insertSql = "INSERT INTO public.external_table VALUES (1);"; | ||
|
||
using (var dbConn = new NpgsqlConnection(ConnectionSource.ConnectionString)) | ||
{ | ||
dbConn.Open(); | ||
|
||
NpgsqlCommand cmd; | ||
using (cmd = new NpgsqlCommand(dropSql, dbConn)) | ||
{ | ||
cmd.ExecuteNonQuery(); | ||
} | ||
|
||
using (cmd = new NpgsqlCommand(createSql, dbConn)) | ||
{ | ||
cmd.ExecuteNonQuery(); | ||
} | ||
|
||
using (cmd = new NpgsqlCommand(insertSql, dbConn)) | ||
{ | ||
cmd.ExecuteNonQuery(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
public class FakeExternalTable: FeatureSchemaBase | ||
{ | ||
public FakeExternalTable(StoreOptions options) : base("fake_external_table", options) | ||
{ | ||
} | ||
|
||
protected override IEnumerable<ISchemaObject> schemaObjects() | ||
{ | ||
var table = new Table(new DbObjectName(Options.DatabaseSchemaName, "external_table")); | ||
table.AddColumn("id", "integer"); | ||
|
||
yield return table; | ||
} | ||
} | ||
|
||
public class ClassWithExternalForeignKey | ||
{ | ||
public int Id { get; set; } | ||
public int ForeignId { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters