Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions employees/tests/test_unit_custom_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,16 @@ def test_month_navigator_get_context_data_method_should_return_all_data_necessar
"path": self.mixin.request.path,
"navigation_text": MonthNavigationText,
"month_form": MonthSwitchForm(initial_date=datetime.date(year=2019, month=3, day=1)),
"next_month_url": "/reports/project/1/2019/4/",
"recent_month_url": f"/reports/project/1/{current_date.year}/{current_date.month}/",
"previous_month_url": "/reports/project/1/2019/2/",
"next_month_url": reverse(
"project-report-list", kwargs={"pk": self.mixin.kwargs["pk"], "year": 2019, "month": 4}
),
"recent_month_url": reverse(
"project-report-list",
kwargs={"pk": self.mixin.kwargs["pk"], "year": current_date.year, "month": current_date.month},
),
"previous_month_url": reverse(
"project-report-list", kwargs={"pk": self.mixin.kwargs["pk"], "year": 2019, "month": 2}
),
"disable_next_button": False,
"disable_previous_button": False,
"title_date": "03/19",
Expand All @@ -140,38 +147,47 @@ def test_month_navigator_get_context_data_method_should_return_all_data_necessar
def test_month_navigator_should_render_html_with_links_to_other_months_for_given_url(self):
rendered_template = self._render_month_navigation_bar(2019, 1, 1)
current_date = timezone.now()
self.assertTrue('<a href="/reports/project/1/2018/12/" class="btn btn-info">' in rendered_template)
self.assertTrue(
f'<a href="/reports/project/1/{current_date.year}/{current_date.month}/" class="btn btn-info">'
in rendered_template
url_previous = reverse("project-report-list", kwargs={"year": 2018, "month": 12, "pk": 1})
url_current = reverse(
"project-report-list", kwargs={"year": current_date.year, "month": current_date.month, "pk": 1}
)
self.assertTrue('<a href="/reports/project/1/2019/2/" class="btn btn-info">' in rendered_template)
url_next = reverse("project-report-list", kwargs={"year": 2019, "month": 2, "pk": 1})
self.assertTrue(f'<a href="{url_previous}" class="btn btn-info">' in rendered_template)
self.assertTrue(f'<a href="{url_current}" class="btn btn-info">' in rendered_template)
self.assertTrue(f'<a href="{url_next}" class="btn btn-info">' in rendered_template)

def test_month_navigator_should_not_render_html_with_link_to_next_month_if_upper_limit_is_met(self):
rendered_template = self._render_month_navigation_bar(2099, 12, 1)
current_date = timezone.now()
self.assertTrue('<a href="/reports/project/1/2099/11/" class="btn btn-info">' in rendered_template)
self.assertTrue(
f'<a href="/reports/project/1/{current_date.year}/{current_date.month}/" class="btn btn-info">'
in rendered_template
url_previous = reverse("project-report-list", kwargs={"year": 2099, "month": 11, "pk": 1})
url_current = reverse(
"project-report-list", kwargs={"year": current_date.year, "month": current_date.month, "pk": 1}
)
self.assertFalse('<a href="/reports/project/1/2100/1/" class="btn btn-info">' in rendered_template)
url_next = reverse("project-report-list", kwargs={"year": 2100, "month": 1, "pk": 1})
self.assertTrue(f'<a href="{url_previous}" class="btn btn-info">' in rendered_template)
self.assertTrue(f'<a href="{url_current}" class="btn btn-info">' in rendered_template)
self.assertFalse(f'<a href="{url_next}" class="btn btn-info">' in rendered_template)

def test_month_navigator_should_not_render_html_with_link_to_previous_month_if_lower_limit_is_met(self):
rendered_template = self._render_month_navigation_bar(2019, 5, 1)
current_date = timezone.now()
self.assertTrue('<a href="/reports/project/1/2019/6/" class="btn btn-info">' in rendered_template)
self.assertTrue(
f'<a href="/reports/project/1/{current_date.year}/{current_date.month}/" class="btn btn-info">'
in rendered_template
url_previous = reverse("project-report-list", kwargs={"year": 2019, "month": 6, "pk": 1})
url_current = reverse(
"project-report-list", kwargs={"year": current_date.year, "month": current_date.month, "pk": 1}
)
self.assertFalse('<a href="/reports/project/1/2000/4/" class="btn btn-info">' in rendered_template)
url_next = reverse("project-report-list", kwargs={"year": 2000, "month": 4, "pk": 1})
self.assertTrue(f'<a href="{url_previous}" class="btn btn-info">' in rendered_template)
self.assertTrue(f'<a href="{url_current}" class="btn btn-info">' in rendered_template)
self.assertFalse(f'<a href="{url_next}" class="btn btn-info">' in rendered_template)

def test_month_navigator_should_render_html_with_month_navigation_form_related_to_post_method_under_request_path(
self
):
rendered_template = self._render_month_navigation_bar(2019, 3, 1)
self.assertTrue('<form action="/reports/project/1/2019/3/" method="POST">' in rendered_template)
self.assertTrue(
f'<form action="{reverse("project-report-list", kwargs={"year": 2019, "month": 3, "pk": 1})}" method="POST">'
in rendered_template
)

def test_redirect_to_another_month_method_should_redirect_to_link_with_provided_date(self):
current_date = timezone.now()
Expand All @@ -187,11 +203,11 @@ def test_redirect_to_another_month_method_should_redirect_to_link_with_provided_
def test_redirect_to_another_month_method_should_redirect_to_request_path_if_provided_date_is_out_of_bonds(self):
self.mixin.kwargs["pk"] = 1
request = HttpRequest()
request.path = "/reports/project/1/2019/3/"
request.path = reverse("project-report-list", kwargs={"year": 2019, "month": 3, "pk": 1})
request.POST["date"] = "04-2019"
response = self.mixin.redirect_to_another_month(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/reports/project/1/2019/3/")
self.assertEqual(response.url, reverse("project-report-list", kwargs={"year": 2019, "month": 3, "pk": 1}))

def test_redirect_to_current_month_method_should_redirect_to_current_month_section(self):
current_date = timezone.now()
Expand Down
36 changes: 26 additions & 10 deletions employees/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ def test_author_report_list_view_should_not_display_reports_from_another_month(s
def test_author_report_list_view_should_redirect_to_another_month_on_post(self):
response = self.client.post(self.url, data={"date": "09-2020"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f"/reports/author/{self.user.pk}/2020/9/")
self.assertEqual(
response.url, reverse("author-report-list", kwargs={"pk": self.user.pk, "year": 2020, "month": 9})
)

def test_author_report_list_view_should_redirect_to_current_date_if_date_parameters_are_out_of_bonds(self):
response = self.client.get(reverse("author-report-list", kwargs={"pk": self.user.pk, "year": 2019, "month": 4}))
current_date = datetime.datetime.now()
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f"/reports/author/{self.user.pk}/{current_date.year}/{current_date.month}/")
self.assertEqual(
response.url,
reverse(
"author-report-list",
kwargs={"pk": self.user.pk, "year": current_date.year, "month": current_date.month},
),
)


class AdminReportViewTests(TestCase):
Expand Down Expand Up @@ -172,7 +180,7 @@ def test_custom_report_list_view_should_dipslay_message_if_there_are_no_projects
def test_custom_report_list_view_should_redirect_to_another_month_if_month_switch_was_called_on_post(self):
response = self.client.post(self.url, data={"date": "09-2020", "month-switch": "month-switch"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/reports/2020/9/")
self.assertEqual(response.url, reverse("custom-report-list", args=("2020", "9")))

def test_custom_report_list_view_should_not_display_reports_from_different_month_than_selected(self):
current_date = datetime.datetime.now().date()
Expand All @@ -196,7 +204,7 @@ def test_custom_report_list_view_form_should_have_latest_report_data_set(self):
self.assertEqual(response.context_data["form"].initial["project"].pk, latest_report.project.pk)

def test_default_date_should_be_first_day_of_the_month_if_there_are_no_reports(self):
response = self.client.get("/reports/2019/5/")
response = self.client.get(reverse("custom-report-list", kwargs={"year": 2019, "month": 5}))
self.assertEqual(response.context_data["form"].initial["date"], datetime.date(year=2019, month=5, day=1))

def test_default_date_should_be_monday_when_the_last_report_was_created_on_friday_of_the_same_month(self):
Expand All @@ -205,27 +213,27 @@ def test_default_date_should_be_monday_when_the_last_report_was_created_on_frida
ReportFactory(date=datetime.date(year=2019, month=5, day=3), author=self.user)
# Monday
with freeze_time("2019-5-06"):
response = self.client.get("/reports/2019/5/")
response = self.client.get(reverse("custom-report-list", kwargs={"year": 2019, "month": 5}))
self.assertEqual(response.context_data["form"].initial["date"], datetime.date(year=2019, month=5, day=6))

def test_default_date_should_be_day_later_than_last_report_if_it_wasnt_created_today(self):
with freeze_time("2019-05-01"):
ReportFactory(date=datetime.date(year=2019, month=5, day=1), author=self.user)
with freeze_time("2019-05-03"):
response = self.client.get("/reports/2019/5/")
response = self.client.get(reverse("custom-report-list", kwargs={"year": 2019, "month": 5}))
self.assertEqual(response.context_data["form"].initial["date"], datetime.date(year=2019, month=5, day=2))

def test_default_date_should_be_last_day_of_the_month_if_thats_the_date_of_last_report(self):
with freeze_time("2019-05-31"):
report = ReportFactory(date=datetime.date(year=2019, month=5, day=31), author=self.user)
with freeze_time("2019-06-01"):
response = self.client.get("/reports/2019/5/")
response = self.client.get(reverse("custom-report-list", kwargs={"year": 2019, "month": 5}))
self.assertEqual(response.context_data["form"].initial["date"], report.date)

def test_default_date_should_be_date_of_the_last_report_if_it_was_created_today(self):
with freeze_time("2019-05-01"):
report = ReportFactory(date=datetime.date(year=2019, month=5, day=1), author=self.user)
response = self.client.get("/reports/2019/5/")
response = self.client.get(reverse("custom-report-list", kwargs={"year": 2019, "month": 5}))
self.assertEqual(response.context_data["form"].initial["date"], report.date)


Expand Down Expand Up @@ -566,15 +574,23 @@ def test_project_report_list_view_should_not_display_reports_from_different_mont
def test_project_report_list_view_should_redirect_to_another_month_on_post(self):
response = self.client.post(self.url, data={"date": "09-2020"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f"/reports/project/{self.project.id}/2020/9/")
self.assertEqual(
response.url, reverse("project-report-list", kwargs={"pk": self.project.id, "year": 2020, "month": 9})
)

def test_project_report_list_view_should_redirect_to_current_date_if_date_parameters_are_out_of_bonds(self):
response = self.client.get(
reverse("project-report-list", kwargs={"pk": self.project.pk, "year": 2019, "month": 4})
)
current_date = datetime.datetime.now()
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f"/reports/project/{self.project.id}/{current_date.year}/{current_date.month}/")
self.assertEqual(
response.url,
reverse(
"project-report-list",
kwargs={"pk": self.project.id, "year": current_date.year, "month": current_date.month},
),
)

def _assert_response_contain_report(self, response, reports):
for report in reports:
Expand Down
5 changes: 2 additions & 3 deletions sheetstorm/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

urlpatterns = [
url(r"^admin/", admin.site.urls),
url(r"^", include("users.urls")),
url(r"^employees/", include("employees.urls")),
url(r"^managers/", include("managers.urls")),
url(r"^", include("employees.urls")),
# url(r"^select2/", include("django_select2.urls")),
url(r"^users/", include("users.urls")),
]
2 changes: 1 addition & 1 deletion users/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_user_create_view_should_add_new_user_on_post(self):
},
)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/users/")
self.assertEqual(response.url, reverse("custom-users-list"))
self.assertEqual(CustomUser.objects.all().count(), 2)

def test_user_create_view_should_not_add_new_user_on_post_if_form_is_invalid(self):
Expand Down