Skip to content

add TypeProviderUser.TSQL.fsproj with a set of tests running on sql server #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/AssemblyInfo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace TypeProviderUser.SQLite.AssemblyInfo

open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[<assembly: AssemblyTitle("TypeProviderUser.TSQL")>]
[<assembly: AssemblyDescription("")>]
[<assembly: AssemblyConfiguration("")>]
[<assembly: AssemblyCompany("")>]
[<assembly: AssemblyProduct("TypeProviderUser.TSQL")>]
[<assembly: AssemblyCopyright("Copyright © 2017")>]
[<assembly: AssemblyTrademark("")>]
[<assembly: AssemblyCulture("")>]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[<assembly: ComVisible(false)>]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[<assembly: Guid("0f05cbc1-ffaa-41b0-89fc-db11d13d823c")>]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [<assembly: AssemblyVersion("1.0.*")>]
[<assembly: AssemblyVersion("1.0.0.0")>]
[<assembly: AssemblyFileVersion("1.0.0.0")>]

do
()
80 changes: 80 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/Shared.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace TypeProviderUser.TSQL
open Rezoom.SQL
open Rezoom.SQL.Mapping
open Rezoom.SQL.Migrations
open Rezoom.SQL.Synchronous
open System.IO

type TestModel = SQLModel<".">

type TestData = SQL<"""
delete from ArticleComments;
delete from Articles;
delete from Users;
delete from Pictures;

insert into Pictures(SHA256, PNGData)
values ( x'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
, x''
);
insert into Pictures(SHA256, PNGData)
values ( x'0000000000000000000000000000000000000000000000000000000000000000'
, x''
);

vendor tsql {
set identity_insert dbo.Users on
};

insert into Users(Id,Name, Email, ProfilePictureSHA256, Created, RandomId)
values ( 1, 'Homer'
, '[email protected]'
, x'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
, 2017-01-01T00:00:00
, (newid())
);
insert into Users(Id, Name, Email, ProfilePictureSHA256, Created, RandomId)
values ( 2, 'Marge'
, '[email protected]'
, x'0000000000000000000000000000000000000000000000000000000000000000'
, 2017-01-01T00:00:00
, (newid())
);

vendor tsql {
set identity_insert dbo.Users off
};

insert into Articles(AuthorId, ArticleTitle, ArticleText)
values ( (select Id from Users where Name = 'Homer')
, 'My first review as a food critic.'
, 'Mmmmmmm... donuts'
);
insert into Articles(AuthorId, ArticleTitle, ArticleText)
values ( (select Id from Users where Name = 'Homer')
, 'My second review as a food critic.'
, 'Mmmmmmm... beer'
);
insert into ArticleComments(AuthorId, ArticleId, CommentText)
values ( (select Id from Users where Name = 'Marge')
, (select Id from Articles where ArticleTitle = 'My first review as a food critic.')
, 'Are you sure you should be eating so many donuts?'
);
insert into ArticleComments(AuthorId, ArticleId, CommentText)
values ( (select Id from Users where Name = 'Marge')
, (select Id from Articles where ArticleTitle = 'My second review as a food critic.')
, 'Are you sure you should be drinking so many beers?'
);
""">

