Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
[client][managed][offline] allow underscore and hyphen in queryId
Browse files Browse the repository at this point in the history
  • Loading branch information
hasankhan committed Nov 20, 2014
1 parent 4761bcf commit 7d192a3
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private async Task InitializeConfig()
if (!MobileServiceLocalSystemTables.All.Contains(table.Key))
{
// preserve system properties setting for non-system tables
string name = String.Format("systemProperties_{0}", table.Key);
string name = String.Format("systemProperties|{0}", table.Key);
string value = ((int)table.Value.SystemProperties).ToString();
await this.SaveSetting(name, value);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
<value>The store is already initialized.</value>
</data>
<data name="MobileServiceSyncTable_InvalidQueryId" xml:space="preserve">
<value>The query id must start with a letter and can contain only letters and digits.</value>
<value>The query id must start with a letter and can contain only letters, digits, hyphen or underscore.</value>
</data>
<data name="MobileServiceSyncTable_IncrementalPullWithOrderNotAllowed" xml:space="preserve">
<value>Incremental pull query must not have orderby clause.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ private async Task<string> GetSettingAsync(string key, string defaultValue)

private string GetDeltaTokenKey(string tableName, string queryId)
{
return "deltaToken_" + tableName + "_" + queryId;
return "deltaToken|" + tableName + "|" + queryId;
}

private static string GetSystemPropertiesKey(string tableName)
{
return "systemProperties_" + tableName;
return "systemProperties|" + tableName;
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.WindowsAzure.MobileServices.Sync
{
internal class MobileServiceSyncTable : IMobileServiceSyncTable
{
private static readonly Regex queryIdRegex = new Regex("^[a-zA-Z][a-zA-Z0-9]{0,24}$");
private static readonly Regex queryIdRegex = new Regex("^[a-zA-Z][a-zA-Z0-9_-]{0,24}$");
private MobileServiceSyncContext syncContext;

public MobileServiceClient MobileServiceClient { get; private set; }
Expand Down Expand Up @@ -126,7 +126,7 @@ private static string EnsureIdIsString(JObject instance)
return EnsureIdIsString(id);
}

private static void ValidateQueryId(string queryId)
internal static void ValidateQueryId(string queryId)
{
if (string.IsNullOrWhiteSpace(queryId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,19 @@ public async Task Upsert_Succeeds_IfCaseIsDifferentButNameIsSame()

await store.InitializeAsync();

await store.UpsertAsync("ITEMwithDATE", new[]{new JObject()
await store.UpsertAsync("ITEMwithDATE", new[]
{
{ "ID", Guid.NewGuid() },
{"dATE", DateTime.UtcNow }
}}, ignoreMissingColumns: false);
new JObject()
{
{ "ID", Guid.NewGuid() },
{"dATE", DateTime.UtcNow }
}
}, ignoreMissingColumns: false);
}
}

[AsyncTestMethod]
public async Task PullAsync_DoesIncrementalSync_WhenQueryKeyIsSpecified()
public async Task PullAsync_DoesIncrementalSync_WhenQueryIdIsSpecified()
{
ResetDatabase(TestTable);

Expand Down Expand Up @@ -304,7 +307,7 @@ public async Task PullAsync_DoesIncrementalSync_WhenQueryKeyIsSpecified()
}

[AsyncTestMethod]
public async Task PullAsync_DoesIncrementalSync_WhenQueryKeyIsSpecified_WithoutCache()
public async Task PullAsync_DoesIncrementalSync_WhenQueryIdIsSpecified_WithoutCache()
{
ResetDatabase(TestTable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SyncSettingsManagerTests : TestBase
{
public static string TestDbName = SQLiteStoreTests.TestDbName;
private const string TestTable = "todoItem";
private const string TestQueryKey = "abc";
private const string TestQueryId = "abc";


[AsyncTestMethod]
Expand All @@ -18,13 +18,13 @@ public async Task ResetDeltaTokenAsync_ResetsTheToken()
MobileServiceSyncSettingsManager settings = await GetSettingManager();

DateTimeOffset saved = new DateTime(2014, 7, 24, 3, 4, 5, DateTimeKind.Local);
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, saved);
await settings.SetDeltaTokenAsync(TestTable, TestQueryId, saved);

DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved.ToUniversalTime());

await settings.ResetDeltaTokenAsync(TestTable, TestQueryKey);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
await settings.ResetDeltaTokenAsync(TestTable, TestQueryId);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUniversalTime());
}

Expand All @@ -33,7 +33,7 @@ public async Task GetDeltaTokenAsync_ReturnsMinValue_WhenTokenDoesNotExist()
{
MobileServiceSyncSettingsManager settings = await GetSettingManager();

DateTimeOffset token = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
DateTimeOffset token = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);

Assert.AreEqual(token, new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUniversalTime());
}
Expand All @@ -44,15 +44,15 @@ public async Task SetDeltaTokenAsync_SavesTheSetting_AsUTCDate()
MobileServiceSyncSettingsManager settings = await GetSettingManager();

DateTimeOffset saved = new DateTime(2014, 7, 24, 3, 4, 5, DateTimeKind.Local);
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, saved);
await settings.SetDeltaTokenAsync(TestTable, TestQueryId, saved);

