Ejemplo n.º 1
0
    public void AddRoleUserTest()
    {
        // ユーザー作成
        NCMBUser expertUser = new NCMBUser();

        expertUser.UserName = "******";
        expertUser.Password = "******";
        expertUser.SignUpAsync((error) => {
            if (error != null)
            {
                Assert.Fail(error.ErrorMessage);
            }
        });
        NCMBTestSettings.AwaitAsync();
        Assert.NotNull(expertUser.ObjectId);

        // ロール作成
        NCMBRole expertPlanRole = new NCMBRole("expertPlan");

        expertPlanRole.SaveAsync((error) => {
            if (error != null)
            {
                Assert.Fail(error.ErrorMessage);
            }
        });
        NCMBTestSettings.AwaitAsync();
        Assert.NotNull(expertPlanRole.ObjectId);

        // 空のロールを検索
        NCMBRole expertPlan = null;

        NCMBRole.GetQuery().WhereEqualTo("roleName", "expertPlan").FindAsync((roleList, error) => {
            if (error != null)
            {
                Assert.Fail(error.ErrorMessage);
            }
            else
            {
                expertPlan = roleList.FirstOrDefault();
            }
        });
        NCMBTestSettings.AwaitAsync();
        Assert.NotNull(expertPlan.ObjectId);

        // 空のロールにユーザーを追加
        expertPlan.Users.Add(expertUser);
        expertPlan.SaveAsync((error) => {
            if (error != null)
            {
                Assert.Fail(error.ErrorMessage);
            }
        });
        NCMBTestSettings.AwaitAsync();

        // ロールを検索
        expertPlan = null;
        NCMBRole.GetQuery().WhereEqualTo("roleName", "expertPlan").FindAsync((roleList, error) => {
            if (error != null)
            {
                Assert.Fail(error.ErrorMessage);
            }
            else
            {
                expertPlan = roleList.FirstOrDefault();
            }
        });
        NCMBTestSettings.AwaitAsync();

        // ロールに所属するユーザーを検索
        expertPlan.Users.GetQuery().FindAsync((userList, error) => {
            if (error != null)
            {
                Assert.Fail(error.ErrorMessage);
            }
            else
            {
                Assert.AreEqual(expertUser.ObjectId, userList.FirstOrDefault().ObjectId);
                NCMBTestSettings.CallbackFlag = true;
                // テストデータ削除
                expertPlan.DeleteAsync();
                expertUser.DeleteAsync();
            }
        });
        NCMBTestSettings.AwaitAsync();
        Assert.True(NCMBTestSettings.CallbackFlag);
    }