|
| 1 | +import os |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("emails")] |
| 6 | +for f in files: |
| 7 | + exec("import " + f[:-3] + " as " + f[:-3]) |
| 8 | + |
| 9 | + |
| 10 | +# test the file if it has the Emails class |
| 11 | +def test_names(): |
| 12 | + for f in files: |
| 13 | + assert "Emails" in dir(eval(f[:-3])), "Emails is not defined in " + f[:-3] |
| 14 | + |
| 15 | + |
| 16 | +# test the Emails class if it is a subclass of list |
| 17 | +def test_subclass(): |
| 18 | + for f in files: |
| 19 | + assert issubclass(eval(f[:-3]).Emails, list), ( |
| 20 | + "Emails is not a subclass of list in " + f[:-3] |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +# check if the Emails class is an instance of the list class |
| 25 | +def test_instance(): |
| 26 | + for f in files: |
| 27 | + assert isinstance(eval(f[:-3]).Emails([]), list), ( |
| 28 | + "Emails is not an instance of list in " + f[:-3] |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +# test the Emails class if it has the validate method |
| 33 | +def test_validate(): |
| 34 | + for f in files: |
| 35 | + assert "validate" in dir(eval(f[:-3]).Emails), ( |
| 36 | + "validate is not defined in " + f[:-3] |
| 37 | + ) |
| 38 | + assert callable(eval(f[:-3]).Emails.validate), ( |
| 39 | + "validate is not callable in " + f[:-3] |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +# test the Emails class if it has the __init__ method |
| 44 | +def test_init(): |
| 45 | + for f in files: |
| 46 | + assert "__init__" in dir(eval(f[:-3]).Emails), ( |
| 47 | + "__init__ is not defined in " + f[:-3] |
| 48 | + ) |
| 49 | + assert callable(eval(f[:-3]).Emails.__init__), ( |
| 50 | + "__init__ is not callable in " + f[:-3] |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +# test the Emails class if it has the __repr__ method |
| 55 | +def test_repr(): |
| 56 | + for f in files: |
| 57 | + assert "__repr__" in dir(eval(f[:-3]).Emails), ( |
| 58 | + "__repr__ is not defined in " + f[:-3] |
| 59 | + ) |
| 60 | + assert callable(eval(f[:-3]).Emails.__repr__), ( |
| 61 | + "__repr__ is not callable in " + f[:-3] |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +# check Emails class can reproduce itself with the __repr__ method |
| 66 | +def test_repr_output(): |
| 67 | + for f in files: |
| 68 | + assert ( |
| 69 | + repr(eval(f[:-3]).Emails(["bora.canbula@cbu.edu.tr", "bora@canbula.com"])) |
| 70 | + == ( |
| 71 | + eval(f[:-3]).Emails(["bora.canbula@cbu.edu.tr", "bora@canbula.com"]) |
| 72 | + ).__repr__() |
| 73 | + ), ( |
| 74 | + "The Emails class does not reproduce itself with the __repr__ method in " |
| 75 | + + f[:-3] |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +# test the Emails class if it has the __str__ method |
| 80 | +def test_str(): |
| 81 | + for f in files: |
| 82 | + assert "__str__" in dir(eval(f[:-3]).Emails), ( |
| 83 | + "__str__ is not defined in " + f[:-3] |
| 84 | + ) |
| 85 | + assert callable(eval(f[:-3]).Emails.__str__), ( |
| 86 | + "__str__ is not callable in " + f[:-3] |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +# check if the validate method allows only strings |
| 91 | +def test_validate_strings(): |
| 92 | + for f in files: |
| 93 | + with pytest.raises(ValueError) as e: |
| 94 | + eval(f[:-3]).Emails([1, 2, 3]) |
| 95 | + assert str(e.type) == "<class 'ValueError'>", ( |
| 96 | + "The validate method does not allow only strings in " + f[:-3] |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +# check if the validate method allows only valid email addresses, and raises ValueError otherwise |
| 101 | +def test_validate_emails(): |
| 102 | + for f in files: |
| 103 | + with pytest.raises(ValueError) as e: |
| 104 | + eval(f[:-3]).Emails(["bora.canbula@cbu.edu.tr", "canbula.com"]) |
| 105 | + assert str(e.type) == "<class 'ValueError'>", ( |
| 106 | + "The validate method does not allow only valid email addresses in " |
| 107 | + + f[:-3] |
| 108 | + ) |
| 109 | + with pytest.raises(ValueError) as e: |
| 110 | + eval(f[:-3]).Emails(["bora@canbulacom"]) |
| 111 | + assert str(e.type) == "<class 'ValueError'>", ( |
| 112 | + "The validate method does not allow only valid email addresses in " |
| 113 | + + f[:-3] |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +# check Emails class does not allow duplicate email addresses, the order is not important |
| 118 | +def test_validate_duplicates(): |
| 119 | + for f in files: |
| 120 | + assert ( |
| 121 | + eval(f[:-3]) |
| 122 | + .Emails( |
| 123 | + [ |
| 124 | + "bora.canbula@cbu.edu.tr", |
| 125 | + "bora@canbula.com", |
| 126 | + "bora.canbula@cbu.edu.tr", |
| 127 | + ] |
| 128 | + ) |
| 129 | + .data.count("bora.canbula@cbu.edu.tr") |
| 130 | + == 1 |
| 131 | + ), ( |
| 132 | + "The Emails class does not allow duplicate email addresses in " + f[:-3] |
| 133 | + ) |
0 commit comments