-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathForeignKeyInfoTest.cs
67 lines (59 loc) · 2.53 KB
/
ForeignKeyInfoTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Ivan Galkin
// Created: 2009.03.23
using System;
using Xtensive.Core;
using Xtensive.Orm;
using Xtensive.Orm.Upgrade.Model;
using NUnit.Framework;
using Xtensive.Orm.Tests;
namespace Xtensive.Orm.Tests.Indexing
{
[TestFixture]
public class ForeignKeyInfoTest
{
private StorageModel storage;
private TableInfo referencingTable;
private SecondaryIndexInfo referencingIndex;
private SecondaryIndexInfo invalideReferencingIndex;
private TableInfo referencedTable;
private PrimaryIndexInfo foreignPrimary;
[SetUp]
public void BuildStorageModel()
{
storage = new StorageModel("storage");
referencingTable = new TableInfo(storage, "referencingTable");
var pkColumn = new StorageColumnInfo(referencingTable, "Id", new StorageTypeInfo(typeof (int), null));
var fkColumn = new StorageColumnInfo(referencingTable, "foreignId", new StorageTypeInfo(typeof (int?), null));
var fkColumn2 = new StorageColumnInfo(referencingTable, "invalideForeignId", new StorageTypeInfo(typeof (string), null));
var primaryKey = new PrimaryIndexInfo(referencingTable, "PK1");
new KeyColumnRef(primaryKey, pkColumn);
referencingIndex = new SecondaryIndexInfo(referencingTable, "FK");
new KeyColumnRef(referencingIndex, fkColumn);
referencingIndex.PopulatePrimaryKeyColumns();
invalideReferencingIndex = new SecondaryIndexInfo(referencingTable, "FK2");
invalideReferencingIndex.PopulatePrimaryKeyColumns();
new KeyColumnRef(invalideReferencingIndex, fkColumn2);
primaryKey.PopulateValueColumns();
referencedTable = new TableInfo(storage, "referencedTable");
pkColumn = new StorageColumnInfo(referencedTable, "Id", new StorageTypeInfo(typeof (int), null));
foreignPrimary = new PrimaryIndexInfo(referencedTable, "Id");
new KeyColumnRef(foreignPrimary, pkColumn);
}
[Test]
public void ForeignKeyValidationTest()
{
storage.Validate();
var foreignKey = new ForeignKeyInfo(referencingTable, "ForeignKey");
AssertEx.Throws<AggregateException>(foreignKey.Validate);
foreignKey.PrimaryKey = foreignPrimary;
AssertEx.Throws<AggregateException>(foreignKey.Validate);
foreignKey.ForeignKeyColumns.Set(invalideReferencingIndex);
AssertEx.Throws<AggregateException>(foreignKey.Validate);
foreignKey.ForeignKeyColumns.Set(referencingIndex);
foreignKey.Validate();
}
}
}