File: Transactions.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 (148 lines) | stat: -rw-r--r-- 4,672 bytes parent folder | download | duplicates (2)
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
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Test_NUnit;

using System.Data;
using System.Data.Common;

using nwind;

// test ns 
#if MYSQL
    namespace Test_NUnit_MySql
#elif ORACLE && ODP
    namespace Test_NUnit_OracleODP
#elif ORACLE
    namespace Test_NUnit_Oracle
#elif POSTGRES
    namespace Test_NUnit_PostgreSql
#elif SQLITE
    namespace Test_NUnit_Sqlite
#elif INGRES
    namespace Test_NUnit_Ingres
#elif MSSQL && L2SQL
    namespace Test_NUnit_MsSql_Strict
#elif MSSQL
    namespace Test_NUnit_MsSql
#elif FIREBIRD
    namespace Test_NUnit_Firebird
#endif
{
    [TestFixture]
    public class Transactions : TestBase
    {
        [SetUp]
        public void Setup_LinqToSqlInsert07()
        {
            Northwind db = CreateDB();
            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)
            {
                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.ProductID % 2 == 0) });
                o.OrderDetails.Add(new OrderDetail { Discount = 0.2f, Quantity = 1, Product = db.Products.First(p => p.ProductID % 2 != 0) });
                db.Orders.InsertOnSubmit(o);
                db.SubmitChanges();
            }
        }

        [Test]
        public void TransactionRollbackDelete()
        {
            Northwind db = CreateDB();
            DbTransaction t = BeginTransaction(db);

            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");

                db.OrderDetails.DeleteAllOnSubmit(orderDetails); //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
            {
                t.Rollback();
            }
        }

        private DbTransaction BeginTransaction(Northwind db)
        {
            db.Connection.Open();
            DbTransaction t = db.Connection.BeginTransaction();
            db.Transaction = t;

            return t;
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void TransactionCheckAndRollbackInsert()
        {
            Northwind db = CreateDB();
            DbTransaction t = BeginTransaction(db);

            var cust = new Customer();
            int beforeCustomersCount = db.Customers.Count();

            string id = new object().GetHashCode().ToString().Substring(0, 5);
            cust.CustomerID = id;
            cust.Country = "Spain";
            cust.CompanyName = "Coco";

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

            int afterCustomercount = db.Customers.Count();
            Assert.IsTrue(beforeCustomersCount + 1 == afterCustomercount);

            t.Rollback();

            afterCustomercount = db.Customers.Count();
            Assert.IsTrue(beforeCustomersCount == afterCustomercount);

            // The Count is correct.  However, DataContext doesn't know that the 
            // transaction was aborted, and will satisfy the following from
            // an internal cache
            var customer = db.Customers.FirstOrDefault(c => c.CustomerID == id);
            Assert.IsNotNull(customer);

            // Let's let DataContext know that it doesn't exist anymore.
            db.Customers.DeleteOnSubmit(customer);
            db.SubmitChanges(); // Note no exception from deleting a non-existent entity

            customer = db.Customers.FirstOrDefault(c => c.CustomerID == id);
            Assert.IsNull(customer);
        }
    }
}