File: test_amply.py

package info (click to toggle)
python-amply 0.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: python: 1,005; sh: 46; makefile: 5
file content (798 lines) | stat: -rw-r--r-- 20,994 bytes parent folder | download
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
import pytest

import unittest
from io import StringIO

from amply import amply
from amply.amply import (
    number,
    param_def_stmt,
    param_stmt,
    set_record,
    set_stmt,
    simple_data,
    single,
    subscript_domain,
    symbol,
)


class TestSubscript:
    def test_subscript(self):
        fixture = """
        {a, b, c}
        {enum}
        {REGION, YEAR}
        {REGION, TECHNOLOGY, YEAR}
        {r in REGION}
        {r in REGION, y in YEAR, g in GOLF}

        """
        success, result = subscript_domain.runTests(fixture)
        assert success

        assert result[0]

    def test_subscript_result(self):
        result = subscript_domain.parseString("{a, b, c}")
        assert result.asDict() == {"subscripts": ["a", "b", "c"]}

    def test_subscript_result_domain(self):
        result = subscript_domain.parseString("{a in A, b in B, c in C}")
        assert result.asDict() == {"subscripts": ["A", "B", "C"]}


class TestNumber:
    def test_not_number(self):
        fixture = """
        one
        _
        1e
        e2
        +
        Jan
        01Jan
        Jan_01
        01_Jan
        """
        result = number.runTests(fixture, failureTests=True)
        assert result[0]

    def test_number(self):
        fixture = """
        1
        1.1
        0.234
        +1e-049
        2
        00
        0.0
        """
        result = number.runTests(fixture)
        assert result[0]


class TestSymbol:
    def test_not_symbol(self):
        fixture = """
        1.1
        0.234
        +1e-049
        skj!adfk
        """
        result = symbol.runTests(fixture, failureTests=True)
        assert result[0]

    def test_symbol(self):
        fixture = """
        Jan

        01Jan

        Jan_01

        01_Jan
        """
        result = symbol.runTests(fixture)
        assert result[0]


class TestParameter:
    def test_param_def(self):
        fixture = """
        param Test{r in REGION, y in YEAR, g in GOLF};
        param square {x, y};
        param Test;
        param Test default 1;
        """
        success, results = param_def_stmt.runTests(fixture)

        assert success

        success, _ = param_stmt.runTests(fixture, failureTests=True)
        assert success

    def test_param_stmt(self):
        fixture = """
        param T := 4;

        param T := -4;

        param T := 0.04;

        param T := -0.04;

        param 01Jan := -0.04;

        param 01_Feb := -0.04;

        """
        result = param_stmt.runTests(fixture)
        assert result[0]

        result = param_def_stmt.runTests(fixture, failureTests=True)
        assert result[0]


class TestSet:
    def test_set_stmt(self):
        fixture = """
        set month := Jan Feb Mar Apr;

        set month := 01Jan 01_Feb Mar A_pr;

        set 1_2_month := 1 2 3 4;
        """
        result = set_stmt.runTests(fixture)
        assert result[0]

    @pytest.fixture()
    def setup(self):
        fixture = """
        Jan Feb Mar Apr

        01Jan 01_Feb Mar A_pr

        1 2 3 4
        """
        return fixture

    def test_set_record(self, setup):
        result = set_record.runTests(setup)
        assert result[0]

    def test_simple_data(self, setup):
        result = simple_data.runTests(setup)
        assert result[0]

    def test_single(self):
        fixture = """
        01Jan
        01
        Jan
        Jan_01
        01_Jan
        """
        result = single.runTests(fixture)
        assert result[0]


