/
LocalDbClassTest.fs
88 lines (79 loc) · 2.68 KB
/
LocalDbClassTest.fs
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
//----------------------------------------------------------------------------
//
// Copyright (c) 2011 The Soma Team.
//
// This source code is subject to terms and conditions of the Apache License, Version 2.0. A
// copy of the license can be found in the License.txt file at the root of this distribution.
// By using this source code in any fashion, you are agreeing to be bound
// by the terms of the Apache License, Version 2.0.
//
// You must not remove this notice, or any other, from this software.
//----------------------------------------------------------------------------
namespace Soma.Core.IT
open System
open System.Data
open System.Data.Common
open System.Transactions
open NUnit.Framework
open Soma.Core
module LocalDbClassTest =
type Department() =
let mutable departmentId:int = 0
let mutable departmentName:string = null
let mutable versionNo:int = 0
[<Id>]
member this.DepartmentId
with get () = departmentId
and set (v) = departmentId <- v
member this.DepartmentName
with get () = departmentName
and set (v) = departmentName <- v
[<Version>]
member this.VersionNo
with get () = versionNo
and set (v) = versionNo <- v
[<AllowNullLiteral>]
type Employee() =
let mutable employeeId:int Nullable = Nullable ()
let mutable employeeName:string = null
let mutable departmentId:int Nullable = Nullable ()
let mutable versionNo:int Nullable = Nullable ()
[<Id(IdKind.Identity)>]
member this.EmployeeId
with get () = employeeId
and set (v) = employeeId <- v
member this.EmployeeName
with get () = employeeName
and set (v) = employeeName <- v
member this.DepartmentId
with get () = departmentId
and set (v) = departmentId <- v
[<Version>]
member this.VersionNo
with get () = versionNo
and set (v) = versionNo <- v
let db = LocalDb(MsSqlCe.config)
[<Test>]
let ``insert``() =
use tx = new TransactionScope ()
use con = db.CreateConnection()
let employee = Employee(EmployeeName = "Hoge")
db.Insert(con, employee)
assert_true employee.EmployeeId.HasValue
[<Test>]
let ``update``() =
use tx = new TransactionScope ()
use con = db.CreateConnection()
let employee = db.Find<Employee>(con, 1)
employee.EmployeeName <- "Hoge"
db.Update(con, employee)
let employee = db.Find<Employee>(con, 1)
assert_equal "Hoge" employee.EmployeeName
[<Test>]
let ``delete``() =
use tx = new TransactionScope ()
use con = db.CreateConnection()
let employee = db.Find<Employee>(con, 1)
db.Delete(con, employee)
let employee = db.TryFind<Employee>(con, 1)
assert_equal null employee