Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from zhelgadis/select_modeling

Added Selection and Projection classes
  • Loading branch information...
commit a8e94c2fe602efb1360282782bd0e0aabd2c0d03 2 parents 0c9998f + 852f0ef
Giacomo Tesio authored May 18, 2012
0  3rdParties/NUnit/nunit-agent-x86.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/nunit-agent.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/nunit-console-x86.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/nunit-x86.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/nunit.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/pnunit-agent.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/pnunit-launcher.exe 100644 → 100755
File mode changed
0  3rdParties/NUnit/runFile.exe 100644 → 100755
File mode changed
5  Code/Epic.Query/Epic.Query.csproj
@@ -62,6 +62,8 @@
62 62
     <Compile Include="..\EpicInfo.cs">
63 63
       <Link>EpicInfo.cs</Link>
64 64
     </Compile>
  65
+    <Compile Include="Relational\Operations\Projection.cs" />
  66
+    <Compile Include="Relational\Operations\Selection.cs" />
65 67
   </ItemGroup>
66 68
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
67 69
   <ItemGroup>
@@ -70,4 +72,7 @@
70 72
       <Name>Epic.Prelude</Name>
71 73
     </ProjectReference>
72 74
   </ItemGroup>
  75
+  <ItemGroup>
  76
+    <Folder Include="Relational\Operations\" />
  77
+  </ItemGroup>
73 78
 </Project>
189  Code/Epic.Query/Relational/Operations/Projection.cs
... ...
@@ -0,0 +1,189 @@
  1
+//  
  2
+//  Projection.cs
  3
+//  
  4
+//  Author:
  5
+//       Marco Veglio <m.veglio@gmail.com>
  6
+// 
  7
+//  Copyright (c) 2010-2012 Giacomo Tesio
  8
+// 
  9
+//  This file is part of Epic.NET.
  10
+// 
  11
+//  Epic.NET is free software: you can redistribute it and/or modify
  12
+//  it under the terms of the GNU Affero General Public License as published by
  13
+//  the Free Software Foundation, either version 3 of the License, or
  14
+//  (at your option) any later version.
  15
+// 
  16
+//  Epic.NET is distributed in the hope that it will be useful,
  17
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19
+//  GNU Affero General Public License for more details.
  20
+// 
  21
+//  You should have received a copy of the GNU Affero General Public License
  22
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23
+//  
  24
+using System;
  25
+using System.Collections.Generic;
  26
+using System.Linq;
  27
+using System.Text;
  28
+
  29
+
  30
+namespace Epic.Query.Relational.Operations
  31
