|
| 1 | +import pytest |
| 2 | + |
| 3 | + |
| 4 | +@pytest.fixture |
| 5 | +def client(app_with_db): |
| 6 | + return app_with_db.test_client() |
| 7 | + |
| 8 | + |
| 9 | +def test_index(client): |
| 10 | + """Test index page""" |
| 11 | + response = client.get("/") |
| 12 | + body = response.get_json() |
| 13 | + |
| 14 | + assert response.status_code == 200 |
| 15 | + assert body["success"] is True |
| 16 | + assert body["message"] == "Welcome to the test case management API" |
| 17 | + |
| 18 | + |
| 19 | +def test_retrieve_tests(client): |
| 20 | + """Test retrieve tests""" |
| 21 | + response = client.get("/tests") |
| 22 | + body = response.get_json() |
| 23 | + |
| 24 | + assert response.status_code == 200 |
| 25 | + assert body["success"] is True |
| 26 | + assert body["test_cases"] |
| 27 | + |
| 28 | + |
| 29 | +def test_405_using_wrong_method_to_retrieve_tests(client): |
| 30 | + """Test 405 using wrong method to retrieve tests""" |
| 31 | + response = client.patch("/tests") |
| 32 | + body = response.get_json() |
| 33 | + |
| 34 | + assert response.status_code == 405 |
| 35 | + assert body["success"] is False |
| 36 | + assert body["message"] |
| 37 | + |
| 38 | + |
| 39 | +def test_create_new_test(client): |
| 40 | + """test create new test""" |
| 41 | + response = client.post( |
| 42 | + "/tests", |
| 43 | + json={ |
| 44 | + "name": "New Test Case", |
| 45 | + "description": "New Test Case Description", |
| 46 | + }, |
| 47 | + ) |
| 48 | + body = response.get_json() |
| 49 | + |
| 50 | + assert response.status_code == 200 |
| 51 | + assert body["success"] is True |
| 52 | + |
| 53 | + |
| 54 | +def test_400_create_new_test_without_name(client): |
| 55 | + """test create new test without providing name""" |
| 56 | + response = client.post("/tests", json={"testing": "xxx"}) |
| 57 | + body = response.get_json() |
| 58 | + |
| 59 | + assert response.status_code == 400 |
| 60 | + assert body["success"] is False |
| 61 | + assert body["message"] |
| 62 | + |
| 63 | + |
| 64 | +def test_405_creation_not_allowed(client): |
| 65 | + """test 405 creation not allowed""" |
| 66 | + response = client.post( |
| 67 | + "/tests/45", |
| 68 | + json={ |
| 69 | + "name": "New Test Case", |
| 70 | + "description": "New Test Case Description", |
| 71 | + }, |
| 72 | + ) |
| 73 | + body = response.get_json() |
| 74 | + |
| 75 | + assert response.status_code == 405 |
| 76 | + assert body["success"] is False |
| 77 | + assert body["message"] |
| 78 | + |
| 79 | + |
| 80 | +def test_get_specific_test(client): |
| 81 | + """Test get specific test with id""" |
| 82 | + response = client.get("/tests/1") |
| 83 | + body = response.get_json() |
| 84 | + |
| 85 | + assert response.status_code == 200 |
| 86 | + assert body["success"] is True |
| 87 | + assert len(body["test_case"]) |
| 88 | + |
| 89 | + |
| 90 | +def test_get_nonexistent_test(client): |
| 91 | + """Test get non existent test""" |
| 92 | + response = client.get("/tests/10000") |
| 93 | + body = response.get_json() |
| 94 | + |
| 95 | + assert response.status_code == 404 |
| 96 | + assert body["success"] is False |
| 97 | + assert body["message"] |
| 98 | + |
| 99 | + |
| 100 | +def test_update_test(client): |
| 101 | + """Test update test""" |
| 102 | + response = client.patch("/tests/1", json={"name": "Updated Test Case"}) |
| 103 | + body = response.get_json() |
| 104 | + |
| 105 | + assert response.status_code == 200 |
| 106 | + assert body["success"] is True |
| 107 | + assert body["test_case"] |
| 108 | + |
| 109 | + |
| 110 | +def test_update_test_without_name(client): |
| 111 | + """Test update test without providing name""" |
| 112 | + response = client.patch("/tests/1", json={"testing": "Updated Test Case"}) |
| 113 | + body = response.get_json() |
| 114 | + |
| 115 | + assert response.status_code == 400 |
| 116 | + assert body["success"] is False |
| 117 | + assert body["message"] |
| 118 | + |
| 119 | + |
| 120 | +def test_404_delete_nonexistent_test(client): |
| 121 | + """test 404 delete nonexistent test""" |
| 122 | + response = client.delete("/tests/10000") |
| 123 | + body = response.get_json() |
| 124 | + |
| 125 | + assert response.status_code == 404 |
| 126 | + assert body["success"] is False |
| 127 | + assert body["message"] |
| 128 | + |
| 129 | + |
| 130 | +def test_get_execution_results(client): |
| 131 | + """Test get execution results""" |
| 132 | + response = client.get("/executions/1") |
| 133 | + body = response.get_json() |
| 134 | + |
| 135 | + assert response.status_code == 200 |
| 136 | + assert body["success"] is True |
| 137 | + assert body["executions"] |
| 138 | + assert body["asset"] |
| 139 | + assert body["total_executions"] |
| 140 | + |
| 141 | + |
| 142 | +def test_add_execution_results(client): |
| 143 | + """Test add execution result""" |
| 144 | + response = client.post( |
| 145 | + "/executions", |
| 146 | + json={ |
| 147 | + "asset_id": "1", |
| 148 | + "test_case_id": "1", |
| 149 | + "status": True, |
| 150 | + "details": "Success", |
| 151 | + }, |
| 152 | + ) |
| 153 | + body = response.get_json() |
| 154 | + |
| 155 | + assert response.status_code == 200 |
| 156 | + assert body["success"] is True |
| 157 | + assert body["execution"] |
| 158 | + assert body["total_executions"] |
0 commit comments