コード例 #1
0
    def test_get_post_verify_contents_id_5(self):
        _calling_method()
        kwargs = {"title": "Encounter in the Dawn",
                  "body": "As he led the tribe down to the river in the dim light of dawn, Moon-Watcher paused uncertainly at a familiar spot. Something, he knew, was missing; but what it was, he could not remember. He wasted no mental effort on the problem, for this morning he had more important matters on his mind.",
                  "category_id": 1,
                  "id": 5
                  }

        api.create_category(name="Test_category", id=4)

        response = api.get_category(id=1)
        category = response.json()

        expected = {"title": kwargs.get("title"),
                    "body": kwargs.get("body"),
                    "category": category.get("name"),
                    "category_id": kwargs.get("category_id"),
                    "id": kwargs.get("id"),
                    }

        new_post = {"title": "Test Title",
                    "body": "Test body",
                    "category_id": 4
                    }

        api.create_post(**new_post)
        self.assertTrue(api._verify_post(expected, id=expected.get("id")))
コード例 #2
0
 def test_update_category_successful_204(self):
     _calling_method()
     api.create_category(name="Test_category")
     response = api.update_category(id=4, name="Update_category_name")
     self.assertEqual(204, response.status_code,
                      f"Incorrect Response Code return : {response.status_code} but expected 204")
     self.assertTrue(api._verify_category(id=4, name="Update_category_name"), "Category was not updated correctly")
コード例 #3
0
    def test_update_post_verify_content(self):
        _calling_method()
        api.create_category(name="Test_category", id=4)
        response = api.get_category(id=4)
        category = response.json()

        new_post = {"title": "Test Title",
                    "body": "Test body",
                    "category_id": 4
                    }

        update_post = {"id": 6,
                       "title": "Test Title updated",
                       "body": "Test body updated",
                       "category": category.get("name"),
                       "category_id": 4
                       }

        api.create_post(**new_post)
        api.update_post(**update_post)

        expected = {"title": update_post.get("title"),
                    "body": update_post.get("body"),
                    "category": category.get("name"),
                    "category_id": update_post.get("category_id"),
                    "id": 5  # to deal with know issue of get_post returning id+1 when selecting id=5 or above
                    }

        self.assertTrue(api._verify_post(expected, id=expected.get("id"), exclude_id=True))
コード例 #4
0
    def test_delete_of_multiple_categories_then_adding_new_categories(self):
        _calling_method()
        # Create categories
        expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
        for index in range(10):
            api.create_category(name=f"Test_category_{index}")

        category_ids = api._get_category_ids()
        self.assertEqual(expected, category_ids, "Category Ids miss match")

        # Delete several Categories
        deleted = [4, 6, 8, 10, 12]
        for index in deleted:
            api.delete_category(index)

        expected_after = set(expected) - set(deleted)
        category_ids = set(api._get_category_ids())
        self.assertEqual(expected_after, category_ids, "Category Ids miss match")

        # Add Categories in middle of ID range
        added = [6, 10]
        for index in added:
            api.create_category(name=f"Test_category_{index}", id=index)

        expected_after_add = set(list(expected_after) + list(added))
        category_ids = set(api._get_category_ids())
        self.assertEqual(expected_after_add, category_ids, "Category Ids miss match")
コード例 #5
0
 def test_delete_category_success_204(self):
     _calling_method()
     api.create_category(name="success_204", id=4)
     categories_before = api._no_of_categories()
     response = api.delete_category(4)
     self.assertEqual(204, response.status_code,
                      f"Incorrect Response Code return : {response.status_code} but expected 204")
     categories_after = api._no_of_categories()
     self.assertEqual(categories_before - 1, categories_after, "Category not deleted")
