using System;
using System.Diagnostics;
using System.Threading;
using AzureStorageMapper.TableStorage;
using AzureStorageMapper.TableStorage.Attributes;
using NUnit.Framework;

namespace AzureStorageMapper.Tests.TableStorage.TableEntityTests
{
    [TestFixture]
    public class CRUDTests
    {
        SampleTableModel model;

        [SetUp]
        public void SetUpTest()
        {
            model = new SampleTableModel("default", Guid.NewGuid().ToString());
            model.ColumnOne = "col 1";
            model.ColumnTwo = 222;
            model.ColumnThree = 333333L;

            Trace.TraceInformation("Creatinig Table ...");
            SampleTableModel.CreateTable<SampleTableModel>();

            Trace.TraceInformation("Sleep for 10 seconds ...");
            Thread.Sleep(TimeSpan.FromSeconds(10));
        }

        [TearDown]
        public void TearDownTest()
        {
            Trace.TraceInformation("Sleep for 10 seconds ...");
            Thread.Sleep(TimeSpan.FromSeconds(10));

            Trace.TraceInformation("Deleting Table ...");
            SampleTableModel.DeleteTable<SampleTableModel>();
        }

        [Test]
        public void CURD_Tests()
        {
            Trace.TraceInformation("Saving data ...");
            model.Save();

            Trace.TraceInformation("Sleep for 10 seconds ...");
            Thread.Sleep(TimeSpan.FromSeconds(10));

            Trace.TraceInformation("Retriving saved data ...");
            SampleTableModel readModel = SampleTableModel.GetEntity<SampleTableModel>(model.PartitionKey, model.RowKey);

            Trace.TraceInformation("Comparing updated data ...");
            Assert.AreEqual(model.ColumnOne, readModel.ColumnOne);
            Assert.AreEqual(model.ColumnTwo, readModel.ColumnTwo);
            Assert.AreEqual(model.ColumnThree, readModel.ColumnThree);

            Trace.TraceInformation("Sleep for 5 seconds ...");
            Thread.Sleep(TimeSpan.FromSeconds(5));

            Trace.TraceInformation("Updating data ...");
            readModel.ColumnOne = "Updated column one";
            readModel.ColumnTwo = 2222;
            readModel.ColumnThree = 3333333L;
            readModel.Update();

            Trace.TraceInformation("Sleep for 10 seconds ...");
            Thread.Sleep(TimeSpan.FromSeconds(10));

            Trace.TraceInformation("Comparing updated data ...");
            SampleTableModel readModel2 = SampleTableModel.GetEntity<SampleTableModel>(model.PartitionKey, model.RowKey);
            Assert.AreEqual(readModel.ColumnOne, readModel2.ColumnOne);
            Assert.AreEqual(readModel.ColumnTwo, readModel2.ColumnTwo);
            Assert.AreEqual(readModel.ColumnThree, readModel2.ColumnThree);

            Trace.TraceInformation("Sleep for 10 seconds ...");
            Thread.Sleep(TimeSpan.FromSeconds(10));

            readModel2.Delete();
        }

        [TableModel(Name = "CRUDTests")]
        internal class SampleTableModel : TableEntity
        {
            [TableColumn]
            public string ColumnOne { get; set; }

            [TableColumn(Name = "Column2")]
            public int ColumnTwo { get; set; }

            [TableColumn]
            public long ColumnThree { get; set; }

            public SampleTableModel(string partitionKey, string rowKey)
                : base(partitionKey, rowKey)
            {
            }
        }
    }
}

Last edited Dec 1, 2011 at 7:21 AM by shrimpy, version 1

Comments

No comments yet.