+{
  32
+    /// <summary>
  33
+    /// This class models the Projection operation, which extracts a given set of fields from a given
  34
+    /// <see cref="Relation"/>.
  35
+    /// </summary>
  36
+    [Serializable]
  37
+    public sealed class Projection: Relation, IEquatable<Projection>
  38
+    {
  39
+        private readonly Relation relation;
  40
+        private readonly IEnumerable<RelationAttribute> attributes;
  41
+
  42
+        /// <summary>
  43
+        /// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Projection"/> class.
  44
+        /// </summary>
  45
+        /// <param name='relation'>
  46
+        /// The <see cref="Relation"/> used as source for the projection.
  47
+        /// </param>
  48
+        /// <param name='attributes'>
  49
+        /// The collection of <see cref="RelationAttributes"/> extracted
  50
+        /// </param>
  51
+        /// <param name='name'>
  52
+        /// A user-defined name used to identify the Projection relation.
  53
+        /// </param>
  54
+        public Projection (Relation relation, IEnumerable<RelationAttribute> attributes, string name):
  55
+            base(RelationType.Projection, name)
  56
+        {
  57
+            if (null == relation) throw new ArgumentNullException("relation");
  58
+            if (null == attributes) throw new ArgumentNullException("attributes");
  59
+            if (null == name) throw new ArgumentNullException("name");
  60
+            this.relation = relation;
  61
+            this.attributes = attributes;
  62
+        }
  63
+
  64
+        /// <summary>
  65
+        /// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Projection"/> class.
  66
+        /// </summary>
  67
+        /// <param name='relation'>
  68
+        /// The <see cref="Relation"/> used as source for the projection.
  69
+        /// </param>
  70
+        /// <param name='attributes'>
  71
+        /// The collection of <see cref="RelationAttributes"/> extracted
  72
+        /// </param>
  73
+        public Projection(Relation relation, IEnumerable<RelationAttribute> attributes):
  74
+            base(RelationType.Projection, getDefaultName(relation, attributes))
  75
+        {
  76
+            if (null == relation) throw new ArgumentNullException("relation");
  77
+            if (null == attributes) throw new ArgumentNullException("attributes");
  78
+            this.relation = relation;
  79
+            this.attributes = attributes;
  80
+        }
  81
+
  82
+        /// <summary>
  83
+        /// Gets the <see cref="Relation"/> used as source for the projection.
  84
+        /// </summary>
  85
+        /// <value>
  86
+        /// The table.
  87
+        /// </value>
  88
+        public Relation Relation { get { return this.relation; } }
  89
+
  90
+        /// <summary>
  91
+        /// Gets the attributes extracted from the given table.
  92
+        /// </summary>
  93
+        /// <value>
  94
+        /// The attributes.
  95
+        /// </value>
  96
+        public IEnumerable<RelationAttribute> Attributes { get { return this.attributes; } }
  97
+
  98
+        /// <summary>
  99
+        /// Determines whether the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current <see cref="Epic.Query.Relational.Operations.Projection"/>.
  100
+        /// </summary>
  101
+        /// <param name='other'>
  102
+        /// The <see cref="Epic.Query.Relational.Relation"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Projection"/>.
  103
+        /// </param>
  104
+        /// <returns>
  105
+        /// <c>true</c> if the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current
  106
+        /// <see cref="Epic.Query.Relational.Operations.Projection"/>; otherwise, <c>false</c>.
  107
+        /// </returns>
  108
+        public override bool Equals (Relation other)
  109
+        {
  110
+            return this.Equals (other as Projection);
  111
+        }
  112
+
  113
+        /// <summary>
  114
+        /// Determines whether the specified <see cref="Epic.Query.Relational.Operations.Projection"/> is equal to the
  115
+        /// current <see cref="Epic.Query.Relational.Operations.Projection"/>.
  116
+        /// </summary>
  117
+        /// <param name='other'>
  118
+        /// The <see cref="Epic.Query.Relational.Operations.Projection"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Projection"/>.
  119
+        /// </param>
  120
+        /// <returns>
  121
+        /// <c>true</c> if the specified <see cref="Epic.Query.Relational.Operations.Projection"/> is equal to the
  122
+        /// current <see cref="Epic.Query.Relational.Operations.Projection"/>; otherwise, <c>false</c>.
  123
+        /// </returns>
  124
+        public bool Equals(Projection other)
  125
+        {
  126
+            if (null == other) return false;
  127
+            return this.Relation.Equals (other.Relation) && this.Attributes.SequenceEqual(other.Attributes);
  128
+        }
  129
+
  130
+        /// <summary>
  131
+        /// Accept the specified visitor and context.
  132
+        /// </summary>
  133
+        /// <param name='visitor'>
  134
+        /// Visitor.
  135
+        /// </param>
  136
+        /// <param name='context'>
  137
+        /// Context.
  138
+        /// </param>
  139
+        /// <typeparam name='TResult'>
  140
+        /// The 1st type parameter.
  141
+        /// </typeparam>
  142
+        public override TResult Accept<TResult> (IVisitor<TResult> visitor, IVisitContext context)
  143
+        {
  144
+            return AcceptMe (this, visitor, context);
  145
+        }
  146
+
  147
+        /// <summary>
  148
+        /// Serves as a hash function for a <see cref="Epic.Query.Relational.Operations.Projection"/> object.
  149
+        /// </summary>
  150
+        /// <returns>
  151
+        /// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
  152
+        /// hash table.
  153
+        /// </returns>
  154
+        public override int GetHashCode ()
  155
+        {
  156
+            int hash = this.Relation.GetHashCode ();
  157
+            foreach (RelationAttribute attribute in this.Attributes)
  158
+                hash ^= attribute.GetHashCode ();
  159
+            return hash;
  160
+        }
  161
+
  162
+        /// <summary>
  163
+        /// Gets the default name for the projection.
  164
+        /// </summary>
  165
+        /// <returns>
  166
+        /// The default name.
  167
+        /// </returns>
  168
+        /// <param name='relation'>
  169
+        /// the <see cref="Relation"/> used as source for the projection.
  170
+        /// </param>
  171
+        /// <param name='attributes'>
  172
+        /// The attributes to be extracted from the relation.
  173
+        /// </param>
  174
+        private static string getDefaultName (Relation relation, IEnumerable<RelationAttribute> attributes)
  175
+        {
  176
+            if (null == relation) throw new ArgumentNullException("relation");
  177
+            if (null == attributes) throw new ArgumentNullException("attributes");
  178
+            StringBuilder sb = new StringBuilder();
  179
+            sb.Append("Take ");
  180
+            foreach (RelationAttribute attribute in attributes)
  181
+                sb.Append (attribute.Name).Append(",");
  182
+            sb.Append (" from ");
  183
+            sb.Append (relation.Name);
  184
+            return sb.ToString ();
  185
+        }
  186
+
  187
+    }
  188
+}
  189
