'Test the branch_setting context manager'

@ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split)
def test_branch_setting(self, default_ms):

    self.initdb(default_ms)
    self._create_block_hierarchy()
    problem_location = self.problem_x1a_1.for_branch(None)
    problem_original_name = 'Problem_x1a_1'
    course_key = problem_location.course_key
    problem_new_name = 'New    Problem    Name'
    def assertNumProblems(display_name, expected_number):
        '\n                                                Asserts    the    number    of    problems    with    the    given    display    name    is    the    given    expected    number.\n                                                '
        self.assertEquals(len(self.store.get_items(course_key.for_branch(None), settings={'display_name': display_name})), expected_number)
    def assertProblemNameEquals(expected_display_name):
        '\n                                                Asserts    the    display_name    of    the    xblock    at    problem_location    matches    the    given    expected    value.\n                                                '
        problem = self.store.get_item(problem_location)
        self.assertEquals(problem.display_name, expected_display_name)
        assertNumProblems(expected_display_name, 1)
    with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, course_key):
        self.assertTrue(self.store.has_item(problem_location))
        assertProblemNameEquals(problem_original_name)
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_key):
        self.assertFalse(self.store.has_item(problem_location))
        with self.assertRaises(ItemNotFoundError):
            self.store.get_item(problem_location)
    self.store.publish(self.vertical_x1a, self.user_id)
    self.store.publish(problem_location, self.user_id)
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_key):
        self.assertTrue(self.store.has_item(problem_location))
        assertProblemNameEquals(problem_original_name)
    with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, course_key):
        assertProblemNameEquals(problem_original_name)
    problem = self.store.get_item(problem_location)
    problem.display_name = problem_new_name
    self.store.update_item(problem, self.user_id)
    with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, course_key):
        assertProblemNameEquals(problem_new_name)
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_key):
        assertProblemNameEquals(problem_original_name)
        assertNumProblems(problem_new_name, 0)
    self.store.publish(problem_location, self.user_id)
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_key):
        assertProblemNameEquals(problem_new_name)
        assertNumProblems(problem_original_name, 0)
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only):
        self.assertEquals(self.store.get_branch_setting(), ModuleStoreEnum.Branch.published_only)
    with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred):
        self.assertEquals(self.store.get_branch_setting(), ModuleStoreEnum.Branch.draft_preferred)
