|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using NUnit.Framework; |
| 5 | +using Xtensive.Orm.Configuration; |
| 6 | +using Xtensive.Orm.Providers; |
| 7 | +using Xtensive.Orm.Tests.Storage.Multinode.ObsoleteSelectStorageNodeTestModel; |
| 8 | + |
| 9 | +namespace Xtensive.Orm.Tests.Storage.Multinode.ObsoleteSelectStorageNodeTestModel |
| 10 | +{ |
| 11 | + [HierarchyRoot] |
| 12 | + public class TestEntity : Entity |
| 13 | + { |
| 14 | + [Field, Key] |
| 15 | + public int Id { get; private set; } |
| 16 | + |
| 17 | + [Field] |
| 18 | + public string NodeTag { get; set; } |
| 19 | + |
| 20 | + public TestEntity(Session session) |
| 21 | + :base(session) |
| 22 | + { |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +namespace Xtensive.Orm.Tests.Storage.Multinode |
| 28 | +{ |
| 29 | + //should be removed with the obsolete method |
| 30 | + public class ObsoleteSelectStorageNodeTest : AutoBuildTest |
| 31 | + { |
| 32 | + private const string AdditionalNodeName = "Additional"; |
| 33 | + private const string DefaultNodeTag = "<default>"; |
| 34 | + private const string AdditionalNodeTag = "<additional>"; |
| 35 | + |
| 36 | + protected override void CheckRequirements() |
| 37 | + { |
| 38 | + Require.ProviderIs(StorageProvider.SqlServer); |
| 39 | + } |
| 40 | + |
| 41 | + protected override DomainConfiguration BuildConfiguration() |
| 42 | + { |
| 43 | + var configuration = base.BuildConfiguration(); |
| 44 | + configuration.Types.Register(typeof(TestEntity)); |
| 45 | + configuration.UpgradeMode = DomainUpgradeMode.Recreate; |
| 46 | + configuration.DefaultSchema = "dbo"; |
| 47 | + return configuration; |
| 48 | + } |
| 49 | + |
| 50 | + protected override Domain BuildDomain(DomainConfiguration configuration) |
| 51 | + { |
| 52 | + var domain = base.BuildDomain(configuration); |
| 53 | + var nodeConfig = new NodeConfiguration(AdditionalNodeName) { UpgradeMode = DomainUpgradeMode.Recreate }; |
| 54 | + nodeConfig.SchemaMapping.Add("dbo", "Model1"); |
| 55 | + _ = domain.StorageNodeManager.AddNode(nodeConfig); |
| 56 | + return domain; |
| 57 | + } |
| 58 | + |
| 59 | + protected override void PopulateData() |
| 60 | + { |
| 61 | + var defaultNodeSelection = Domain.SelectStorageNode(WellKnown.DefaultNodeId); |
| 62 | + using (var nodeSession = defaultNodeSelection.OpenSession()) |
| 63 | + using (var tx = nodeSession.OpenTransaction()) { |
| 64 | + _ = new TestEntity(nodeSession) { NodeTag = DefaultNodeTag }; |
| 65 | + _ = new TestEntity(nodeSession) { NodeTag = DefaultNodeTag }; |
| 66 | + _ = new TestEntity(nodeSession) { NodeTag = DefaultNodeTag }; |
| 67 | + tx.Complete(); |
| 68 | + } |
| 69 | + |
| 70 | + var additonalNodeSelection = Domain.SelectStorageNode(AdditionalNodeName); |
| 71 | + using (var nodeSession = additonalNodeSelection.OpenSession()) |
| 72 | + using (var tx = nodeSession.OpenTransaction()) { |
| 73 | + _ = new TestEntity(nodeSession) { NodeTag = AdditionalNodeTag }; |
| 74 | + _ = new TestEntity(nodeSession) { NodeTag = AdditionalNodeTag }; |
| 75 | + _ = new TestEntity(nodeSession) { NodeTag = AdditionalNodeTag }; |
| 76 | + tx.Complete(); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void SelectExistingNodeTest() |
| 84 | + { |
| 85 | + using (var session = Domain.OpenSession()) { |
| 86 | + session.SelectStorageNode(WellKnown.DefaultNodeId); |
| 87 | + using(var tx = session.OpenTransaction()) { |
| 88 | + var count = 0; |
| 89 | + foreach(var item in session.Query.All<TestEntity>()) { |
| 90 | + Assert.That(item.NodeTag, Is.EqualTo(DefaultNodeTag)); |
| 91 | + count++; |
| 92 | + } |
| 93 | + Assert.That(count, Is.GreaterThan(0)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + using (var session = Domain.OpenSession()) { |
| 98 | + session.SelectStorageNode(AdditionalNodeName); |
| 99 | + using (var tx = session.OpenTransaction()) { |
| 100 | + var count = 0; |
| 101 | + foreach (var item in session.Query.All<TestEntity>()) { |
| 102 | + Assert.That(item.NodeTag, Is.EqualTo(AdditionalNodeTag)); |
| 103 | + count++; |
| 104 | + } |
| 105 | + Assert.That(count, Is.GreaterThan(0)); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void SelectNonExistingNodeTest() |
| 112 | + { |
| 113 | + using (var session = Domain.OpenSession()) { |
| 114 | + _ = Assert.Throws<KeyNotFoundException>(() => session.SelectStorageNode(WellKnown.DefaultNodeId + "does not exist")); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + public void ReSelectNodeTest() |
| 120 | + { |
| 121 | + using (var session = Domain.OpenSession()) { |
| 122 | + session.SelectStorageNode(WellKnown.DefaultNodeId); |
| 123 | + _ = Assert.Throws<InvalidOperationException>(() => session.SelectStorageNode(WellKnown.DefaultNodeId)); |
| 124 | + } |
| 125 | + |
| 126 | + using (var session = Domain.OpenSession()) { |
| 127 | + session.SelectStorageNode(AdditionalNodeName); |
| 128 | + |
| 129 | + _ = Assert.Throws<InvalidOperationException>(() => session.SelectStorageNode(AdditionalNodeName)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + [Test] |
| 134 | + public void SessionWithoutSelectedNodeTest() |
| 135 | + { |
| 136 | + using (var session = Domain.OpenSession()) { |
| 137 | + Assert.That(session.GetStorageNodeInternal(), Is.Null); |
| 138 | + using (var tx = session.OpenTransaction()) { |
| 139 | + var count = 0; |
| 140 | + foreach (var item in session.Query.All<TestEntity>()) { |
| 141 | + Assert.That(item.NodeTag, Is.EqualTo(DefaultNodeTag)); |
| 142 | + count++; |
| 143 | + } |
| 144 | + Assert.That(count, Is.GreaterThan(0)); |
| 145 | + } |
| 146 | + Assert.That(session.GetStorageNodeInternal(), Is.Not.Null); |
| 147 | + Assert.That(session.GetStorageNodeInternal().Id, Is.EqualTo(WellKnown.DefaultNodeId)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 153 | + } |
| 154 | +} |
0 commit comments