public void SQliteDatasetsTests()
{
string dbPath = Path.Combine(PCLStorage.FileSystem.Current.LocalStorage.Path, DB_FILE_NAME);
string randomId = "old";
string randomDataset = Guid.NewGuid().ToString();
using (SQLiteLocalStorage storage = new SQLiteLocalStorage())
{
storage.WipeData();
storage.CreateDataset(randomId, randomDataset);
storage.PutValue(randomId, randomDataset, "Voldemort", "He who must not be named");
using (SQLiteConnection connection = new SQLiteConnection(dbPath))
{
string query = "select count(*) from datasets where dataset_name = @dataset_name and identity_id = @identity_id ";
using (var sqliteStatement = connection.Prepare(query))
{
BindData(sqliteStatement, randomDataset, randomId);
if(sqliteStatement.Step()==SQLiteResult.ROW)
{
var count = sqliteStatement.GetInteger(0);
Assert.IsTrue(count == 1);
}
else
{
Assert.Fail();
}
}
query = "select count(*) from records where dataset_name = @dataset_name and identity_id = @identity_id ";
using( var sqliteStatement = connection.Prepare(query))
{
BindData(sqliteStatement, randomDataset, randomId);
if (sqliteStatement.Step() == SQLiteResult.ROW)
{
var count = sqliteStatement.GetInteger(0);
Assert.IsTrue(count == 1);
}
else
{
Assert.Fail();
}
}
}
var datasets = storage.GetDatasetMetadata(randomId);
Assert.IsTrue(datasets.Count == 1);
var Id = "new";
storage.ChangeIdentityId(randomId, Id);
randomId = Id;
using (SQLiteConnection connection = new SQLiteConnection(dbPath))
{
var query = "select count(*) from datasets where dataset_name = @dataset_name and identity_id = @identity_id ";
using (var sqliteStatement = connection.Prepare(query))
{
BindData(sqliteStatement, randomDataset, randomId);
if (sqliteStatement.Step() == SQLiteResult.ROW)
{
var count = sqliteStatement.GetInteger(0);
Assert.IsTrue(count == 1);
}
else
{
Assert.Fail();
}
}
query = "select count(*) from records where dataset_name = @dataset_name and identity_id = @identity_id ";
using (var sqliteStatement = connection.Prepare(query))
{
BindData(sqliteStatement, randomDataset, randomId);
if (sqliteStatement.Step() == SQLiteResult.ROW)
{
var count = sqliteStatement.GetInteger(0);
Assert.IsTrue(count == 1);
}
else
{
Assert.Fail();
}
}
}
storage.DeleteDataset(randomId, randomDataset);
using (SQLiteConnection connection = new SQLiteConnection(dbPath))
{
var query = "select last_sync_count from datasets where dataset_name = @dataset_name and identity_id = @identity_id";
using (var sqliteStatement = connection.Prepare(query))
{
BindData(sqliteStatement, randomDataset, randomId);
if (sqliteStatement.Step() == SQLiteResult.ROW)
{
var count = sqliteStatement.GetInteger(0);
Assert.IsTrue(count == -1);
}
else
{
Assert.Fail();
}
}
}
Assert.IsNotNull(storage.GetDatasetMetadata(randomId)[0]);
}
}