class AmplyTest(unittest.TestCase):
    def test_data(self):
        result = amply.Amply("param T := 4;")["T"]
        assert result == 4
        result = amply.Amply("param T := -4;")["T"]
        assert result == -4
        result = amply.Amply("param T := 0.04;")["T"]
        assert result == 0.04
        result = amply.Amply("param T := -0.04;")["T"]
        assert result == -0.04

    def test_set(self):
        result = amply.Amply("set month := Jan Feb Mar Apr;")["month"]
        assert result == ["Jan", "Feb", "Mar", "Apr"]

        result = amply.Amply("set month Jan Feb Mar Apr;")["month"]
        assert result == ["Jan", "Feb", "Mar", "Apr"]
        assert [i for i in result] == ["Jan", "Feb", "Mar", "Apr"]
        assert result != []

        assert "Jan" in result
        assert "Foo" not in result
        assert len(result) == 4

    def test_set_alphanumerical(self):
        result = amply.Amply("set month := 01Jan 01_Feb Mar A_pr;")["month"]
        assert result == ["01Jan", "01_Feb", "Mar", "A_pr"]

    def test_param_definition(self):
        result = amply.Amply("param T;")
        assert result != [4]

    def test_param(self):
        result = amply.Amply("param T := 4;")["T"]
        assert result != [4]

    def test_param_subscript(self):
        result = amply.Amply("param T{foo};param T := 1 2;")["T"]
        assert not (result == 2)
        assert result != 2

    def test_attr_access(self):
        result = amply.Amply("param T:= 4;").T
        assert result == 4

    def test_from_file(self):
        try:
            s = StringIO("param T:= 4;")
        except TypeError:
            s = StringIO("param T:= 4;")
        assert amply.Amply.from_file(s).T == 4

    def test_load_string(self):
        a = amply.Amply("param T:= 4; param X{foo};")
        a.load_string("param S := 6; param X := 1 2;")
        assert a.T == 4
        assert a.S == 6
        assert a.X[1] == 2

    def test_load_file(self):
        a = amply.Amply("param T:= 4; param X{foo};")
        try:
            s = StringIO("param S := 6; param X := 1 2;")
        except TypeError:
            s = StringIO("param S := 6; param X := 1 2;")
        a.load_file(s)
        assert a.T == 4
        assert a.S == 6
        assert a.X[1] == 2

    def test_empty_init(self):
        a = amply.Amply()
        a.load_string("param T := 4;")
        assert a.T == 4

    def test_set_dimen2(self):
        result = amply.Amply(
            """
            set twotups dimen 2;
            set twotups := (1, 2) (2, 3) (4, 2) (3, 1);
            """
        )["twotups"]
        assert result == [(1, 2), (2, 3), (4, 2), (3, 1)]

    def test_set_dimen_error(self):
        a = """
            set dim1 dimen 1;
            set dim1 := (1, 2) (2, 3) (3, 2);
            """
        self.assertRaises(amply.AmplyError, lambda: amply.Amply(a))

    def test_set_dimen2_noparen(self):
        result = amply.Amply(
            """
            set twotups dimen 2;
            set twotups := 1 2 2 3 4 2 3 1;
            """
        )["twotups"]
        assert result == [(1, 2), (2, 3), (4, 2), (3, 1)]

    def test_set_subscript(self):
        result = amply.Amply(
            """
            set days{months};
            set days[Jan] := 1 2 3 4;
            set days[Feb] := 5 6 7 8;
            """
        )["days"]
        j = result["Jan"]
        assert j == [1, 2, 3, 4]
        f = result["Feb"]
        assert f == [5, 6, 7, 8]

    def test_set_subscript2(self):
        result = amply.Amply(
            """
            set days{months, days};
            set days[Jan, 3] := 1 2 3 4;
            set days[Feb, 'Ham '] := 5 6 7 8;
            """
        )["days"]
        j = result["Jan"][3]
        assert j == [1, 2, 3, 4]
        f = result["Feb"]["Ham "]
        assert f == [5, 6, 7, 8]

    def test_set_subscript2_tuples(self):
        result = amply.Amply(
            """
            set days{months, days};
            set days[Jan, 3] := 1 2 3 4;
            set days[Feb, 'Ham '] := 5 6 7 8;
            """
        )["days"]
        j = result["Jan", 3]
        assert j == [1, 2, 3, 4]
        f = result["Feb", "Ham "]
        assert f == [5, 6, 7, 8]

    def test_set_matrix(self):
        result = amply.Amply(
            """
            set A : 1 2 3 :=
                1   + - -
                2   + + -
                3   - + -
            ;
            """
        )
        a = result.A
        assert a == [(1, 1), (2, 1), (2, 2), (3, 2)]

    def test_set_matrix_tr(self):
        result = amply.Amply(
            """
            set A (tr) : 1 2 3 :=
                     1   + - -
                     2   + + -
                     3   - + -
            ;
            """
        )
        a = result.A
        assert a == [(1, 1), (1, 2), (2, 2), (2, 3)]

    def test_set_splice(self):
        result = amply.Amply(
            """
            set A dimen 3;
            set A := (1, 2, 3), (1, 1, *) 2 4 (3, *, *) 1 1;
            """
        )
        a = result.A
        assert a == [(1, 2, 3), (1, 1, 2), (1, 1, 4), (3, 1, 1)]

    def test_set_splice_matrix(self):
        result = amply.Amply(
            """
            set A dimen 3;
            set A (1, *, *) : 1 2 3 :=
                        1     + - -
                        2     + - +
                        3     - - -
                  (2, *, *) : 1 2 3 :=
                        1     + - +
                        2     - + -
                        3     - - +
            ;
            """
        )
        a = result.A
        assert a == [
            (1, 1, 1),
            (1, 2, 1),
            (1, 2, 3),
            (2, 1, 1),
            (2, 1, 3),
            (2, 2, 2),
            (2, 3, 3),
        ]

    def test_simple_params(self):
        result = amply.Amply("param T := 4;")["T"]
        assert result == 4

    def test_sub1_params(self):
        result = amply.Amply(
            """
            param foo {s};
            param foo := 1 Jan 2 Feb 3 Mar;
            """
        )
        j = result["foo"][1]
        assert j == "Jan"
        f = result["foo"][2]
        assert f == "Feb"

    def test_sub1_param_error(self):
        a = """
            param foo{s};
            param foo := 1 Jan 2 Feb 3;
            """
        self.assertRaises(amply.AmplyError, lambda: amply.Amply(a))

    def test_param_default(self):
        result = amply.Amply(
            """
            param foo {s} default 3;
            param foo := Jan 1 Feb 2 Mar 3;
            """
        )
        options = [("Jan", 1), ("Mar", 3), ("FOO", 3)]
        for name, value in options:
            self.assertEqual(result["foo"][name], value)

    def test_param_undefined(self):
        result = amply.Amply(
            """
            param foo {s} ;
            param foo := Jan 1 Feb 2 Mar 3;
            """
        )
        j = result["foo"]["Jan"]
        assert j == 1
        with self.assertRaises(KeyError):
            result["foo"]["Apr"]

    def test_sub2_params(self):
        result = amply.Amply(
            """
            param foo {s, t};
            param foo := 1 2 Hi 99 3 4;
            """
        )
        h = result["foo"][1][2]
        assert h == "Hi"
        f = result["foo"][99][3]
        assert f == 4

    def test_2d_param(self):
        result = amply.Amply(
            """
            param demand {item, location};
            param demand
                :   FRA DET LAN :=
            spoons  200 100 30
            plates  30  120 90
            cups    666 13  29 ;
            """
        )["demand"]

        options = [
            ("spoons", {"FRA": 200, "DET": 100, "LAN": 30}),
            ("plates", {"FRA": 30, "DET": 120, "LAN": 90}),
            ("cups", {"FRA": 666, "DET": 13, "LAN": 29}),
        ]
        for name, _dict in options:
            self.assertDictEqual(result[name], _dict)

    def test_2d_numeric_param(self):
        result = amply.Amply(
            """
            param square {x, y};
            param square : 1 2 :=
                4       4   8
                3       3   6
            ;
            """
        )["square"]
        f = result[4, 1]
        assert f == 4
        assert result[4, 2] == 8
        assert result[3, 1] == 3
        assert result[3, 2] == 6

    def test_2d_param_defaults(self):
        result = amply.Amply(
            """
            param demand {item, location};
            param demand default 42
                :   FRA DET LAN :=
            spoons  200 . 30
            plates  30  120 .
            cups    . .  29 ;
            """
        )["demand"]

        options = [
            ("spoons", {"FRA": 200, "DET": 42, "LAN": 30}),
            ("plates", {"FRA": 30, "DET": 120, "LAN": 42}),
            ("cups", {"FRA": 42, "DET": 42, "LAN": 29}),
        ]
        for name, _dict in options:
            self.assertDictEqual(result[name], _dict)

    def test_2tables(self):
        result = amply.Amply(
            """
            param demand {item, location};
            param demand default 42
                :   FRA DET LAN :=
            spoons  200 . 30
            plates  30  120 .
            cups    . .  29
            ;

            param square {foo, foo};
            param square
                :   A   B :=
            A       1   6
            B       6   36
            ;
            """
        )
        demand = result["demand"]
        options = [
            ("spoons", {"FRA": 200, "DET": 42, "LAN": 30}),
            ("plates", {"FRA": 30, "DET": 120, "LAN": 42}),
            ("cups", {"FRA": 42, "DET": 42, "LAN": 29}),
        ]
        for name, _dict in options:
            self.assertDictEqual(demand[name], _dict)

        square = result["square"]
        options = [
            ("A", {"A": 1, "B": 6}),
            ("B", {"A": 6, "B": 36}),
        ]
        for name, _dict in options:
            self.assertDictEqual(square[name], _dict)

    def test_2d_param_transpose(self):
        result = amply.Amply(
            """
            param demand {location, item};
            param demand default 42 (tr)
                :   FRA DET LAN :=
            spoons  200 . 30
            plates  30  120 .
            cups    . .  29 ;
            """
        )["demand"]

        self.assertEqual(result["FRA"], {"spoons": 200, "plates": 30, "cups": 42})
        self.assertEqual(result["DET"], {"spoons": 42, "plates": 120, "cups": 42})
        self.assertEqual(result["LAN"], {"spoons": 30, "plates": 42, "cups": 29})

    def test_2d_slice1(self):
        result = amply.Amply(
            """
            param demand {location, item};
            param demand :=
                [Jan, *] Foo 1 Bar 2;
            """
        )["demand"]
        f = result["Jan"]["Foo"]
        assert f == 1
        assert result["Jan"]["Bar"] == 2

    def test_3d_slice2(self):
        result = amply.Amply(
            """
            param trans_cost{src, dest, product};
            param trans_cost :=
                [*,*,bands]: FRA DET LAN :=
                    GARY     30  10  8
                    CLEV     22  7   10
                [*,*,coils]: FRA DET LAN :=
                    GARY     39  14  11
                    CLEV     27  9   12
                [*,*,plate]: FRA DET LAN :=
                    GARY     41  15  12
                    CLEV     29  9   13
            ;
            """
        )["trans_cost"]

        f = result["GARY"]["FRA"]["bands"]
        assert f == 30
        assert result["GARY"]["DET"]["plate"] == 15
        assert result["CLEV"]["LAN"]["coils"] == 12

    def test_3d_slice2b(self):
        result = amply.Amply(
            """
            param trans_cost{src, product, dest};
            param trans_cost :=
                [*,bands,*]: FRA DET LAN :=
                    GARY     30  10  8
                    CLEV     22  7   10
                [*,coils,*]: FRA DET LAN :=
                    GARY     39  14  11
                    CLEV     27  9   12
                [*,plate,*]: FRA DET LAN :=
                    GARY     41  15  12
                    CLEV     29  9   13
            ;
            """
        )["trans_cost"]

        f = result["GARY"]["bands"]["FRA"]
        assert f == 30
        assert result["GARY"]["plate"]["DET"] == 15
        assert result["CLEV"]["coils"]["LAN"] == 12

    def test_3d_slide2c(self):
        amply.Amply(
            """
            set REGION := Kenya;
            set TECHNOLOGY := TRLV_1_0;
            set YEAR := 2016 2017 2018 2019 2020;
            param Peakdemand {REGION,TECHNOLOGY,YEAR};
            param Peakdemand default 1 :=
            [Kenya,*,*]:
            2016 2017 2018 2019 2020 :=
            TRLV_1_0 0 0 0 0.035503748 0.073847796
            ;
            """
        )

    def test_single_tabbing_data(self):
        result = amply.Amply(
            """
            set elem;
            param init_stock{elem};
            param cost{elem};
            param value{elem};
            param : init_stock  cost    value :=
            iron    7           25      1
            nickel  35          3       2
            ;
            """
        )
        s = result["init_stock"]
        assert s == {"iron": 7, "nickel": 35}
        assert result["cost"] == {"iron": 25, "nickel": 3}
        assert result["value"] == {"iron": 1, "nickel": 2}

    def test_single_tabbing_data_with_set(self):
        result = amply.Amply(
            """
            set elem;
            param init_stock{elem};
            param cost{elem};
            param value{elem};
            param : elem : init_stock  cost    value :=
            iron    7           25      1
            nickel  35          3       2
            ;
            """
        )
        s = result["init_stock"]
        assert s == {"iron": 7, "nickel": 35}
        assert result["cost"] == {"iron": 25, "nickel": 3}
        assert result["value"] == {"iron": 1, "nickel": 2}

    def test_set2_tabbing(self):
        result = amply.Amply(
            """
            set elem dimen 2;
            set elem := 0 0 1 1 2 2;
            param cost{elem};
            param value{elem};
            param : cost value :=
            0 0     7   25
            1 1     35  3
            ;
            """
        )

        assert result["elem"] == [(0, 0), (1, 1), (2, 2)]

    def test_undefined_tabbing_param(self):
        a = """
            param cost{elem};
            param : cost value :=
            0       1   2
            3       4   5
            ;
            """
        self.assertRaises(amply.AmplyError, lambda: amply.Amply(a))

    def test_2dset_simpleparam(self):
        result = amply.Amply(
            """
            set elem dimen 2;
            param foo{elem};
            param foo :=
                1   2   3
                2   3   4
                3   4   5
            ;
            """
        )["foo"]

        f = result[1][2]
        assert f == 3
        assert result[2][3] == 4
        assert result[3][4] == 5

    def test_tuple_param(self):
        result = amply.Amply(
            """
            set elem dimen 2;
            param foo{elem};
            param foo :=
                1   2   3
                2   3   4
                3   4   5
            ;
            """
        )["foo"]

        f = result[1, 2]
        assert f == 3
        assert result[2, 3] == 4
        assert result[3, 4] == 5

    def test_comment(self):
        result = amply.Amply(
            """
            # a comment
            set elem dimen 2;
            param foo{elem};
            param foo :=
                1   2   3
                2   3   4
                3   4   5
            ;
            """
        )["foo"]

        f = result[1, 2]
        assert f == 3
        assert result[2, 3] == 4
        assert result[3, 4] == 5

    def test_empty_tabbing_parameter_statement(self):

        result = amply.Amply(
            """
            set x;
            param square {x};
            param default 99 : square :=
            ;
            """
        )
        assert "square" in result.symbols.keys()
        assert result.square == {}

    def test_empty_tabbing_parameters(self):

        result = amply.Amply(
            """
            set x;
            param square {x};
            param triangle {x};
            param default 99 : square triangle :=
            ;
            """
        )
        assert "square" in result.symbols.keys()
        assert result.square == {}

    def test_empty_parameter_statement(self):
        result = amply.Amply(
            """
            param square {x};
            param square default 99 :=
            ;
            """
        )
        assert "square" in result.symbols.keys()
        assert result.square == {}

    def test_high_dim_tabbing(self):
        result = amply.Amply(
            """
            set x;
            set y;
            param square {x,y};
            param default 99 : square :=
            a a 34
            a b 35
            a c 36
            b a 53
            b b 45.3
            b c 459.2
            ;
            """
        )
        assert "square" in result.symbols.keys()
        print(result.square["b"])
        assert result.square["a"] == {"a": 34.0, "b": 35.0, "c": 36.0}
        assert result.square["b"] == {"a": 53.0, "b": 45.3, "c": 459.2}


if __name__ == "__main__":
    unittest.main()