// with cache
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved.ToUniversalTime());

// without cache
settings = await GetSettingManager(resetDb: false);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved.ToUniversalTime());
}

Expand All @@ -62,15 +62,15 @@ public async Task SetDeltaTokenAsync_SavesTheSetting()
MobileServiceSyncSettingsManager settings = await GetSettingManager();

var saved = new DateTimeOffset(2014, 7, 24, 3, 4, 5, TimeSpan.Zero);
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, saved);
await settings.SetDeltaTokenAsync(TestTable, TestQueryId, saved);

// with cache
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved);

// without cache
settings = await GetSettingManager(resetDb: false);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved);
}

Expand All @@ -81,19 +81,19 @@ public async Task SetDeltaTokenAsync_UpdatesCacheAndDatabase()

// first save
var saved = new DateTimeOffset(2014, 7, 24, 3, 4, 5, TimeSpan.Zero);
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, saved);
await settings.SetDeltaTokenAsync(TestTable, TestQueryId, saved);

// then read and update
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
await settings.SetDeltaTokenAsync(TestTable, TestQueryKey, read.AddDays(1));
DateTimeOffset read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
await settings.SetDeltaTokenAsync(TestTable, TestQueryId, read.AddDays(1));

// then read again
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved.AddDays(1));

