summaryrefslogtreecommitdiff
path: root/pint/testsuite/test_contexts.py
blob: ea6525d161fe298da7b8d989bb92b1c5c1e00df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
import itertools
import logging
import math
import re
from collections import defaultdict

import pytest

from pint import (
    Context,
    DefinitionSyntaxError,
    DimensionalityError,
    UndefinedUnitError,
    UnitRegistry,
)
from pint.testsuite import helpers
from pint.util import UnitsContainer


def add_ctxs(ureg):
    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
    d = Context("lc")
    d.add_transformation(a, b, lambda ureg, x: ureg.speed_of_light / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.speed_of_light / x)

    ureg.add_context(d)

    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[current]": 1})
    d = Context("ab")
    d.add_transformation(a, b, lambda ureg, x: ureg.ampere * ureg.meter / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.ampere * ureg.meter / x)

    ureg.add_context(d)


def add_arg_ctxs(ureg):
    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
    d = Context("lc")
    d.add_transformation(a, b, lambda ureg, x, n: ureg.speed_of_light / x / n)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.speed_of_light / x / n)

    ureg.add_context(d)

    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[current]": 1})
    d = Context("ab")
    d.add_transformation(a, b, lambda ureg, x: ureg.ampere * ureg.meter / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.ampere * ureg.meter / x)

    ureg.add_context(d)


def add_argdef_ctxs(ureg):
    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
    d = Context("lc", defaults=dict(n=1))
    assert d.defaults == dict(n=1)

    d.add_transformation(a, b, lambda ureg, x, n: ureg.speed_of_light / x / n)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.speed_of_light / x / n)

    ureg.add_context(d)

    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[current]": 1})
    d = Context("ab")
    d.add_transformation(a, b, lambda ureg, x: ureg.ampere * ureg.meter / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.ampere * ureg.meter / x)

    ureg.add_context(d)


def add_sharedargdef_ctxs(ureg):
    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
    d = Context("lc", defaults=dict(n=1))
    assert d.defaults == dict(n=1)

    d.add_transformation(a, b, lambda ureg, x, n: ureg.speed_of_light / x / n)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.speed_of_light / x / n)

    ureg.add_context(d)

    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[current]": 1})
    d = Context("ab", defaults=dict(n=0))
    d.add_transformation(a, b, lambda ureg, x, n: ureg.ampere * ureg.meter * n / x)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.ampere * ureg.meter * n / x)

    ureg.add_context(d)


