示例#1
0
        public void InitialPushOfEntireDictionaryToEmptyCache()
        {
            // this should simulate first time connection is made and all data is returned in a batch in a form of a dictionary
            var cache     = new FirebaseCache <Dinosaur>();
            var dinosaurs = @"{
  ""lambeosaurus"": {
    ""ds"": {
      ""height"" : 2,
      ""length"" : 2,
      ""weight"": 2
    }
  },
  ""stegosaurus"": {
    ""ds"": {
      ""height"" : 3,
      ""length"" : 3,
      ""weight"" : 3
    }
  }
}";

            var entities    = cache.PushData("/", dinosaurs).ToList();
            var expectation = new[]
            {
                new FirebaseObject <Dinosaur>("lambeosaurus", new Dinosaur(2, 2, 2)),
                new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(3, 3, 3))
            };

            entities.ShouldAllBeEquivalentTo(expectation);
        }
示例#2
0
        public void PatchPatches()
        {
            FirebaseCache cache = new FirebaseCache();

            using (StringReader r = new StringReader("{\"a\": 1, \"b\": 2, \"c\": {\"foo\": true, \"bar\": false}}"))
                using (JsonReader reader = new JsonTextReader(r))
                {
                    cache.Replace("/", reader);
                }

            using (StringReader r = new StringReader("{\"foo\": 3, \"baz\": 4}"))
                using (JsonReader reader = new JsonTextReader(r))
                {
                    cache.Update("/c", reader);
                }


            /*
             * a = 1
             * b = 2
             * c = {
             *         foo = 3
             *         bar = false
             *         baz = 4
             *     }
             *
             */
            Assert.IsTrue(cache.Root.Children.First(a => a.Name == "a").Value == "1");
            Assert.IsTrue(cache.Root.Children.First(a => a.Name == "b").Value == "2");
            CacheItem c = cache.Root.Children.First(a => a.Name == "c");

            Assert.IsTrue(c.Children.First(foo => foo.Name == "foo").Value == "3");
            Assert.IsTrue(c.Children.First(foo => foo.Name == "bar").Value == "False");
            Assert.IsTrue(c.Children.First(foo => foo.Name == "baz").Value == "4");
        }
示例#3
0
        public void ItemPatchedInDictionary()
        {
            var cache     = new FirebaseCache <Dinosaur>();
            var dinosaurs = @"{
  ""lambeosaurus"": {
    ""ds"": {
      ""height"" : 2,
      ""length"" : 2,
      ""weight"": 2
    }
  },
  ""stegosaurus"": {
    ""ds"": {
      ""height"" : 3,
      ""length"" : 3,
      ""weight"" : 3
    }
  }
}";

            var patch = @"
{
    ""height"" : 8,
}
";

            var entities = cache.PushData("/", dinosaurs).ToList();

            // delete top level item from dictionary
            entities = cache.PushData("/stegosaurus/ds", patch).ToList();

            entities.First().ShouldBeEquivalentTo(new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(8, 3, 3)));
        }
示例#4
0
        public void ExistingKeyShouldReplace()
        {
            var cache       = new FirebaseCache <HNChanges>();
            var original    = @"
                {
                    ""items"": [1, 2, 3],
                    ""profiles"": [""a"", ""b"", ""c""]
                }";
            var incoming    = @"
                {
                    ""items"": [4, 5, 6],
                    ""profiles"": [""d"", ""e"", ""f""]
                }";
            var expectation = new []
            {
                new FirebaseObject <HNChanges>("updates", new HNChanges()
                {
                    items = new List <long>()
                    {
                        4, 5, 6
                    },
                    profiles = new List <string>()
                    {
                        "d", "e", "f"
                    }
                }
                                               )
            };

            cache.PushData("updates/", original).ToList();
            var entities = cache.PushData("updates/", incoming).ToList();

            entities.ShouldAllBeEquivalentTo(expectation);
        }
示例#5
0
        public void InitialPushOfEntireArrayToEmptyCache()
        {
            // Data that looks like an array will be returned as an array (without keys).
            // https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

            // this should simulate first time connection is made and all data is returned in a batch in a form of a dictionary
            var cache     = new FirebaseCache <Dinosaur>();
            var dinosaurs = @"[
  {
    ""ds"": {
      ""height"" : 2,
      ""length"" : 2,
      ""weight"": 2
    }
  },
  {
    ""ds"": {
      ""height"" : 3,
      ""length"" : 3,
      ""weight"" : 3
    }
  }
]";

            var entities    = cache.PushData("/", dinosaurs).ToList();
            var expectation = new[]
            {
                new FirebaseObject <Dinosaur>("0", new Dinosaur(2, 2, 2)),
                new FirebaseObject <Dinosaur>("1", new Dinosaur(3, 3, 3))
            };

            entities.ShouldAllBeEquivalentTo(expectation);
        }
        public void ObjectWithinDictionaryChanged()
        {
            var jurassicPrague = new FirebaseObject <JurassicWorld>("jurassicPrague", new JurassicWorld());

            jurassicPrague.Object.Dinosaurs.Add("lambeosaurus", new Dinosaur(2, 2, 2));
            jurassicPrague.Object.Dinosaurs.Add("stegosaurus", new Dinosaur(3, 3, 3));

            var cache = new FirebaseCache <JurassicWorld>(new Dictionary <string, JurassicWorld>()
            {
                { jurassicPrague.Key, jurassicPrague.Object }
            }
                                                          );

            var stegosaurusds = @"
{ 
    ""height"" : 4,
    ""length"" : 4,
    ""weight"": 4
}";
            var entities      = cache.PushData("/jurassicPrague/dinosaurs/stegosaurus/ds", stegosaurusds).ToList();

            entities.Should().HaveCount(1);
            entities.First().Should().BeEquivalentTo(jurassicPrague);
            jurassicPrague.Object.Dinosaurs["stegosaurus"].Dimensions.Should().BeEquivalentTo(new Dimensions {
                Height = 4, Length = 4, Weight = 4
            });

            //var entities = cache.PushData("/jurassicPrague/dinosaurs/stegosaurus/Name", "").ToList();
        }
示例#7
0
        public void ObjectWithDictionaryPropertyInserted()
        {
            var jurassicPrague = new FirebaseObject <JurassicWorld>("jurassicPrague", new JurassicWorld());

            jurassicPrague.Object.Dinosaurs.Add("lambeosaurus", new Dinosaur(2, 2, 2));
            jurassicPrague.Object.Dinosaurs.Add("stegosaurus", new Dinosaur(3, 3, 3));

            var cache = new FirebaseCache <JurassicWorld>();
            var jurassicPragueJson = @"
{ 
  ""jurassicPrague"": {
      ""dinosaurs"" : {
          ""lambeosaurus"": {
            ""ds"": {
              ""height"" : 2,
              ""length"" : 2,
              ""weight"": 2
            }
          },
          ""stegosaurus"": {
            ""ds"": {
              ""height"" : 3,
              ""length"" : 3,
              ""weight"" : 3
            }
          }
        }
    }
}";
            var entities           = cache.PushData("/", jurassicPragueJson).ToList();

            entities.Should().HaveCount(1);
            entities.First().ShouldBeEquivalentTo(jurassicPrague);
        }
示例#8
0
        public void NestedDictionaryChanged()
        {
            var jurassicPrague = new FirebaseObject <JurassicWorld>("jurassicPrague", new JurassicWorld());

            jurassicPrague.Object.Dinosaurs.Add("lambeosaurus", new Dinosaur(2, 2, 2));
            jurassicPrague.Object.Dinosaurs.Add("stegosaurus", new Dinosaur(3, 3, 3));

            var cache = new FirebaseCache <JurassicWorld>(new Dictionary <string, JurassicWorld>()
            {
                { jurassicPrague.Key, jurassicPrague.Object }
            }
                                                          );

            var trex     = @"
{
    ""trex"": {
        ""ds"": {
            ""height"" : 5,
            ""length"" : 4,
            ""weight"": 4
        }
    }
}";
            var entities = cache.PushData("/jurassicPrague/dinosaurs/", trex).ToList();

            entities.Should().HaveCount(1);
            entities.First().ShouldBeEquivalentTo(jurassicPrague);
            jurassicPrague.Object.Dinosaurs["trex"].Dimensions.ShouldBeEquivalentTo(new Dimensions {
                Height = 5, Length = 4, Weight = 4
            });
        }
        public void CanUpdateEnumInObject()
        {
            var cache       = new FirebaseCache <Card>();
            var newObject   = @"{ ""Suit"": 2 }";
            var expectation = new[] { new FirebaseObject <Card>("Card", JsonConvert.DeserializeObject <Card>(newObject)) };

            cache.PushData("Card/", @"{ ""Suit"": 0 }");
            var entities = cache.PushData("Card/Suit", "2");

            entities.Should().BeEquivalentTo(expectation);
        }
示例#10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RealtimeDatabase{T}"/> class.
        /// </summary>
        /// <param name="childQuery"> The child query.  </param>
        /// <param name="elementRoot"> The element Root. </param>
        /// <param name="offlineDatabaseFactory"> The offline database factory.  </param>
        /// <param name="filenameModifier"> Custom string which will get appended to the file name.  </param>
        /// <param name="streamChanges"> Specifies whether changes should be streamed from the server.  </param>
        /// <param name="pullEverythingOnStart"> Specifies if everything should be pull from the online storage on start. It only makes sense when <see cref="streamChanges"/> is set to true. </param>
        /// <param name="pushChanges"> Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. </param>
        public RealtimeDatabase(ChildQuery childQuery, string elementRoot, Func <Type, string, IDictionary <string, OfflineEntry> > offlineDatabaseFactory, string filenameModifier, bool streamChanges, InitialPullStrategy initialPullStrategy, bool pushChanges)
        {
            this.childQuery          = childQuery;
            this.elementRoot         = elementRoot;
            this.streamChanges       = streamChanges;
            this.initialPullStrategy = initialPullStrategy;
            this.pushChanges         = pushChanges;
            this.Database            = offlineDatabaseFactory(typeof(T), filenameModifier);
            this.firebaseCache       = new FirebaseCache <T>(new OfflineCacheAdapter <string, T>(this.Database));
            this.subject             = new Subject <FirebaseEvent <T> >();

            this.PutHandler = new SetHandler <T>();

            Task.Factory.StartNew(this.SynchronizeThread, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }
        public void AddedItemShouldBeAppendedToCollection()
        {
            var cache       = new FirebaseCache <List <long> >();
            var original    = @"[1, 2, 3]";
            var incoming    = @"[1, 2, 3, 4]";
            var expectation = new []
            {
                new FirebaseObject <List <long> >("updates", new List <long>()
                {
                    1, 2, 3, 4
                })
            };

            cache.PushData("updates/", original);
            var entities = cache.PushData("updates/", incoming);

            entities.Should().BeEquivalentTo(expectation);
        }
示例#12
0
        public void PrimitiveItemWhichBelongsToExistingObjectChanged()
        {
            // this should simulate when some primitive data of an existing object changed
            var dinosaurs = new List <FirebaseObject <Dinosaur> >(new[]
            {
                new FirebaseObject <Dinosaur>("lambeosaurus", new Dinosaur(2, 2, 2)),
                new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(3, 3, 3))
            });

            var cache = new FirebaseCache <Dinosaur>(dinosaurs.ToDictionary(f => f.Key, f => f.Object));

            var height = "4";

            var entities    = cache.PushData("/stegosaurus/ds/height", height).ToList();
            var stegosaurus = new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(4, 3, 3));

            entities.Should().HaveCount(1);
            entities.First().ShouldBeEquivalentTo(stegosaurus);
        }
示例#13
0
        public void DictionaryCache()
        {
            var cache = new FirebaseCache <Dictionary <string, string> >();
            var data  = @"
{
    ""a"": ""aa"",
    ""b"": ""bb""
}";

            var updateData = @"aaa";

            cache.PushData("root/", data).ToList();
            var entities = cache.PushData("root/a", updateData).ToList();

            entities.First().ShouldBeEquivalentTo(new FirebaseObject <Dictionary <string, string> >("root", new Dictionary <string, string>()
            {
                ["a"] = "aaa",
                ["b"] = "bb",
            }));
        }
示例#14
0
        public void ItemChangesInDictionaryOfPrimitiveBooleans()
        {
            var cache          = new FirebaseCache <bool>();
            var boolDictionary = new [] { new FirebaseObject <bool>("a", true), new FirebaseObject <bool>("b", true), new FirebaseObject <bool>("c", false) };
            var bools          = @"
{ 
    ""a"" : true,
    ""b"" : true,
    ""c"": false
}";

            var entities = cache.PushData("/", bools).ToList();

            entities.ShouldBeEquivalentTo(boolDictionary);

            entities = cache.PushData("/d", "true").ToList();
            entities.First().ShouldBeEquivalentTo(new FirebaseObject <bool>("d", true));

            entities = cache.PushData("/c", "true").ToList();
            entities.First().ShouldBeEquivalentTo(new FirebaseObject <bool>("c", true));
        }
        public void ItemChangesInDictionaryOfPrimitiveStrings()
        {
            var cache            = new FirebaseCache <string>();
            var stringDictionary = new[] { new FirebaseObject <string>("a", "a"), new FirebaseObject <string>("b", "b"), new FirebaseObject <string>("c", "c") };
            var strings          = @"
{ 
    ""a"" : ""a"",
    ""b"" : ""b"",
    ""c"": ""c""
}";

            var entities = cache.PushData("/", strings).ToList();

            entities.ShouldBeEquivalentTo(stringDictionary);

            entities = cache.PushData("/d", @"""d""").ToList();
            entities.First().ShouldBeEquivalentTo(new FirebaseObject <string>("d", "d"));

            entities = cache.PushData("/c", @"""cc""").ToList();
            entities.First().ShouldBeEquivalentTo(new FirebaseObject <string>("c", "cc"));
        }
