DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TableQueryTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 
4 using Deveel.Data.Mapping;
5 using Deveel.Data.Sql;
7 using Deveel.Data.Sql.Objects;
8 using Deveel.Data.Sql.Tables;
9 using Deveel.Data.Types;
10 
11 using NUnit.Framework;
12 
13 namespace Deveel.Data.Linq {
14  [TestFixture]
15  public class TableQueryTests : ContextBasedTest {
16  private QueryContext Context { get; set; }
17  protected override ISession CreateAdminSession(IDatabase database) {
18  using (var session = base.CreateAdminSession(database)) {
19  using (var query = session.CreateQuery()) {
20  CreateTestTable(query);
21  AddTestData(query);
22 
23  query.Commit();
24  }
25  }
26 
27  return base.CreateAdminSession(database);
28  }
29 
30  private void CreateTestTable(IQuery context) {
31  var tableInfo = new TableInfo(ObjectName.Parse("APP.people"));
32  var idColumn = tableInfo.AddColumn("id", PrimitiveTypes.Integer());
33  idColumn.DefaultExpression = SqlExpression.FunctionCall("UNIQUEKEY",
34  new SqlExpression[] { SqlExpression.Constant(tableInfo.TableName.FullName) });
35  tableInfo.AddColumn("first_name", PrimitiveTypes.String());
36  tableInfo.AddColumn("last_name", PrimitiveTypes.String());
37  tableInfo.AddColumn("birth_date", PrimitiveTypes.DateTime());
38  tableInfo.AddColumn("active", PrimitiveTypes.Boolean());
39 
40  context.CreateTable(tableInfo);
41  context.AddPrimaryKey(tableInfo.TableName, "id", "PK_PEOPLE_TABLE");
42  }
43 
44  private void AddTestData(IQuery context) {
45  var table = context.GetMutableTable(ObjectName.Parse("APP.people"));
46  var row = table.NewRow();
47 
48  // row.SetValue("id", DataObject.Integer(0));
49  row.SetDefault(0, context);
50  row.SetValue("first_name", DataObject.String("John"));
51  row.SetValue("last_name", DataObject.String("Doe"));
52  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1977, 01, 01)));
53  row.SetValue("active", DataObject.Boolean(false));
54  table.AddRow(row);
55 
56  row = table.NewRow();
57 
58  // row.SetValue("id", DataObject.Integer(1));
59  row.SetDefault(0, context);
60  row.SetValue("first_name", DataObject.String("Jane"));
61  row.SetValue("last_name", DataObject.String("Doe"));
62  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1978, 11, 01)));
63  row.SetValue("active", DataObject.Boolean(true));
64  table.AddRow(row);
65 
66  row = table.NewRow();
67 
68  // row.SetValue("id", DataObject.Integer(2));
69  row.SetDefault(0, context);
70  row.SetValue("first_name", DataObject.String("Roger"));
71  row.SetValue("last_name", DataObject.String("Rabbit"));
72  row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1985, 05, 05)));
73  row.SetValue("active", DataObject.Boolean(true));
74  table.AddRow(row);
75 
76  context.Commit();
77  }
78 
79  protected override void OnSetUp(string testName) {
81  }
82 
83  [Test]
84  public void FindById() {
85  Person entity = null;
86  Assert.DoesNotThrow(() => entity = Context.Table<Person>().FindById(1));
87  Assert.IsNotNull(entity);
88  Assert.AreEqual(1, entity.Id);
89  Assert.AreEqual("John", entity.FirstName);
90  }
91 
92  [Test]
93  public void QueryById() {
94  Person entity = null;
95  Assert.DoesNotThrow(() => entity = Context.Table<Person>().FirstOrDefault(x => x.Id == 1));
96  Assert.IsNotNull(entity);
97  Assert.AreEqual(1, entity.Id);
98  Assert.AreEqual("John", entity.FirstName);
99  }
100 
102  public TestQueryContext(IQuery context)
103  : base(context) {
104  }
105 
106  protected override void OnBuildMap(MappingContext mappingContext) {
107  mappingContext.Map<Person>()
108  .ToTable("people");
109  mappingContext.Map<Person>()
110  .Member(person => person.Id)
111  .HasName("id")
112  .IsPrimaryKey();
113  mappingContext.Map<Person>()
114  .Member(person => person.FirstName)
115  .HasName("first_name")
116  .IsNotNull();
117  }
118  }
119 
120  #region Person
121 
122  class Person {
123  public int Id { get; set; }
124 
125  public string FirstName { get; set; }
126  }
127 
128  #endregion
129  }
130 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static DataObject Date(DateTimeOffset value)
Definition: DataObject.cs:600
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
override void OnSetUp(string testName)
static BooleanType Boolean()
void AddTestData(IQuery context)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
override ISession CreateAdminSession(IDatabase database)
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
The representation of a single database in the system.
Definition: IDatabase.cs:40
void AddColumn(ColumnInfo column)
Adds a new column to the table at the last position of the columns list in the table metadata...
Definition: TableInfo.cs:230
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
void CreateTestTable(IQuery context)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static SqlFunctionCallExpression FunctionCall(ObjectName functionName)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
override void OnBuildMap(MappingContext mappingContext)