[<AutoOpen>]
module Helpers =
let runOnTestData (cmd : Command<'a>) =
TestModel.Migrate(MigrationConfig.Default)
do
use cxt = new ConnectionContext()
TestData.Command().Execute(cxt)
TestModel.Migrate(MigrationConfig.Default)
use cxt = new ConnectionContext()
TestData.Command().Execute(cxt)
cmd.Execute(cxt)
167 changes: 167 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/TestSelects.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
module TypeProviderUser.TSQL.TestSelects
open System
open System.Threading
open System.Threading.Tasks
open NUnit.Framework
open FsUnit
open Rezoom
open Rezoom.SQL
open Rezoom.SQL.Plans
open MBrace.FsPickler
open Rezoom

type TestEqualInteger = SQL<"""
select * from Users where Id = @userId
""">

[<Test>]
let ``test = integer`` () =
let results = TestEqualInteger.Command(1L) |> runOnTestData
printfn "%A" results
Assert.AreEqual
( [ "Homer" ]
, [ for r in results -> r.Name ] |> List.sort
)

type TestInInteger = SQL<"""
select * from Users where Id in @userIds
""">

[<Test>]
let ``test in integer`` () =
let results = TestInInteger.Command([| 1L; 2L |]) |> runOnTestData
printfn "%A" results
Assert.AreEqual
( [ "Homer"; "Marge" ]
, [ for r in results -> r.Name ] |> List.sort
)

type TestInByteArrays = SQL<"""
select * from Pictures where SHA256 in @hashes
""">

[<Test>]
let ``test in byte arrays`` () =
let results = TestInByteArrays.Command([| Array.create 32 0uy; Array.create 32 0xffuy |]) |> runOnTestData
printfn "%A" results
Assert.AreEqual([ [||]; [||] ], [ for r in results -> r.PNGData ])

type TestDateTimeParameter = SQL<"""
select * from Users where Created > @created
""">

[<Test>]
let ``test datetime parameter`` () =
let results = TestDateTimeParameter.Command(DateTime.UtcNow) |> runOnTestData
printfn "%A" results

type TestOptionalDateTimeParameter = SQL<"""
select * from Users where Created > @created or @created is null
""">

[<Test>]
let ``test optional datetime parameter`` () =
let results = TestOptionalDateTimeParameter.Command(Some DateTime.UtcNow) |> runOnTestData
printfn "%A" results

type TestGuidParameter = SQL<"""
select * from Users where RandomId = @id;
""">

[<Test>]
let ``test guid parameter`` () =
let results = TestGuidParameter.Command(Guid.NewGuid()) |> runOnTestData
printfn "%A" results

type TestOptionalGuidParameter = SQL<"""
select * from Users where RandomId = @id or @id is null
""">


type TestInEmptySet = SQL<"""
select * from Users where RandomId in @ids
""">

[<Test>]
let ``test in empty set`` () =
let results = TestInEmptySet.Command([||]) |> runOnTestData
Assert.AreEqual(0, results.Count)
printfn "%A" results

[<Test>]
let ``test optional guid parameter`` () =
let results = TestOptionalGuidParameter.Command(Some (Guid.NewGuid())) |> runOnTestData
printfn "%A" results

type TestEmptyMany = SQL<"""
select p.*, many Children(c.*)
from Users p
left join Users c on false
""">

[<Test>]
let ``test empty many`` () =
let results = TestEmptyMany.Command() |> runOnTestData
Assert.AreEqual(2, results.Count)
for result in results do
Assert.AreEqual(0, result.Children.Count)
printfn "%A" results

[<Test>]
let ``replay works`` () =
let plan =
plan {
let! r1 = TestInInteger.Command([| 1L |]).Plan()
let! r2 = TestInInteger.Command([| 2L |]).Plan()
return r1.[0].Email, r2.[0].Email
}
let config = Execution.ExecutionConfig.Default
let serializer =
let bin = FsPickler.CreateBinarySerializer()
{ new Replay.IReplaySerializer with
member __.Serialize(o) = bin.Pickle(o)
member __.Deserialize(o) = bin.UnPickle(o)
}
let mutable saved = None
let save state arr =
saved <- Some (arr())
let recording =
Replay.RecordingExecutionStrategy.Create
( Execution.defaultExecutionStrategy
, serializer
, save
)
let played = recording.Execute(config, plan, CancellationToken.None).Result
match saved with
| None -> failwith "not saved"
| Some blob ->
let replayed = (Replay.replay config serializer blob).Result
if played = unbox replayed then
()
else failwith "not equal"

type InsertPicture = SQL<"insert into Pictures row SHA256 = @sha, PNGData = @png">

[<Test>]
let ``lotsa parameters`` () =
let task =
plan {
let g() = Guid.NewGuid().ToByteArray()
for i in batch [0..2000] do
do! InsertPicture.Command(g(), g()).Plan()
} |> Execution.execute Execution.ExecutionConfig.Default
task.Wait()

open Rezoom.SQL.Raw
open System.Data

type RawSQLQuery = SQL<"""
select * from Users where unsafe_inject_raw(@whereClause)
""">

[<Test>]
let ``test raw sql parameter`` () =
let results =
RawSQLQuery.Command(whereClause = [| sql "1="; arg 1 |]) |> runOnTestData
for result in results do
printfn "%A" result.Email
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net45</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Include="app.config" />
<Content Include="rzsql.json" />
<None Include="packages.config" />
<None Include="V1.model.sql" />
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Shared.fs" />
<Compile Include="TestSelects.fs" />
</ItemGroup>

<ItemGroup>
<Reference Include="Rezoom">
<HintPath>..\..\Rezoom.SQL.Provider\bin\Debug\Rezoom.dll</HintPath>
</Reference>
<Reference Include="Rezoom.SQL.Mapping">
<HintPath>..\..\Rezoom.SQL.Provider\bin\Debug\Rezoom.SQL.Mapping.dll</HintPath>
</Reference>
<Reference Include="Rezoom.SQL.Provider">
<HintPath>..\..\Rezoom.SQL.Provider\bin\Debug\Rezoom.SQL.Provider.dll</HintPath>
</Reference>
<PackageReference Include="FSharp.Core" Version="4.2.1" />
<PackageReference Include="FsPickler" Version="3.2.0" />
<PackageReference Include="FsUnit" Version="3.0.0" />
<PackageReference Include="NUnit" Version="3.6.0" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/V1.model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
create table Pictures
( SHA256 binary(32) primary key
, PNGData binary(4000)
);

create table Users
( Id int64 primary key autoincrement
, Name string(80)
, Email string(254)
, ProfilePictureSHA256 binary(32) null references Pictures(SHA256)
, Created datetime
, RandomId guid
);

create table Articles
( Id int64 primary key autoincrement
, AuthorId int64 references Users(Id)
, ArticleTitle string(80)
, ArticleText string(4000)
);

create index IX_Articles_AuthorId on Articles(AuthorId);

create table ArticleComments
( Id int64 primary key autoincrement
, ArticleId int64 references Articles(Id)
, AuthorId int64 references Users(Id)
, CommentText string(512)
);

create index IX_ArticleComments_AuthorId on ArticleComments(AuthorId);

14 changes: 14 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.1.0" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<connectionStrings>
<add name="rzsql" providerName="System.Data.Sqlclient" connectionString="Data Source=(localdb)\v12.0;Integrated Security=true;Initial Catalog=rzsql" />
</connectionStrings>
</configuration>
8 changes: 8 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FSharp.Core" version="4.2.1" targetFramework="net45" />
<package id="FsUnit" version="3.0.0" targetFramework="net45" />
<package id="NUnit" version="3.6.0" targetFramework="net45" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net45" />
<package id="System.ValueTuple" version="4.3.1" targetFramework="net461" />
</packages>
3 changes: 3 additions & 0 deletions src/TypeProviderUsers/TypeProviderUser.TSQL/rzsql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"backend": "tsql"
}
Loading