<commit_before>from unittest.mock import patch, PropertyMock
import unittest
from pathlib import Path

import jinja2

import portinus
from portinus import Application

test_dir = Path(__file__).absolute().parent
test_data_dir = Path(test_dir).joinpath("testdata")
portinus_dir = str(test_data_dir.joinpath('portinus_dir'))

class testInit(unittest.TestCase):

    def setUp(self):
        pass

    @patch.object(portinus, 'service_dir')
    @patch('builtins.print')
    def test_list(self, fake_print, fake_service_dir):
        portinus.service_dir = portinus_dir
        portinus.list()
        self.assertEqual(str(fake_print.mock_calls[-2]),"call('bar')")
        self.assertEqual(str(fake_print.mock_calls[-1]),"call('baz')")

    def test_get_instance_dir(self):
        res = portinus.get_instance_dir('foo')

    def test_get_template(self):
        template = portinus.get_template('instance.service')
        self.assertTrue(isinstance(template, jinja2.Template))

class testApplication(unittest.TestCase):

    def setUp(self):
        pass

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    def test_init(self, fake_environment_file, fake_service,
                  fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        self.assertTrue(fake_restart_timer.called)
        self.assertTrue(fake_monitor_service.called)
        self.assertTrue(fake_service.called)
        self.assertTrue(fake_environment_file.called)

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    @patch.object(Application, '_create_service_dir')
    def test_ensure(self, fake__create_service_dir, fake_environment_file,
                    fake_service, fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        app.ensure()

        self.assertTrue(fake__create_service_dir.called)
        self.assertTrue(fake_environment_file().ensure.called)
        self.assertTrue(fake_service().ensure.called)
        self.assertTrue(fake_monitor_service().ensure.called)
        self.assertTrue(fake_restart_timer().ensure.called)

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    def test_remove(self, fake_environment_file,
                    fake_service, fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        app.remove()

        self.assertTrue(fake_environment_file().remove.called)
        self.assertTrue(fake_service().remove.called)
        self.assertTrue(fake_monitor_service().remove.called)
        self.assertTrue(fake_restart_timer().remove.called)<commit_msg>Add more tests<commit_after>from unittest.mock import patch, PropertyMock
import unittest
from pathlib import Path

import jinja2

import portinus
from portinus import Application

test_dir = Path(__file__).absolute().parent
test_data_dir = Path(test_dir).joinpath("testdata")
portinus_dir = str(test_data_dir.joinpath('portinus_dir'))

class testInit(unittest.TestCase):

    def setUp(self):
        pass

    @patch.object(portinus, 'service_dir')
    @patch('builtins.print')
    def test_list(self, fake_print, fake_service_dir):
        portinus.service_dir = portinus_dir
        portinus.list()
        self.assertEqual(str(fake_print.mock_calls[-2]),"call('bar')")
        self.assertEqual(str(fake_print.mock_calls[-1]),"call('baz')")

    def test_get_instance_dir(self):
        res = portinus.get_instance_dir('foo')

    def test_get_template(self):
        template = portinus.get_template('instance.service')
        self.assertTrue(isinstance(template, jinja2.Template))

class testApplication(unittest.TestCase):

    def setUp(self):
        pass

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    def test_init(self, fake_environment_file, fake_service,
                  fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        self.assertTrue(fake_restart_timer.called)
        self.assertTrue(fake_monitor_service.called)
        self.assertTrue(fake_service.called)
        self.assertTrue(fake_environment_file.called)

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    @patch.object(Application, '_create_service_dir')
    def test_ensure(self, fake__create_service_dir, fake_environment_file,
                    fake_service, fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        app.ensure()

        self.assertTrue(fake__create_service_dir.called)
        self.assertTrue(fake_environment_file().ensure.called)
        self.assertTrue(fake_service().ensure.called)
        self.assertTrue(fake_monitor_service().ensure.called)
        self.assertTrue(fake_restart_timer().ensure.called)

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    def test_remove(self, fake_environment_file,
                    fake_service, fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        app.remove()

        self.assertTrue(fake_environment_file().remove.called)
        self.assertTrue(fake_service().remove.called)
        self.assertTrue(fake_monitor_service().remove.called)
        self.assertTrue(fake_restart_timer().remove.called)

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    @patch('os.mkdir')
    def test__create_service_dir(self, fake_mkdir, fake_environment_file,
                                 fake_service, fake_monitor_service,
                                 fake_restart_timer):
        app = Application('foo')
        app._create_service_dir()
        fake_mkdir.assert_called_with(portinus.service_dir)

    @patch.object(portinus.restart, 'Timer')
    @patch.object(portinus.monitor, 'Service')
    @patch.object(portinus, 'Service')
    @patch.object(portinus, 'EnvironmentFile')
    @patch('os.mkdir', side_effect=FileExistsError)
    def test__create_service_dir_already_exists(self, fake_mkdir, fake_environment_file, fake_service, fake_monitor_service, fake_restart_timer):
        app = Application('foo')
        app._create_service_dir()
        fake_mkdir.assert_called_with(portinus.service_dir)
{'fraction_non_alphanumeric': 0.11473272490221642, 'fraction_numerical': 0.0011589164131537012, 'mean_word_length': 3.903409090909091, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7104436', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2059, 'n_words': 288}