+
176  Code/Epic.Query/Relational/Operations/Selection.cs
... ...
@@ -0,0 +1,176 @@
  1
+//  
  2
+//  Select.cs
  3
+//  
  4
+//  Author:
  5
+//       Marco Veglio <m.veglio@gmail.com>
  6
+// 
  7
+//  Copyright (c) 2010-2012 Giacomo Tesio
  8
+// 
  9
+//  This file is part of Epic.NET.
  10
+// 
  11
+//  Epic.NET is free software: you can redistribute it and/or modify
  12
+//  it under the terms of the GNU Affero General Public License as published by
  13
+//  the Free Software Foundation, either version 3 of the License, or
  14
+//  (at your option) any later version.
  15
+// 
  16
+//  Epic.NET is distributed in the hope that it will be useful,
  17
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19
+//  GNU Affero General Public License for more details.
  20
+// 
  21
+//  You should have received a copy of the GNU Affero General Public License
  22
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23
+//  
  24
+using System;
  25
+using Epic.Query.Relational.Predicates;
  26
+
  27
+namespace Epic.Query.Relational.Operations
  28
+{
  29
+
  30
+    /// <summary>
  31
+    /// This class models the Select operation which extracts those record from a given table matching
  32
+    /// a given <see cref="Predicate"/>
  33
+    /// </summary>
  34
+    [Serializable]
  35
+    public sealed class Selection: Relation, IEquatable<Selection>
  36
+    {
  37
+        private readonly Predicate condition;
  38
+        private readonly Relation relation;
  39
+        
  40
+        /// <summary>
  41
+        /// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Selection"/> class.
  42
+        /// </summary>
  43
+        /// <param name='relation'>
  44
+        /// The <see cref="Relation"/> used as source for the selection.
  45
+        /// </param>
  46
+        /// <param name='condition'>
  47
+        /// The <see cref="Predicate"/> against which the records are matched.
  48
+        /// </param>
  49
+        public Selection (Relation relation, Predicate condition): 
  50
+            base(RelationType.Selection, getDefaultName (relation, condition))
  51
+        {
  52
+            if (null == relation) throw new ArgumentNullException("relation");
  53
+            if (null == condition) throw new ArgumentNullException("condition");
  54
+            this.condition = condition;
  55
+            this.relation = relation;
  56
+        }
  57
+        
  58
+        /// <summary>
  59
+        /// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Selection"/> class.
  60
+        /// </summary>
  61
+        /// <param name='relation'>
  62
+        /// The table used as source for the selection.
  63
+        /// </param>
  64
+        /// <param name='condition'>
  65
+        /// The <see cref="Predicate"/> against which the records are matched.
  66
+        /// </param>
  67
+        /// <param name='name'>
  68
+        /// A user-defined name to identify the Selection relation.
  69
+        /// </param>
  70
+        public Selection (Relation relation, Predicate condition, string name): base(RelationType.Selection, name)
  71
+        {
  72
+            if (null == relation) throw new ArgumentNullException("relation");
  73
+            if (null == condition) throw new ArgumentNullException("condition");
  74
+            if (null == name) throw new ArgumentNullException("name");
  75
+            this.condition = condition;
  76
+            this.relation = relation;
  77
+        }
  78
+        
  79
+        /// <summary>
  80
+        /// Gets the condition against which the table's records are matched.
  81
+        /// </summary>
  82
+        /// <value>
  83
+        /// The condition.
  84
+        /// </value>
  85
+        public Predicate Condition { get { return this.condition; } }
  86
+        
  87
+        /// <summary>
  88
+        /// Gets the <see cref="Relation"/> used as source for the selection.
  89
+        /// </summary>
  90
+        /// <value>
  91
+        /// The table.
  92
+        /// </value>
  93
+        public Relation Relation { get { return this.relation; } }
  94
+        
  95
+        /// <summary>
  96
+        /// Determines whether the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current <see cref="Epic.Query.Relational.Operations.Selection"/>.
  97
+        /// </summary>
  98
+        /// <param name='other'>
  99
+        /// The <see cref="Epic.Query.Relational.Relation"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Selection"/>.
  100
+        /// </param>
  101
+        /// <returns>
  102
+        /// <c>true</c> if the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current
  103
+        /// <see cref="Epic.Query.Relational.Operations.Selection"/>; otherwise, <c>false</c>.
  104
+        /// </returns>
  105
+        public override bool Equals (Relation other)
  106
+        {
  107
+            return this.Equals (other as Selection);
  108
+        }
  109
+        
  110
+        /// <summary>
  111
+        /// Determines whether the specified <see cref="Epic.Query.Relational.Operations.Selection"/> is equal to the
  112
+        /// current <see cref="Epic.Query.Relational.Operations.Selection"/>.
  113
+        /// </summary>
  114
+        /// <param name='other'>
  115
+        /// The <see cref="Epic.Query.Relational.Operations.Selection"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Selection"/>.
  116
+        /// </param>
  117
+        /// <returns>
  118
+        /// <c>true</c> if the specified <see cref="Epic.Query.Relational.Operations.Selection"/> is equal to the current
  119
+        /// <see cref="Epic.Query.Relational.Operations.Selection"/>; otherwise, <c>false</c>.
  120
+        /// </returns>
  121
+        public bool Equals(Selection other)
  122
+        {
  123
+            if (null == other) return false;
  124
+            return this.Relation.Equals (other.Relation) && this.Condition.Equals (other.Condition);
  125
+        }
  126
+
  127
+        /// <summary>
  128
+        /// Serves as a hash function for a <see cref="Epic.Query.Relational.Operations.Selection"/> object.
  129
+        /// </summary>
  130
+        /// <returns>
  131
+        /// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
  132
+        /// hash table.
  133
+        /// </returns>
  134
+        public override int GetHashCode ()
  135
+        {
  136
+            return this.Relation.GetHashCode () ^ this.Condition.GetHashCode ();
  137
+        }
  138
+        
  139
+        /// <summary>
  140
+        /// Accept the specified visitor and context.
  141
+        /// </summary>
  142
+        /// <param name='visitor'>
  143
+        /// Visitor.
  144
+        /// </param>
  145
+        /// <param name='context'>
  146
+        /// Context.
  147
+        /// </param>
  148
+        /// <typeparam name='TResult'>
  149
+        /// The 1st type parameter.
  150
+        /// </typeparam>
  151
+        public override TResult Accept<TResult> (IVisitor<TResult> visitor, IVisitContext context)
  152
+        {
  153
+            return AcceptMe (this, visitor, context);
  154
+        }
  155
+        
  156
+        /// <summary>
  157
+        /// Gets the default name for the selection.
  158
+        /// </summary>
  159
+        /// <returns>
  160
+        /// The default name.
  161
+        /// </returns>
  162
+        /// <param name='relation'>
  163
+        /// The <see cref="Relation"/> used as source for the selection.
  164
+        /// </param>
  165
+        /// <param name='predicate'>
  166
+        /// The condition against which the records are matched.
  167
+        /// </param>
  168
+        private static string getDefaultName(Relation relation, Predicate predicate)
  169
+        {
  170
+            if (null == relation) throw new ArgumentNullException("relation");
  171
+            if (null == predicate) throw new ArgumentNullException("predicate");
  172
+            return string.Format ("SELECT * from {0} where {1}", relation.Name, predicate.ToString ());
  173
+        }
  174
+    }
  175
+}
  176
