def test_add_tested_path_does_not_exists(self, datetime_patch): """ Tests the addition method for the case that the flle we are testing is not indexed. """ self.inactive_db.database = {} our_value = datetime(1970, 1, 1, 1, 0, 2, 0, tzinfo=TZ("+", hours=1).get()) datetime_patch = Mock(wraps=datetime) datetime_patch.utcnow = Mock(return_value=our_value) patcher = patch("PyFunceble.database.inactive.datetime", new=datetime_patch) patcher.start() subject = "hello.world" expected = { self.file_to_test: { subject: { "included_at_epoch": our_value.timestamp(), "included_at_iso": our_value.isoformat(), "last_retested_at_epoch": our_value.timestamp(), "last_retested_at_iso": our_value.isoformat(), "status": PyFunceble.STATUS.official.down, } } } self.inactive_db.add(subject, PyFunceble.STATUS.official.down) self.assertEqual(expected, self.inactive_db.database) subject = "world.hello" expected[self.file_to_test][subject] = expected[self.file_to_test][ "hello.world" ].copy() self.inactive_db.add(subject, PyFunceble.STATUS.official.down) self.assertEqual(expected, self.inactive_db.database) self.inactive_db.add(subject, PyFunceble.STATUS.official.down) self.assertEqual(expected, self.inactive_db.database) patcher.stop()
def test_stay_safe(self, datetime_patch): """ Tests the correctness of the desired message. """ self.tearDown() self.setUp() expected = f""" {Fore.GREEN}{Style.BRIGHT}Thanks for using PyFunceble! """ datetime_patch = Mock(wraps=datetime) datetime_patch.now = Mock(return_value=datetime( 1970, 1, 1, 1, 0, 2, 0, tzinfo=TZ("+", hours=1).get())) patch("PyFunceble.core.cli.datetime", new=datetime_patch).start() CLI().stay_safe() actual = sys.stdout.getvalue() self.assertEqual(expected, actual)
def test_stay_safe_share_experiance(self, datetime_patch): """ Tests the correctness of the desired message. """ self.tearDown() self.setUp() expected = f""" {Fore.GREEN}{Style.BRIGHT}Thanks for using PyFunceble! {Fore.YELLOW}{Style.BRIGHT}Share your experience on {Fore.CYAN}Twitter{Fore.YELLOW} with {Fore.CYAN}#PyFunceble{Fore.YELLOW}! {Fore.GREEN}{Style.BRIGHT}Have a feedback, an issue or an improvement idea? {Fore.YELLOW}{Style.BRIGHT}Let us know on {Fore.CYAN}GitHub{Fore.YELLOW}! """ datetime_patch = Mock(wraps=datetime) datetime_patch.now = Mock(return_value=datetime( 1970, 1, 1, 1, 0, 3, 0, tzinfo=TZ("+", hours=1).get())) patch("PyFunceble.core.cli.datetime", new=datetime_patch).start() CLI().stay_safe() actual = sys.stdout.getvalue() self.assertEqual(expected, actual)
def test_before_header(self, datetime_patch): """ Tests the method which prints some information before the generation of the content of a file. """ expected = False actual = self.file_instance.exists() self.assertEqual(expected, actual) datetime_patch = Mock(wraps=datetime) datetime_patch.utcnow = Mock( return_value=datetime(1970, 1, 1, 1, 0, 2, 0, tzinfo=TZ("+", hours=1).get()) ) patch("PyFunceble.output.prints.datetime", new=datetime_patch).start() # pylint: disable=line-too-long expected = f"""# Generated by {PyFunceble.NAME} (v{PyFunceble.VERSION.split()[0]}) / {PyFunceble.abstracts.Infrastructure.REPO_LINK} # Date of generation: {datetime_patch.utcnow().isoformat()} """ Prints( None, None, output_file=self.file_instance.path, only_on_file=False ).before_header() self.assertEqual(expected, self.file_instance.read()) self.file_instance.delete() expected = False actual = self.file_instance.exists() self.assertEqual(expected, actual) # pylint: disable=line-too-long expected = f"""# Generated by {PyFunceble.NAME} (v{PyFunceble.VERSION.split()[0]}) / {PyFunceble.abstracts.Infrastructure.REPO_LINK} # Date of generation: {datetime_patch.utcnow().isoformat()} Hello World """ printer = Prints( None, "Generic_File", output_file=self.file_instance.path, only_on_file=False, ) printer.currently_used_header = OrderedDict( zip( [ "Hello", "World", "Expiration Date", "Source", "HTTP Code", "Analyze Date", ], [5, 5], ) ) printer.before_header() self.assertEqual(expected, self.file_instance.read())
def test_load_file_exists(self, datetime_patch): """ Tests the case that we load the file. """ our_value = datetime(1970, 1, 1, 1, 3, 10, 0, tzinfo=TZ("+", hours=1).get()) datetime_patch = Mock(wraps=datetime) datetime_patch.now = Mock(return_value=our_value) datetime_patch.fromtimestamp = Mock(return_value=our_value) patcher = patch("PyFunceble.database.inactive.datetime", new=datetime_patch) patcher.start() # We also test the merging. to_write = { self.file_to_test: { "190": { "example.com": PyFunceble.STATUS.official.invalid } }, "this_is_a_well_informed_ghost": { "example.com": { "included_at_epoch": 0.0, "included_at_iso": "1970-01-01T01:00:00", "last_retested_at_epoch": 0.0, "last_retested_at_iso": "1970-01-01T01:00:00", "status": PyFunceble.STATUS.official.invalid, }, }, } expected = { self.file_to_test: { "example.com": { "included_at_epoch": our_value.timestamp(), "included_at_iso": our_value.isoformat(), "last_retested_at_epoch": our_value.timestamp(), "last_retested_at_iso": our_value.isoformat(), "status": PyFunceble.STATUS.official.invalid, }, }, "this_is_a_well_informed_ghost": { "example.com": { "included_at_epoch": 0.0, "included_at_iso": "1970-01-01T01:00:00", "last_retested_at_epoch": 0.0, "last_retested_at_iso": "1970-01-01T01:00:00", "status": PyFunceble.STATUS.official.invalid, }, }, } PyFunceble.helpers.Dict(to_write).to_json_file(self.storage_file) self.inactive_db.load() self.assertEqual(expected, self.inactive_db.database) patcher.stop()