public IEnumerator PushJson()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";
            string pushId   = null;

            string json = JsonUtility.ToJson(new TestObject(1, "PushedName"));

            firebase.Child("unitTest/push").Push(json).
            OnSuccess(res =>
            {
                isDone = true;
                pushId = res;
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }

            Assert.IsNotNull(pushId, "Encountered issue while Pushing. PushID is null.");
        }
    void ErrorHandling(Exception err)
    {
        string errorText = "";

        RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorText += x.Key + " - " + x.Value + "\n");
        Debug.LogError(errorText);
    }
        public IEnumerator Remove()
        {
            //we make sure all event is cleared before proceeding with remove
            var unsubscriberGO = GameObject.Find("FirebaseRestUnsubscriber");

            if (unsubscriberGO != null)
            {
                MonoBehaviour.Destroy(unsubscriberGO);
            }

            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";


            firebase.Child("unitTest").Remove().OnSuccess(() => isDone = true).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }
        public IEnumerator ReadKeyValuePairsObject()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            Dictionary <string, TestObject> result = null;

            firebase.Child("unitTest/writeValueAsDictionaryObject").ReadKeyValuePairs <TestObject>().
            OnSuccess(res =>
            {
                isDone = true;
                result = res.ToDictionary(x => x.Key, x => x.Value);
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }

            Assert.IsNotNull(result, "Could not read key-value pairs properly.");
            //type-check
            result.ToList().ForEach(x => Assert.AreEqual(typeof(TestObject), x.Value.GetType(), "Could not parse response to desired type or unknow issue occured."));
            //object-check
            result.ToList().ForEach(x => Assert.IsNull(x.Value, "Could not parse response to desired type or unknow issue occured."));
        }
        public IEnumerator ReadValue()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            string result = null;

            firebase.Child("unitTest/writeValuePrimitive").ReadValue().
            OnSuccess(res =>
            {
                isDone = true;
                result = (string)res;
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }

            Assert.IsNotNull(result, "Could not read properly or encountered issue while parsing response to desired type.");
        }
        public IEnumerator RawRead()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            string jsonExpected = JsonUtility.ToJson(new TestObject());
            string jsonResult   = "";

            firebase.Child("unitTest/writeValueObject").RawRead().
            OnSuccess(res =>
            {
                isDone     = true;
                jsonResult = res;
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }

            Assert.AreEqual(jsonExpected, jsonResult);
        }
        public IEnumerator WriteValueAsDictionaryObject()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            var dictionary = new Dictionary <string, TestObject>()
            {
                { "1", new TestObject(1, "orange") },
                { "2", new TestObject(2, "apple") },
                { "3", new TestObject(3, "mango") }
            };

            firebase.Child("unitTest/writeValueAsDictionaryObject").WriteValueAsDictionary(dictionary).
            OnSuccess(() => isDone = true).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }
        public IEnumerator WriteValueAsDictionary()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            var dictionary = new Dictionary <string, int>()
            {
                { "orange", 145 },
                { "banana", 456 },
                { "apple", 598 }
            };

            firebase.Child("unitTest/writeValueAsDictionary").WriteValueAsDictionary(dictionary).
            OnSuccess(() => isDone = true).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }
        public IEnumerator UpdateObjectJson()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            string json   = JsonUtility.ToJson(new TestObject(1, "Updater"));
            string result = "";

            firebase.Child("unitTest/updateObjectJson").WriteValue(json).OnSuccess(() =>
            {
                //change obj values
                json = JsonUtility.ToJson(new TestObject(123, "NewUpdated"));

                //Update new value
                firebase.Child("unitTest/updateObjectJson").Update(json).OnSuccess(() =>
                {
                    //Read and Check
                    firebase.Child("unitTest/updateObjectJson").RawRead().OnSuccess(res =>
                    {
                        result = res;
                        isDone = true;
                    }).
                    OnError(err =>
                    {
                        RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                        isDone = true;
                    });
                }).
                OnError(err =>
                {
                    RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                    isDone = true;
                });
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }

            Assert.AreEqual(json, result, "Couldn't update properly.");
        }
        public IEnumerator UpdateObject()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            var obj = new TestObject(1, "Updater");

            firebase.Child("unitTest/updateObject").WriteValue(obj).OnSuccess(() =>
            {
                //change obj values
                obj.id   = 100;
                obj.name = "UpdatedNow";

                //Update new value
                firebase.Child("unitTest/updateObject").Update(obj).OnSuccess(() =>
                {
                    //Read and Check
                    firebase.Child("unitTest/updateObject").Read <TestObject>().OnSuccess(res =>
                    {
                        isDone = true;
                        Assert.AreEqual(obj, res, "Couldn't update properly.");
                    }).
                    OnError(err =>
                    {
                        RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                        isDone = true;
                    });
                }).
                OnError(err =>
                {
                    RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                    isDone = true;
                });
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }
        public IEnumerator ChildRemoved()
        {
            var    firebase      = new RealtimeDatabase();
            bool   isDone        = false;
            string errorMsg      = "";
            bool   isInitialDone = false;

            string json = JsonUtility.ToJson(new TestObject(111, "ChildRemove"));

            firebase.Child("unitTest/childRemoved").ChildRemoved(cb =>
            {
                if (cb.isInitial)
                {
                    isInitialDone = true;
                    return;
                }
                isDone = true;
                Assert.AreEqual("null", cb.data, "Encountered issue while parsing callback Data.");
            });

            yield return(new WaitUntil(() => isInitialDone)); //we wait for initial cb to reduce chance of race condition

            firebase.Child("unitTest/childRemoved").WriteValue(json).OnSuccess(() =>
            {
                firebase.Child("unitTest/childRemoved").Remove().
                OnError(err =>
                {
                    RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                    isDone = true;
                });
            }).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }
        public IEnumerator WriteKeyValuePair()
        {
            var    firebase = new RealtimeDatabase();
            bool   isDone   = false;
            string errorMsg = "";

            firebase.Child("unitTest/writeKeyValuePair").WriteKeyValuePair("abc", 123).
            OnSuccess(() => isDone = true).
            OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
                isDone = true;
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }
        public IEnumerator ChildChanged()
        {
            var    firebase   = new RealtimeDatabase();
            bool   isDone     = false;
            string errorMsg   = "";
            int    eventCount = 0;

            bool isInitialDone = false;

            bool hasWriteVal   = false;
            bool hasUpdatedVal = false;

            string json = JsonUtility.ToJson(new TestObject(111, "ChildRemove"));

            firebase.Child("unitTest/childChanged").ChildChanged(cb =>
            {
                if (cb.isInitial)
                {
                    isInitialDone = true;
                    return;
                }

                switch (eventCount)
                {
                //child added
                case 0:
                    eventCount++;
                    hasWriteVal = true;
                    Assert.AreEqual(json, cb.data, "Encountered issue while parsing ChildAdded callback Data.");
                    break;

                //child updated
                case 1:
                    eventCount++;
                    hasUpdatedVal = true;
                    Assert.AreEqual(json, cb.data, "Encountered issue while parsing ChildUpdated callback Data.");
                    break;

                //child removed
                case 2:
                    eventCount++;
                    Assert.AreEqual("null", cb.data, "Encountered issue while parsing ChildRemoved callback Data.");
                    break;
                }

                if (eventCount == 2)
                {
                    isDone = true;
                }
            });

            yield return(new WaitUntil(() => isInitialDone)); //we wait for initial cb to reduce chance of race condition

            firebase.Child("unitTest/childChanged").WriteValue(json).OnError(err =>
            {
                hasWriteVal = true;
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
            });

            yield return(new WaitUntil(() => hasWriteVal));

            json = JsonUtility.ToJson(new TestObject(123, "NewUpdated"));
            firebase.Child("unitTest/childChanged").Update(json).OnError(err =>
            {
                hasUpdatedVal = true;
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
            });

            yield return(new WaitUntil(() => hasUpdatedVal));

            firebase.Child("unitTest/childChanged").Remove().OnError(err =>
            {
                RequestErrorHelper.ToDictionary(err).ToList().ForEach(x => errorMsg += x + "\n");
            });

            yield return(new WaitUntil(() => isDone));

            if (errorMsg.Length > 0)
            {
                Assert.Fail(errorMsg);
            }
        }