+
3  Code/UnitTests/Epic.Query.UnitTests/Epic.Query.UnitTests.csproj
@@ -76,8 +76,11 @@
76 76
     <Compile Include="Fakes\FakeRelationFunction.cs" />
77 77
     <Compile Include="Fakes\FakeScalar.cs" />
78 78
     <Compile Include="Fakes\FakeScalarFunction.cs" />
  79
+    <Compile Include="Relational\Operations\SelectionQA.cs" />
  80
+    <Compile Include="Relational\Operations\ProjectionQA.cs" />
79 81
   </ItemGroup>
80 82
   <ItemGroup>
81 83
     <Folder Include="Fakes\" />
  84
+    <Folder Include="Relational\Operations\" />
82 85
   </ItemGroup>
83 86
 </Project>
178  Code/UnitTests/Epic.Query.UnitTests/Relational/Operations/ProjectionQA.cs
... ...
@@ -0,0 +1,178 @@
  1
+//  
  2
+//  ProjectionQA.cs
  3
+//  
  4
+//  Author:
  5
+//       Marco Veglio <m.veglio@gmail.com>
  6
+// 
  7
+//  Copyright (c) 2010-2012 Giacomo Tesio
  8
+// 
  9
+//  This file is part of Epic.NET.
  10
+// 
  11
