-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathCustomUserDataExamples.cs
131 lines (106 loc) · 4.11 KB
/
CustomUserDataExamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Examples;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using NUnit.Framework;
using Realms;
using Realms.Sync;
namespace Examples
{
public class CustomUserDataExamples
{
App app;
User user;
const string myRealmAppId = Config.AppId;
MongoClient mongoClient;
MongoClient.Database dbTracker;
MongoClient.Collection<CustomUserData> userDataCollection;
[OneTimeSetUp]
public async Task Creates()
{
// :snippet-start: create
// :replace-start: {
// "terms": {
// "dotnet_tests": "tracker"}
// }
app = App.Create(myRealmAppId);
user = await app.LogInAsync(Credentials.Anonymous());
mongoClient = user.GetMongoClient("mongodb-atlas");
dbTracker = mongoClient.GetDatabase("dotnet_tests");
userDataCollection = dbTracker.GetCollection<CustomUserData>("user_data");
var cud = new CustomUserData(user.Id)
{
FavoriteColor = "pink",
LocalTimeZone = "+8",
HasPets = true
};
var insertResult = await userDataCollection.InsertOneAsync(cud);
// :replace-end:
// :snippet-end:
Assert.AreEqual(user.Id, insertResult.InsertedId);
}
[Test, Order(0)]
public async Task Reads()
{
// :snippet-start: read
await user.RefreshCustomDataAsync();
// Tip: define a class that represents the custom data
// and use the gerneic overload of GetCustomData<>()
var customUserData = user.GetCustomData<CustomUserData>();
Console.WriteLine($"User has pets: {customUserData.HasPets}");
Console.WriteLine($"User's favorite color is {customUserData.FavoriteColor}");
Console.WriteLine($"User's timezone is {customUserData.LocalTimeZone}");
// :snippet-end:
Assert.IsTrue(customUserData.HasPets);
}
[Test, Order(1)]
public async Task Updates()
{
// :snippet-start: update
var updateResult = await userDataCollection.UpdateOneAsync(
new BsonDocument("_id", user.Id),
new BsonDocument("$set", new BsonDocument("HasPets", false)));
await user.RefreshCustomDataAsync();
var customUserData = user.GetCustomData<CustomUserData>();
Console.WriteLine($"User has pets: {customUserData.HasPets}");
Console.WriteLine($"User's favorite color is {customUserData.FavoriteColor}");
Console.WriteLine($"User's timezone is {customUserData.LocalTimeZone}");
// :snippet-end:
Assert.AreEqual(1, updateResult.ModifiedCount);
Assert.IsFalse(customUserData.HasPets);
}
[OneTimeTearDown]
public async Task TearDown()
{
// :snippet-start: delete
var deleteResult = await userDataCollection.DeleteOneAsync(
new BsonDocument("_id", user.Id));
// The `DeletedCount` should be 1
Console.WriteLine(deleteResult.DeletedCount);
// There should no longer be a custom user document for the user
var customData = await userDataCollection.FindOneAsync(
new BsonDocument("_id", user.Id));
Console.WriteLine(customData == null);
// :snippet-end:
//await userDataCollection.DeleteManyAsync();
}
}
// :snippet-start: cud
public class CustomUserData
{
public string _id { get; private set; }
public string _partition { get; private set; }
public string FavoriteColor { get; set; }
public string LocalTimeZone { get; set; }
public bool HasPets { get; set; }
public CustomUserData(string id, string partition = "myPart")
{
this._id = id;
this._partition = partition;
}
}
// :snippet-end:
}