File: Insert_Update_Delete.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (341 lines) | stat: -rw-r--r-- 13,925 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Test_NUnit;
using Test_NUnit.Linq_101_Samples;

#if MONO_STRICT
using System.Data.Linq;
#else
using DbLinq.Data.Linq;
#endif

using nwind;

// test ns Linq_101_Samples
#if MYSQL
    namespace Test_NUnit_MySql.Linq_101_Samples
#elif ORACLE && ODP
    namespace Test_NUnit_OracleODP.Linq_101_Samples
#elif ORACLE
    namespace Test_NUnit_Oracle.Linq_101_Samples
#elif POSTGRES
    namespace Test_NUnit_PostgreSql.Linq_101_Samples
#elif SQLITE
    namespace Test_NUnit_Sqlite.Linq_101_Samples
#elif INGRES
    namespace Test_NUnit_Ingres.Linq_101_Samples
#elif MSSQL && L2SQL
    namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
#elif MSSQL
    namespace Test_NUnit_MsSql.Linq_101_Samples
#elif FIREBIRD
    namespace Test_NUnit_Firebird.Linq_101_Samples
#endif
{
    [TestFixture]
    public class Insert_Update_Delete : TestBase
    {
        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Test(Description = "Insert - Simple. This sample uses the Add method to add a new Customer to the Customers Table object. The call to SubmitChanges persists this new Customer to the database.")]
        public void LinqToSqlInsert01()
        {
            Northwind db = CreateDB();

            Customer cust = db.Customers.FirstOrDefault(c => c.CustomerID == "MCSFT");
            if (cust != null)
            {
                try
                {
                    db.Customers.DeleteOnSubmit(cust);
                    db.SubmitChanges();
                }
                catch
                {
                    Assert.Ignore("Inconclusive: the object already exist. And the couldn't be removed");
                }
            }

            var q = from c in db.Customers
                    where c.Region == "WA"
                    select c;

            var newCustomer = new Customer
            {
                CustomerID = "MCSFT",
                CompanyName = "Microsoft",
                ContactName = "John Doe",
                ContactTitle = "Sales Manager",
                Address = "1 Microsoft Way",
                City = "Redmond",
                Region = "WA",
                PostalCode = "98052",
                Country = "USA",
                Phone = "(425) 555-1234",
                Fax = null
            };

            db.Customers.InsertOnSubmit(newCustomer);
            db.SubmitChanges();

            var reloadedCustomer = db.Customers.First(c => c.CustomerID == newCustomer.CustomerID);

            Assert.AreEqual(reloadedCustomer.CompanyName, newCustomer.CompanyName);
            Assert.AreEqual(reloadedCustomer.ContactName, newCustomer.ContactName);
            Assert.AreEqual(reloadedCustomer.ContactTitle, newCustomer.ContactTitle);
            Assert.AreEqual(reloadedCustomer.Address, newCustomer.Address);
            Assert.AreEqual(reloadedCustomer.City, newCustomer.City);
            Assert.AreEqual(reloadedCustomer.Region, newCustomer.Region);
            Assert.AreEqual(reloadedCustomer.PostalCode, newCustomer.PostalCode);
            Assert.AreEqual(reloadedCustomer.Country, newCustomer.Country);
            Assert.AreEqual(reloadedCustomer.Phone, newCustomer.Phone);
            Assert.AreEqual(reloadedCustomer.Fax, newCustomer.Fax);

            db.Customers.DeleteOnSubmit(reloadedCustomer);
            db.SubmitChanges();
        }

#if !SQLITE
#if !DEBUG && (POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Linq101SamplesModified("The original sample didn't compile, db2 Northwind context was used for nothing")]
        [Test(Description = "Insert - 1-to-Many. This sample uses the Add method to add a new Category to the Categories table object, and a new Product to the Products Table object with a foreign key relationship to the new Category. The call to SubmitChanges persists these new objects and their relationships to the database.")]
        public void LinqToSqlInsert02()
        {
            Northwind db = CreateDB();

            var ds = new DataLoadOptions();

            ds.LoadWith<Category>(c => c.Products);
            db.LoadOptions = ds;

            var q = from c in db.Categories
                    where c.CategoryName == "Temp Widgets"
                    select c;

            var newCategory = new Category
                                {
                                    CategoryName = "Temp Widgets",
                                    Description = "Widgets are the customer-facing analogues to sprockets and cogs."
                                };

            var newProduct = new Product
            {
                ProductName = "temp Blue Widget",
                UnitPrice = 34.56m,
                Category = newCategory
            };
            db.Categories.InsertOnSubmit(newCategory);
            db.SubmitChanges();

            var reloadedProduct = db.Products.First(p => p.ProductID == newProduct.ProductID);

            Assert.AreEqual(reloadedProduct.ProductName, newProduct.ProductName);
            Assert.AreEqual(reloadedProduct.UnitPrice, newProduct.UnitPrice);
            Assert.AreEqual(reloadedProduct.Category.CategoryID, newProduct.CategoryID);

            var reloadedCategory = reloadedProduct.Category;

            Assert.AreEqual(reloadedCategory.CategoryName, newCategory.CategoryName);
            Assert.AreEqual(reloadedCategory.Description, reloadedCategory.Description);

            db.Products.DeleteOnSubmit(newProduct);
            db.Categories.DeleteOnSubmit(newCategory);
            db.SubmitChanges();
        }
#endif

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Linq101SamplesModified("The original sample didn't compile, db2 Northwind context was used for nothing")]
        [Test(Description = "Insert - Many-to-Many. This sample uses the Add method to add a new Employee to the Employees table object, a new Territory to the Territories table object, and a new EmployeeTerritory to the EmployeeTerritories table object with foreign key relationships to the new Employee and Territory. The call to SubmitChanges persists these new objects and their relationships to the database.")]
        public void LinqToSqlInsert03()
        {
            Northwind db = CreateDB();

            var ds = new DataLoadOptions();
            ds.LoadWith<Employee>(p => p.EmployeeTerritories);
            ds.LoadWith<EmployeeTerritory>(p => p.Territory);

            db.LoadOptions = ds;
            var q = from e in db.Employees where e.FirstName == "Nancy" select e;


            if (db.Employees.Any(e => e.FirstName == "Test Kira" && e.LastName == "Test Smith"))
                Assert.Ignore();


            var newEmployee = new Employee { FirstName = "Test Kira", LastName = "Test Smith" };
            var newTerritory = new Territory
            {
                TerritoryID = "12345",
                TerritoryDescription = "Test Anytown",
                Region = db.Regions.First()
            };

            var newEmployeeTerritory = new EmployeeTerritory { Employee = newEmployee, Territory = newTerritory };
            db.Employees.InsertOnSubmit(newEmployee);
            db.Territories.InsertOnSubmit(newTerritory);
            db.EmployeeTerritories.InsertOnSubmit(newEmployeeTerritory);
            db.SubmitChanges();

            // cleanup
            db.EmployeeTerritories.DeleteOnSubmit(newEmployeeTerritory);
            db.Territories.DeleteOnSubmit(newTerritory);
            db.Employees.DeleteOnSubmit(newEmployee);
            db.SubmitChanges();
        }

        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Test(Description = "Update - Simple. This sample uses SubmitChanges to persist an update made to a retrieved Customer object back to the database.")]
        public void LinqToSqlInsert04()
        {
            Northwind db = CreateDB();

            var q = from c in db.Customers
                    where c.CustomerID == "ALFKI"
                    select c;

            Customer cust = (from c in db.Customers
                             where c.CustomerID == "ALFKI"
                             select c).First();

            var oldContactTitle = cust.ContactTitle;
            cust.ContactTitle = "Vice President";
            db.SubmitChanges();

            Customer reloadedCustomer = db.Customers.First(c => c.CustomerID == cust.CustomerID);
            Assert.AreEqual(reloadedCustomer.ContactTitle, cust.ContactTitle);

            // undo
            reloadedCustomer.ContactTitle = oldContactTitle;
            db.SubmitChanges();
        }

        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Test(Description = "Update - Multiple. This sample uses SubmitChanges to persist updates made to multiple retrieved Product objects back to the database.")]
        public void LinqToSqlInsert05()
        {
            Northwind db = CreateDB();

            var q = from p in db.Products
                    where p.CategoryID.Value == 1
                    select p;

            foreach (var p in q)
                p.UnitPrice += 1.0m;

            db.SubmitChanges();


            var reloadedProducts = db.Products.Where(p => p.CategoryID.Value == 1);
            IEnumerator<Product> original = q.GetEnumerator();
            IEnumerator<Product> reloaded = reloadedProducts.GetEnumerator();

            while (original.MoveNext() && reloaded.MoveNext())
                Assert.AreEqual(original.Current.UnitPrice, reloaded.Current.UnitPrice);

            Assert.AreEqual(original.MoveNext(), reloaded.MoveNext());

            // undo
            foreach (var p in q)
                p.UnitPrice -= 1.0m;
            db.SubmitChanges();
        }


        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Test(Description = "Delete - Simple. This sample uses the Remove method to delete an OrderDetail from the OrderDetails Table object. The call to SubmitChanges persists this deletion to the database.")]
        public void LinqToSqlInsert06()
        {
            Northwind db = CreateDB();

            db.Connection.Open();
            db.Transaction = db.Connection.BeginTransaction();
            try
            {
                OrderDetail ode = db.OrderDetails.First();
                decimal orderID = ode.OrderID;
                decimal productID = ode.ProductID;


                OrderDetail order = (from c in db.OrderDetails
                                     where c.OrderID == orderID && c.ProductID == productID
                                     select c).First();

                //what happened to Table.Remove()?
                //The Add and AddAll methods are now InsertOnSubmit and InsertAllOnSubmit. The Remove and RemoveAll are now DeleteOnSubmit and DeleteAllOnSubmit.
                //http://blogs.vertigo.com/personal/petar/Blog/Lists/Posts/Post.aspx?List=9441ab3e%2Df290%2D4a5b%2Da591%2D49a8226de525&ID=3

                db.OrderDetails.DeleteOnSubmit(order); //formerly Remove(order);
                db.SubmitChanges();

                Assert.IsFalse(db.OrderDetails.Any(od => od.OrderID == orderID && od.ProductID == productID));
            }
            finally
            {
                db.Transaction.Rollback();
                db.Transaction = null;
            }
        }

        [SetUp]
        public void Setup_LinqToSqlInsert07()
        {
            //Northwind db = CreateDB();
            //var o = new Order { CustomerID = "WARTH", Employee = db.Employees.First() };
            //o.OrderDetails.Add(new OrderDetail { Discount = 0.1f, Quantity = 1, Product = db.Products.First(p => p.Discontinued) });
            //o.OrderDetails.Add(new OrderDetail { Discount = 0.2f, Quantity = 1, Product = db.Products.First(p => !p.Discontinued) });
            //db.Orders.InsertOnSubmit(o);
            //db.SubmitChanges();
        }

        [Linq101SamplesModified("Console and ObjectDummper references deleted")]
        [Test(Description = "Delete - One-to-Many. This sample uses the Remove method to delete an Order and Order Detail from the Order Details and Orders tables. First deleting Order Details and then deleting from Orders. The call to SubmitChanges persists this deletion to the database.")]
        public void LinqToSqlInsert07()
        {
            Northwind db = CreateDB();

            db.Connection.Open();
            db.Transaction = db.Connection.BeginTransaction();
            try
            {
                var orderDetails =
                    from o in db.OrderDetails
                    where o.Order.CustomerID == "WARTH"
                    select o;

                var order =
                    (from o in db.Orders
                     where o.CustomerID == "WARTH"
                     select o).FirstOrDefault();

                if (!orderDetails.Any() || order == null)
                    Assert.Ignore("Preconditions");


                foreach (var od in orderDetails)
                {
                    db.OrderDetails.DeleteOnSubmit(od); //formerly Remove(od);
                }

                db.Orders.DeleteOnSubmit(order); //formerly Remove(order);
                db.SubmitChanges();

                Assert.IsFalse(
                    db.OrderDetails.Any(od => od.Order.Customer.CustomerID == "WARTH" && od.Order.EmployeeID == 3));
                Assert.IsFalse(db.Orders.Any(ord => ord.OrderID == order.OrderID));
            }
            finally
            {
                db.Transaction.Rollback();
                db.Transaction = null;
            }
        }
    }
}