class TestContexts:
    def test_known_context(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)
        with ureg.context("lc"):
            assert ureg._active_ctx
            assert ureg._active_ctx.graph

        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

        with ureg.context("lc", n=1):
            assert ureg._active_ctx
            assert ureg._active_ctx.graph

        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

    def test_known_context_enable(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)
        ureg.enable_contexts("lc")
        assert ureg._active_ctx
        assert ureg._active_ctx.graph
        ureg.disable_contexts(1)

        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

        ureg.enable_contexts("lc", n=1)
        assert ureg._active_ctx
        assert ureg._active_ctx.graph
        ureg.disable_contexts(1)

        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

    def test_graph(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)
        l = UnitsContainer({"[length]": 1.0})  # noqa: E741
        t = UnitsContainer({"[time]": -1.0})
        c = UnitsContainer({"[current]": 1.0})

        g_sp = defaultdict(set)
        g_sp.update({l: {t}, t: {l}})

        g_ab = defaultdict(set)
        g_ab.update({l: {c}, c: {l}})

        g = defaultdict(set)
        g.update({l: {t, c}, t: {l}, c: {l}})

        with ureg.context("lc"):
            assert ureg._active_ctx.graph == g_sp

        with ureg.context("lc", n=1):
            assert ureg._active_ctx.graph == g_sp

        with ureg.context("ab"):
            assert ureg._active_ctx.graph == g_ab

        with ureg.context("lc"):
            with ureg.context("ab"):
                assert ureg._active_ctx.graph == g

        with ureg.context("ab"):
            with ureg.context("lc"):
                assert ureg._active_ctx.graph == g

        with ureg.context("lc", "ab"):
            assert ureg._active_ctx.graph == g

        with ureg.context("ab", "lc"):
            assert ureg._active_ctx.graph == g

    def test_graph_enable(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)
        l = UnitsContainer({"[length]": 1.0})  # noqa: E741
        t = UnitsContainer({"[time]": -1.0})
        c = UnitsContainer({"[current]": 1.0})

        g_sp = defaultdict(set)
        g_sp.update({l: {t}, t: {l}})

        g_ab = defaultdict(set)
        g_ab.update({l: {c}, c: {l}})

        g = defaultdict(set)
        g.update({l: {t, c}, t: {l}, c: {l}})

        ureg.enable_contexts("lc")
        assert ureg._active_ctx.graph == g_sp
        ureg.disable_contexts(1)

        ureg.enable_contexts("lc", n=1)
        assert ureg._active_ctx.graph == g_sp
        ureg.disable_contexts(1)

        ureg.enable_contexts("ab")
        assert ureg._active_ctx.graph == g_ab
        ureg.disable_contexts(1)

        ureg.enable_contexts("lc")
        ureg.enable_contexts("ab")
        assert ureg._active_ctx.graph == g
        ureg.disable_contexts(2)

        ureg.enable_contexts("ab")
        ureg.enable_contexts("lc")
        assert ureg._active_ctx.graph == g
        ureg.disable_contexts(2)

        ureg.enable_contexts("lc", "ab")
        assert ureg._active_ctx.graph == g
        ureg.disable_contexts(2)

        ureg.enable_contexts("ab", "lc")
        assert ureg._active_ctx.graph == g
        ureg.disable_contexts(2)

    def test_known_nested_context(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)

        with ureg.context("lc"):
            x = dict(ureg._active_ctx)
            y = dict(ureg._active_ctx.graph)
            assert ureg._active_ctx
            assert ureg._active_ctx.graph

            with ureg.context("ab"):
                assert ureg._active_ctx
                assert ureg._active_ctx.graph
                assert x != ureg._active_ctx
                assert y != ureg._active_ctx.graph

            assert x == ureg._active_ctx
            assert y == ureg._active_ctx.graph

        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

    def test_unknown_context(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)
        with pytest.raises(KeyError):
            with ureg.context("la"):
                pass
        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

    def test_unknown_nested_context(self, func_registry):
        ureg = func_registry
        add_ctxs(ureg)

        with ureg.context("lc"):
            x = dict(ureg._active_ctx)
            y = dict(ureg._active_ctx.graph)
            with pytest.raises(KeyError):
                with ureg.context("la"):
                    pass

            assert x == ureg._active_ctx
            assert y == ureg._active_ctx.graph

        assert not ureg._active_ctx
        assert not ureg._active_ctx.graph

    def test_one_context(self, func_registry):
        ureg = func_registry

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        meter_units = ureg.get_compatible_units(ureg.meter)
        hertz_units = ureg.get_compatible_units(ureg.hertz)

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc"):
            assert q.to("Hz") == s
            assert ureg.get_compatible_units(q) == meter_units | hertz_units
        with pytest.raises(DimensionalityError):
            q.to("Hz")
        assert ureg.get_compatible_units(q) == meter_units

    def test_multiple_context(self, func_registry):
        ureg = func_registry

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        meter_units = ureg.get_compatible_units(ureg.meter)
        hertz_units = ureg.get_compatible_units(ureg.hertz)
        ampere_units = ureg.get_compatible_units(ureg.ampere)

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc", "ab"):
            assert q.to("Hz") == s
            assert (
                ureg.get_compatible_units(q) == meter_units | hertz_units | ampere_units
            )
        with pytest.raises(DimensionalityError):
            q.to("Hz")
        assert ureg.get_compatible_units(q) == meter_units

    def test_nested_context(self, func_registry):
        ureg = func_registry

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc"):
            assert q.to("Hz") == s
            with ureg.context("ab"):
                assert q.to("Hz") == s
            assert q.to("Hz") == s

        with ureg.context("ab"):
            with pytest.raises(DimensionalityError):
                q.to("Hz")
            with ureg.context("lc"):
                assert q.to("Hz") == s
            with pytest.raises(DimensionalityError):
                q.to("Hz")

    def test_context_with_arg(self, func_registry):
        ureg = func_registry

        add_arg_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc", n=1):
            assert q.to("Hz") == s
            with ureg.context("ab"):
                assert q.to("Hz") == s
            assert q.to("Hz") == s

        with ureg.context("ab"):
            with pytest.raises(DimensionalityError):
                q.to("Hz")
            with ureg.context("lc", n=1):
                assert q.to("Hz") == s
            with pytest.raises(DimensionalityError):
                q.to("Hz")

        with ureg.context("lc"):
            with pytest.raises(TypeError):
                q.to("Hz")

    def test_enable_context_with_arg(self, func_registry):
        ureg = func_registry

        add_arg_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        ureg.enable_contexts("lc", n=1)
        assert q.to("Hz") == s
        ureg.enable_contexts("ab")
        assert q.to("Hz") == s
        assert q.to("Hz") == s
        ureg.disable_contexts(1)
        ureg.disable_contexts(1)

        ureg.enable_contexts("ab")
        with pytest.raises(DimensionalityError):
            q.to("Hz")
        ureg.enable_contexts("lc", n=1)
        assert q.to("Hz") == s
        ureg.disable_contexts(1)
        with pytest.raises(DimensionalityError):
            q.to("Hz")
        ureg.disable_contexts(1)

        ureg.enable_contexts("lc")
        with pytest.raises(TypeError):
            q.to("Hz")
        ureg.disable_contexts(1)

    def test_context_with_arg_def(self, func_registry):
        ureg = func_registry

        add_argdef_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc"):
            assert q.to("Hz") == s
            with ureg.context("ab"):
                assert q.to("Hz") == s
            assert q.to("Hz") == s

        with ureg.context("ab"):
            with pytest.raises(DimensionalityError):
                q.to("Hz")
            with ureg.context("lc"):
                assert q.to("Hz") == s
            with pytest.raises(DimensionalityError):
                q.to("Hz")

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc", n=2):
            assert q.to("Hz") == s / 2
            with ureg.context("ab"):
                assert q.to("Hz") == s / 2
            assert q.to("Hz") == s / 2

        with ureg.context("ab"):
            with pytest.raises(DimensionalityError):
                q.to("Hz")
            with ureg.context("lc", n=2):
                assert q.to("Hz") == s / 2
            with pytest.raises(DimensionalityError):
                q.to("Hz")

    def test_context_with_sharedarg_def(self, func_registry):
        ureg = func_registry

        add_sharedargdef_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")
        u = (1 / 500) * ureg.ampere

        with ureg.context("lc"):
            assert q.to("Hz") == s
            with ureg.context("ab"):
                assert q.to("ampere") == u

        with ureg.context("ab"):
            assert q.to("ampere") == 0 * u
            with ureg.context("lc"):
                with pytest.raises(ZeroDivisionError):
                    ureg.Quantity.to(q, "Hz")

        with ureg.context("lc", n=2):
            assert q.to("Hz") == s / 2
            with ureg.context("ab"):
                assert q.to("ampere") == 2 * u

        with ureg.context("ab", n=3):
            assert q.to("ampere") == 3 * u
            with ureg.context("lc"):
                assert q.to("Hz") == s / 3

        with ureg.context("lc", n=2):
            assert q.to("Hz") == s / 2
            with ureg.context("ab", n=4):
                assert q.to("ampere") == 4 * u

        with ureg.context("ab", n=3):
            assert q.to("ampere") == 3 * u
            with ureg.context("lc", n=6):
                assert q.to("Hz") == s / 6

    def test_anonymous_context(self, func_registry):
        ureg = func_registry
        c = Context()
        c.add_transformation("[length]", "[time]", lambda ureg, x: x / ureg("5 cm/s"))
        with pytest.raises(ValueError):
            ureg.add_context(c)

        x = ureg("10 cm")
        expect = ureg("2 s")
        helpers.assert_quantity_equal(x.to("s", c), expect)

        with ureg.context(c):
            helpers.assert_quantity_equal(x.to("s"), expect)

        ureg.enable_contexts(c)
        helpers.assert_quantity_equal(x.to("s"), expect)
        ureg.disable_contexts(1)
        with pytest.raises(DimensionalityError):
            x.to("s")

        # Multiple anonymous contexts
        c2 = Context()
        c2.add_transformation("[length]", "[time]", lambda ureg, x: x / ureg("10 cm/s"))
        c2.add_transformation("[mass]", "[time]", lambda ureg, x: x / ureg("10 kg/s"))
        with ureg.context(c2, c):
            helpers.assert_quantity_equal(x.to("s"), expect)
            # Transformations only in c2 are still working even if c takes priority
            helpers.assert_quantity_equal(ureg("100 kg").to("s"), ureg("10 s"))
        with ureg.context(c, c2):
            helpers.assert_quantity_equal(x.to("s"), ureg("1 s"))

    def _test_ctx(self, ctx, ureg):
        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        nctx = len(ureg._contexts)

        assert ctx.name not in ureg._contexts
        ureg.add_context(ctx)

        assert ctx.name in ureg._contexts
        assert len(ureg._contexts) == nctx + 1 + len(ctx.aliases)

        with ureg.context(ctx.name):
            assert q.to("Hz") == s
            assert s.to("meter") == q

        ureg.remove_context(ctx.name)
        assert ctx.name not in ureg._contexts
        assert len(ureg._contexts) == nctx

    @pytest.mark.parametrize(
        "badrow",
        (
            "[length] = 1 / [time]: c / value",
            "1 / [time] = [length]: c / value",
            "[length] <- [time] = c / value",
            "[length] - [time] = c / value",
        ),
    )
    def test_parse_invalid(self, badrow):
        with pytest.raises(DefinitionSyntaxError):
            Context.from_lines(["@context c", badrow])

    @pytest.mark.parametrize(
        "source, name, aliases, defaults",
        [
            (
                [
                    "@context longcontextname",
                    "[length] -> 1 / [time]: c / value",
                    "1 / [time] -> [length]: c / value",
                ],
                "longcontextname",
                (),
                {},
            ),
            (
                ["@context longcontextname = lc", "[length] <-> 1 / [time]: c / value"],
                "longcontextname",
                ("lc",),
                {},
            ),
            (
                [
                    "@context longcontextname = lc = lcn",
                    "[length] <-> 1 / [time]: c / value",
                ],
                "longcontextname",
                ("lc", "lcn"),
                {},
            ),
        ],
    )
    def test_parse_simple(self, func_registry, source, name, aliases, defaults):
        a = Context.__keytransform__(
            UnitsContainer({"[time]": -1}), UnitsContainer({"[length]": 1})
        )
        b = Context.__keytransform__(
            UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
        )

        c = Context.from_lines(source)
        assert c.name == name
        assert c.aliases == aliases
        assert c.defaults == defaults
        assert c.funcs.keys() == {a, b}
        self._test_ctx(c, func_registry)

    def test_parse_auto_inverse(self, func_registry):
        a = Context.__keytransform__(
            UnitsContainer({"[time]": -1.0}), UnitsContainer({"[length]": 1.0})
        )
        b = Context.__keytransform__(
            UnitsContainer({"[length]": 1.0}), UnitsContainer({"[time]": -1.0})
        )

        s = ["@context longcontextname", "[length] <-> 1 / [time]: c / value"]

        c = Context.from_lines(s)
        assert c.defaults == {}
        assert c.funcs.keys() == {a, b}
        self._test_ctx(c, func_registry)

    def test_parse_define(self, func_registry):
        a = Context.__keytransform__(
            UnitsContainer({"[time]": -1}), UnitsContainer({"[length]": 1.0})
        )
        b = Context.__keytransform__(
            UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1.0})
        )

        s = ["@context longcontextname", "[length] <-> 1 / [time]: c / value"]
        c = Context.from_lines(s)
        assert c.defaults == {}
        assert c.funcs.keys() == {a, b}
        self._test_ctx(c, func_registry)

    def test_parse_parameterized(self, func_registry):
        a = Context.__keytransform__(
            UnitsContainer({"[time]": -1.0}), UnitsContainer({"[length]": 1.0})
        )
        b = Context.__keytransform__(
            UnitsContainer({"[length]": 1.0}), UnitsContainer({"[time]": -1.0})
        )

        s = ["@context(n=1) longcontextname", "[length] <-> 1 / [time]: n * c / value"]

        c = Context.from_lines(s)
        assert c.defaults == {"n": 1}
        assert c.funcs.keys() == {a, b}
        self._test_ctx(c, func_registry)

        s = [
            "@context(n=1, bla=2) longcontextname",
            "[length] <-> 1 / [time]: n * c / value / bla",
        ]

        c = Context.from_lines(s)
        assert c.defaults == {"n": 1, "bla": 2}
        assert c.funcs.keys() == {a, b}

        # If the variable is not present in the definition, then raise an error
        s = ["@context(n=1) longcontextname", "[length] <-> 1 / [time]: c / value"]
        with pytest.raises(DefinitionSyntaxError):
            Context.from_lines(s)

    def test_warnings(self, caplog, func_registry):
        ureg = func_registry

        with caplog.at_level(logging.DEBUG, "pint"):
            add_ctxs(ureg)

            d = Context("ab")
            ureg.add_context(d)

            assert len(caplog.records) == 1
            assert "ab" in str(caplog.records[-1].args)

            d = Context("ab1", aliases=("ab",))
            ureg.add_context(d)

            assert len(caplog.records) == 2
            assert "ab" in str(caplog.records[-1].args)