+//  Epic.NET is free software: you can redistribute it and/or modify
  12
+//  it under the terms of the GNU Affero General Public License as published by
  13
+//  the Free Software Foundation, either version 3 of the License, or
  14
+//  (at your option) any later version.
  15
+// 
  16
+//  Epic.NET is distributed in the hope that it will be useful,
  17
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19
+//  GNU Affero General Public License for more details.
  20
+// 
  21
+//  You should have received a copy of the GNU Affero General Public License
  22
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23
+//  
  24
+using System;
  25
+using System.Collections.Generic;
  26
+using NUnit.Framework;
  27
+using Epic.Query.Relational.Predicates;
  28
+using System.IO;
  29
+using Rhino.Mocks;
  30
+using System.Linq;
  31
+
  32
+
  33
+namespace Epic.Query.Relational.Operations
  34
+{
  35
+    [TestFixture]
  36
+    public class ProjectionQA:RhinoMocksFixtureBase
  37
+    {
  38
+        private readonly Relation fakeRelation = new Fakes.FakeRelation(RelationType.BaseRelation, "test");
  39
+        private string attributeName = "attribute";
  40
+
  41
+        [Test]
  42
+        public void Initialize_WithEitherArgumentNull_Fails()
  43
+        {
  44
+            // arrange:
  45
+            string tableName = "testTable";
  46
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  47
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  48
+
  49
+            // assert:
  50
+            Assert.Throws<ArgumentNullException>(() => new Projection(table, null));
  51
+            Assert.Throws<ArgumentNullException>(() => new Projection(null, attributes));
  52
+            Assert.Throws<ArgumentNullException>(() => new Projection(table, attributes, null));
  53
+            Assert.Throws<ArgumentNullException>(() => new Projection(null, null));
  54
+        }
  55
+
  56
+        [Test]
  57
+        public void Initialize_WithFakeArguments_Works()
  58
+        {
  59
+            // arrange:
  60
+            string tableName = "testTable";
  61
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  62
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  63
+            string operationName = "select * from testTable where 1 = 1";
  64
+
  65
+            // act:
  66
+            Projection firstProjection = new Projection(table, attributes);
  67
+            Projection secondProjection = new Projection(table, attributes, operationName);
  68
+
  69
+            // assert:
  70
+            Assert.IsTrue (firstProjection.Relation.Equals (table));
  71
+            Assert.IsTrue (firstProjection.Relation.Equals (secondProjection.Relation));
  72
+
  73
+            Assert.IsTrue (firstProjection.Attributes.SequenceEqual (attributes));
  74
+            Assert.IsTrue (firstProjection.Attributes.SequenceEqual (secondProjection.Attributes));
  75
+
  76
+            Assert.IsTrue (secondProjection.Name.Equals (operationName));
  77
+            Assert.IsTrue (firstProjection.Equals (secondProjection));
  78
+        }
  79
+
  80
+        [Test]
  81
+        public void Equals_AgainstSameObject_works()
  82
+        {
  83
+            // arrange:
  84
+            string tableName = "testTable";
  85
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  86
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  87
+            string operationName = "select * from testTable where 1 = 1";
  88
+
  89
+            // act:
  90
+            Projection firstProjection = new Projection(table, attributes, operationName);
  91
+            Projection secondProjection = firstProjection;
  92
+
  93
+            // assert:
  94
+            Assert.IsTrue (firstProjection.Equals (secondProjection));
  95
+        }
  96
+
  97
+        [Test]
  98
+        public void Equals_AgainstEquivalentObject_works()
  99
+        {
  100
+            // arrange:
  101
+            string tableName = "testTable";
  102
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  103
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  104
+            string operationName = "select * from testTable where 1 = 1";
  105
+
  106
+            // act:
  107
+            Projection firstProjection = new Projection(table, attributes, operationName);
  108
+            Projection secondProjection = new Projection(table, attributes,  operationName);
  109
+
  110
+            // assert:
  111
+            Assert.IsTrue (firstProjection.Equals (secondProjection));
  112
+            Assert.AreEqual (firstProjection.GetHashCode (), secondProjection.GetHashCode ());
  113
+        }
  114
+
  115
+                [Test]
  116
+        public void Test_Serialization_Works()
  117
+        {
  118
+            // arrange:
  119
+            string tableName = "testTable";
  120
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  121
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  122
+            string operationName = "select * from testTable where 1 = 1";
  123
+            Projection selection = new Projection(table, attributes, operationName);
  124
+
  125
+            // act:
  126
+            Stream stream = SerializationUtilities.Serialize<Projection>(selection);
  127
+
  128
+            // assert:
  129
+            Assert.IsNotNull (stream);
  130
+        }
  131
+
  132
+        [Test]
  133
+        public void Test_deserialization_Works()
  134
+        {
  135
+            // arrange:
  136
+            string tableName = "testTable";
  137
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  138
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  139
+            string operationName = "select * from testTable where 1 = 1";
  140
+            Projection selection = new Projection(table, attributes, operationName);
  141
+
  142
+            // act:
  143
+            Stream stream = SerializationUtilities.Serialize<Projection>(selection);
  144
+            Projection deserialized = SerializationUtilities.Deserialize<Projection>(stream);
  145
+
  146
+            // assert:
  147
+            // Assert.AreSame(and, deserialized);
  148
+            Assert.IsTrue (selection.Equals (deserialized));
  149
+        }
  150
+
  151
+        [Test]
  152
+        public void Accept_withValidArguments_delegateVisitToTheRightVisitor()
  153
+        {
  154
+            // arrange:
  155
+            object expectedResult = new object();
  156
+            IVisitContext context = GenerateStrictMock<IVisitContext>();
  157
+            string tableName = "testTable";
  158
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  159
+            IEnumerable<RelationAttribute> attributes = new [] { new RelationAttribute(attributeName, fakeRelation) };
  160
+            string operationName = "select * from testTable where 1 = 1";
  161
+            Projection selection = new Projection(table, attributes, operationName);
  162
+
  163
+            IVisitor<object, Projection> selectionVisitor =
  164
+            GenerateStrictMock<IVisitor<object, Projection>>();
  165
+            selectionVisitor.Expect(v => v.Visit(selection, context)).Return(expectedResult).Repeat.Once();
  166
+            IVisitor<object> visitor = GenerateStrictMock<IVisitor<object>>();
  167
+            visitor.Expect(v => v.GetVisitor(selection)).Return(selectionVisitor).Repeat.Once ();
  168
+
  169
+            // act:
  170
+            object result = selection.Accept(visitor, context);
  171
+
  172
+            // assert:
  173
+            Assert.AreSame(expectedResult, result);
  174
+        }
  175
+
  176
+    }
  177
+}
  178
