def setUp(self): SimpleTestCase.setUp(self) """ ready up all variables and test class """ print '=' * 100 print "<%s> currently run: %s" % (self.__class__.__name__, self._testMethodName) print '-' * 100 + '\n'
def _teardown_database(self): SimpleTestCase._post_teardown(self) query = ''' START n = node(*) OPTIONAL MATCH n-[r]-() DELETE n, r; ''' self.graph_db.cypher.execute(query)
def _pre_setup(self): """ (MongoTestMixin) -> (NoneType) create a new mongo connection. Note:- It explicitly uses Class name to call methods, since the calling overriden _setup_database and _teardown_database is not required behaviour, as it would not work with other Test Mixins like Redis or Neo4j, which have their own _setup_database and _teardown_database """ SimpleTestCase._pre_setup(self) MongoTestMixin._setup_database(self)
def check_redirects(response, expected_url): test_case = SimpleTestCase() test_case.assertRedirects(response, expected_url=expected_url) return True
def __init__(self, tester=None, *args, **kwargs): self.tester = tester if tester else SimpleTestCase()
def check_form_error(response, form_name, field, errors, msg_prefix=''): test_case = SimpleTestCase() test_case.assertFormError(response, form_name, field, errors, msg_prefix) return True
def test_signout(self): response = self.client.get(reverse('signout')) SimpleTestCase().assertRedirects(response, reverse('signin'))
def tearDown(self): """ remove variables after test """ SimpleTestCase.tearDown(self) print '\n' + '=' * 100 + '\n\n'
def check_in_html(needle, haystack): test_case = SimpleTestCase() test_case.assertInHTML(needle, haystack) return True
def helpers(): return SimpleTestCase()
def _pre_setup(self): SimpleTestCase._pre_setup(self) RedisTestMixin._setup_database(self)
def _pre_setup(self): SimpleTestCase._pre_setup(self) Neo4jTestMixin._setup_database(self)
def assert_redirects() -> Callable: """Provide redirect assertion.""" return SimpleTestCase().assertRedirects
def test_roster_list_redirect_if_not_logged_in(client): """Test roster list view redirects if not logged in.""" response = client.get(reverse("roster_by_staff")) SimpleTestCase().assertRedirects( response, "/accounts/login/?next=/rosters/roster_by_staff/" )
def test_contains_partial(): c = SimpleTestCase() needle = "<span></span>" html = "<form>%s</form>" c.assertTrue(contains_partial(html % needle, needle)) needle = "<span></span><b></b>" c.assertRaises(NotImplementedError, contains_partial, html % needle, needle) needle = "<span>a</span>" c.assertRaises(NotImplementedError, contains_partial, html % needle, needle) needle = '<span id="e"></span>' html = '<form id="tt"><span id="f"></span>%s</form>' c.assertTrue(contains_partial(html % needle, needle)) missing = "<script></script>" c.assertFalse(contains_partial(html % missing, needle)) needle = '<span id="e"></span>' html = '<form id="tt"><span id="f"></span>%s</form>' missing = '<span id="g"></span>' c.assertFalse(contains_partial(html % missing, needle)) needle = '<div id="r"><span>toto</span></div>' html = '<form><div id="r"></div></form>' c.assertRaises(NotImplementedError, contains_partial, html, needle) # as we do not look at the children, needle is equivalent to <div id="r"></div> which IS in html c.assertTrue(contains_partial(html, needle, ignore_needle_children=True))
def STC(): return SimpleTestCase()
def check_template_used(response, template_name): test_case = SimpleTestCase() test_case.assertTemplateUsed(response, template_name=template_name) return True
def test_edit_animal_count( client, user_base, enclosure_factory, animal_factory, animal_A, enclosure_base, animal_count_A_BAR_datetime_factory, ): client.force_login(user_base) yesterday_time = timezone.localtime() - dt.timedelta(days=1) yesterday = yesterday_time.date() enc_not_permit = enclosure_factory("not_permit", None) anim_not_permit = animal_factory( name="kermitthefrog", identifier="green", sex="m", accession_number="abc123", enclosure=enc_not_permit, ) # not permitted resp = client.get(("/edit_animal_count/" f"{anim_not_permit.accession_number}/" f"{yesterday.year}/{yesterday.month}/{yesterday.day}/")) assert resp.status_code == 302 SimpleTestCase().assertRedirects(resp, "/") count = animal_count_A_BAR_datetime_factory(yesterday_time) # GET resp = client.get(("/edit_animal_count/" f"{animal_A.accession_number}/" f"{yesterday.year}/{yesterday.month}/{yesterday.day}/")) assert resp.status_code == 200 assert resp.context["count"] == count assert resp.context["animal"] == animal_A assert resp.context["enclosure"] == enclosure_base assert resp.context["dateday"].year == yesterday.year assert resp.context["dateday"].month == yesterday.month assert resp.context["dateday"].day == yesterday.day # todo: form context # POST post_data = { "condition": "NA", "comment": "this is a test count", "animal": animal_A.id, "enclosure": enclosure_base.id, } resp = client.post( ("/edit_animal_count/" f"{animal_A.accession_number}/" f"{yesterday.year}/{yesterday.month}/{yesterday.day}/"), data=post_data, follow=True, ) assert resp.status_code == 200 assert resp.redirect_chain == [(f"/count/{enclosure_base.slug}/", 302)] latest_count = animal_A.conditions.latest("datetimecounted") assert latest_count.condition == "NA" assert latest_count.comment == "this is a test count" assert latest_count.enclosure == enclosure_base
def check_contains(response, text): test_case = SimpleTestCase() test_case.assertContains(response, text=text) return True
def test_signin_not_authenticated(self): response = self.client.get('/signin/') SimpleTestCase().assertEqual(response.status_code, 200)
def _post_teardown(self): SimpleTestCase._post_teardown(self) MongoTestMixin._teardown_database(self)
def test_battle(self): response = self.client.get(reverse('backend:fight'), follow=True) SimpleTestCase().assertRedirects(response, expected_url='/login/?next=/battle/', status_code=302, target_status_code=200)
def test_get_compatible_column_names(self): """ tests getting column types compatible with paracoords library function """ self.assertIsInstance(file_helper.get_compatible_column_types(self.dataframe), dict) def test_get_html_friendly_names(self): """ tests removal of illegal characters in the column names (HTML escaping) function """ self.assertIsInstance(file_helper.get_html_friendly_names(self.dataframe.columns), list) class TestColumnNameFormatting(SimpleTestCase): def test_remove_illegal_characters(self): """ tests function to remove illegal characters """ self.assertEqual(file_helper.remove_illegal_characters("str^5"), 'str5') if __name__ == '__main__': # suite1 = SimpleTestCase.TestLoader().loadTestsFromTestCase(TestFileUpload) # suite2 = SimpleTestCase.TestLoader().loadTestsFromTestCase(TestDataFrameOperations) # suite3 = SimpleTestCase.TestLoader().loadTestsFromTestCase(TestColumnNameFormatting) # alltests = SimpleTestCase.TestSuite([suite1, suite2,suite3]) # SimpleTestCase.TextTestRunner(verbosity=2).run(alltests) SimpleTestCase.main()
def test_edit_group_count( client, user_base, group_B, group_factory, enclosure_base, enclosure_factory, group_B_count_datetime_factory, ): client.force_login(user_base) yesterday_time = timezone.localtime() - dt.timedelta(days=1) yesterday = yesterday_time.date() enc_not_permit = enclosure_factory("not_permit", None) group_not_permit = group_factory("abc123", 5, 4, 3, 12, enclosure=enc_not_permit) # not permitted resp = client.get(("/edit_group_count/" f"{group_not_permit.accession_number}/" f"{yesterday.year}/{yesterday.month}/{yesterday.day}/")) assert resp.status_code == 302 SimpleTestCase().assertRedirects(resp, "/") count = group_B_count_datetime_factory(yesterday_time) # GET resp = client.get(("/edit_group_count/" f"{group_B.accession_number}/" f"{yesterday.year}/{yesterday.month}/{yesterday.day}/")) assert resp.status_code == 200 assert resp.context["count"] == count assert resp.context["group"] == group_B assert resp.context["enclosure"] == enclosure_base assert resp.context["dateday"].year == yesterday.year assert resp.context["dateday"].month == yesterday.month assert resp.context["dateday"].day == yesterday.day # todo: form context # POST count_bar = randint(1, 400) count_seen = count_bar + randint(5, 25) count_total = count_bar + count_seen post_data = { "count_seen": count_seen, "enclosure": enclosure_base.id, "count_bar": count_bar, "comment": "this is a randomly generated count", "group": group_B.id, "count_total": count_total, "needs_attn": False, } resp = client.post( ("/edit_group_count/" f"{group_B.accession_number}/" f"{yesterday.year}/{yesterday.month}/{yesterday.day}/"), data=post_data, follow=True, ) assert resp.status_code == 200 assert resp.redirect_chain == [(f"/count/{enclosure_base.slug}/", 302)] latest_count = group_B.counts.latest("datetimecounted") assert latest_count.count_seen == post_data["count_seen"] assert latest_count.count_bar == post_data["count_bar"]