Skip to content

Instantly share code, notes, and snippets.

@eschneider999
Last active April 26, 2018 01:21
Show Gist options
  • Save eschneider999/0618655459d00472f6d067af20458f2d to your computer and use it in GitHub Desktop.
Save eschneider999/0618655459d00472f6d067af20458f2d to your computer and use it in GitHub Desktop.
Symbiotic ORM
Imports FrozenElephant.Symbiotic
<TestClass()>
Public Class TestsSimpleCRUD
Private m_DBTypesFactory As IDatabaseTypesFactory
''' <summary>
''' Initializes the tests.
''' </summary>
<TestInitialize()>
Public Sub Initialize()
m_DBTypesFactory = New DatabaseTypesFactorySqlServer
' Intialize database connection string
m_DBTypesFactory.ConnectionString = "Data Source=win-svr-2012r2;Initial Catalog=Symbiotic;User ID=madmax;Password=bunnies123;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;Enlist=false"
End Sub
<TestCategory("Simple_CRUD_VB")>
<TestMethod()>
Public Sub Create()
Dim newItem = New SimpleEntity
newItem.Description = "Description " + DateTime.Now.ToString()
Dim writer As IObjectWriter = m_DBTypesFactory.CreateObjectWriter()
writer.Create(newItem)
Assert.IsTrue(newItem.EntityId > 0)
End Sub
<TestCategory("Simple_CRUD_VB")>
<TestMethod()>
Public Sub Read()
' create the item first
Dim newItem = New SimpleEntity
newItem.Description = "Description " + DateTime.Now.ToString()
Dim writer As IObjectWriter = m_DBTypesFactory.CreateObjectWriter()
writer.Create(newItem)
'read the record to confirm it was created
Dim loader As IObjectLoader = m_DBTypesFactory.CreateObjectLoader()
Dim item As SimpleEntity = loader.ObtainItem(Of SimpleEntity)(newItem.EntityId)
Assert.IsNotNull(item)
End Sub
<TestCategory("Simple_CRUD_VB")>
<TestMethod()>
Public Sub ReadItems()
Dim loader As IObjectLoader = m_DBTypesFactory.CreateObjectLoader()
Dim sql As String = "Select * from simpleEntities"
Dim query As ISqlQuery = m_DBTypesFactory.CreateSqlQuery(Sql, "My simple sql")
Dim items As IList(Of SimpleEntity) = loader.ObtainItems(Of SimpleEntity)(query)
Assert.IsNotNull(items)
Assert.IsTrue(items.Count > 0)
End Sub
<TestCategory("Simple_CRUD_VB")>
<TestMethod()>
Public Sub Update()
' create the item first
Dim newItem = New SimpleEntity
newItem.Description = "Description " + DateTime.Now.ToString()
Dim writer As IObjectWriter = m_DBTypesFactory.CreateObjectWriter()
writer.Create(newItem)
'update the item
Dim newDesc As String = "Updated " + DateTime.Now.ToString()
newItem.Description = newDesc
writer.Update(newItem)
'read the record to confirm update
Dim loader As IObjectLoader = m_DBTypesFactory.CreateObjectLoader()
Dim loadedItem As SimpleEntity = loader.ObtainItem(Of SimpleEntity)(newItem.EntityId)
Assert.AreEqual(loadedItem.Description, newDesc)
End Sub
<TestCategory("Simple_CRUD_VB")>
<TestMethod()>
Public Sub InsertUpdate()
Dim loader As IObjectLoader = m_DBTypesFactory.CreateObjectLoader()
' create the item first
Dim newItem = New SimpleEntity
newItem.Description = "Description " + DateTime.Now.ToString()
Dim writer As IObjectWriter = m_DBTypesFactory.CreateObjectWriter()
writer.InsertUpdate(newItem)
Dim createdItem As SimpleEntity = loader.ObtainItem(Of SimpleEntity)(newItem.EntityId)
Assert.IsNotNull(createdItem)
'update the item
Dim newDesc As String = "Updated " + DateTime.Now.ToString()
newItem.Description = newDesc
writer.InsertUpdate(newItem)
'read the record to confirm update
Dim updatedItem As SimpleEntity = loader.ObtainItem(Of SimpleEntity)(newItem.EntityId)
Assert.AreEqual(updatedItem.Description, newDesc)
End Sub
<TestCategory("Simple_CRUD_VB")>
<TestMethod()>
Public Sub Delete()
Dim loader As IObjectLoader = m_DBTypesFactory.CreateObjectLoader()
' create the item first
Dim newItem = New SimpleEntity
newItem.Description = "Description " + DateTime.Now.ToString()
Dim writer As IObjectWriter = m_DBTypesFactory.CreateObjectWriter()
writer.Create(newItem)
'read the record to confirm it was created
Dim item As SimpleEntity = loader.ObtainItem(Of SimpleEntity)(newItem.EntityId)
Assert.IsNotNull(item)
'delete the item
writer.Delete(item)
'read the record to confirm it was deleted
Dim deletedItem As SimpleEntity = loader.ObtainItem(Of SimpleEntity)(item.EntityId)
Assert.IsNull(deletedItem)
End Sub
''' <summary>
''' A Simple class to demonstrate basic crud operations.
''' </summary>
<Serializable>
<DatabaseTable("SimpleEntities")>
<DebuggerDisplay("SimpleEntity: EntityId= {EntityId}, Description= {Description}")>
Public Class SimpleEntity
<DatabaseColumnAttribute("Id", IsPrimaryKey:=True, IsIdentityColumn:=True)>
Public Property EntityId() As Long
<DatabaseColumnAttribute("Description")>
Public Property Description As String
End Class
End Class
/****** Sql Server Script used to create table needed for the TestSimpleCRUD unit tests ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SimpleEntities](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Description] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_SimpleEntities] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment