How to use test_now method in localstack

Best Python code snippet using localstack_python

test_date_filter.py

Source:test_date_filter.py Github

copy

Full Screen

1# Standard Packages2import re3from datetime import datetime4from math import inf5# External Packages6import torch7# Application Packages8from src.search_filter.date_filter import DateFilter9def test_date_filter():10 embeddings = torch.randn(3, 10)11 entries = [12 {'compiled': '', 'raw': 'Entry with no date'},13 {'compiled': '', 'raw': 'April Fools entry: 1984-04-01'},14 {'compiled': '', 'raw': 'Entry with date:1984-04-02'}]15 q_with_no_date_filter = 'head tail'16 ret_query, entry_indices = DateFilter().apply(q_with_no_date_filter, entries)17 assert ret_query == 'head tail'18 assert entry_indices == {0, 1, 2}19 q_with_dtrange_non_overlapping_at_boundary = 'head dt>"1984-04-01" dt<"1984-04-02" tail'20 ret_query, entry_indices = DateFilter().apply(q_with_dtrange_non_overlapping_at_boundary, entries)21 assert ret_query == 'head tail'22 assert entry_indices == set()23 query_with_overlapping_dtrange = 'head dt>"1984-04-01" dt<"1984-04-03" tail'24 ret_query, entry_indices = DateFilter().apply(query_with_overlapping_dtrange, entries)25 assert ret_query == 'head tail'26 assert entry_indices == {2}27 query_with_overlapping_dtrange = 'head dt>="1984-04-01" dt<"1984-04-02" tail'28 ret_query, entry_indices = DateFilter().apply(query_with_overlapping_dtrange, entries)29 assert ret_query == 'head tail'30 assert entry_indices == {1}31 query_with_overlapping_dtrange = 'head dt>"1984-04-01" dt<="1984-04-02" tail'32 ret_query, entry_indices = DateFilter().apply(query_with_overlapping_dtrange, entries)33 assert ret_query == 'head tail'34 assert entry_indices == {2}35 query_with_overlapping_dtrange = 'head dt>="1984-04-01" dt<="1984-04-02" tail'36 ret_query, entry_indices = DateFilter().apply(query_with_overlapping_dtrange, entries)37 assert ret_query == 'head tail'38 assert entry_indices == {1, 2}39def test_extract_date_range():40 assert DateFilter().extract_date_range('head dt>"1984-01-04" dt<"1984-01-07" tail') == [datetime(1984, 1, 5, 0, 0, 0).timestamp(), datetime(1984, 1, 7, 0, 0, 0).timestamp()]41 assert DateFilter().extract_date_range('head dt<="1984-01-01"') == [0, datetime(1984, 1, 2, 0, 0, 0).timestamp()]42 assert DateFilter().extract_date_range('head dt>="1984-01-01"') == [datetime(1984, 1, 1, 0, 0, 0).timestamp(), inf]43 assert DateFilter().extract_date_range('head dt:"1984-01-01"') == [datetime(1984, 1, 1, 0, 0, 0).timestamp(), datetime(1984, 1, 2, 0, 0, 0).timestamp()]44 # Unparseable date filter specified in query45 assert DateFilter().extract_date_range('head dt:"Summer of 69" tail') == None46 # No date filter specified in query47 assert DateFilter().extract_date_range('head tail') == None48 # Non intersecting date ranges49 assert DateFilter().extract_date_range('head dt>"1984-01-01" dt<"1984-01-01" tail') == None50def test_parse():51 test_now = datetime(1984, 4, 1, 21, 21, 21)52 # day variations53 assert DateFilter().parse('today', relative_base=test_now) == (datetime(1984, 4, 1, 0, 0, 0), datetime(1984, 4, 2, 0, 0, 0))54 assert DateFilter().parse('tomorrow', relative_base=test_now) == (datetime(1984, 4, 2, 0, 0, 0), datetime(1984, 4, 3, 0, 0, 0))55 assert DateFilter().parse('yesterday', relative_base=test_now) == (datetime(1984, 3, 31, 0, 0, 0), datetime(1984, 4, 1, 0, 0, 0))56 assert DateFilter().parse('5 days ago', relative_base=test_now) == (datetime(1984, 3, 27, 0, 0, 0), datetime(1984, 3, 28, 0, 0, 0))57 # week variations58 assert DateFilter().parse('last week', relative_base=test_now) == (datetime(1984, 3, 18, 0, 0, 0), datetime(1984, 3, 25, 0, 0, 0))59 assert DateFilter().parse('2 weeks ago', relative_base=test_now) == (datetime(1984, 3, 11, 0, 0, 0), datetime(1984, 3, 18, 0, 0, 0))60 # month variations61 assert DateFilter().parse('next month', relative_base=test_now) == (datetime(1984, 5, 1, 0, 0, 0), datetime(1984, 6, 1, 0, 0, 0))62 assert DateFilter().parse('2 months ago', relative_base=test_now) == (datetime(1984, 2, 1, 0, 0, 0), datetime(1984, 3, 1, 0, 0, 0))63 # year variations64 assert DateFilter().parse('this year', relative_base=test_now) == (datetime(1984, 1, 1, 0, 0, 0), datetime(1985, 1, 1, 0, 0, 0))65 assert DateFilter().parse('20 years later', relative_base=test_now) == (datetime(2004, 1, 1, 0, 0, 0), datetime(2005, 1, 1, 0, 0, 0))66 # specific month/date variation67 assert DateFilter().parse('in august', relative_base=test_now) == (datetime(1983, 8, 1, 0, 0, 0), datetime(1983, 8, 2, 0, 0, 0))68 assert DateFilter().parse('on 1983-08-01', relative_base=test_now) == (datetime(1983, 8, 1, 0, 0, 0), datetime(1983, 8, 2, 0, 0, 0))69def test_date_filter_regex():70 dtrange_match = re.findall(DateFilter().date_regex, 'multi word head dt>"today" dt:"1984-01-01"')71 assert dtrange_match == [('>', 'today'), (':', '1984-01-01')]72 dtrange_match = re.findall(DateFilter().date_regex, 'head dt>"today" dt:"1984-01-01" multi word tail')73 assert dtrange_match == [('>', 'today'), (':', '1984-01-01')]74 dtrange_match = re.findall(DateFilter().date_regex, 'multi word head dt>="today" dt="1984-01-01"')75 assert dtrange_match == [('>=', 'today'), ('=', '1984-01-01')]76 dtrange_match = re.findall(DateFilter().date_regex, 'dt<"multi word date" multi word tail')77 assert dtrange_match == [('<', 'multi word date')]78 dtrange_match = re.findall(DateFilter().date_regex, 'head dt<="multi word date"')79 assert dtrange_match == [('<=', 'multi word date')]80 dtrange_match = re.findall(DateFilter().date_regex, 'head tail')...

