4
4
import sys
5
5
from typing import Type , TYPE_CHECKING , TypeVar
6
6
from tests .django_routines_tests .models import TestModel as _TestModel
7
+ from collections import Counter
8
+ import math
7
9
8
10
from django .core .management import call_command , CommandError
9
11
from django_typer .management import get_command , TyperCommand
23
25
TestError ,
24
26
)
25
27
28
+ WORD = re .compile (r"\w+" )
29
+
30
+
31
+ def get_cosine (vec1 , vec2 ):
32
+ intersection = set (vec1 .keys ()) & set (vec2 .keys ())
33
+ numerator = sum ([vec1 [x ] * vec2 [x ] for x in intersection ])
34
+
35
+ sum1 = sum ([vec1 [x ] ** 2 for x in list (vec1 .keys ())])
36
+ sum2 = sum ([vec2 [x ] ** 2 for x in list (vec2 .keys ())])
37
+ denominator = math .sqrt (sum1 ) * math .sqrt (sum2 )
38
+
39
+ if not denominator :
40
+ return 0.0
41
+ else :
42
+ return float (numerator ) / denominator
43
+
44
+
45
+ def text_to_vector (text ):
46
+ words = WORD .findall (text )
47
+ return Counter (words )
48
+
49
+
50
+ def similarity (text1 , text2 ):
51
+ """
52
+ Compute the cosine similarity between two texts.
53
+ https://en.wikipedia.org/wiki/Cosine_similarity
54
+
55
+ We use this to lazily evaluate the output of --help to our
56
+ renderings.
57
+ #"""
58
+ vector1 = text_to_vector (text1 )
59
+ vector2 = text_to_vector (text2 )
60
+ return get_cosine (vector1 , vector2 )
61
+
26
62
27
63
manage_py = Path (__file__ ).parent .parent / "manage.py"
28
64
@@ -347,8 +383,8 @@ def test_list(self, no_color=True):
347
383
"[3] track 4 (demo=6, flag=True)" ,
348
384
"[4] track 1" ,
349
385
"[6] track 5 | demo" ,
350
- f"[7] tests{ os .sep } system_cmd.py sys 1" ,
351
- f"[8] tests{ os .sep } system_cmd.py sys 2" ,
386
+ f"[7] python tests{ os .sep } system_cmd.py sys 1" ,
387
+ f"[8] python tests{ os .sep } system_cmd.py sys 2" ,
352
388
],
353
389
)
354
390
@@ -361,8 +397,8 @@ def test_list(self, no_color=True):
361
397
"[3] track 3 (demo=2)" ,
362
398
"[3] track 4 (demo=6, flag=True)" ,
363
399
"[4] track 1" ,
364
- f"[7] tests{ os .sep } system_cmd.py sys 1" ,
365
- f"[8] tests{ os .sep } system_cmd.py sys 2" ,
400
+ f"[7] python tests{ os .sep } system_cmd.py sys 1" ,
401
+ f"[8] python tests{ os .sep } system_cmd.py sys 2" ,
366
402
],
367
403
)
368
404
@@ -377,8 +413,8 @@ def test_list(self, no_color=True):
377
413
"[3] track 4 (demo=6, flag=True)" ,
378
414
"[4] track 1" ,
379
415
"[6] track 5 | demo" ,
380
- f"[7] tests{ os .sep } system_cmd.py sys 1" ,
381
- f"[8] tests{ os .sep } system_cmd.py sys 2" ,
416
+ f"[7] python tests{ os .sep } system_cmd.py sys 1" ,
417
+ f"[8] python tests{ os .sep } system_cmd.py sys 2" ,
382
418
],
383
419
)
384
420
@@ -394,8 +430,8 @@ def test_list(self, no_color=True):
394
430
"[3] track 4 (demo=6, flag=True)" ,
395
431
"[4] track 1" ,
396
432
"[6] track 5 | demo" ,
397
- f"[7] tests{ os .sep } system_cmd.py sys 1" ,
398
- f"[8] tests{ os .sep } system_cmd.py sys 2" ,
433
+ f"[7] python tests{ os .sep } system_cmd.py sys 1" ,
434
+ f"[8] python tests{ os .sep } system_cmd.py sys 2" ,
399
435
],
400
436
)
401
437
@@ -410,8 +446,8 @@ def test_list(self, no_color=True):
410
446
"[3] track 3 (demo=2)" ,
411
447
"[3] track 4 (demo=6, flag=True)" ,
412
448
"[4] track 1" ,
413
- f"[7] tests{ os .sep } system_cmd.py sys 1" ,
414
- f"[8] tests{ os .sep } system_cmd.py sys 2" ,
449
+ f"[7] python tests{ os .sep } system_cmd.py sys 1" ,
450
+ f"[8] python tests{ os .sep } system_cmd.py sys 2" ,
415
451
],
416
452
)
417
453
@@ -543,8 +579,8 @@ def test_subprocess(self):
543
579
[3] track 4 (demo=6, flag=True)
544
580
[4] track 1
545
581
[6] track 5 | demo
546
- [7] tests{ os .sep } system_cmd.py sys 1
547
- [8] tests{ os .sep } system_cmd.py sys 2
582
+ [7] python tests{ os .sep } system_cmd.py sys 1
583
+ [8] python tests{ os .sep } system_cmd.py sys 2
548
584
549
585
╭─ Options ────────────────────────────────────────────────────────────────────╮
550
586
│ --subprocess Run commands as subprocesses. │
@@ -584,9 +620,12 @@ def test_helps_rich(self):
584
620
585
621
routine = get_command ("routine" , TyperCommand , stdout = stdout , no_color = True )
586
622
routine .print_help ("./manage.py" , "routine" , "import" )
587
- self .assertEqual (
588
- self .strip_ansi (stdout .getvalue ()).strip ().replace ("\x08 " , "" ),
589
- self .routine_test_help_rich .strip (),
623
+ self .assertGreater (
624
+ similarity (
625
+ self .strip_ansi (stdout .getvalue ()).strip ().replace ("\x08 " , "" ),
626
+ self .routine_test_help_rich .strip (),
627
+ ),
628
+ 0.99 ,
590
629
)
591
630
592
631
routine_help_no_rich = """
@@ -635,8 +674,8 @@ def test_helps_rich(self):
635
674
[3] track 4 (demo=6, flag=True)
636
675
[4] track 1
637
676
[6] track 5 | demo
638
- [7] tests{ os .sep } system_cmd.py sys 1
639
- [8] tests{ os .sep } system_cmd.py sys 2
677
+ [7] python tests{ os .sep } system_cmd.py sys 1
678
+ [8] python tests{ os .sep } system_cmd.py sys 2
640
679
641
680
Options:
642
681
--subprocess Run commands as subprocesses.
@@ -665,18 +704,21 @@ def test_helps_no_rich(self):
665
704
)
666
705
self .assertEqual (result .returncode , 0 )
667
706
self .assertFalse (result .stderr )
668
- self .assertEqual (
669
- result .stdout .strip ().decode (),
670
- self .routine_help_no_rich .format (script = sys .argv [0 ]).strip (),
707
+ self .assertGreater (
708
+ similarity (
709
+ result .stdout .strip ().decode (),
710
+ self .routine_help_no_rich .format (script = sys .argv [0 ]).strip (),
711
+ ),
712
+ 0.99 ,
671
713
)
672
714
673
715
stdout = StringIO ()
674
716
675
717
routine = get_command ("routine" , TyperCommand , stdout = stdout , no_color = True )
676
718
routine .print_help ("./manage.py" , "routine" , "import" )
677
719
printed = stdout .getvalue ().strip ().replace ("\x08 " , "" )
678
- expected = self .routine_test_help_no_rich . strip ()
679
- self .assertEqual ( printed , expected )
720
+ expected = self .routine_test_help_no_rich
721
+ self .assertGreater ( similarity ( printed , expected ), 0.99 )
680
722
681
723
def test_settings_format (self ):
682
724
routines = getattr (settings , ROUTINE_SETTING )
@@ -819,13 +861,13 @@ def test_settings_format(self):
819
861
"switches" : ("demo" ,),
820
862
},
821
863
{
822
- "command" : ("tests/ system_cmd.py" , "sys 1" ),
864
+ "command" : ("python" , f" tests{ os . sep } system_cmd.py" , "sys 1" ),
823
865
"kind" : "system" ,
824
866
"priority" : 7 ,
825
867
"switches" : (),
826
868
},
827
869
{
828
- "command" : ("tests/ system_cmd.py" , "sys 2" ),
870
+ "command" : ("python" , f" tests{ os . sep } system_cmd.py" , "sys 2" ),
829
871
"kind" : "system" ,
830
872
"priority" : 8 ,
831
873
"switches" : (),
0 commit comments