class TestDefinedContexts:
    def test_defined(self, class_registry):
        ureg = class_registry
        with ureg.context("sp"):
            pass

        a = Context.__keytransform__(
            UnitsContainer({"[time]": -1.0}), UnitsContainer({"[length]": 1.0})
        )
        b = Context.__keytransform__(
            UnitsContainer({"[length]": 1.0}), UnitsContainer({"[time]": -1.0})
        )
        assert a in ureg._contexts["sp"].funcs
        assert b in ureg._contexts["sp"].funcs
        with ureg.context("sp"):
            assert a in ureg._active_ctx
            assert b in ureg._active_ctx

    def test_spectroscopy(self, class_registry):
        ureg = class_registry
        eq = (532.0 * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV)
        with ureg.context("sp"):
            from pint.util import find_shortest_path

            for a, b in itertools.product(eq, eq):
                for x in range(2):
                    if x == 1:
                        a = a.to_base_units()
                        b = b.to_base_units()
                    da, db = Context.__keytransform__(
                        a.dimensionality, b.dimensionality
                    )
                    p = find_shortest_path(ureg._active_ctx.graph, da, db)
                    assert p
                    msg = f"{a} <-> {b}"
                    # assertAlmostEqualRelError converts second to first
                    helpers.assert_quantity_almost_equal(b, a, rtol=0.01, msg=msg)

        for a, b in itertools.product(eq, eq):
            helpers.assert_quantity_almost_equal(a.to(b.units, "sp"), b, rtol=0.01)

    def test_textile(self, class_registry):
        ureg = class_registry
        qty_direct = 1.331 * ureg.tex
        with pytest.raises(DimensionalityError):
            qty_indirect = qty_direct.to("Nm")

        with ureg.context("textile"):
            from pint.util import find_shortest_path

            qty_indirect = qty_direct.to("Nm")
            a = qty_direct.to_base_units()
            b = qty_indirect.to_base_units()
            da, db = Context.__keytransform__(a.dimensionality, b.dimensionality)
            p = find_shortest_path(ureg._active_ctx.graph, da, db)
            assert p
            msg = f"{a} <-> {b}"
            helpers.assert_quantity_almost_equal(b, a, rtol=0.01, msg=msg)

            # Check RKM <-> cN/tex conversion
            helpers.assert_quantity_almost_equal(
                1 * ureg.RKM, 0.980665 * ureg.cN / ureg.tex
            )
            helpers.assert_quantity_almost_equal(
                (1 / 0.980665) * ureg.RKM, 1 * ureg.cN / ureg.tex
            )
            assert (
                round(abs((1 * ureg.RKM).to(ureg.cN / ureg.tex).m - 0.980665), 7) == 0
            )
            assert (
                round(abs((1 * ureg.cN / ureg.tex).to(ureg.RKM).m - 1 / 0.980665), 7)
                == 0
            )

    def test_decorator(self, class_registry):
        ureg = class_registry

        a = 532.0 * ureg.nm
        with ureg.context("sp"):
            b = a.to("terahertz")

        def f(wl):
            return wl.to("terahertz")

        with pytest.raises(DimensionalityError):
            f(a)

        @ureg.with_context("sp")
        def g(wl):
            return wl.to("terahertz")

        assert b == g(a)

    def test_decorator_composition(self, class_registry):
        ureg = class_registry

        a = 532.0 * ureg.nm
        with ureg.context("sp"):
            b = a.to("terahertz")

        @ureg.with_context("sp")
        @ureg.check("[length]")
        def f(wl):
            return wl.to("terahertz")

        @ureg.with_context("sp")
        @ureg.check("[length]")
        def g(wl):
            return wl.to("terahertz")

        assert b == f(a)
        assert b == g(a)


def test_redefine(subtests):
    ureg = UnitRegistry(
        """
        foo = [d] = f = foo_alias
        bar = 2 foo = b = bar_alias
        baz = 3 bar = _ = baz_alias
        asd = 4 baz

        @context c
            # Note how we're redefining a symbol, not the plain name, as a
            # function of another name
            b = 5 f
        @end
        """.splitlines()
    )
    # Units that are somehow directly or indirectly defined as a function of the
    # overridden unit are also affected
    foo = ureg.Quantity(1, "foo")
    bar = ureg.Quantity(1, "bar")
    asd = ureg.Quantity(1, "asd")

    # Test without context before and after, to verify that the cache and units have
    # not been polluted
    for enable_ctx in (False, True, False):
        with subtests.test(enable_ctx):
            if enable_ctx:
                ureg.enable_contexts("c")
                k = 5
            else:
                k = 2

            assert foo.to("b").magnitude == 1 / k
            assert foo.to("bar").magnitude == 1 / k
            assert foo.to("bar_alias").magnitude == 1 / k
            assert foo.to("baz").magnitude == 1 / k / 3
            assert bar.to("foo").magnitude == k
            assert bar.to("baz").magnitude == 1 / 3
            assert asd.to("foo").magnitude == 4 * 3 * k
            assert asd.to("bar").magnitude == 4 * 3
            assert asd.to("baz").magnitude == 4

        ureg.disable_contexts()


def test_define_nan():
    ureg = UnitRegistry(
        """
        USD = [currency]
        EUR = nan USD
        GBP = nan USD

        @context c
            EUR = 1.11 USD
            # Note that we're changing which unit GBP is defined against
            GBP = 1.18 EUR
        @end
        """.splitlines()
    )

    q = ureg.Quantity("10 GBP")
    assert q.magnitude == 10
    assert q.units.dimensionality == {"[currency]": 1}
    assert q.to("GBP").magnitude == 10
    assert math.isnan(q.to("USD").magnitude)
    assert math.isclose(q.to("USD", "c").magnitude, 10 * 1.18 * 1.11)


def test_non_multiplicative(subtests):
    ureg = UnitRegistry(
        """
        kelvin = [temperature]
        fahrenheit = 5 / 9 * kelvin; offset: 255
        bogodegrees = 9 * kelvin

        @context nonmult_to_nonmult
            fahrenheit = 7 * kelvin; offset: 123
        @end
        @context nonmult_to_mult
            fahrenheit = 123 * kelvin
        @end
        @context mult_to_nonmult
            bogodegrees = 5 * kelvin; offset: 123
        @end
        """.splitlines()
    )
    k = ureg.Quantity(100, "kelvin")

    with subtests.test("baseline"):
        helpers.assert_quantity_almost_equal(
            k.to("fahrenheit").magnitude, (100 - 255) * 9 / 5
        )
        helpers.assert_quantity_almost_equal(k.to("bogodegrees").magnitude, 100 / 9)

    with subtests.test("nonmult_to_nonmult"):
        with ureg.context("nonmult_to_nonmult"):
            helpers.assert_quantity_almost_equal(
                k.to("fahrenheit").magnitude, (100 - 123) / 7
            )

    with subtests.test("nonmult_to_mult"):
        with ureg.context("nonmult_to_mult"):
            helpers.assert_quantity_almost_equal(
                k.to("fahrenheit").magnitude, 100 / 123
            )

    with subtests.test("mult_to_nonmult"):
        with ureg.context("mult_to_nonmult"):
            helpers.assert_quantity_almost_equal(
                k.to("bogodegrees").magnitude, (100 - 123) / 5
            )