コード例 #6
0
    def test_create_post_required_category_id_404(self):
        _calling_method()
        kwargs = {"title": "This is the title",
                  "body": "This is the body of text",
                  }

        api.create_category(name="Test_category", id=4)
        response = api.create_post(**kwargs)
        self.assertEqual(404, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 404")
コード例 #7
0
    def test_create_category_10(self):
        _calling_method()
        expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

        for i in range(10):
            api.create_category(name=f"Test_category_{i}")

        category_ids = api._get_category_ids()

        self.assertEqual(expected, category_ids, "Category Ids miss match")
コード例 #8
0
    def test_create_post_success_200(self):
        _calling_method()
        kwargs = {"title": "This is the title",
                  "body": "This is the body of text",
                  "category_id": 4
                  }

        api.create_category(name="Test_category", id=4)
        response = api.create_post(**kwargs)
        self.assertEqual(200, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 200")
コード例 #9
0
    def test_delete_post_success_204(self):
        _calling_method()
        new_post = {"title": "Test Title",
                    "body": "Test body",
                    "category_id": 4
                    }
        api.create_category(name="Test_category", id=4)
        api.create_post(**new_post)

        response = api.delete_post(id=6)
        self.assertEqual(204, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 204")
コード例 #10
0
    def test_delete_of_multiple_categories(self):
        _calling_method()
        expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
        for index in range(10):
            api.create_category(name=f"Test_category_{index}")

        category_ids = api._get_category_ids()
        self.assertEqual(expected, category_ids, "Category Ids miss match")

        deleted = [4, 6, 8, 10, 12]
        for index in deleted:
            api.delete_category(index)

        expected_after = set(expected) - set(deleted)
        category_ids = set(api._get_category_ids())
        self.assertEqual(expected_after, category_ids, "Category Ids miss match")
コード例 #11
0
    def test_get_posts_per_page_50(self):
        _calling_method()
        expected = {"items": 50,
                    "page": 1,
                    "pages": 2,
                    "total": 55}

        kwargs = {"page": 1,
                  "per_page": 50}

        api.create_category(name="Test_category", id=4)

        for index in range(expected.get("items")):
            logging.info(f"Post {index}")
            api.create_post(title=f"post {index}", body=f"This is a test body {index}", category_id=4)

        self.assertTrue(api._verify_posts(expected=expected, **kwargs))
コード例 #12
0
    def test_create_category_duplicate_id_500(self):
        _calling_method()
        categories_before = api._no_of_categories()
        response = api.create_category(name="Test_category_1", id=4)
        self.assertEqual(201, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 201")

        categories_after = api._no_of_categories()

        self.assertEqual(categories_after, categories_before + 1, "Category not created")

        # Second insert of ID
        response = api.create_category(name="Test_category_2", id=4)
        self.assertEqual(500, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 500")

        categories_after = api._no_of_categories()
        self.assertEqual(categories_before + 1, categories_after, "Category with duplicate ID created")
コード例 #13
0
    def test_update_post_fail_404(self):
        _calling_method()
        api.create_category(name="Test_category", id=4)
        new_post = {"title": "Test Title",
                    "body": "Test body",
                    "category_id": 4
                    }

        update_post = {"id": 99,
                       "title": "Test Title updated",
                       "body": "Test body updated",
                       "category_id": 4
                       }

        api.create_post(**new_post)

        response = api.update_post(**update_post)
        self.assertEqual(404, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 404")
コード例 #14
0
    def test_create_category_successful_201_name(self):
        _calling_method()
        categories_before = api._no_of_categories()

        response = api.create_category(name="Test_category", id=4)
        self.assertEqual(201, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 201")

        categories_after = api._no_of_categories()
        self.assertEqual(categories_before + 1, categories_after, "Category not created")
        self.assertTrue(api._verify_category(id=4, name="Test_category"), "Category created incorrectly")
コード例 #15
0
    def test_delete_category_in_use_not_deleted_409(self):
        _calling_method()

        # Functions to soak category with updates to prevent delete
        def _update_category(category_id, no=1):
            for index in range(no):
                time.sleep(random.randint(1, 25)/1000)
                logging.info(f"update: category {category_id}")
                api.update_category(id=category_id, name=f"Update_category_name_{category_id}")

        def _post_lots(category_id, no=1):
            for index in range(no):
                time.sleep(random.randint(1, 25) / 1000)
                logging.info(f"Post {index}")
                api.create_post(title=f"post {index}", body=f"This is a test body {index}", category_id=category_id)

        for i in range(50):
            api.create_category(name=f"Test_category", id=4)
            pool = ThreadPool(processes=5)

            pool.apply_async(_post_lots, args=[4, 2])
            pool.apply_async(_update_category, args=[4, 2])

            _response = pool.apply_async(api.delete_category, args=[4])

            pool.apply_async(_post_lots, args=[4, 2])
            pool.apply_async(_update_category, args=[4, 2])

            pool.close()
            pool.join()
            response = _response._value
            if response is not None:
                if response.status_code != 204:
                    break

        self.assertEqual(409, response.status_code,
                         f"Incorrect Response Code return : {response.status_code} but expected 409")
コード例 #16
0
 def test_get_category_found_200(self):
     _calling_method()
     api.create_category(name="Test_category", id=4)
     response = api.get_category(4)
     self.assertEqual(200, response.status_code,
                      f"Incorrect Response Code return : {response.status_code} but expected 200")
コード例 #17
0
 def test_get_category_values_4(self):
     _calling_method()
     api.create_category(name="Test_category", id=4)
     api._verify_category(id=4)
     self.assertTrue(api._verify_category(id=4), f"Category has incorrect Id returned")
コード例 #18
0
 def test_get_category_value_10(self):
     _calling_method()
     for index in range(4, 10):
         api.create_category(name=f"Test_category_{index}")
         self.assertTrue(api._verify_category(id=index), f"Category has incorrect Id returned")
コード例 #19
0
 def test_update_category_id_fail_404(self):
     _calling_method()
     api.create_category(name="Test_category", id=4)
     response = api.update_category(id=5, name="Update_category_name")
     self.assertEqual(404, response.status_code,
                      f"Incorrect Response Code return : {response.status_code} but expected 404")