示例#16
0
        public void SecondLevelItemWhichBelongsToExistingObjectChanged()
        {
            // this should simulate when some data of an existing object changed
            var dinosaurs = new List <FirebaseObject <Dinosaur> >(new[]
            {
                new FirebaseObject <Dinosaur>("lambeosaurus", new Dinosaur(2, 2, 2)),
                new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(3, 3, 3))
            });

            var cache = new FirebaseCache <Dinosaur>(dinosaurs.ToDictionary(f => f.Key, f => f.Object));

            var stegosaurusds = @"
{
  ""height"" : 4,
  ""length"" : 4,
  ""weight"": 4
}";

            var entities    = cache.PushData("/stegosaurus/ds", stegosaurusds).ToList();
            var stegosaurus = new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(4, 4, 4));

            entities.Should().HaveCount(1);
            entities.First().ShouldBeEquivalentTo(stegosaurus);
        }
示例#17
0
        public void NewTopLevelItemInsertedToNonEmptyCache()
        {
            // this should simulate when connection had already been established with some data populated when new top-level item arrived
            var dinosaurs = new List <FirebaseObject <Dinosaur> >(new[]
            {
                new FirebaseObject <Dinosaur>("lambeosaurus", new Dinosaur(2, 2, 2)),
                new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(3, 3, 3))
            });

            var cache    = new FirebaseCache <Dinosaur>(dinosaurs.ToDictionary(f => f.Key, f => f.Object));
            var trexData = @"{
    ""ds"": {
      ""height"" : 4,
      ""length"" : 4,
      ""weight"": 4
    }
}";

            var entities = cache.PushData("/trex", trexData).ToList();
            var trex     = new FirebaseObject <Dinosaur>("trex", new Dinosaur(4, 4, 4));

            entities.Should().HaveCount(1);
            entities.First().ShouldBeEquivalentTo(trex);
        }
示例#18
0
        public void ItemDeletedInDictionary()
        {
            var cache     = new FirebaseCache <Dinosaur>();
            var dinosaurs = @"{
  ""lambeosaurus"": {
    ""ds"": {
      ""height"" : 2,
      ""length"" : 2,
      ""weight"": 2
    }
  },
  ""stegosaurus"": {
    ""ds"": {
      ""height"" : 3,
      ""length"" : 3,
      ""weight"" : 3
    }
  }
}";

            var entities = cache.PushData("/", dinosaurs).ToList();

            // delete top level item from dictionary
            entities = cache.PushData("/stegosaurus", " ").ToList();

            cache.Count().ShouldBeEquivalentTo(1);
            entities.ShouldAllBeEquivalentTo(new[] { new FirebaseObject <Dinosaur>("stegosaurus", new Dinosaur(3, 3, 3)) });

            // delete a property - it should be set to null
            entities = cache.PushData("/lambeosaurus/ds", " ").ToList();

            cache.Count().ShouldBeEquivalentTo(1);
            entities.ShouldAllBeEquivalentTo(new[] { new FirebaseObject <Dinosaur>("lambeosaurus", new Dinosaur {
                    Dimensions = null
                }) });
        }
        public void CanUpdateObjectInsideList()
        {
            var cache       = new FirebaseCache <HandOfCards>();
            var original    = @"
                {
                    ""Cards"": [
                        { ""Suit"": 0 },
                    ]
                }
            ";
            var newObject   = @"
                {
                    ""Cards"": [
                        { ""Suit"": 2 },
                    ]
                }
            ";
            var expectation = new[] { new FirebaseObject <HandOfCards>("HandOfCards", JsonConvert.DeserializeObject <HandOfCards>(newObject)) };

            cache.PushData("HandOfCards/", original);
            var entities = cache.PushData("HandOfCards/Cards/0/Suit", "2");

            entities.Should().BeEquivalentTo(expectation);
        }