// then read again in fresh instance
settings = await GetSettingManager(resetDb: false);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryKey);
read = await settings.GetDeltaTokenAsync(TestTable, TestQueryId);
Assert.AreEqual(read, saved.AddDays(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Table\MobileServiceSyncContextTests.cs" />
<Compile Include="Table\Query\MobileServiceTableQueryDescriptionTests.cs" />
<Compile Include="Table\Sync\MobileServiceSyncTableTests.cs" />
<Compile Include="Table\Sync\Queue\Actions\PullActionTests.cs" />
<Compile Include="Table\Sync\Queue\Actions\PushActionTests.cs" />
<Compile Include="Table\Sync\Queue\Operations\DeleteOperationTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.MobileServices.Sync;

namespace Microsoft.WindowsAzure.MobileServices.Test.Unit.Table.Sync
{
[TestClass]
public class MobileServiceSyncTableTests
{
[TestMethod]
public void ValidateQueryId_Throws_OnInvalidId()
{
var testCases = new[] { "-myitems", "_myitems", "|myitems", "s|myitems", "asdf@#$!@" };
foreach (var queryId in testCases)
{
var ex = AssertEx.Throws<ArgumentException>(() => MobileServiceSyncTable.ValidateQueryId(queryId));
Assert.AreEqual(ex.Message, "The query id must start with a letter and can contain only letters, digits, hyphen or underscore.");
}
}

[TestMethod]
public void ValidateQueryId_Succeeds_OnValidId()
{
var testCases = new[] { "myitems1", "myItems_yourItems1", "my-items123" };
foreach (var queryId in testCases)
{
MobileServiceSyncTable.ValidateQueryId(queryId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public async Task PullAsync_UpdatesDeltaToken_AfterEachResult_IfOrderByIsSupport
AssertEx.MatchUris(hijack.Requests, "http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'1970-01-01T00:00:00.0000000%2B00:00')&$orderby=__updatedAt&$skip=0&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted",
"http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'2001-02-04T00:00:00.0000000%2B00:00')&$orderby=__updatedAt&$skip=0&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted");

Assert.Equals(store.TableMap[MobileServiceLocalSystemTables.Config]["deltaToken_stringId_test_table_items"]["value"], "2001-02-04T00:00:00.0000000+00:00");
Assert.Equals(store.TableMap[MobileServiceLocalSystemTables.Config]["deltaToken|stringId_test_table|items"]["value"], "2001-02-04T00:00:00.0000000+00:00");
}

[AsyncTestMethod]
Expand Down Expand Up @@ -288,7 +288,7 @@ public async Task PullAsync_DoesNotUpdateDeltaToken_AfterEachResult_IfOrderByIsN
AssertEx.MatchUris(hijack.Requests, "http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'1970-01-01T00:00:00.0000000%2B00:00')&$skip=0&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted",
"http://www.test.com/tables/stringId_test_table?$filter=(__updatedAt ge datetimeoffset'1970-01-01T00:00:00.0000000%2B00:00')&$skip=2&$top=50&__includeDeleted=true&__systemproperties=__updatedAt%2C__version%2C__deleted");

Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("deltaToken_stringId_test_table_items"));
Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("deltaToken|stringId_test_table|items"));
}

[AsyncTestMethod]
Expand Down Expand Up @@ -771,9 +771,9 @@ private static async Task TestIncrementalPull(MobileServiceLocalStoreMock store,
hijack.AddResponseContent(@"[]");


store.TableMap[MobileServiceLocalSystemTables.Config]["systemProperties_stringId_test_table"] = new JObject
store.TableMap[MobileServiceLocalSystemTables.Config]["systemProperties|stringId_test_table"] = new JObject
{
{ MobileServiceSystemColumns.Id, "systemProperties_stringId_test_table" },
{ MobileServiceSystemColumns.Id, "systemProperties|stringId_test_table" },
{ "value", "1" }
};

Expand Down Expand Up @@ -899,15 +899,15 @@ public async Task PurgeAsync_ResetsDeltaToken_WhenQueryIdIsSpecified()
Assert.AreEqual(store.TableMap["stringId_test_table"]["def"].Value<string>("String"), "How");

// ensure delta token was updated
Assert.Equals(store.TableMap[MobileServiceLocalSystemTables.Config]["deltaToken_stringId_test_table_items"]["value"], "2001-02-04T00:00:00.0000000+00:00");
Assert.Equals(store.TableMap[MobileServiceLocalSystemTables.Config]["deltaToken|stringId_test_table|items"]["value"], "2001-02-04T00:00:00.0000000+00:00");

// now purge and forget the delta token
await table.PurgeAsync("items", null, false, CancellationToken.None);

// make sure data is purged
Assert.AreEqual(store.TableMap["stringId_test_table"].Count, 0);
// make sure delta token is removed
Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("deltaToken_stringId_test_table_items"));
Assert.IsFalse(store.TableMap[MobileServiceLocalSystemTables.Config].ContainsKey("deltaToken|stringId_test_table|items"));

// pull again
await table.PullAsync("items", table.CreateQuery());
Expand Down Expand Up @@ -983,7 +983,7 @@ public async Task PurgeAsync_DeletesOperations_WhenThereIsOperationInTable_AndFo
await service.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());

// put a dummy delta token
string deltaKey = "deltaToken_someTable_abc";
string deltaKey = "deltaToken|someTable|abc";
store.TableMap[MobileServiceLocalSystemTables.Config] = new Dictionary<string, JObject>() { { deltaKey, new JObject() } };

// insert an item but don't push
Expand Down

0 comments on commit 7d192a3

Please sign in to comment.