def test_stack_contexts():
    ureg = UnitRegistry(
        """
        a = [dim1]
        b = 1/2 a
        c = 1/3 a
        d = [dim2]

        @context c1
            b = 1/4 a
            c = 1/6 a
            [dim1]->[dim2]: value * 2 d/a
        @end
        @context c2
            b = 1/5 a
            [dim1]->[dim2]: value * 3 d/a
        @end
        """.splitlines()
    )
    q = ureg.Quantity(1, "a")
    assert q.to("b").magnitude == 2
    assert q.to("c").magnitude == 3
    assert q.to("b", "c1").magnitude == 4
    assert q.to("c", "c1").magnitude == 6
    assert q.to("d", "c1").magnitude == 2
    assert q.to("b", "c2").magnitude == 5
    assert q.to("c", "c2").magnitude == 3
    assert q.to("d", "c2").magnitude == 3
    assert q.to("b", "c1", "c2").magnitude == 5  # c2 takes precedence
    assert q.to("c", "c1", "c2").magnitude == 6  # c2 doesn't change it, so use c1
    assert q.to("d", "c1", "c2").magnitude == 3  # c2 takes precedence


def test_err_change_base_unit():
    ureg = UnitRegistry(
        """
        foo = [d1]
        bar = [d2]

        @context c
            bar = foo
        @end
        """.splitlines()
    )

    expected = "Can't redefine a plain unit to a derived one"
    with pytest.raises(ValueError, match=expected):
        ureg.enable_contexts("c")


def test_err_to_base_unit():
    expected = ".*can't define plain units within a context"
    with pytest.raises(DefinitionSyntaxError, match=expected):
        Context.from_lines(["@context c", "x = [d]"])


def test_err_change_dimensionality():
    ureg = UnitRegistry(
        """
        foo = [d1]
        bar = [d2]
        baz = foo

        @context c
            baz = bar
        @end
        """.splitlines()
    )

    expected = re.escape(
        "Can't change dimensionality of baz from [d1] to [d2] in a context"
    )
    with pytest.raises(ValueError, match=expected):
        ureg.enable_contexts("c")


def test_err_cyclic_dependency():
    ureg = UnitRegistry(
        """
        foo = [d]
        bar = foo
        baz = bar

        @context c
            bar = baz
        @end
        """.splitlines()
    )
    # TODO align this exception and the one you get when you implement a cyclic
    #      dependency within the plain registry. Ideally this exception should be
    #      raised by enable_contexts.
    ureg.enable_contexts("c")
    q = ureg.Quantity("bar")
    with pytest.raises(RecursionError):
        q.to("foo")


def test_err_dimension_redefinition():
    with pytest.raises(DefinitionSyntaxError):
        Context.from_lines(["@context c", "[d1] = [d2] * [d3]"])


def test_err_prefix_redefinition():
    with pytest.raises(DefinitionSyntaxError):
        Context.from_lines(["@context c", "[d1] = [d2] * [d3]"])


def test_err_redefine_alias(subtests):
    expected = ".*can't change a unit's symbol or aliases within a context"
    for s in ("foo = bar = f", "foo = bar = _ = baz"):
        with subtests.test(s):
            with pytest.raises(DefinitionSyntaxError, match=expected):
                Context.from_lines(["@context c", s])


def test_err_redefine_with_prefix():
    ureg = UnitRegistry(
        """
        kilo- = 1000
        gram = [mass]
        pound = 454 gram

        @context c
            kilopound = 500000 gram
        @end
        """.splitlines()
    )

    expected = "Can't redefine a unit with a prefix: kilopound"
    with pytest.raises(ValueError, match=expected):
        ureg.enable_contexts("c")


def test_err_new_unit():
    ureg = UnitRegistry(
        """
        foo = [d]
        @context c
            bar = foo
        @end
        """.splitlines()
    )
    expected = "'bar' is not defined in the unit registry"
    with pytest.raises(UndefinedUnitError, match=expected):
        ureg.enable_contexts("c")