|
4 | 4 | import random |
5 | 5 | import string |
6 | 6 | import unittest |
| 7 | +from pathlib import Path |
7 | 8 | from typing import Any |
| 9 | +from typing import ClassVar |
8 | 10 |
|
9 | 11 | from mailjet_rest import Client |
10 | 12 |
|
@@ -216,5 +218,106 @@ def test_user_agent(self) -> None: |
216 | 218 | self.assertEqual(self.client.config.user_agent, "mailjet-apiv3-python/v1.4.0") |
217 | 219 |
|
218 | 220 |
|
| 221 | +class TestCsvImport(unittest.TestCase): |
| 222 | + """Tests for Mailjet API csv import functionality. |
| 223 | +
|
| 224 | + This class provides setup and teardown functionality for tests involving the |
| 225 | + csv import functionality, with authentication and client initialization handled |
| 226 | + in `setUp`. Each test in this suite operates with the configured Mailjet client |
| 227 | + instance to simulate API interactions. |
| 228 | +
|
| 229 | + Attributes: |
| 230 | + - _shared_state (dict[str, str]): A dictionary containing values taken from tests to share them in other tests. |
| 231 | + """ |
| 232 | + |
| 233 | + _shared_state: ClassVar[dict[str, Any]] = {} |
| 234 | + |
| 235 | + @classmethod |
| 236 | + def get_shared(cls, key: str) -> Any: |
| 237 | + """Retrieve a value from shared test state. |
| 238 | +
|
| 239 | + Parameters: |
| 240 | + - key (str): The key to look up in shared state. |
| 241 | +
|
| 242 | + Returns: |
| 243 | + - Any: The stored value, or None if key doesn't exist. |
| 244 | + """ |
| 245 | + return cls._shared_state.get(key) |
| 246 | + |
| 247 | + @classmethod |
| 248 | + def set_shared(cls, key: str, value: Any) -> None: |
| 249 | + """Store a value in shared test state. |
| 250 | +
|
| 251 | + Parameters: |
| 252 | + - key (str): The key to store the value under. |
| 253 | + - value (Any): The value to store. |
| 254 | + """ |
| 255 | + cls._shared_state[key] = value |
| 256 | + |
| 257 | + def setUp(self) -> None: |
| 258 | + """Set up the test environment by initializing authentication credentials and the Mailjet client. |
| 259 | +
|
| 260 | + This method is called before each test to ensure a consistent testing |
| 261 | + environment. It retrieves the API keys and ID_CONTACTSLIST from environment variables and |
| 262 | + uses them to create an instance of the Mailjet `Client` for authenticated |
| 263 | + API interactions. |
| 264 | +
|
| 265 | + Attributes: |
| 266 | + - self.auth (tuple[str, str]): A tuple containing the public and private API keys obtained from the environment variables 'MJ_APIKEY_PUBLIC' and 'MJ_APIKEY_PRIVATE' respectively. |
| 267 | + - self.client (Client): An instance of the Mailjet Client class, initialized with the provided authentication credentials. |
| 268 | + - self.id_contactslist (str): A string of the contacts list ID from https://app.mailjet.com/contacts |
| 269 | + """ |
| 270 | + self.auth: tuple[str, str] = ( |
| 271 | + os.environ["MJ_APIKEY_PUBLIC"], |
| 272 | + os.environ["MJ_APIKEY_PRIVATE"], |
| 273 | + ) |
| 274 | + self.client: Client = Client(auth=self.auth) |
| 275 | + self.id_contactslist: str = os.environ["ID_CONTACTSLIST"] |
| 276 | + |
| 277 | + def test_01_upload_the_csv(self) -> None: |
| 278 | + """Test uploading a csv file. |
| 279 | +
|
| 280 | + POST https://api.mailjet.com/v3/DATA/contactslist |
| 281 | + /$ID_CONTACTLIST/CSVData/text:plain |
| 282 | + """ |
| 283 | + result = self.client.contactslist_csvdata.create( |
| 284 | + id=self.id_contactslist, |
| 285 | + data=Path("tests/doc_tests/files/data.csv").read_text(encoding="utf-8"), |
| 286 | + ) |
| 287 | + self.assertEqual(result.status_code, 200) |
| 288 | + |
| 289 | + self.set_shared("data_id", result.json().get("ID")) |
| 290 | + data_id = self.get_shared("data_id") |
| 291 | + self.assertIsNotNone(data_id) |
| 292 | + |
| 293 | + def test_02_import_csv_content_to_a_list(self) -> None: |
| 294 | + """Test importing a csv content to a list. |
| 295 | +
|
| 296 | + POST https://api.mailjet.com/v3/REST/csvimport |
| 297 | + """ |
| 298 | + data_id = self.get_shared("data_id") |
| 299 | + self.assertIsNotNone(data_id) |
| 300 | + data = { |
| 301 | + "Method": "addnoforce", |
| 302 | + "ContactsListID": self.id_contactslist, |
| 303 | + "DataID": data_id, |
| 304 | + } |
| 305 | + result = self.client.csvimport.create(data=data) |
| 306 | + self.assertEqual(result.status_code, 201) |
| 307 | + self.assertIn("ID", result.json()["Data"][0]) |
| 308 | + |
| 309 | + self.set_shared("id_value", result.json()["Data"][0]["ID"]) |
| 310 | + |
| 311 | + def test_03_monitor_the_import_progress(self) -> None: |
| 312 | + """Test getting a csv content import. |
| 313 | +
|
| 314 | + GET https://api.mailjet.com/v3/REST/csvimport/$importjob_ID |
| 315 | + """ |
| 316 | + result = self.client.csvimport.get(id=self.get_shared("id_value")) |
| 317 | + self.assertEqual(result.status_code, 200) |
| 318 | + self.assertIn("ID", result.json()["Data"][0]) |
| 319 | + self.assertEqual(0, result.json()["Data"][0]["Errcount"]) |
| 320 | + |
| 321 | + |
219 | 322 | if __name__ == "__main__": |
220 | 323 | unittest.main() |
0 commit comments