Full Screen

Full Screen

test_ExpirationCheck.py

Source:test_ExpirationCheck.py Github

copy

Full Screen

1# test_ExpirationCheck2import unittest3import unittest.mock as mock4from datetime import datetime, timedelta, time5from TestSuite import TestSuite6from wall_cycler.Interval.ExpirationCheck import ExpirationCheck, AlwaysExpired, NeverExpires7from wall_cycler.Interval.Intervals import BaseInterval8TEST_NOW = datetime(year=2019, month=12, day=29, hour=15, minute=40, second=20)9TEST_MSG = "test message"10class ExpirationCheckTests(TestSuite):11 @classmethod12 def setUpClass(cls):13 super().setUpClass()14 cls._uutFactory = ExpirationCheck15 cls._mockIntervals = cls._prepareTestIntervals()16 @staticmethod17 def _prepareTestIntervals():18 return [19 _MockInterval(False, TEST_NOW + timedelta(days=1)),20 _MockInterval(True, TEST_NOW + timedelta(days=1))21 ]22 def test_expirationCheck(self):23 for interval in self._mockIntervals:24 timestampStore = _MockTimestampStore(TEST_NOW)25 uut = self._uutFactory(interval, timestampStore)26 self.assertEqual(uut.isExpired(), interval._isExpired)27 self.assertEqual(interval._testedAgainstTimestamp, TEST_NOW)28 def test_mark(self):29 for interval in self._mockIntervals:30 timestampStore = _MockTimestampStore(TEST_NOW)31 uut = self._uutFactory(interval, timestampStore)32 uut.mark()33 self.assertTrue(interval._calledMark)34 self.assertEqual(timestampStore.timestamp, TEST_NOW)35 self.assertEqual(timestampStore.msg, TEST_MSG)36 def test_getNext(self):37 for interval in self._mockIntervals:38 timestampStore = _MockTimestampStore(TEST_NOW)39 uut = self._uutFactory(interval, timestampStore)40 nextChange = uut.getNext()41 self.assertEqual(nextChange, interval._next)42 self.assertTrue(timestampStore._calledRead)43 def test_alwaysTrueWhenNoTimestampInCache(self):44 for interval in self._mockIntervals:45 timestampStore = _MockTimestampStore(None)46 uut = self._uutFactory(interval, timestampStore)47 self.assertEqual(uut.isExpired(), True)48 self.assertEqual(interval._testedAgainstTimestamp, None)49class AlwaysExpiredTests(ExpirationCheckTests):50 @classmethod51 def setUpClass(cls):52 super().setUpClass()53 cls._uutFactory = AlwaysExpired54 def test_expirationCheck(self):55 for interval in self._mockIntervals:56 timestampStore = _MockTimestampStore(TEST_NOW)57 uut = self._uutFactory(interval, timestampStore)58 self.assertTrue(uut.isExpired())59 def test_getNext(self):60 for interval in self._mockIntervals:61 timestampStore = _MockTimestampStore(TEST_NOW)62 uut = self._uutFactory(interval, timestampStore)63 self.assertIsNone(uut.getNext())64class NeverExpiresTests(AlwaysExpiredTests):65 @classmethod66 def setUpClass(cls):67 super().setUpClass()68 cls._uutFactory = NeverExpires69 test_alwaysTrueWhenNoTimestampInCache = None70 def test_expirationCheck(self):71 for interval in self._mockIntervals:72 timestampStore = _MockTimestampStore(TEST_NOW)73 uut = self._uutFactory(interval, timestampStore)74 self.assertFalse(uut.isExpired())75 def test_alwaysFalseWhenNoTimestampInCache(self):76 for interval in self._mockIntervals:77 timestampStore = _MockTimestampStore(None)78 uut = self._uutFactory(interval, timestampStore)79 self.assertFalse(uut.isExpired())80 self.assertEqual(interval._testedAgainstTimestamp, None)81class _MockTimestampStore:82 def __init__(self, lastTimestamp):83 self.msg = None84 self.timestamp = lastTimestamp85 self._now = TEST_NOW86 self._calledRead = False87 def readTimestamp(self):88 self._calledRead = True89 return self.timestamp, self.msg90 def writeTimestamp(self, msg):91 self.timestamp = self._now92 self.msg = msg93class _MockInterval(BaseInterval):94 def __init__(self, isExpired, nextChange):95 self._isExpired = isExpired96 self._next = nextChange97 self._calledMark = False98 self._testedAgainstTimestamp = None99 def isExpired(self, lastChange):100 self._testedAgainstTimestamp = lastChange101 return self._isExpired102 def mark(self):103 self._calledMark = True104 return TEST_MSG105 def getNext(self, lastChange):106 self._testedAgainstTimestamp = lastChange107 return self._next108 def __str__(self):109 return "testInterval({}, {}, {}, {})".format(self._isExpired, self._next, self._calledMark,110 self._testedAgainstTimestamp)111if __name__ == '__main__':...

Full Screen

Full Screen

test_logger.py

Source:test_logger.py Github

copy

Full Screen

1from unittest.mock import patch2from fileinfo.logger import Logger3# To patch the immutable method now in datetime doesn't work4# @patch('datetime.datetime.now')5# def test_log(mock_now):6# test_now = 1237# test_message = 'A test message'8# mock_now.return_value = test_now9 # test_logger = Logger()10 # test_logger.log(test_message)11 # assert test_logger.messages == [(test_now, test_message)]12# Solution to solve this problem:13# 1 - To patch the module datetime instead of datetime.now()14# 2 - To do this importing from the logger.py file instead of the 15# global scope16@patch('fileinfo.logger.datetime.datetime')17def test_log(mock_datetime):18 test_now = 12319 test_message = 'A test message'20 mock_datetime.now.return_value = test_now21 test_logger = Logger()22 test_logger.log(test_message)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful