|
| 1 | +import sys |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +import tensorflow as tf |
| 6 | + |
| 7 | +from tf_notify import EmailCallback |
| 8 | + |
| 9 | + |
| 10 | +class TestEmailCallback: |
| 11 | + @pytest.mark.skipif( |
| 12 | + sys.version_info < (3, 8), reason="requires python3.8 or higher" |
| 13 | + ) |
| 14 | + def test_callback_occurs_on_train_end(self): |
| 15 | + |
| 16 | + # define tf.keras model to add callback to |
| 17 | + model = tf.keras.Sequential(name="neural-network") |
| 18 | + model.add(tf.keras.layers.Dense(1, input_dim=784)) |
| 19 | + model.compile( |
| 20 | + optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.1), |
| 21 | + loss="mean_squared_error", |
| 22 | + metrics=["mean_absolute_error"], |
| 23 | + ) |
| 24 | + |
| 25 | + # load example MNIST data and pre-process it |
| 26 | + (x_train, y_train), _ = tf.keras.datasets.mnist.load_data() |
| 27 | + x_train = x_train.reshape(-1, 784).astype("float32") / 255.0 |
| 28 | + |
| 29 | + # limit the data to 1000 samples |
| 30 | + x_train = x_train[:1000] |
| 31 | + y_train = y_train[:1000] |
| 32 | + |
| 33 | + with mock.patch("smtplib.SMTP", autospec=True) as smtp_mock: |
| 34 | + # initiate training |
| 35 | + model.fit( |
| 36 | + x_train, |
| 37 | + y_train, |
| 38 | + batch_size=128, |
| 39 | + epochs=2, |
| 40 | + verbose=0, |
| 41 | + validation_split=0.5, |
| 42 | + callbacks=[ |
| 43 | + EmailCallback( |
| 44 | + to="toaddress@yahoo.com", |
| 45 | + from_="fromaddress@yahoo.com", |
| 46 | + host="smtp.mail.yahoo.com", |
| 47 | + port=465, |
| 48 | + username="my-cool-username", |
| 49 | + password="my-cool-password", # one-time app password |
| 50 | + ssl=False, |
| 51 | + ) |
| 52 | + ], |
| 53 | + ) |
| 54 | + |
| 55 | + # validate smtplib.SMTP was called twice |
| 56 | + assert len(smtp_mock.method_calls) == 2 |
| 57 | + |
| 58 | + login, send_message = smtp_mock.method_calls |
| 59 | + |
| 60 | + # one time to authenticate against the SMTP server |
| 61 | + assert login.args == ( |
| 62 | + "my-cool-username", |
| 63 | + "my-cool-password", |
| 64 | + ) # .args available in py3.8+, this fails on py3.7 |
| 65 | + |
| 66 | + # and another, to send the message |
| 67 | + email = send_message.args[0] |
| 68 | + components = dict(email.items()) |
| 69 | + |
| 70 | + assert components["To"] == "toaddress@yahoo.com" |
| 71 | + assert components["From"] == "fromaddress@yahoo.com" |
| 72 | + assert components["Subject"] == "New mail from 'tf-notify'!" |
| 73 | + |
| 74 | + payload = email.get_payload()[0].as_string() |
| 75 | + |
| 76 | + assert "model <neural-network> has completed its training!" in payload |
| 77 | + |
| 78 | + @pytest.mark.skipif( |
| 79 | + sys.version_info < (3, 8), reason="requires python3.8 or higher" |
| 80 | + ) |
| 81 | + def test_callback_occurs_on_train_end_while_both_message_and_subject_are_overridden( |
| 82 | + self, |
| 83 | + ): |
| 84 | + |
| 85 | + # define tf.keras model to add callback to |
| 86 | + model = tf.keras.Sequential(name="neural-network") |
| 87 | + model.add(tf.keras.layers.Dense(1, input_dim=784)) |
| 88 | + model.compile( |
| 89 | + optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.1), |
| 90 | + loss="mean_squared_error", |
| 91 | + metrics=["mean_absolute_error"], |
| 92 | + ) |
| 93 | + |
| 94 | + # load example MNIST data and pre-process it |
| 95 | + (x_train, y_train), _ = tf.keras.datasets.mnist.load_data() |
| 96 | + x_train = x_train.reshape(-1, 784).astype("float32") / 255.0 |
| 97 | + |
| 98 | + # limit the data to 1000 samples |
| 99 | + x_train = x_train[:1000] |
| 100 | + y_train = y_train[:1000] |
| 101 | + |
| 102 | + with mock.patch("smtplib.SMTP", autospec=True) as smtp_mock: |
| 103 | + # initiate training |
| 104 | + model.fit( |
| 105 | + x_train, |
| 106 | + y_train, |
| 107 | + batch_size=128, |
| 108 | + epochs=2, |
| 109 | + verbose=0, |
| 110 | + validation_split=0.5, |
| 111 | + callbacks=[ |
| 112 | + EmailCallback( |
| 113 | + message="This is a custom message!", |
| 114 | + subject="You have mail!", |
| 115 | + to="toaddress@yahoo.com", |
| 116 | + from_="fromaddress@yahoo.com", |
| 117 | + host="smtp.mail.yahoo.com", |
| 118 | + port=465, |
| 119 | + username="my-cool-username", |
| 120 | + password="my-cool-password", # one-time app password |
| 121 | + ssl=False, # if this is True, patching of smtplib.SMTP_SSL is needed instead |
| 122 | + ) |
| 123 | + ], |
| 124 | + ) |
| 125 | + |
| 126 | + # validate smtplib.SMTP was called twice |
| 127 | + assert len(smtp_mock.method_calls) == 2 |
| 128 | + |
| 129 | + login, send_message = smtp_mock.method_calls |
| 130 | + |
| 131 | + # one time to authenticate against the SMTP server |
| 132 | + assert login.args == ( |
| 133 | + "my-cool-username", |
| 134 | + "my-cool-password", |
| 135 | + ) # .args available in py3.8+, this fails on py3.7 |
| 136 | + |
| 137 | + # and another, to send the message |
| 138 | + email = send_message.args[0] |
| 139 | + components = dict(email.items()) |
| 140 | + |
| 141 | + assert components["To"] == "toaddress@yahoo.com" |
| 142 | + assert components["From"] == "fromaddress@yahoo.com" |
| 143 | + assert components["Subject"] == "You have mail!" |
| 144 | + |
| 145 | + payload = email.get_payload()[0].as_string() |
| 146 | + |
| 147 | + assert "This is a custom message!" in payload |
0 commit comments