Explorar el Código

Merge pull request #17151 from phot0n/fix-format-date-dashboard-charts

version-14
Suraj Shetty hace 3 años
committed by GitHub
padre
commit
c7e89267cc
No se encontró ninguna clave conocida en la base de datos para esta firma ID de clave GPG: 4AEE18F83AFDEB23
Se han modificado 2 ficheros con 45 adiciones y 22 borrados
  1. +8
    -4
      frappe/desk/doctype/dashboard_chart/dashboard_chart.py
  2. +37
    -18
      frappe/desk/doctype/dashboard_chart/test_dashboard_chart.py

+ 8
- 4
frappe/desk/doctype/dashboard_chart/dashboard_chart.py Ver fichero

@@ -14,6 +14,7 @@ from frappe.model.naming import append_number_if_name_exists
from frappe.modules.export_file import export_to_files
from frappe.utils import cint, get_datetime, getdate, now_datetime, nowdate
from frappe.utils.dashboard import cache_source
from frappe.utils.data import format_date
from frappe.utils.dateutils import (
get_dates_from_timegrain,
get_from_date_from_timespan,
@@ -221,13 +222,16 @@ def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date):

result = get_result(data, timegrain, from_date, to_date, chart.chart_type)

chart_config = {
"labels": [get_period(r[0], timegrain) for r in result],
return {
"labels": [
format_date(get_period(r[0], timegrain))
if timegrain in ("Daily", "Weekly")
else get_period(r[0], timegrain)
for r in result
],
"datasets": [{"name": chart.name, "values": [r[1] for r in result]}],
}

return chart_config


def get_heatmap_chart_config(chart, filters, heatmap_year):
aggregate_function = get_aggregate_function(chart.chart_type)


+ 37
- 18
frappe/desk/doctype/dashboard_chart/test_dashboard_chart.py Ver fichero

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and Contributors
# License: MIT. See LICENSE
import unittest
from datetime import datetime
from unittest.mock import patch

@@ -9,11 +9,12 @@ from dateutil.relativedelta import relativedelta

import frappe
from frappe.desk.doctype.dashboard_chart.dashboard_chart import get
from frappe.tests.utils import FrappeTestCase
from frappe.utils import formatdate, get_last_day, getdate
from frappe.utils.dateutils import get_period, get_period_ending


class TestDashboardChart(unittest.TestCase):
class TestDashboardChart(FrappeTestCase):
def test_period_ending(self):
self.assertEqual(get_period_ending("2019-04-10", "Daily"), getdate("2019-04-10"))

@@ -57,8 +58,6 @@ class TestDashboardChart(unittest.TestCase):
self.assertEqual(result.get("labels")[idx], get_period(month))
cur_date += relativedelta(months=1)

frappe.db.rollback()

def test_empty_dashboard_chart(self):
if frappe.db.exists("Dashboard Chart", "Test Empty Dashboard Chart"):
frappe.delete_doc("Dashboard Chart", "Test Empty Dashboard Chart")
@@ -89,8 +88,6 @@ class TestDashboardChart(unittest.TestCase):
self.assertEqual(result.get("labels")[idx], get_period(month))
cur_date += relativedelta(months=1)

frappe.db.rollback()

def test_chart_wih_one_value(self):
if frappe.db.exists("Dashboard Chart", "Test Empty Dashboard Chart 2"):
frappe.delete_doc("Dashboard Chart", "Test Empty Dashboard Chart 2")
@@ -127,8 +124,6 @@ class TestDashboardChart(unittest.TestCase):
# only 1 data point with value
self.assertEqual(result.get("datasets")[0].get("values")[2], 0)

frappe.db.rollback()

def test_group_by_chart_type(self):
if frappe.db.exists("Dashboard Chart", "Test Group By Dashboard Chart"):
frappe.delete_doc("Dashboard Chart", "Test Group By Dashboard Chart")
@@ -151,8 +146,6 @@ class TestDashboardChart(unittest.TestCase):

self.assertEqual(result.get("datasets")[0].get("values")[0], todo_status_count)

frappe.db.rollback()

def test_daily_dashboard_chart(self):
insert_test_records()

@@ -180,11 +173,10 @@ class TestDashboardChart(unittest.TestCase):

self.assertEqual(result.get("datasets")[0].get("values"), [200.0, 400.0, 300.0, 0.0, 100.0, 0.0])
self.assertEqual(
result.get("labels"), ["06-01-19", "07-01-19", "08-01-19", "09-01-19", "10-01-19", "11-01-19"]
result.get("labels"),
["06-01-2019", "07-01-2019", "08-01-2019", "09-01-2019", "10-01-2019", "11-01-2019"],
)

frappe.db.rollback()

def test_weekly_dashboard_chart(self):
insert_test_records()

@@ -212,9 +204,7 @@ class TestDashboardChart(unittest.TestCase):
result = get(chart_name="Test Weekly Dashboard Chart", refresh=1)

self.assertEqual(result.get("datasets")[0].get("values"), [50.0, 300.0, 800.0, 0.0])
self.assertEqual(result.get("labels"), ["30-12-18", "06-01-19", "13-01-19", "20-01-19"])

frappe.db.rollback()
self.assertEqual(result.get("labels"), ["12-30-2018", "06-01-2019", "01-13-2019", "01-20-2019"])

def test_avg_dashboard_chart(self):
insert_test_records()
@@ -241,10 +231,39 @@ class TestDashboardChart(unittest.TestCase):

with patch.object(frappe.utils.data, "get_first_day_of_the_week", return_value="Monday"):
result = get(chart_name="Test Average Dashboard Chart", refresh=1)
self.assertEqual(result.get("labels"), ["30-12-18", "06-01-19", "13-01-19", "20-01-19"])
self.assertEqual(result.get("labels"), ["12-30-2018", "06-01-2019", "01-13-2019", "01-20-2019"])
self.assertEqual(result.get("datasets")[0].get("values"), [50.0, 150.0, 266.6666666666667, 0.0])

frappe.db.rollback()
def test_user_date_label_dashboard_chart(self):
frappe.delete_doc_if_exists("Dashboard Chart", "Test Dashboard Chart Date Label")

frappe.get_doc(
dict(
doctype="Dashboard Chart",
chart_name="Test Dashboard Chart Date Label",
chart_type="Count",
document_type="DocType",
based_on="creation",
timespan="Select Date Range",
time_interval="Weekly",
from_date=datetime(2018, 12, 30),
to_date=datetime(2019, 1, 15),
filters_json="[]",
timeseries=1,
)
).insert()

with patch.object(frappe.utils.data, "get_user_date_format", return_value="dd.mm.yyyy"):
result = get(chart_name="Test Dashboard Chart Date Label")
self.assertEqual(
sorted(result.get("labels")), sorted(["01.05.2019", "01.12.2019", "19.01.2019"])
)

with patch.object(frappe.utils.data, "get_user_date_format", return_value="mm-dd-yyyy"):
result = get(chart_name="Test Dashboard Chart Date Label")
self.assertEqual(
sorted(result.get("labels")), sorted(["01-19-2019", "05-01-2019", "12-01-2019"])
)


def insert_test_records():


Cargando…
Cancelar
Guardar