test: Add test for lazy_import

This commit is contained in:
Gavin D'souza 2022-08-11 16:59:08 +05:30 committed by gavin
parent 605e5a3010
commit 3968c32fa9
2 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,4 @@
# File for testing lazy_import util via test_lazy_import_module
import time
print("Module `frappe.tests.data.load_sleep` loaded")

View file

@ -4,10 +4,12 @@
import io
import json
import os
import sys
import unittest
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from enum import Enum
from io import StringIO
from mimetypes import guess_type
from unittest.mock import patch
@ -48,6 +50,19 @@ from frappe.utils.make_random import can_make, get_random, how_many
from frappe.utils.response import json_handler
class Capturing(list):
# ref: https://stackoverflow.com/a/16571630/10309266
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio
sys.stdout = self._stdout
class TestFilters(unittest.TestCase):
def test_simple_dict(self):
self.assertTrue(evaluate_filters({"doctype": "User", "status": "Open"}, {"status": "Open"}))
@ -691,3 +706,16 @@ class TestMakeRandom(unittest.TestCase):
def test_how_many(self):
self.assertIsInstance(how_many("User"), int)
class TestLazyLoader(unittest.TestCase):
def test_lazy_import_module(self):
from frappe.utils.lazy_loader import lazy_import
with Capturing() as output:
ls = lazy_import("frappe.tests.data.load_sleep")
self.assertEqual(output, [])
with Capturing() as output:
ls.time
self.assertEqual(["Module `frappe.tests.data.load_sleep` loaded"], output)