public async Task FetchByKey() {
   var em1 = await TestFns.NewEm(_serviceName);
   var q = new EntityQuery<Fruit>().From("Fruits");
   var r0 = await em1.ExecuteQuery(q);
   Assert.IsTrue(r0.Count() > 0);
   var fruit1 = r0.First();
   var id = fruit1.Id;
   var ek = new EntityKey(typeof(Fruit), em1.MetadataStore, id);
   var r1 = await em1.ExecuteQuery(ek.ToQuery());
   var fruits = r1.Cast<Fruit>();
   Assert.IsTrue(fruits.First() == fruit1);
   
 }
        public async Task FetchByAbstractEntityKey()
        {
            var em1 = await TestFns.NewEm(_serviceName);

            var q  = new EntityQuery <ItemOfProduce>().From("ItemsOfProduce");
            var r0 = await em1.ExecuteQuery(q);

            Assert.IsTrue(r0.Count() > 0);
            var iop1 = r0.First();
            var id   = iop1.Id;
            var ek   = new EntityKey(typeof(ItemOfProduce), em1.MetadataStore, id);
            var r1   = await em1.ExecuteQuery(ek.ToQuery());

            var fruits = r1.Cast <ItemOfProduce>();

            Assert.IsTrue(fruits.First() == iop1);
        }
        public async Task FetchByKey()
        {
            var em1 = await TestFns.NewEm(_serviceName);

            var q  = new EntityQuery <Fruit>().From("Fruits");
            var r0 = await em1.ExecuteQuery(q);

            Assert.IsTrue(r0.Count() > 0);
            var fruit1 = r0.First();
            var id     = fruit1.Id;
            var ek     = new EntityKey(typeof(Fruit), em1.MetadataStore, id);
            var r1     = await em1.ExecuteQuery(ek.ToQuery());

            var fruits = r1.Cast <Fruit>();

            Assert.IsTrue(fruits.First() == fruit1);
        }
        public async Task FetchEntityByKeyItemOfProduce()
        {
            var em1 = await TestFns.NewEm(_serviceName);

            var appleId = new Guid("13f1c9f5-3189-45fa-ba6e-13314fafaa92");

            // var appleId = new Guid("D35E9669-2BAE-4D69-A27A-252B31800B74");

            var ek = new EntityKey(typeof(ItemOfProduce), em1.MetadataStore, appleId);
            var fr = await em1.FetchEntityByKey(ek);

            Assert.IsTrue(fr.Entity != null && fr.Entity is Apple && !fr.FromCache);
            // and again
            var r0 = await em1.ExecuteQuery(ek.ToQuery <ItemOfProduce>());

            Assert.IsTrue(r0.Count() > 0);
            Assert.IsTrue(r0.First().Id == appleId);
        }
    public async Task FetchByKeyWithDefaultResource() {
      var em1 = await TestFns.NewEm(_serviceName);
      var rn = em1.MetadataStore.GetDefaultResourceName(typeof(Fruit));
      Assert.IsTrue(rn != "Fruits");
      em1.MetadataStore.SetResourceName("Fruits", typeof(Fruit), true);
      var rn2 = em1.MetadataStore.GetDefaultResourceName(typeof(Fruit));
      Assert.IsTrue(rn2 == "Fruits");
      var q = new EntityQuery<Fruit>();
      var r0 = await em1.ExecuteQuery(q);
      Assert.IsTrue(r0.Count() > 0);
      var fruit1 = r0.First();
      var id = fruit1.Id;
      var ek = new EntityKey(typeof(Fruit), em1.MetadataStore, id);
      var r1 = await em1.ExecuteQuery(ek.ToQuery());
      var fruits = r1.Cast<Fruit>();
      Assert.IsTrue(fruits.First() == fruit1);

    }
        public async Task FetchByKeyWithDefaultResource()
        {
            var em1 = await TestFns.NewEm(_serviceName);

            var rn = em1.MetadataStore.GetDefaultResourceName(typeof(Fruit));

            Assert.IsTrue(rn != "Fruits");
            em1.MetadataStore.SetResourceName("Fruits", typeof(Fruit), true);
            var rn2 = em1.MetadataStore.GetDefaultResourceName(typeof(Fruit));

            Assert.IsTrue(rn2 == "Fruits");
            var q  = new EntityQuery <Fruit>();
            var r0 = await em1.ExecuteQuery(q);

            Assert.IsTrue(r0.Count() > 0);
            var fruit1 = r0.First();
            var id     = fruit1.Id;
            var ek     = new EntityKey(typeof(Fruit), em1.MetadataStore, id);
            var r1     = await em1.ExecuteQuery(ek.ToQuery());

            var fruits = r1.Cast <Fruit>();

            Assert.IsTrue(fruits.First() == fruit1);
        }
    public async Task FetchByAbstractEntityKey() {
      var em1 = await TestFns.NewEm(_serviceName);
      var q = new EntityQuery<ItemOfProduce>().From("ItemsOfProduce");
      var r0 = await em1.ExecuteQuery(q);
      Assert.IsTrue(r0.Count() > 0);
      var iop1 = r0.First();
      var id = iop1.Id;
      var ek = new EntityKey(typeof(ItemOfProduce), em1.MetadataStore, id);
      var r1 = await em1.ExecuteQuery(ek.ToQuery());
      var fruits = r1.Cast<ItemOfProduce>();
      Assert.IsTrue(fruits.First() == iop1);

    }
    public async Task FetchEntityByKeyItemOfProduce() {
      var em1 = await TestFns.NewEm(_serviceName);
      var appleId = new Guid("13f1c9f5-3189-45fa-ba6e-13314fafaa92");
      
      // var appleId = new Guid("D35E9669-2BAE-4D69-A27A-252B31800B74");

      var ek = new EntityKey(typeof(ItemOfProduce), em1.MetadataStore, appleId);
      var fr = await em1.FetchEntityByKey(ek);
      Assert.IsTrue(fr.Entity != null && fr.Entity is Apple && !fr.FromCache);
      // and again
      var r0 = await em1.ExecuteQuery(ek.ToQuery<ItemOfProduce>());
      Assert.IsTrue(r0.Count() > 0);
      Assert.IsTrue(r0.First().Id == appleId);
    }