Beispiel #1
0
        public void UpdateRootMatchesInput()
        {
            var expected = JToken.Parse(_weather);
            var client   = A.Fake <IFirebaseNetworkConnection>();

            A.CallTo(() => client.Send(A <FirebaseMessage> ._)).Invokes((FirebaseMessage message) =>
            {
                Assert.AreEqual(WriteBehavior.Merge, message.Behavior);
                Assert.AreEqual(new FirebasePath(), message.Path);
                Assert.IsTrue(JToken.DeepEquals(expected, JToken.Parse(message.Value)),
                              "The contents being written did not match the provided contents");

                message.Callback(null);
            });

            var jc = new SyncDatabase(null, client);

            using (var mre = new ManualResetEvent(false))
            {
                jc.Update(new FirebasePath(), _weather, (error) => { mre.Set(); });

                Assert.IsTrue(mre.WaitOne(TimeSpan.FromSeconds(5)),
                              "Time out waiting for cache callback");
            }

            Assert.IsTrue(JToken.DeepEquals(expected, JToken.Parse(jc.Dump())),
                          "The cache contents did not match the expected structure");
        }
Beispiel #2
0
        public void MultiUpdateMatchesOutput()
        {
            var data = new List <Tuple <Tuple <string, string>, string> >()
            {
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/me",
                        "{\"name\": \"Robert\"}"),
                    "{\"people\": { \"me\": {\"name\": \"Robert\"}}}"),
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/me/name",
                        "Bob"),
                    "{\"people\": { \"me\": {\"name\": \"Bob\"}}}"),
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/you",
                        "{\"name\": \"Susan\"}"),
                    "{\"people\": { \"me\": {\"name\": \"Bob\"}, \"you\": {\"name\": \"Susan\"}}}"),
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/me/age",
                        "38"),
                    "{\"people\": { \"me\": {\"name\": \"Bob\", \"age\": \"38\"}, \"you\": {\"name\": \"Susan\"}}}"),
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/me",
                        "{\"name\": \"Bobby\"}"),
                    "{\"people\": { \"me\": {\"name\": \"Bobby\", \"age\": \"38\"}, \"you\": {\"name\": \"Susan\"}}}"),
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/",
                        "{ \"me\": {\"name\": \"Bobert\"}}"),
                    "{\"people\": { \"me\": {\"name\": \"Bobert\" }, \"you\": {\"name\": \"Susan\"}}}"),

                // nulls delete
                new Tuple <Tuple <string, string>, string>(
                    new Tuple <string, string>(
                        "/people/me",
                        "{\"name\": null}"),
                    "{\"people\": { \"me\": { }, \"you\": {\"name\": \"Susan\"}}}"),
            };

            var client = A.Fake <IFirebaseNetworkConnection>();

            A.CallTo(() => client.Send(A <FirebaseMessage> ._))
            .Invokes((FirebaseMessage message) => { message.Callback(null); });

            var jc = new SyncDatabase(null, client);
            ManualResetEvent called = new ManualResetEvent(false);

            foreach (var item in data)
            {
                called.Reset();
                string path     = item.Item1.Item1;
                string value    = item.Item1.Item2;
                var    expected = JToken.Parse(item.Item2);
                jc.Update(new FirebasePath(path), value, error =>
                {
                    JToken actual = JToken.Parse(jc.Dump());
                    Assert.IsTrue(JToken.DeepEquals(expected, actual),
                                  "The cache state did not match the expected cache state");
                    called.Set();
                });

                Assert.IsTrue(called.WaitOne(TimeSpan.FromSeconds(2)), "The callback never fired");
            }
        }