import smtplib
import unittest
from unittest import mock

from action import PrintAction, EmailAction


@mock.patch("builtins.print")
class PrintActionTest(unittest.TestCase):
    def test_executing_action_prints_message(self, mock_print):
        action = PrintAction()
        action.execute("GOOG > $10")
        mock_print.assert_called_with("GOOG > $10")


@mock.patch("smtplib.SMTP")
class EmailActionTest(unittest.TestCase):
    def setUp(self):
        self.action = EmailAction(to="bsmukasa@gmail.com")

    def test_email_is_sent_to_the_right_server(self, mock_smtp_class):
        self.action.execute("MSFT has crossed $10 price level")
        mock_smtp_class.assert_called_with("email.stocks.com")

import smtplib
import unittest
from unittest import mock

from action import PrintAction, EmailAction


@mock.patch("builtins.print")
class PrintActionTest(unittest.TestCase):
    def test_executing_action_prints_message(self, mock_print):
        action = PrintAction()
        action.execute("GOOG > $10")
        mock_print.assert_called_with("GOOG > $10")


@mock.patch("smtplib.SMTP")
class EmailActionTest(unittest.TestCase):
    def setUp(self):
        self.action = EmailAction(to="bsmukasa@gmail.com")

    def test_email_is_sent_to_the_right_server(self, mock_smtp_class):
        self.action.execute("MSFT has crossed $10 price level")
        mock_smtp_class.assert_called_with("email.stocks.com")

    def test_connection_closed_after_sending_mail(self, mock_smtp_class):
        mock_smtp = mock_smtp_class.return_value
        self.action.execute("MSFT has crossed $10 price level")
        mock_smtp.send_message.assert_called_with(mock.ANY)
        self.assertTrue(mock_smtp.quit.called)
        mock_smtp.assert_has_calls([
            mock.call.send_message(mock.ANY),
            mock.call.quit()
        ])

Add test to check if connection is closed after email is sent.
Add test to check if connection is closed after email is sent.

  import smtplib
  import unittest
  from unittest import mock
  
  from action import PrintAction, EmailAction
  
  
  @mock.patch("builtins.print")
  class PrintActionTest(unittest.TestCase):
      def test_executing_action_prints_message(self, mock_print):
          action = PrintAction()
          action.execute("GOOG > $10")
          mock_print.assert_called_with("GOOG > $10")
  
  
  @mock.patch("smtplib.SMTP")
  class EmailActionTest(unittest.TestCase):
      def setUp(self):
          self.action = EmailAction(to="bsmukasa@gmail.com")
  
      def test_email_is_sent_to_the_right_server(self, mock_smtp_class):
          self.action.execute("MSFT has crossed $10 price level")
          mock_smtp_class.assert_called_with("email.stocks.com")
  
+     def test_connection_closed_after_sending_mail(self, mock_smtp_class):
+         mock_smtp = mock_smtp_class.return_value
+         self.action.execute("MSFT has crossed $10 price level")
+         mock_smtp.send_message.assert_called_with(mock.ANY)
+         self.assertTrue(mock_smtp.quit.called)
+         mock_smtp.assert_has_calls([
+             mock.call.send_message(mock.ANY),
+             mock.call.quit()
+         ])
+ 
Add test to check if connection is closed after email is sent.
## Code Before:
import smtplib
import unittest
from unittest import mock

from action import PrintAction, EmailAction


@mock.patch("builtins.print")
class PrintActionTest(unittest.TestCase):
    def test_executing_action_prints_message(self, mock_print):
        action = PrintAction()
        action.execute("GOOG > $10")
        mock_print.assert_called_with("GOOG > $10")


@mock.patch("smtplib.SMTP")
class EmailActionTest(unittest.TestCase):
    def setUp(self):
        self.action = EmailAction(to="bsmukasa@gmail.com")

    def test_email_is_sent_to_the_right_server(self, mock_smtp_class):
        self.action.execute("MSFT has crossed $10 price level")
        mock_smtp_class.assert_called_with("email.stocks.com")

## Instruction:
Add test to check if connection is closed after email is sent.
## Code After:
import smtplib
import unittest
from unittest import mock

from action import PrintAction, EmailAction


@mock.patch("builtins.print")
class PrintActionTest(unittest.TestCase):
    def test_executing_action_prints_message(self, mock_print):
        action = PrintAction()
        action.execute("GOOG > $10")
        mock_print.assert_called_with("GOOG > $10")


@mock.patch("smtplib.SMTP")
class EmailActionTest(unittest.TestCase):
    def setUp(self):
        self.action = EmailAction(to="bsmukasa@gmail.com")

    def test_email_is_sent_to_the_right_server(self, mock_smtp_class):
        self.action.execute("MSFT has crossed $10 price level")
        mock_smtp_class.assert_called_with("email.stocks.com")

    def test_connection_closed_after_sending_mail(self, mock_smtp_class):
        mock_smtp = mock_smtp_class.return_value
        self.action.execute("MSFT has crossed $10 price level")
        mock_smtp.send_message.assert_called_with(mock.ANY)
        self.assertTrue(mock_smtp.quit.called)
        mock_smtp.assert_has_calls([
            mock.call.send_message(mock.ANY),
            mock.call.quit()
        ])