+
173  Code/UnitTests/Epic.Query.UnitTests/Relational/Operations/SelectionQA.cs
... ...
@@ -0,0 +1,173 @@
  1
+//  
  2
+//  SelectionQA.cs
  3
+//  
  4
+//  Author:
  5
+//       Marco Veglio <m.veglio@gmail.com>
  6
+// 
  7
+//  Copyright (c) 2010-2012 Giacomo Tesio
  8
+// 
  9
+//  This file is part of Epic.NET.
  10
+// 
  11
+//  Epic.NET is free software: you can redistribute it and/or modify
  12
+//  it under the terms of the GNU Affero General Public License as published by
  13
+//  the Free Software Foundation, either version 3 of the License, or
  14
+//  (at your option) any later version.
  15
+// 
  16
+//  Epic.NET is distributed in the hope that it will be useful,
  17
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19
+//  GNU Affero General Public License for more details.
  20
+// 
  21
+//  You should have received a copy of the GNU Affero General Public License
  22
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23
+//  
  24
+using System;
  25
+using NUnit.Framework;
  26
+using Epic.Query.Relational.Predicates;
  27
+using System.IO;
  28
+using Rhino.Mocks;
  29
+
  30
+
  31
+namespace Epic.Query.Relational.Operations
  32
+{
  33
+    [TestFixture]
  34
+    public class SelectionQA:RhinoMocksFixtureBase
  35
+    {
  36
+        [Test]
  37
+        public void Initialize_WithEitherArgumentNull_Fails()
  38
+        {
  39
+            // arrange:
  40
+            string tableName = "testTable";
  41
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  42
+            Predicate predicate = new Fakes.FakePredicate();
  43
+
  44
+            // assert:
  45
+            Assert.Throws<ArgumentNullException>(() => new Selection(table, null));
  46
+            Assert.Throws<ArgumentNullException>(() => new Selection(null, predicate));
  47
+            Assert.Throws<ArgumentNullException>(() => new Selection(table, predicate, null));
  48
+            Assert.Throws<ArgumentNullException>(() => new Selection(null, null));
  49
+        }
  50
+
  51
+        [Test]
  52
+        public void Initialize_WithFakeArguments_Works()
  53
+        {
  54
+            // arrange:
  55
+            string tableName = "testTable";
  56
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  57
+            Predicate predicate = new Fakes.FakePredicate();
  58
+            string operationName = "select * from testTable where 1 = 1";
  59
+
  60
+            // act:
  61
+            Selection firstSelection = new Selection(table, predicate);
  62
+            Selection secondSelection = new Selection(table, predicate, operationName);
  63
+
  64
+            // assert:
  65
+            Assert.IsTrue (firstSelection.Relation.Equals (table));
  66
+            Assert.IsTrue (firstSelection.Relation.Equals (secondSelection.Relation));
  67
+
  68
+            Assert.IsTrue (firstSelection.Condition.Equals (predicate));
  69
+            Assert.IsTrue (firstSelection.Condition.Equals (secondSelection.Condition));
  70
+
  71
+            Assert.IsTrue (secondSelection.Name.Equals (operationName));
  72
+            Assert.IsTrue (firstSelection.Equals (secondSelection));
  73
+        }
  74
+
  75
+        [Test]
  76
+        public void Equals_AgainstSameObject_works()
  77
+        {
  78
+            // arrange:
  79
+            string tableName = "testTable";
  80
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  81
+            Predicate predicate = new Fakes.FakePredicate();
  82
+            string operationName = "select * from testTable where 1 = 1";
  83
+
  84
+            // act:
  85
+            Selection firstSelection = new Selection(table, predicate, operationName);
  86
+            Selection secondSelection = firstSelection;
  87
+
  88
+            // assert:
  89
+            Assert.IsTrue (firstSelection.Equals (secondSelection));
  90
+        }
  91
+
  92
+        [Test]
  93
+        public void Equals_AgainstEquivalentObject_works()
  94
+        {
  95
+            // arrange:
  96
+            string tableName = "testTable";
  97
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  98
+            Predicate predicate = new Fakes.FakePredicate();
  99
+            string operationName = "select * from testTable where 1 = 1";
  100
+
  101
+            // act:
  102
+            Selection firstSelection = new Selection(table, predicate, operationName);
  103
+            Selection secondSelection = new Selection(table, predicate,  operationName);
  104
+
  105
+            // assert:
  106
+            Assert.IsTrue (firstSelection.Equals (secondSelection));
  107
+            Assert.AreEqual (firstSelection.GetHashCode (), secondSelection.GetHashCode ());
  108
+        }
  109
+
  110
+                [Test]
  111
+        public void Test_Serialization_Works()
  112
+        {
  113
+            // arrange:
  114
+            string tableName = "testTable";
  115
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  116
+            Predicate predicate = new Fakes.FakePredicate();
  117
+            string operationName = "select * from testTable where 1 = 1";
  118
+            Selection selection = new Selection(table, predicate, operationName);
  119
+
  120
+            // act:
  121
+            Stream stream = SerializationUtilities.Serialize<Selection>(selection);
  122
+
  123
+            // assert:
  124
+            Assert.IsNotNull (stream);
  125
+        }
  126
+
  127
+        [Test]
  128
+        public void Test_deserialization_Works()
  129
+        {
  130
+            // arrange:
  131
+            string tableName = "testTable";
  132
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  133
+            Predicate predicate = new Fakes.FakePredicate();
  134
+            string operationName = "select * from testTable where 1 = 1";
  135
+            Selection selection = new Selection(table, predicate, operationName);
  136
+
  137
+            // act:
  138
+            Stream stream = SerializationUtilities.Serialize<Selection>(selection);
  139
+            Selection deserialized = SerializationUtilities.Deserialize<Selection>(stream);
  140
+
  141
+            // assert:
  142
+            // Assert.AreSame(and, deserialized);
  143
+            Assert.IsTrue (selection.Equals (deserialized));
  144
+        }
  145
+
  146
+        [Test]
  147
+        public void Accept_withValidArguments_delegateVisitToTheRightVisitor()
  148
+        {
  149
+            // arrange:
  150
+            object expectedResult = new object();
  151
+            IVisitContext context = GenerateStrictMock<IVisitContext>();
  152
+            string tableName = "testTable";
  153
+            Relation table = new Fakes.FakeRelation(RelationType.BaseRelation, tableName);
  154
+            Predicate predicate = new Fakes.FakePredicate();
  155
+            string operationName = "select * from testTable where 1 = 1";
  156
+            Selection selection = new Selection(table, predicate, operationName);
  157
+
  158
+            IVisitor<object, Selection> selectionVisitor =
  159
+            GenerateStrictMock<IVisitor<object, Selection>>();
  160
+            selectionVisitor.Expect(v => v.Visit(selection, context)).Return(expectedResult).Repeat.Once();
  161
+            IVisitor<object> visitor = GenerateStrictMock<IVisitor<object>>();
  162
+            visitor.Expect(v => v.GetVisitor(selection)).Return(selectionVisitor).Repeat.Once ();
  163
+
  164
+            // act:
  165
+            object result = selection.Accept(visitor, context);
  166
+
  167
+            // assert:
  168
+            Assert.AreSame(expectedResult, result);
  169
+        }
  170
+
  171
+    }
  172
+}
  173
+

0 notes on commit a8e94c2

Please sign in to comment.
Something went wrong with that request. Please try again.