Skip to content

Commit 594c4d1

Browse files
committed
Added a Views tests to the C# regression tests
1 parent 599229d commit 594c4d1

9 files changed

Lines changed: 318 additions & 1 deletion

File tree

sdks/csharp/examples~/regression-tests/client/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ void OnSubscriptionApplied(SubscriptionEventContext context)
133133
ValidateBTreeIndexes(ctx);
134134
waiting--;
135135
});
136+
137+
// Views test
138+
Log.Debug("Calling View");
139+
var viewRows = context.Db.GetUserByContext.Iter().FirstOrDefault();
140+
Debug.Assert(viewRows != null && viewRows.IdentityString == context.Identity?.ToString());
141+
142+
Log.Debug("Calling Anonymous View");
143+
var anonViewRows = context.Db.GetUserByString.RemoteQuery("Where IdentityString = identityStringExample");
144+
Debug.Assert(anonViewRows != null && anonViewRows.Result.Length > 0);
136145
}
137146

138147
System.AppDomain.CurrentDomain.UnhandledException += (sender, args) =>

sdks/csharp/examples~/regression-tests/client/module_bindings/Reducers/ClientConnected.g.cs

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/client/module_bindings/Reducers/CreateNewUser.g.cs

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/client/module_bindings/SpacetimeDBClient.g.cs

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/client/module_bindings/Tables/GetUserByContext.g.cs

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/client/module_bindings/Tables/GetUserByString.g.cs

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/client/module_bindings/Tables/User.g.cs

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/client/module_bindings/Types/User.g.cs

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdks/csharp/examples~/regression-tests/server/Lib.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public partial struct ExampleData
1414
[SpacetimeDB.Index.BTree]
1515
public uint Indexed;
1616
}
17+
18+
[SpacetimeDB.Table(Name = "User", Public = true)]
19+
public partial struct User
20+
{
21+
[PrimaryKey]
22+
public string IdentityString;
23+
public bool GeneratedByConnectedClient;
24+
}
1725

1826
[SpacetimeDB.View(Name = "GetExampleDataById", Public = true)]
1927
public static ExampleData? GetExampleDataById(ViewContext ctx)//, uint id)
@@ -26,6 +34,18 @@ public partial struct ExampleData
2634
{
2735
return ctx.Db.ExampleData.Id.Find(0);
2836
}
37+
38+
[SpacetimeDB.View(Name = "GetUserByContext", Public = true)]
39+
public static User? GetUserByContext(ViewContext ctx)
40+
{
41+
return ctx.Db.User.IdentityString.Find(ctx.Sender.ToString());
42+
}
43+
44+
[SpacetimeDB.View(Name = "GetUserByString", Public = true)]
45+
public static User? GetUserByString(AnonymousViewContext ctx) //, string identityString)
46+
{
47+
return ctx.Db.User.IdentityString.Find("identityStringExample");
48+
}
2949

3050
[SpacetimeDB.Reducer]
3151
public static void Delete(ReducerContext ctx, uint id)
@@ -44,4 +64,38 @@ public static void ThrowError(ReducerContext ctx, string error)
4464
{
4565
throw new Exception(error);
4666
}
67+
68+
[SpacetimeDB.Reducer]
69+
public static void CreateNewUser(ReducerContext ctx, string identityString)
70+
{
71+
ctx.Db.User.Insert(
72+
new User
73+
{
74+
IdentityString = identityString,
75+
GeneratedByConnectedClient = false,
76+
}
77+
);
78+
}
79+
80+
[SpacetimeDB.Reducer(ReducerKind.ClientConnected)]
81+
public static void ClientConnected(ReducerContext ctx)
82+
{
83+
Log.Info($"Connect {ctx.Sender}");
84+
85+
if (ctx.Db.User.IdentityString.Find(ctx.Sender.ToString()!) is User thisUser)
86+
{
87+
ctx.Db.User.IdentityString.Update(thisUser);
88+
}
89+
else
90+
{
91+
// If this is a new User, create a `User` object for the `IdentityString`,
92+
ctx.Db.User.Insert(
93+
new User
94+
{
95+
IdentityString = ctx.Sender.ToString()!,
96+
GeneratedByConnectedClient = true,
97+
}
98+
);
99+
}
100+
}
47101
}

0 commit comments

Comments
 (0)