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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=F0401,E1002
"""Tests for Branches."""
__metaclass__ = type
from datetime import (
datetime,
timedelta,
)
from bzrlib.bzrdir import BzrDir
from bzrlib.revision import NULL_REVISION
from pytz import UTC
import simplejson
from sqlobject import SQLObjectNotFound
from storm.locals import Store
from testtools import ExpectedException
import transaction
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
from canonical.config import config
from canonical.database.constants import UTC_NOW
from canonical.launchpad import _
from canonical.launchpad.interfaces.lpstorm import IStore
from canonical.launchpad.webapp.interfaces import IOpenLaunchBag
from canonical.testing.layers import (
AppServerLayer,
DatabaseFunctionalLayer,
LaunchpadZopelessLayer,
)
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.blueprints.enums import NewSpecificationDefinitionStatus
from lp.blueprints.interfaces.specification import ISpecificationSet
from lp.blueprints.model.specificationbranch import SpecificationBranch
from lp.bugs.interfaces.bug import (
CreateBugParams,
IBugSet,
)
from lp.bugs.model.bugbranch import BugBranch
from lp.buildmaster.model.buildqueue import BuildQueue
from lp.code.bzr import (
BranchFormat,
ControlFormat,
RepositoryFormat,
)
from lp.code.enums import (
BranchLifecycleStatus,
BranchSubscriptionNotificationLevel,
BranchType,
BranchVisibilityRule,
CodeReviewNotificationLevel,
)
from lp.code.errors import (
AlreadyLatestFormat,
BranchCannotBePrivate,
BranchCannotBePublic,
BranchCreatorNotMemberOfOwnerTeam,
BranchCreatorNotOwner,
BranchTargetError,
CannotDeleteBranch,
CannotUpgradeNonHosted,
InvalidBranchMergeProposal,
InvalidMergeQueueConfig,
UpgradePending,
)
from lp.code.interfaces.branch import (
DEFAULT_BRANCH_STATUS_IN_LISTING,
IBranch,
)
from lp.code.interfaces.branchjob import (
IBranchScanJobSource,
IBranchUpgradeJobSource,
)
from lp.code.interfaces.branchlookup import IBranchLookup
from lp.code.interfaces.branchmergeproposal import (
BRANCH_MERGE_PROPOSAL_FINAL_STATES as FINAL_STATES,
)
from lp.code.interfaces.branchnamespace import IBranchNamespaceSet
from lp.code.interfaces.branchrevision import IBranchRevision
from lp.code.interfaces.codehosting import branch_id_alias
from lp.code.interfaces.linkedbranch import ICanHasLinkedBranch
from lp.code.interfaces.seriessourcepackagebranch import (
IFindOfficialBranchLinks,
)
from lp.code.model.branch import (
ClearDependentBranch,
ClearOfficialPackageBranch,
ClearSeriesBranch,
DeleteCodeImport,
DeletionCallable,
DeletionOperation,
update_trigger_modified_fields,
)
from lp.code.model.branchjob import (
BranchJob,
BranchJobType,
ReclaimBranchSpaceJob,
)
from lp.code.model.branchmergeproposal import BranchMergeProposal
from lp.code.model.branchrevision import BranchRevision
from lp.code.model.codeimport import (
CodeImport,
CodeImportSet,
)
from lp.code.model.codereviewcomment import CodeReviewComment
from lp.code.model.revision import Revision
from lp.code.tests.helpers import add_revision_to_branch
from lp.codehosting.safe_open import BadUrl
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.model.sourcepackage import SourcePackage
from lp.services.osutils import override_environ
from lp.services.propertycache import clear_property_cache
from lp.testing import (
ANONYMOUS,
celebrity_logged_in,
launchpadlib_for,
login,
login_person,
logout,
person_logged_in,
run_with_login,
TestCase,
TestCaseWithFactory,
time_counter,
ws_object,
)
from lp.testing.factory import LaunchpadObjectFactory
from lp.translations.model.translationtemplatesbuildjob import (
ITranslationTemplatesBuildJobSource,
)
class TestCodeImport(TestCase):
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestCodeImport, self).setUp()
login('test@canonical.com')
self.factory = LaunchpadObjectFactory()
def test_branchCodeImport(self):
"""Ensure the codeImport property works correctly."""
code_import = self.factory.makeCodeImport()
branch = code_import.branch
self.assertEqual(code_import, branch.code_import)
CodeImportSet().delete(code_import)
clear_property_cache(branch)
self.assertEqual(None, branch.code_import)
class TestBranchChanged(TestCaseWithFactory):
"""Tests for `IBranch.branchChanged`."""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.arbitrary_formats = (
ControlFormat.BZR_METADIR_1, BranchFormat.BZR_BRANCH_6,
RepositoryFormat.BZR_CHK_2A)
def test_branchChanged_sets_last_mirrored_id(self):
# branchChanged sets the last_mirrored_id attribute on the branch.
revid = self.factory.getUniqueString()
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
branch.branchChanged('', revid, *self.arbitrary_formats)
self.assertEqual(revid, branch.last_mirrored_id)
def test_branchChanged_sets_stacked_on(self):
# branchChanged sets the stacked_on attribute based on the unique_name
# passed in.
branch = self.factory.makeAnyBranch()
stacked_on = self.factory.makeAnyBranch()
login_person(branch.owner)
branch.branchChanged(
stacked_on.unique_name, '', *self.arbitrary_formats)
self.assertEqual(stacked_on, branch.stacked_on)
def test_branchChanged_sets_stacked_on_branch_id_alias(self):
# branchChanged sets the stacked_on attribute based on the id of the
# branch if it is valid.
branch = self.factory.makeAnyBranch()
stacked_on = self.factory.makeAnyBranch()
login_person(branch.owner)
stacked_on_location = branch_id_alias(stacked_on)
branch.branchChanged(stacked_on_location, '', *self.arbitrary_formats)
self.assertEqual(stacked_on, branch.stacked_on)
def test_branchChanged_unsets_stacked_on(self):
# branchChanged clears the stacked_on attribute on the branch if '' is
# passed in as the stacked_on location.
branch = self.factory.makeAnyBranch()
removeSecurityProxy(branch).stacked_on = self.factory.makeAnyBranch()
login_person(branch.owner)
branch.branchChanged('', '', *self.arbitrary_formats)
self.assertIs(None, branch.stacked_on)
def test_branchChanged_sets_last_mirrored(self):
# branchChanged sets the last_mirrored attribute on the branch to the
# current time.
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
branch.branchChanged('', '', *self.arbitrary_formats)
self.assertSqlAttributeEqualsDate(
branch, 'last_mirrored', UTC_NOW)
def test_branchChanged_records_bogus_stacked_on_url(self):
# If a bogus location is passed in as the stacked_on parameter,
# mirror_status_message is set to indicate the problem and stacked_on
# set to None.
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
branch.branchChanged('~does/not/exist', '', *self.arbitrary_formats)
self.assertIs(None, branch.stacked_on)
self.assertTrue('~does/not/exist' in branch.mirror_status_message)
def test_branchChanged_clears_mirror_status_message_if_no_error(self):
# branchChanged() clears any error that's currently mentioned in
# mirror_status_message.
branch = self.factory.makeAnyBranch()
removeSecurityProxy(branch).mirror_status_message = 'foo'
login_person(branch.owner)
branch.branchChanged('', '', *self.arbitrary_formats)
self.assertIs(None, branch.mirror_status_message)
def test_branchChanged_creates_scan_job(self):
# branchChanged() creates a scan job for the branch.
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
jobs = list(getUtility(IBranchScanJobSource).iterReady())
self.assertEqual(0, len(jobs))
branch.branchChanged('', 'rev1', *self.arbitrary_formats)
jobs = list(getUtility(IBranchScanJobSource).iterReady())
self.assertEqual(1, len(jobs))
def test_branchChanged_doesnt_create_scan_job_for_noop_change(self):
# branchChanged() doesn't create a scan job if the tip revision id
# hasn't changed.
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
removeSecurityProxy(branch).last_mirrored_id = 'rev1'
removeSecurityProxy(branch).last_scanned_id = 'rev1'
jobs = list(getUtility(IBranchScanJobSource).iterReady())
self.assertEqual(0, len(jobs))
branch.branchChanged('', 'rev1', *self.arbitrary_formats)
jobs = list(getUtility(IBranchScanJobSource).iterReady())
self.assertEqual(0, len(jobs))
def test_branchChanged_creates_scan_job_for_broken_scan(self):
# branchChanged() if the last_scanned_id is different to the newly
# changed revision, then a scan job is created.
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
removeSecurityProxy(branch).last_mirrored_id = 'rev1'
removeSecurityProxy(branch).last_scanned_id = 'old'
jobs = list(getUtility(IBranchScanJobSource).iterReady())
self.assertEqual(0, len(jobs))
branch.branchChanged('', 'rev1', *self.arbitrary_formats)
jobs = list(getUtility(IBranchScanJobSource).iterReady())
self.assertEqual(1, len(jobs))
def test_branchChanged_packs_format(self):
# branchChanged sets the branch_format etc attributes to the passed in
# values.
branch = self.factory.makeAnyBranch()
login_person(branch.owner)
branch.branchChanged(
'', 'rev1', ControlFormat.BZR_METADIR_1,
BranchFormat.BZR_BRANCH_6, RepositoryFormat.BZR_KNITPACK_1)
login(ANONYMOUS)
self.assertEqual(
(ControlFormat.BZR_METADIR_1, BranchFormat.BZR_BRANCH_6,
RepositoryFormat.BZR_KNITPACK_1),
(branch.control_format, branch.branch_format,
branch.repository_format))
class TestBranchRevisionMethods(TestCaseWithFactory):
"""Test the branch methods for adding and removing branch revisions."""
layer = DatabaseFunctionalLayer
def _getBranchRevision(self, branch, rev_id):
"""Get the branch revision for the specified branch and rev_id."""
resultset = IStore(BranchRevision).find(
BranchRevision,
BranchRevision.branch == branch,
BranchRevision.revision == Revision.id,
Revision.revision_id == rev_id)
return resultset.one()
def test_createBranchRevision(self):
# createBranchRevision adds the link for the revision to the branch.
branch = self.factory.makeBranch()
rev = self.factory.makeRevision()
# Nothing there to start with.
self.assertIs(None, self._getBranchRevision(branch, rev.revision_id))
branch.createBranchRevision(1, rev)
# Now there is one.
br = self._getBranchRevision(branch, rev.revision_id)
self.assertEqual(branch, br.branch)
self.assertEqual(rev, br.revision)
def test_removeBranchRevisions(self):
# removeBranchRevisions can remove a single linked revision.
branch = self.factory.makeBranch()
rev = self.factory.makeRevision()
branch.createBranchRevision(1, rev)
# Now remove the branch revision.
branch.removeBranchRevisions(rev.revision_id)
# Revision not there now.
self.assertIs(None, self._getBranchRevision(branch, rev.revision_id))
def test_removeBranchRevisions_multiple(self):
# removeBranchRevisions can remove multiple revision links at once.
branch = self.factory.makeBranch()
rev1 = self.factory.makeRevision()
rev2 = self.factory.makeRevision()
rev3 = self.factory.makeRevision()
branch.createBranchRevision(1, rev1)
branch.createBranchRevision(2, rev2)
branch.createBranchRevision(3, rev3)
# Now remove the branch revision.
branch.removeBranchRevisions(
[rev1.revision_id, rev2.revision_id, rev3.revision_id])
# No mainline revisions there now.
# The revision_history attribute is tested above.
self.assertEqual([], list(branch.revision_history))
class TestBranchGetRevision(TestCaseWithFactory):
"""Make sure that `Branch.getBranchRevision` works as expected."""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.branch = self.factory.makeAnyBranch()
def _makeRevision(self, revno):
# Make a revision and add it to the branch.
rev = self.factory.makeRevision()
self.branch.createBranchRevision(revno, rev)
return rev
def testGetBySequenceNumber(self):
rev1 = self._makeRevision(1)
branch_revision = self.branch.getBranchRevision(sequence=1)
self.assertEqual(rev1, branch_revision.revision)
self.assertEqual(1, branch_revision.sequence)
def testGetByRevision(self):
rev1 = self._makeRevision(1)
branch_revision = self.branch.getBranchRevision(revision=rev1)
self.assertEqual(rev1, branch_revision.revision)
self.assertEqual(1, branch_revision.sequence)
def testGetByRevisionId(self):
rev1 = self._makeRevision(1)
branch_revision = self.branch.getBranchRevision(
revision_id=rev1.revision_id)
self.assertEqual(rev1, branch_revision.revision)
self.assertEqual(1, branch_revision.sequence)
def testNonExistant(self):
self._makeRevision(1)
self.assertTrue(self.branch.getBranchRevision(sequence=2) is None)
rev2 = self.factory.makeRevision()
self.assertTrue(self.branch.getBranchRevision(revision=rev2) is None)
self.assertTrue(
self.branch.getBranchRevision(revision_id='not found') is None)
def testInvalidParams(self):
self.assertRaises(AssertionError, self.branch.getBranchRevision)
rev1 = self._makeRevision(1)
self.assertRaises(AssertionError, self.branch.getBranchRevision,
sequence=1, revision=rev1,
revision_id=rev1.revision_id)
self.assertRaises(AssertionError, self.branch.getBranchRevision,
sequence=1, revision=rev1)
self.assertRaises(AssertionError, self.branch.getBranchRevision,
revision=rev1, revision_id=rev1.revision_id)
self.assertRaises(AssertionError, self.branch.getBranchRevision,
sequence=1, revision_id=rev1.revision_id)
class TestBranch(TestCaseWithFactory):
"""Test basic properties about Launchpad database branches."""
layer = DatabaseFunctionalLayer
def test_pullURLMirrored(self):
# Mirrored branches are pulled from their actual URLs -- that's the
# point.
branch = self.factory.makeAnyBranch(branch_type=BranchType.MIRRORED)
self.assertEqual(branch.url, branch.getPullURL())
def test_pullURLImported(self):
# Imported branches are pulled from the import servers at locations
# corresponding to the hex id of the branch being mirrored.
import_server = config.launchpad.bzr_imports_root_url
branch = self.factory.makeAnyBranch(branch_type=BranchType.IMPORTED)
self.assertEqual(
'%s/%08x' % (import_server, branch.id), branch.getPullURL())
def test_pullURLRemote(self):
# We cannot mirror remote branches. getPullURL raises an
# AssertionError.
branch = self.factory.makeAnyBranch(branch_type=BranchType.REMOTE)
self.assertRaises(AssertionError, branch.getPullURL)
def test_owner_name(self):
# The owner_name attribute is set to be the name of the branch owner
# through a db trigger.
branch = self.factory.makeAnyBranch()
self.assertEqual(
branch.owner.name, removeSecurityProxy(branch).owner_name)
def test_owner_name_updated(self):
# When the owner of a branch is changed, the denormalised owner_name
# attribute is updated too.
branch = self.factory.makeAnyBranch()
new_owner = self.factory.makePerson()
removeSecurityProxy(branch).owner = new_owner
# Call the function that is normally called through the event system
# to auto reload the fields updated by the db triggers.
update_trigger_modified_fields(branch)
self.assertEqual(
new_owner.name, removeSecurityProxy(branch).owner_name)
def test_target_suffix_product(self):
# The target_suffix for a product branch is the name of the product.
branch = self.factory.makeProductBranch()
self.assertEqual(
branch.product.name, removeSecurityProxy(branch).target_suffix)
def test_target_suffix_junk(self):
# The target_suffix for a junk branch is None.
branch = self.factory.makePersonalBranch()
self.assertIs(None, removeSecurityProxy(branch).target_suffix)
def test_target_suffix_package(self):
# A package branch has the target_suffix set to the name of the source
# package.
branch = self.factory.makePackageBranch()
self.assertEqual(
branch.sourcepackagename.name,
removeSecurityProxy(branch).target_suffix)
def test_unique_name_product(self):
branch = self.factory.makeProductBranch()
self.assertEqual(
'~%s/%s/%s' % (
branch.owner.name, branch.product.name, branch.name),
branch.unique_name)
def test_unique_name_junk(self):
branch = self.factory.makePersonalBranch()
self.assertEqual(
'~%s/+junk/%s' % (branch.owner.name, branch.name),
branch.unique_name)
def test_unique_name_source_package(self):
branch = self.factory.makePackageBranch()
self.assertEqual(
'~%s/%s/%s/%s/%s' % (
branch.owner.name, branch.distribution.name,
branch.distroseries.name, branch.sourcepackagename.name,
branch.name),
branch.unique_name)
def test_target_name_junk(self):
branch = self.factory.makePersonalBranch()
self.assertEqual('+junk', branch.target.name)
def test_target_name_product(self):
branch = self.factory.makeProductBranch()
self.assertEqual(branch.product.name, branch.target.name)
def test_target_name_package(self):
branch = self.factory.makePackageBranch()
self.assertEqual(
'%s/%s/%s' % (
branch.distribution.name, branch.distroseries.name,
branch.sourcepackagename.name),
branch.target.name)
def makeLaunchBag(self):
return getUtility(IOpenLaunchBag)
def test_addToLaunchBag_product(self):
# Branches are not added directly to the launchbag. Instead,
# information about their target is added.
branch = self.factory.makeProductBranch()
launchbag = self.makeLaunchBag()
branch.addToLaunchBag(launchbag)
self.assertEqual(branch.product, launchbag.product)
def test_addToLaunchBag_personal(self):
# Junk branches may also be added to the launchbag.
branch = self.factory.makePersonalBranch()
launchbag = self.makeLaunchBag()
branch.addToLaunchBag(launchbag)
self.assertIs(None, launchbag.product)
def test_addToLaunchBag_package(self):
# Package branches can be added to the launchbag.
branch = self.factory.makePackageBranch()
launchbag = self.makeLaunchBag()
branch.addToLaunchBag(launchbag)
self.assertEqual(branch.distroseries, launchbag.distroseries)
self.assertEqual(branch.distribution, launchbag.distribution)
self.assertEqual(branch.sourcepackage, launchbag.sourcepackage)
self.assertIs(None, branch.product)
def test_distribution_personal(self):
# The distribution property of a branch is None for personal branches.
branch = self.factory.makePersonalBranch()
self.assertIs(None, branch.distribution)
def test_distribution_product(self):
# The distribution property of a branch is None for product branches.
branch = self.factory.makeProductBranch()
self.assertIs(None, branch.distribution)
def test_distribution_package(self):
# The distribution property of a branch is the distribution of the
# distroseries for package branches.
branch = self.factory.makePackageBranch()
self.assertEqual(
branch.distroseries.distribution, branch.distribution)
def test_sourcepackage_personal(self):
# The sourcepackage property of a branch is None for personal
# branches.
branch = self.factory.makePersonalBranch()
self.assertIs(None, branch.sourcepackage)
def test_sourcepackage_product(self):
# The sourcepackage property of a branch is None for product branches.
branch = self.factory.makeProductBranch()
self.assertIs(None, branch.sourcepackage)
def test_sourcepackage_package(self):
# The sourcepackage property of a branch is the ISourcePackage built
# from the distroseries and sourcepackagename of the branch.
branch = self.factory.makePackageBranch()
self.assertEqual(
SourcePackage(branch.sourcepackagename, branch.distroseries),
branch.sourcepackage)
def test_implements_IBranch(self):
# Instances of Branch provide IBranch.
branch = self.factory.makeBranch()
# We don't care about security, we just want to check that it
# implements the interface.
self.assertProvides(removeSecurityProxy(branch), IBranch)
def test_associatedProductSeries_initial(self):
# By default, a branch has no associated product series.
branch = self.factory.makeBranch()
self.assertEqual([], list(branch.associatedProductSeries()))
def test_associatedProductSeries_linked(self):
# When a branch is linked to a product series, that product series is
# included in associatedProductSeries.
branch = self.factory.makeProductBranch()
product = removeSecurityProxy(branch.product)
ICanHasLinkedBranch(product).setBranch(branch)
self.assertEqual(
[product.development_focus],
list(branch.associatedProductSeries()))
class TestBranchUpgrade(TestCaseWithFactory):
"""Test the upgrade functionalities of branches."""
layer = DatabaseFunctionalLayer
def test_needsUpgrading_empty_formats(self):
branch = self.factory.makePersonalBranch()
self.assertFalse(branch.needs_upgrading)
def test_checkUpgrade_empty_formats(self):
branch = self.factory.makePersonalBranch()
with ExpectedException(
AlreadyLatestFormat,
'Branch lp://dev/~person-name.*junk/branch.* is in the latest'
' format, so it cannot be upgraded.'):
branch.checkUpgrade()
def test_needsUpgrade_mirrored_branch(self):
branch = self.factory.makeBranch(
branch_type=BranchType.MIRRORED,
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_REPOSITORY_4)
self.assertFalse(branch.needs_upgrading)
def test_checkUpgrade_mirrored_branch(self):
branch = self.factory.makeBranch(
branch_type=BranchType.MIRRORED,
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_REPOSITORY_4)
with ExpectedException(
CannotUpgradeNonHosted,
'Cannot upgrade non-hosted branch %s' % branch.bzr_identity):
branch.checkUpgrade()
def test_needsUpgrade_remote_branch(self):
branch = self.factory.makeBranch(
branch_type=BranchType.REMOTE,
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_REPOSITORY_4)
self.assertFalse(branch.needs_upgrading)
def test_needsUpgrade_import_branch(self):
branch = self.factory.makeBranch(
branch_type=BranchType.IMPORTED,
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_REPOSITORY_4)
self.assertFalse(branch.needs_upgrading)
def test_needsUpgrading_already_requested(self):
# A branch has a needs_upgrading attribute that returns whether or not
# a branch needs to be upgraded or not. If the format is
# unrecognized, we don't try to upgrade it.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_CHK_2A)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
branch.requestUpgrade(branch.owner)
self.assertFalse(branch.needs_upgrading)
def test_checkUpgrade_already_requested(self):
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_CHK_2A)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
branch.requestUpgrade(branch.owner)
with ExpectedException(
UpgradePending,
'An upgrade is already in progress for branch'
' lp://dev/~person-name.*junk/branch.*.'):
branch.checkUpgrade()
def test_needsUpgrading_branch_format_unrecognized(self):
# A branch has a needs_upgrading attribute that returns whether or not
# a branch needs to be upgraded or not. If the format is
# unrecognized, we don't try to upgrade it.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.UNRECOGNIZED,
repository_format=RepositoryFormat.BZR_CHK_2A)
self.assertFalse(branch.needs_upgrading)
def test_needsUpgrading_branch_format_upgrade_not_needed(self):
# A branch has a needs_upgrading attribute that returns whether or not
# a branch needs to be upgraded or not. If a branch is up-to-date, it
# doesn't need to be upgraded.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_8,
repository_format=RepositoryFormat.BZR_CHK_2A)
self.assertFalse(branch.needs_upgrading)
def test_checkUpgrade_branch_format_upgrade_not_needed(self):
# If a branch is up-to-date, checkUpgrade raises AlreadyLatestFormat
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_8,
repository_format=RepositoryFormat.BZR_CHK_2A)
with ExpectedException(
AlreadyLatestFormat,
'Branch lp://dev/~person-name.*junk/branch.* is in the latest'
' format, so it cannot be upgraded.'):
branch.checkUpgrade()
def test_needsUpgrading_branch_format_upgrade_needed(self):
# A branch has a needs_upgrading attribute that returns whether or not
# a branch needs to be upgraded or not. If a branch doesn't support
# stacking, it needs to be upgraded.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_6,
repository_format=RepositoryFormat.BZR_CHK_2A)
self.assertTrue(branch.needs_upgrading)
def test_needsUpgrading_repository_format_unrecognized(self):
# A branch has a needs_upgrading attribute that returns whether or not
# a branch needs to be upgraded or not. In the repo format is
# unrecognized, we don't try to upgrade it.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_8,
repository_format=RepositoryFormat.UNRECOGNIZED)
self.assertFalse(branch.needs_upgrading)
def test_needsUpgrading_repository_format_upgrade_not_needed(self):
# A branch has a needs_upgrading method that returns whether or not a
# branch needs to be upgraded or not. If the repo format is up to
# date, there's no need to upgrade it.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_8,
repository_format=RepositoryFormat.BZR_CHK_2A)
self.assertFalse(branch.needs_upgrading)
def test_needsUpgrading_repository_format_upgrade_needed(self):
# A branch has a needs_upgrading method that returns whether or not a
# branch needs to be upgraded or not. If the format doesn't support
# stacking, it needs to be upgraded.
branch = self.factory.makePersonalBranch(
branch_format=BranchFormat.BZR_BRANCH_8,
repository_format=RepositoryFormat.BZR_REPOSITORY_4)
self.assertTrue(branch.needs_upgrading)
def test_requestUpgrade(self):
# A BranchUpgradeJob can be created by calling IBranch.requestUpgrade.
branch = self.factory.makeAnyBranch(
branch_format=BranchFormat.BZR_BRANCH_6)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
job = removeSecurityProxy(branch.requestUpgrade(branch.owner))
jobs = list(getUtility(IBranchUpgradeJobSource).iterReady())
self.assertEqual(
jobs,
[job, ])
def test_requestUpgrade_no_upgrade_needed(self):
# If a branch doesn't need to be upgraded, requestUpgrade raises an
# AlreadyLatestFormat.
branch = self.factory.makeAnyBranch(
branch_format=BranchFormat.BZR_BRANCH_8,
repository_format=RepositoryFormat.BZR_CHK_2A)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
self.assertRaises(
AlreadyLatestFormat, branch.requestUpgrade, branch.owner)
def test_requestUpgrade_upgrade_pending(self):
# If there is a pending upgrade already requested, requestUpgrade
# raises an UpgradePending.
branch = self.factory.makeAnyBranch(
branch_format=BranchFormat.BZR_BRANCH_6)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
branch.requestUpgrade(branch.owner)
self.assertRaises(UpgradePending, branch.requestUpgrade, branch.owner)
def test_upgradePending(self):
# If there is a BranchUpgradeJob pending for the branch, return True.
branch = self.factory.makeAnyBranch(
branch_format=BranchFormat.BZR_BRANCH_6)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
branch.requestUpgrade(branch.owner)
self.assertTrue(branch.upgrade_pending)
def test_upgradePending_no_upgrade_requested(self):
# If the branch never had an upgrade requested, return False.
branch = self.factory.makeAnyBranch()
self.assertFalse(branch.upgrade_pending)
def test_upgradePending_old_job_exists(self):
# If the branch had an upgrade pending, but then the job was
# completed, then upgrade_pending should return False.
branch = self.factory.makeAnyBranch(
branch_format=BranchFormat.BZR_BRANCH_6)
owner = removeSecurityProxy(branch).owner
login_person(owner)
self.addCleanup(logout)
branch_job = removeSecurityProxy(branch.requestUpgrade(branch.owner))
branch_job.job.start()
branch_job.job.complete()
self.assertFalse(branch.upgrade_pending)
class TestBranchLinksAndIdentites(TestCaseWithFactory):
"""Test IBranch.branchLinks and IBranch.branchIdentities."""
layer = DatabaseFunctionalLayer
def test_default_identities(self):
# If there are no links, the only branch identity is the unique name.
branch = self.factory.makeAnyBranch()
self.assertEqual(
[('lp://dev/' + branch.unique_name, branch)],
branch.branchIdentities())
def test_linked_to_product(self):
# If a branch is linked to the product, it is also by definition
# linked to the development focus of the product.
fooix = removeSecurityProxy(self.factory.makeProduct(name='fooix'))
fooix.development_focus.name = 'devel'
eric = self.factory.makePerson(name='eric')
branch = self.factory.makeProductBranch(
product=fooix, owner=eric, name='trunk')
linked_branch = ICanHasLinkedBranch(fooix)
linked_branch.setBranch(branch)
self.assertEqual(
[linked_branch, ICanHasLinkedBranch(fooix.development_focus)],
branch.branchLinks())
self.assertEqual(
[('lp://dev/fooix', fooix),
('lp://dev/fooix/devel', fooix.development_focus),
('lp://dev/~eric/fooix/trunk', branch)],
branch.branchIdentities())
def test_linked_to_product_series(self):
# If a branch is linked to a non-development series of a product and
# not linked to the product itself, then only the product series is
# returned in the links.
fooix = removeSecurityProxy(self.factory.makeProduct(name='fooix'))
future = self.factory.makeProductSeries(product=fooix, name='future')
eric = self.factory.makePerson(name='eric')
branch = self.factory.makeProductBranch(
product=fooix, owner=eric, name='trunk')
linked_branch = ICanHasLinkedBranch(future)
login_person(fooix.owner)
linked_branch.setBranch(branch)
self.assertEqual(
[linked_branch],
branch.branchLinks())
self.assertEqual(
[('lp://dev/fooix/future', future),
('lp://dev/~eric/fooix/trunk', branch)],
branch.branchIdentities())
def test_linked_to_package(self):
# If a branch is linked to a suite source package where the
# distroseries is the current series for the distribution, there is a
# link for both the distribution source package and the suite source
# package.
mint = self.factory.makeDistribution(name='mint')
dev = self.factory.makeDistroSeries(
distribution=mint, version='1.0', name='dev')
eric = self.factory.makePerson(name='eric')
branch = self.factory.makePackageBranch(
distroseries=dev, sourcepackagename='choc', name='tip',
owner=eric)
dsp = self.factory.makeDistributionSourcePackage('choc', mint)
distro_link = ICanHasLinkedBranch(dsp)
development_package = dsp.development_version
suite_sourcepackage = development_package.getSuiteSourcePackage(
PackagePublishingPocket.RELEASE)
suite_sp_link = ICanHasLinkedBranch(suite_sourcepackage)
registrant = suite_sourcepackage.distribution.owner
run_with_login(
registrant,
suite_sp_link.setBranch, branch, registrant)
self.assertEqual(
[distro_link, suite_sp_link],
branch.branchLinks())
self.assertEqual(
[('lp://dev/mint/choc', dsp),
('lp://dev/mint/dev/choc', suite_sourcepackage),
('lp://dev/~eric/mint/dev/choc/tip', branch)],
branch.branchIdentities())
def test_linked_to_package_not_release_pocket(self):
# If a branch is linked to a suite source package where the
# distroseries is the current series for the distribution, but the
# pocket is not the RELEASE pocket, then there is only the link for
# the suite source package.
mint = self.factory.makeDistribution(name='mint')
dev = self.factory.makeDistroSeries(
distribution=mint, version='1.0', name='dev')
eric = self.factory.makePerson(name='eric')
branch = self.factory.makePackageBranch(
distroseries=dev, sourcepackagename='choc', name='tip',
owner=eric)
dsp = self.factory.makeDistributionSourcePackage('choc', mint)
development_package = dsp.development_version
suite_sourcepackage = development_package.getSuiteSourcePackage(
PackagePublishingPocket.BACKPORTS)
suite_sp_link = ICanHasLinkedBranch(suite_sourcepackage)
registrant = suite_sourcepackage.distribution.owner
run_with_login(
registrant,
suite_sp_link.setBranch, branch, registrant)
self.assertEqual(
[suite_sp_link],
branch.branchLinks())
self.assertEqual(
[('lp://dev/mint/dev-backports/choc', suite_sourcepackage),
('lp://dev/~eric/mint/dev/choc/tip', branch)],
branch.branchIdentities())
def test_linked_to_package_not_current_series(self):
# If the branch is linked to a suite source package where the distro
# series is not the current series, only the suite source package is
# returned in the links.
mint = self.factory.makeDistribution(name='mint')
self.factory.makeDistroSeries(
distribution=mint, version='1.0', name='dev')
supported = self.factory.makeDistroSeries(
distribution=mint, version='0.9', name='supported')
eric = self.factory.makePerson(name='eric')
branch = self.factory.makePackageBranch(
distroseries=supported, sourcepackagename='choc', name='tip',
owner=eric)
suite_sp = self.factory.makeSuiteSourcePackage(
distroseries=supported, sourcepackagename='choc',
pocket=PackagePublishingPocket.RELEASE)
suite_sp_link = ICanHasLinkedBranch(suite_sp)
registrant = suite_sp.distribution.owner
run_with_login(
registrant,
suite_sp_link.setBranch, branch, registrant)
self.assertEqual(
[suite_sp_link],
branch.branchLinks())
self.assertEqual(
[('lp://dev/mint/supported/choc', suite_sp),
('lp://dev/~eric/mint/supported/choc/tip', branch)],
branch.branchIdentities())
def test_linked_across_project_to_package(self):
# If a product branch is linked to a suite source package, the links
# are the same as if it was a source package branch.
mint = self.factory.makeDistribution(name='mint')
self.factory.makeDistroSeries(
distribution=mint, version='1.0', name='dev')
eric = self.factory.makePerson(name='eric')
fooix = self.factory.makeProduct(name='fooix')
branch = self.factory.makeProductBranch(
product=fooix, owner=eric, name='trunk')
dsp = self.factory.makeDistributionSourcePackage('choc', mint)
distro_link = ICanHasLinkedBranch(dsp)
development_package = dsp.development_version
suite_sourcepackage = development_package.getSuiteSourcePackage(
PackagePublishingPocket.RELEASE)
suite_sp_link = ICanHasLinkedBranch(suite_sourcepackage)
registrant = suite_sourcepackage.distribution.owner
run_with_login(
registrant,
suite_sp_link.setBranch, branch, registrant)
self.assertEqual(
[distro_link, suite_sp_link],
branch.branchLinks())
self.assertEqual(
[('lp://dev/mint/choc', dsp),
('lp://dev/mint/dev/choc', suite_sourcepackage),
('lp://dev/~eric/fooix/trunk', branch)],
branch.branchIdentities())
def test_junk_branch_links(self):
# If a junk branch has links, those links are returned in the
# branchLinks, but the branchIdentities just has the branch unique
# name.
eric = self.factory.makePerson(name='eric')
branch = self.factory.makePersonalBranch(owner=eric, name='foo')
fooix = removeSecurityProxy(self.factory.makeProduct())
linked_branch = ICanHasLinkedBranch(fooix)
linked_branch.setBranch(branch)
self.assertEqual(
[linked_branch, ICanHasLinkedBranch(fooix.development_focus)],
branch.branchLinks())
self.assertEqual(
[('lp://dev/~eric/+junk/foo', branch)],
branch.branchIdentities())
class TestBzrIdentity(TestCaseWithFactory):
"""Test IBranch.bzr_identity."""
layer = DatabaseFunctionalLayer
def assertBzrIdentity(self, branch, identity_path):
"""Assert that the bzr identity of 'branch' is 'identity_path'.
Actually, it'll be lp://dev/<identity_path>.
"""
self.assertEqual(
'lp://dev/%s' % identity_path, branch.bzr_identity,
"bzr identity")
def test_default_identity(self):
# By default, the bzr identity is an lp URL with the branch's unique
# name.
branch = self.factory.makeAnyBranch()
self.assertBzrIdentity(branch, branch.unique_name)
def test_linked_to_product(self):
# If a branch is the development focus branch for a product, then it's
# bzr identity is lp:product.
branch = self.factory.makeProductBranch()
product = removeSecurityProxy(branch.product)
linked_branch = ICanHasLinkedBranch(product)
linked_branch.setBranch(branch)
self.assertBzrIdentity(branch, linked_branch.bzr_path)
def test_linked_to_product_series(self):
# If a branch is the development focus branch for a product series,
# then it's bzr identity is lp:product/series.
branch = self.factory.makeProductBranch()
product = branch.product
series = self.factory.makeProductSeries(product=product)
linked_branch = ICanHasLinkedBranch(series)
login_person(series.owner)
linked_branch.setBranch(branch)
self.assertBzrIdentity(branch, linked_branch.bzr_path)
def test_private_linked_to_product(self):
# Private branches also have a short lp:url.
branch = self.factory.makeProductBranch(private=True)
with celebrity_logged_in('admin'):
product = branch.product
ICanHasLinkedBranch(product).setBranch(branch)
self.assertBzrIdentity(branch, product.name)
def test_linked_to_series_and_dev_focus(self):
# If a branch is the development focus branch for a product and the
# branch for a series, the bzr identity will be the storter of the two
# URLs.
branch = self.factory.makeProductBranch()
series = self.factory.makeProductSeries(product=branch.product)
product_link = ICanHasLinkedBranch(
removeSecurityProxy(branch.product))
series_link = ICanHasLinkedBranch(series)
product_link.setBranch(branch)
login_person(series.owner)
series_link.setBranch(branch)
self.assertBzrIdentity(branch, product_link.bzr_path)
def test_junk_branch_always_unique_name(self):
# For junk branches, the bzr identity is always based on the unique
# name of the branch, even if it's linked to a product, product series
# or whatever.
branch = self.factory.makePersonalBranch()
product = removeSecurityProxy(self.factory.makeProduct())
ICanHasLinkedBranch(product).setBranch(branch)
self.assertBzrIdentity(branch, branch.unique_name)
def test_linked_to_package(self):
# If a branch is linked to a pocket of a package, then the
# bzr identity is the path to that package.
branch = self.factory.makePackageBranch()
# Have to pick something that's not RELEASE in order to guarantee that
# it's not the dev focus source package.
pocket = PackagePublishingPocket.BACKPORTS
linked_branch = ICanHasLinkedBranch(
branch.sourcepackage.getSuiteSourcePackage(pocket))
registrant = branch.sourcepackage.distribution.owner
login_person(registrant)
linked_branch.setBranch(branch, registrant)
logout()
login(ANONYMOUS)
self.assertBzrIdentity(branch, linked_branch.bzr_path)
def test_linked_to_dev_package(self):
# If a branch is linked to the development focus version of a package
# then the bzr identity is distro/package.
sourcepackage = self.factory.makeSourcePackage()
distro_package = sourcepackage.distribution_sourcepackage
branch = self.factory.makePackageBranch(
sourcepackage=distro_package.development_version)
linked_branch = ICanHasLinkedBranch(distro_package)
registrant = sourcepackage.distribution.owner
run_with_login(
registrant,
linked_branch.setBranch, branch, registrant)
self.assertBzrIdentity(branch, linked_branch.bzr_path)
class TestBranchDeletion(TestCaseWithFactory):
"""Test the different cases that makes a branch deletable or not."""
layer = LaunchpadZopelessLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.user = self.factory.makePerson()
self.product = self.factory.makeProduct(owner=self.user)
self.branch = self.factory.makeProductBranch(
name='to-delete', owner=self.user, product=self.product)
# The owner of the branch is subscribed to the branch when it is
# created. The tests here assume no initial connections, so
# unsubscribe the branch owner here.
self.branch.unsubscribe(self.branch.owner, self.branch.owner)
# Make sure that the tests all flush the database changes.
self.addCleanup(Store.of(self.branch).flush)
login_person(self.user)
def test_deletable(self):
"""A newly created branch can be deleted without any problems."""
self.assertEqual(self.branch.canBeDeleted(), True,
"A newly created branch should be able to be "
"deleted.")
branch_id = self.branch.id
branch_set = getUtility(IBranchLookup)
self.branch.destroySelf()
self.assert_(branch_set.get(branch_id) is None,
"The branch has not been deleted.")
def test_stackedBranchDisablesDeletion(self):
# A branch that is stacked upon cannot be deleted.
self.factory.makeAnyBranch(stacked_on=self.branch)
self.assertFalse(self.branch.canBeDeleted())
def test_subscriptionDoesntDisableDeletion(self):
"""A branch that has a subscription can be deleted."""
self.branch.subscribe(
self.user, BranchSubscriptionNotificationLevel.NOEMAIL, None,
CodeReviewNotificationLevel.NOEMAIL, self.user)
self.assertEqual(True, self.branch.canBeDeleted())
def test_codeImportCanStillBeDeleted(self):
"""A branch that has an attached code import can be deleted."""
code_import = LaunchpadObjectFactory().makeCodeImport()
branch = code_import.branch
self.assertEqual(
branch.canBeDeleted(), True,
"A branch that has a import is deletable.")
def test_bugBranchLinkDisablesDeletion(self):
"""A branch linked to a bug cannot be deleted."""
params = CreateBugParams(
owner=self.user, title='Firefox bug', comment='blah')
params.setBugTarget(product=self.product)
bug = getUtility(IBugSet).createBug(params)
bug.linkBranch(self.branch, self.user)
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch linked to a bug is not deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_specBranchLinkDisablesDeletion(self):
"""A branch linked to a spec cannot be deleted."""
spec = getUtility(ISpecificationSet).new(
name='some-spec', title='Some spec', product=self.product,
owner=self.user, summary='', specurl=None,
definition_status=NewSpecificationDefinitionStatus.NEW)
spec.linkBranch(self.branch, self.user)
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch linked to a spec is not deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_associatedProductSeriesBranchDisablesDeletion(self):
"""A branch linked as a branch to a product series cannot be
deleted.
"""
self.product.development_focus.branch = self.branch
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch that is a user branch for a product series"
" is not deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_productSeriesTranslationsBranchDisablesDeletion(self):
self.product.development_focus.translations_branch = self.branch
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch that is a translations branch for a "
"product series is not deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_revisionsDeletable(self):
"""A branch that has some revisions can be deleted."""
revision = self.factory.makeRevision()
self.branch.createBranchRevision(0, revision)
# Need to commit the addition to make sure that the branch revisions
# are recorded as there and that the appropriate deferred foreign keys
# are set up.
transaction.commit()
self.assertEqual(self.branch.canBeDeleted(), True,
"A branch that has a revision is deletable.")
unique_name = self.branch.unique_name
self.branch.destroySelf()
# Commit again to trigger the deferred indices.
transaction.commit()
branch_lookup = getUtility(IBranchLookup)
self.assertEqual(branch_lookup.getByUniqueName(unique_name), None,
"Branch was not deleted.")
def test_landingTargetDisablesDeletion(self):
"""A branch with a landing target cannot be deleted."""
target_branch = self.factory.makeProductBranch(
name='landing-target', owner=self.user, product=self.product)
self.branch.addLandingTarget(self.user, target_branch)
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch with a landing target is not deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_landingCandidateDisablesDeletion(self):
"""A branch with a landing candidate cannot be deleted."""
source_branch = self.factory.makeProductBranch(
name='landing-candidate', owner=self.user, product=self.product)
source_branch.addLandingTarget(self.user, self.branch)
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch with a landing candidate is not"
" deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_prerequisiteBranchDisablesDeletion(self):
"""A branch that is a prerequisite branch cannot be deleted."""
source_branch = self.factory.makeProductBranch(
name='landing-candidate', owner=self.user, product=self.product)
target_branch = self.factory.makeProductBranch(
name='landing-target', owner=self.user, product=self.product)
source_branch.addLandingTarget(self.user, target_branch, self.branch)
self.assertEqual(self.branch.canBeDeleted(), False,
"A branch with a prerequisite target is not "
"deletable.")
self.assertRaises(CannotDeleteBranch, self.branch.destroySelf)
def test_relatedBranchJobsDeleted(self):
# A branch with an associated branch job will delete those jobs.
branch = self.factory.makeBranch(
branch_format=BranchFormat.BZR_BRANCH_6)
removeSecurityProxy(branch).requestUpgrade(branch.owner)
branch.destroySelf()
# Need to commit the transaction to fire off the constraint checks.
transaction.commit()
def test_related_TranslationTemplatesBuildJob_cleaned_out(self):
# A TranslationTemplatesBuildJob is a type of BranchJob that
# comes with a BuildQueue entry referring to the same Job.
# Deleting the branch cleans up the BuildQueue before it can
# remove the Job and BranchJob.
branch = self.factory.makeAnyBranch()
getUtility(ITranslationTemplatesBuildJobSource).create(branch)
branch.destroySelf(break_references=True)
def test_linked_translations_branch_cleared(self):
# The translations_branch of a series that is linked to the branch
# should be cleared.
dev_focus = self.branch.product.development_focus
dev_focus.translations_branch = self.branch
self.branch.destroySelf(break_references=True)
def test_unrelated_TranslationTemplatesBuildJob_intact(self):
# No innocent BuildQueue entries are harmed in deleting a
# branch.
branch = self.factory.makeAnyBranch()
other_branch = self.factory.makeAnyBranch()
source = getUtility(ITranslationTemplatesBuildJobSource)
job = source.create(branch)
other_job = source.create(other_branch)
store = Store.of(branch)
branch.destroySelf(break_references=True)
# The BuildQueue for the job whose branch we deleted is gone.
buildqueue = store.find(BuildQueue, BuildQueue.job == job.job)
self.assertEqual([], list(buildqueue))
# The other job's BuildQueue entry is still there.
other_buildqueue = store.find(
BuildQueue, BuildQueue.job == other_job.job)
self.assertNotEqual([], list(other_buildqueue))
def test_createsJobToReclaimSpace(self):
# When a branch is deleted from the database, a job to remove the
# branch from disk as well.
branch = self.factory.makeAnyBranch()
branch_id = branch.id
store = Store.of(branch)
branch.destroySelf()
jobs = store.find(
BranchJob,
BranchJob.job_type == BranchJobType.RECLAIM_BRANCH_SPACE)
self.assertEqual(
[branch_id],
[ReclaimBranchSpaceJob(job).branch_id for job in jobs])
def test_destroySelf_with_SourcePackageRecipe(self):
"""If branch is a base_branch in a recipe, it is deleted."""
recipe = self.factory.makeSourcePackageRecipe()
recipe.base_branch.destroySelf(break_references=True)
def test_destroySelf_with_SourcePackageRecipe_as_non_base(self):
"""If branch is referred to by a recipe, it is deleted."""
branch1 = self.factory.makeAnyBranch()
branch2 = self.factory.makeAnyBranch()
self.factory.makeSourcePackageRecipe(
branches=[branch1, branch2])
branch2.destroySelf(break_references=True)
class TestBranchDeletionConsequences(TestCase):
"""Test determination and application of branch deletion consequences."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestBranchDeletionConsequences, self).setUp()
login('test@canonical.com')
self.factory = LaunchpadObjectFactory()
# Has to be a product branch because of merge proposals.
self.branch = self.factory.makeProductBranch()
# The owner of the branch is subscribed to the branch when it is
# created. The tests here assume no initial connections, so
# unsubscribe the branch owner here.
self.branch.unsubscribe(self.branch.owner, self.branch.owner)
def test_plainBranch(self):
"""Ensure that a fresh branch has no deletion requirements."""
self.assertEqual({}, self.branch.deletionRequirements())
def makeMergeProposals(self):
"""Produce a merge proposal for testing purposes."""
target_branch = self.factory.makeProductBranch(
product=self.branch.product)
prerequisite_branch = self.factory.makeProductBranch(
product=self.branch.product)
# Remove the implicit subscriptions.
target_branch.unsubscribe(target_branch.owner, target_branch.owner)
prerequisite_branch.unsubscribe(
prerequisite_branch.owner, prerequisite_branch.owner)
merge_proposal1 = self.branch.addLandingTarget(
self.branch.owner, target_branch, prerequisite_branch)
# Disable this merge proposal, to allow creating a new identical one
lp_admins = getUtility(ILaunchpadCelebrities).admin
merge_proposal1.rejectBranch(lp_admins, 'null:')
merge_proposal2 = self.branch.addLandingTarget(
self.branch.owner, target_branch, prerequisite_branch)
return merge_proposal1, merge_proposal2
def test_branchWithMergeProposal(self):
"""Ensure that deletion requirements with a merge proposal are right.
Each branch related to the merge proposal is tested to ensure it
produces a unique, correct result.
"""
merge_proposal1, merge_proposal2 = self.makeMergeProposals()
self.assertEqual({
merge_proposal1:
('delete', _('This branch is the source branch of this merge'
' proposal.')),
merge_proposal2:
('delete', _('This branch is the source branch of this merge'
' proposal.'))},
self.branch.deletionRequirements())
self.assertEqual({
merge_proposal1:
('delete', _('This branch is the target branch of this merge'
' proposal.')),
merge_proposal2:
('delete', _('This branch is the target branch of this merge'
' proposal.'))},
merge_proposal1.target_branch.deletionRequirements())
self.assertEqual({
merge_proposal1:
('alter', _('This branch is the prerequisite branch of this merge'
' proposal.')),
merge_proposal2:
('alter', _('This branch is the prerequisite branch of this merge'
' proposal.'))},
merge_proposal1.prerequisite_branch.deletionRequirements())
def test_deleteMergeProposalSource(self):
"""Merge proposal source branches can be deleted with break_links."""
merge_proposal1, merge_proposal2 = self.makeMergeProposals()
merge_proposal1_id = merge_proposal1.id
BranchMergeProposal.get(merge_proposal1_id)
self.branch.destroySelf(break_references=True)
self.assertRaises(SQLObjectNotFound,
BranchMergeProposal.get, merge_proposal1_id)
def test_deleteMergeProposalTarget(self):
"""Merge proposal target branches can be deleted with break_links."""
merge_proposal1, merge_proposal2 = self.makeMergeProposals()
merge_proposal1_id = merge_proposal1.id
BranchMergeProposal.get(merge_proposal1_id)
merge_proposal1.target_branch.destroySelf(break_references=True)
self.assertRaises(SQLObjectNotFound,
BranchMergeProposal.get, merge_proposal1_id)
def test_deleteMergeProposalDependent(self):
"""break_links enables deleting merge proposal dependant branches."""
merge_proposal1, merge_proposal2 = self.makeMergeProposals()
merge_proposal1.prerequisite_branch.destroySelf(break_references=True)
self.assertEqual(None, merge_proposal1.prerequisite_branch)
def test_deleteSourceCodeReviewComment(self):
"""Deletion of branches that have CodeReviewComments works."""
comment = self.factory.makeCodeReviewComment()
comment_id = comment.id
branch = comment.branch_merge_proposal.source_branch
branch.destroySelf(break_references=True)
self.assertRaises(
SQLObjectNotFound, CodeReviewComment.get, comment_id)
def test_deleteTargetCodeReviewComment(self):
"""Deletion of branches that have CodeReviewComments works."""
comment = self.factory.makeCodeReviewComment()
comment_id = comment.id
branch = comment.branch_merge_proposal.target_branch
branch.destroySelf(break_references=True)
self.assertRaises(
SQLObjectNotFound, CodeReviewComment.get, comment_id)
def test_branchWithBugRequirements(self):
"""Deletion requirements for a branch with a bug are right."""
bug = self.factory.makeBug()
bug.linkBranch(self.branch, self.branch.owner)
self.assertEqual({bug.default_bugtask:
('delete', _('This bug is linked to this branch.'))},
self.branch.deletionRequirements())
def test_branchWithBugDeletion(self):
"""break_links allows deleting a branch with a bug."""
bug1 = self.factory.makeBug()
bug1.linkBranch(self.branch, self.branch.owner)
bug_branch1 = bug1.linked_branches[0]
bug_branch1_id = bug_branch1.id
self.branch.destroySelf(break_references=True)
self.assertRaises(SQLObjectNotFound, BugBranch.get, bug_branch1_id)
def test_branchWithSpecRequirements(self):
"""Deletion requirements for a branch with a spec are right."""
spec = self.factory.makeSpecification()
spec.linkBranch(self.branch, self.branch.owner)
self.assertEqual({self.branch.spec_links[0]:
('delete', _(
'This blueprint is linked to this branch.'))},
self.branch.deletionRequirements())
def test_branchWithSpecDeletion(self):
"""break_links allows deleting a branch with a spec."""
spec1 = self.factory.makeSpecification()
spec1.linkBranch(self.branch, self.branch.owner)
spec1_branch_id = self.branch.spec_links[0].id
spec2 = self.factory.makeSpecification()
spec2.linkBranch(self.branch, self.branch.owner)
spec2_branch_id = self.branch.spec_links[1].id
self.branch.destroySelf(break_references=True)
self.assertRaises(SQLObjectNotFound, SpecificationBranch.get,
spec1_branch_id)
self.assertRaises(SQLObjectNotFound, SpecificationBranch.get,
spec2_branch_id)
def test_branchWithSeriesRequirements(self):
"""Deletion requirements for a series' branch are right."""
series = self.factory.makeProductSeries(branch=self.branch)
self.assertEqual(
{series: ('alter',
_('This series is linked to this branch.'))},
self.branch.deletionRequirements())
def test_branchWithSeriesDeletion(self):
"""break_links allows deleting a series' branch."""
series1 = self.factory.makeProductSeries(branch=self.branch)
series2 = self.factory.makeProductSeries(branch=self.branch)
self.branch.destroySelf(break_references=True)
self.assertEqual(None, series1.branch)
self.assertEqual(None, series2.branch)
def test_official_package_requirements(self):
# If a branch is officially linked to a source package, then the
# deletion requirements indicate the fact.
branch = self.factory.makePackageBranch()
package = branch.sourcepackage
pocket = PackagePublishingPocket.RELEASE
run_with_login(
package.distribution.owner,
package.development_version.setBranch,
pocket, branch, package.distribution.owner)
self.assertEqual(
{package: ('alter',
_('Branch is officially linked to a source package.'))},
branch.deletionRequirements())
def test_official_package_branch_deleted(self):
# A branch that's an official package branch can be deleted if you are
# allowed to modify package branch links, and you pass in
# break_references.
branch = self.factory.makePackageBranch()
package = branch.sourcepackage
pocket = PackagePublishingPocket.RELEASE
run_with_login(
package.distribution.owner,
package.development_version.setBranch,
pocket, branch, package.distribution.owner)
self.assertEqual(False, branch.canBeDeleted())
branch.destroySelf(break_references=True)
self.assertIs(None, package.getBranch(pocket))
def test_branchWithCodeImportRequirements(self):
"""Deletion requirements for a code import branch are right"""
code_import = self.factory.makeCodeImport()
# Remove the implicit branch subscription first.
code_import.branch.unsubscribe(
code_import.branch.owner, code_import.branch.owner)
self.assertEqual({}, code_import.branch.deletionRequirements())
def test_branchWithCodeImportDeletion(self):
"""break_links allows deleting a code import branch."""
code_import = self.factory.makeCodeImport()
code_import_id = code_import.id
code_import.branch.destroySelf(break_references=True)
self.assertRaises(
SQLObjectNotFound, CodeImport.get, code_import_id)
def test_sourceBranchWithCodeReviewVoteReference(self):
"""Break_references handles CodeReviewVoteReference source branch."""
merge_proposal = self.factory.makeBranchMergeProposal()
merge_proposal.nominateReviewer(self.factory.makePerson(),
self.factory.makePerson())
merge_proposal.source_branch.destroySelf(break_references=True)
def test_targetBranchWithCodeReviewVoteReference(self):
"""Break_references handles CodeReviewVoteReference target branch."""
merge_proposal = self.factory.makeBranchMergeProposal()
merge_proposal.nominateReviewer(self.factory.makePerson(),
self.factory.makePerson())
merge_proposal.target_branch.destroySelf(break_references=True)
def test_ClearDependentBranch(self):
"""ClearDependent.__call__ must clear the prerequisite branch."""
merge_proposal = removeSecurityProxy(self.makeMergeProposals()[0])
with person_logged_in(merge_proposal.prerequisite_branch.owner):
ClearDependentBranch(merge_proposal)()
self.assertEqual(None, merge_proposal.prerequisite_branch)
def test_ClearOfficialPackageBranch(self):
# ClearOfficialPackageBranch.__call__ clears the official package
# branch.
branch = self.factory.makePackageBranch()
package = branch.sourcepackage
pocket = PackagePublishingPocket.RELEASE
run_with_login(
package.distribution.owner,
package.development_version.setBranch,
pocket, branch, package.distribution.owner)
series_set = getUtility(IFindOfficialBranchLinks)
[link] = list(series_set.findForBranch(branch))
ClearOfficialPackageBranch(link)()
self.assertIs(None, package.getBranch(pocket))
def test_ClearSeriesBranch(self):
"""ClearSeriesBranch.__call__ must clear the user branch."""
series = removeSecurityProxy(self.factory.makeProductSeries(
branch=self.branch))
ClearSeriesBranch(series, self.branch)()
self.assertEqual(None, series.branch)
def test_DeletionOperation(self):
"""DeletionOperation.__call__ is not implemented."""
self.assertRaises(NotImplementedError, DeletionOperation('a', 'b'))
def test_DeletionCallable(self):
"""DeletionCallable must invoke the callable."""
spec = self.factory.makeSpecification()
spec_link = spec.linkBranch(self.branch, self.branch.owner)
spec_link_id = spec_link.id
DeletionCallable(spec, 'blah', spec_link.destroySelf)()
self.assertRaises(SQLObjectNotFound, SpecificationBranch.get,
spec_link_id)
def test_DeleteCodeImport(self):
"""DeleteCodeImport.__call__ must delete the CodeImport."""
code_import = self.factory.makeCodeImport()
code_import_id = code_import.id
DeleteCodeImport(code_import)()
self.assertRaises(
SQLObjectNotFound, CodeImport.get, code_import_id)
def test_deletionRequirements_with_SourcePackageRecipe(self):
"""Recipes are listed as deletion requirements."""
recipe = self.factory.makeSourcePackageRecipe()
self.assertEqual(
{recipe: ('delete', 'This recipe uses this branch.')},
recipe.base_branch.deletionRequirements())
class StackedBranches(TestCaseWithFactory):
"""Tests for showing branches stacked on another."""
layer = DatabaseFunctionalLayer
def testNoBranchesStacked(self):
# getStackedBranches returns an empty collection if there are no
# branches stacked on it.
branch = self.factory.makeAnyBranch()
self.assertEqual(set(), set(branch.getStackedBranches()))
def testSingleBranchStacked(self):
# some_branch.getStackedBranches returns a collection of branches
# stacked on some_branch.
branch = self.factory.makeAnyBranch()
stacked_branch = self.factory.makeAnyBranch(stacked_on=branch)
self.assertEqual(
set([stacked_branch]), set(branch.getStackedBranches()))
def testMultipleBranchesStacked(self):
# some_branch.getStackedBranches returns a collection of branches
# stacked on some_branch.
branch = self.factory.makeAnyBranch()
stacked_a = self.factory.makeAnyBranch(stacked_on=branch)
stacked_b = self.factory.makeAnyBranch(stacked_on=branch)
self.assertEqual(
set([stacked_a, stacked_b]), set(branch.getStackedBranches()))
class BranchAddLandingTarget(TestCaseWithFactory):
"""Exercise all the code paths for adding a landing target."""
layer = DatabaseFunctionalLayer
def setUp(self):
super(BranchAddLandingTarget, self).setUp('admin@canonical.com')
self.product = self.factory.makeProduct()
self.user = self.factory.makePerson()
self.reviewer = self.factory.makePerson(name='johndoe')
self.source = self.factory.makeProductBranch(
name='source-branch', owner=self.user, product=self.product)
self.target = self.factory.makeProductBranch(
name='target-branch', owner=self.user, product=self.product)
self.prerequisite = self.factory.makeProductBranch(
name='prerequisite-branch', owner=self.user, product=self.product)
def tearDown(self):
logout()
super(BranchAddLandingTarget, self).tearDown()
def assertOnePendingReview(self, proposal, reviewer, review_type=None):
# There should be one pending vote for the reviewer with the specified
# review type.
[vote] = list(proposal.votes)
self.assertEqual(reviewer, vote.reviewer)
self.assertEqual(self.user, vote.registrant)
self.assertIs(None, vote.comment)
if review_type is None:
self.assertIs(None, vote.review_type)
else:
self.assertEqual(review_type, vote.review_type)
def test_junkSource(self):
"""Junk branches cannot be used as a source for merge proposals."""
self.source.setTarget(user=self.source.owner)
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target)
def test_targetProduct(self):
"""The product of the target branch must match the product of the
source branch.
"""
self.target.setTarget(user=self.target.owner)
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target)
project = self.factory.makeProduct()
self.target.setTarget(user=self.target.owner, project=project)
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target)
def test_targetMustNotBeTheSource(self):
"""The target and source branch cannot be the same."""
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.source)
def test_prerequisiteBranchSameProduct(self):
"""The prerequisite branch, if any, must be for the same product.
"""
self.prerequisite.setTarget(user=self.prerequisite.owner)
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target, self.prerequisite)
project = self.factory.makeProduct()
self.prerequisite.setTarget(
user=self.prerequisite.owner, project=project)
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target, self.prerequisite)
def test_prerequisiteMustNotBeTheSource(self):
"""The target and source branch cannot be the same."""
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target, self.source)
def test_prerequisiteMustNotBeTheTarget(self):
"""The target and source branch cannot be the same."""
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target, self.target)
def test_existingMergeProposal(self):
"""If there is an existing merge proposal for the source and target
branch pair, then another landing target specifying the same pair
raises.
"""
self.source.addLandingTarget(
self.user, self.target, self.prerequisite)
self.assertRaises(
InvalidBranchMergeProposal, self.source.addLandingTarget,
self.user, self.target, self.prerequisite)
def test_existingRejectedMergeProposal(self):
"""If there is an existing rejected merge proposal for the source and
target branch pair, then another landing target specifying the same
pair is fine.
"""
proposal = self.source.addLandingTarget(
self.user, self.target, self.prerequisite)
proposal.rejectBranch(self.user, 'some_revision')
self.source.addLandingTarget(
self.user, self.target, self.prerequisite)
def test_default_reviewer(self):
"""If the target branch has a default reviewer set, this reviewer
should be assigned to the merge proposal.
"""
target_with_default_reviewer = self.factory.makeProductBranch(
name='target-branch-with-reviewer', owner=self.user,
product=self.product, reviewer=self.reviewer)
proposal = self.source.addLandingTarget(
self.user, target_with_default_reviewer)
self.assertOnePendingReview(proposal, self.reviewer)
def test_default_reviewer_when_owner(self):
"""If the target branch has a no default reviewer set, the branch
owner should be assigned as the reviewer for the merge proposal.
"""
proposal = self.source.addLandingTarget(
self.user, self.target)
self.assertOnePendingReview(proposal, self.source.owner)
def test_attributeAssignment(self):
"""Smoke test to make sure the assignments are there."""
commit_message = u'Some commit message'
proposal = self.source.addLandingTarget(
self.user, self.target, self.prerequisite,
commit_message=commit_message)
self.assertEqual(proposal.registrant, self.user)
self.assertEqual(proposal.source_branch, self.source)
self.assertEqual(proposal.target_branch, self.target)
self.assertEqual(proposal.prerequisite_branch, self.prerequisite)
self.assertEqual(proposal.commit_message, commit_message)
def test__createMergeProposal_with_reviewers(self):
person1 = self.factory.makePerson()
person2 = self.factory.makePerson()
e = self.assertRaises(ValueError,
self.source._createMergeProposal, self.user, self.target,
reviewers=[person1, person2])
self.assertEqual(
'reviewers and review_types must be equal length.', str(e))
e = self.assertRaises(ValueError,
self.source._createMergeProposal, self.user, self.target,
reviewers=[person1, person2], review_types=['review1'])
self.assertEqual(
'reviewers and review_types must be equal length.', str(e))
bmp = self.source._createMergeProposal(
self.user, self.target, reviewers=[person1, person2],
review_types=['review1', 'review2'])
votes = set((vote.reviewer, vote.review_type) for vote in bmp.votes)
self.assertEqual(
set([(person1, 'review1'), (person2, 'review2')]), votes)
class TestLandingCandidates(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_private_branch(self):
"""landing_candidates works for private branches."""
branch = self.factory.makeBranch(private=True)
with person_logged_in(removeSecurityProxy(branch).owner):
mp = self.factory.makeBranchMergeProposal(target_branch=branch)
self.assertContentEqual([mp], branch.landing_candidates)
class BranchDateLastModified(TestCaseWithFactory):
"""Exercies the situations where date_last_modifed is udpated."""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self, 'test@canonical.com')
def test_initialValue(self):
"""Initially the date_last_modifed is the date_created."""
branch = self.factory.makeAnyBranch()
self.assertEqual(branch.date_last_modified, branch.date_created)
def test_bugBranchLinkUpdates(self):
"""Linking a branch to a bug updates the last modified time."""
date_created = datetime(2000, 1, 1, 12, tzinfo=UTC)
branch = self.factory.makeAnyBranch(date_created=date_created)
self.assertEqual(branch.date_last_modified, date_created)
params = CreateBugParams(
owner=branch.owner, title='A bug', comment='blah')
params.setBugTarget(product=branch.product)
bug = getUtility(IBugSet).createBug(params)
bug.linkBranch(branch, branch.owner)
self.assertTrue(branch.date_last_modified > date_created,
"Date last modified was not updated.")
def test_updateScannedDetails_with_null_revision(self):
# If updateScannedDetails is called with a null revision, it
# effectively means that there is an empty branch, so we can't use the
# revision date, so we set the last modified time to UTC_NOW.
date_created = datetime(2000, 1, 1, 12, tzinfo=UTC)
branch = self.factory.makeAnyBranch(date_created=date_created)
branch.updateScannedDetails(None, 0)
self.assertSqlAttributeEqualsDate(
branch, 'date_last_modified', UTC_NOW)
def test_updateScannedDetails_with_revision(self):
# If updateScannedDetails is called with a revision with which has a
# revision date set in the past (the usual case), the last modified
# time of the branch is set to be the date from the Bazaar revision
# (Revision.revision_date).
date_created = datetime(2000, 1, 1, 12, tzinfo=UTC)
branch = self.factory.makeAnyBranch(date_created=date_created)
revision_date = datetime(2005, 2, 2, 12, tzinfo=UTC)
revision = self.factory.makeRevision(revision_date=revision_date)
branch.updateScannedDetails(revision, 1)
self.assertEqual(revision_date, branch.date_last_modified)
def test_updateScannedDetails_with_future_revision(self):
# If updateScannedDetails is called with a revision with which has a
# revision date set in the future, UTC_NOW is used as the last modifed
# time. date_created = datetime(2000, 1, 1, 12, tzinfo=UTC)
date_created = datetime(2000, 1, 1, 12, tzinfo=UTC)
branch = self.factory.makeAnyBranch(date_created=date_created)
revision_date = datetime.now(UTC) + timedelta(days=1000)
revision = self.factory.makeRevision(revision_date=revision_date)
branch.updateScannedDetails(revision, 1)
self.assertSqlAttributeEqualsDate(
branch, 'date_last_modified', UTC_NOW)
class TestBranchLifecycleStatus(TestCaseWithFactory):
"""Exercises changes in lifecycle status."""
layer = DatabaseFunctionalLayer
def checkStatusAfterUpdate(self, initial_state, expected_state):
# Make sure that the lifecycle status of the branch with the initial
# lifecycle state to be the expected_state after a revision has been
# scanned.
branch = self.factory.makeAnyBranch(lifecycle_status=initial_state)
revision = self.factory.makeRevision()
branch.updateScannedDetails(revision, 1)
self.assertEqual(expected_state, branch.lifecycle_status)
def test_updateScannedDetails_active_branch(self):
# If a new revision is scanned, and the branch is in an active state,
# then the lifecycle status isn't changed.
for state in DEFAULT_BRANCH_STATUS_IN_LISTING:
self.checkStatusAfterUpdate(state, state)
def test_updateScannedDetails_inactive_branch(self):
# If a branch is inactive (merged or abandonded) and a new revision is
# scanned, the branch is moved to the development state.
for state in (BranchLifecycleStatus.MERGED,
BranchLifecycleStatus.ABANDONED):
self.checkStatusAfterUpdate(
state, BranchLifecycleStatus.DEVELOPMENT)
class TestCreateBranchRevisionFromIDs(TestCaseWithFactory):
"""Tests for `Branch.createBranchRevisionFromIDs`."""
layer = DatabaseFunctionalLayer
def test_simple(self):
# createBranchRevisionFromIDs when passed a single revid, sequence
# pair, creates the appropriate BranchRevision object.
branch = self.factory.makeAnyBranch()
rev = self.factory.makeRevision()
revision_number = self.factory.getUniqueInteger()
branch.createBranchRevisionFromIDs(
[(rev.revision_id, revision_number)])
branch_revision = branch.getBranchRevision(revision=rev)
self.assertEqual(revision_number, branch_revision.sequence)
def test_multiple(self):
# createBranchRevisionFromIDs when passed multiple revid, sequence
# pairs, creates the appropriate BranchRevision objects.
branch = self.factory.makeAnyBranch()
revision_to_number = {}
revision_id_sequence_pairs = []
for i in range(10):
rev = self.factory.makeRevision()
revision_number = self.factory.getUniqueInteger()
revision_to_number[rev] = revision_number
revision_id_sequence_pairs.append(
(rev.revision_id, revision_number))
branch.createBranchRevisionFromIDs(revision_id_sequence_pairs)
for rev in revision_to_number:
branch_revision = branch.getBranchRevision(revision=rev)
self.assertEqual(
revision_to_number[rev], branch_revision.sequence)
def test_empty(self):
# createBranchRevisionFromIDs does not fail when passed no pairs.
branch = self.factory.makeAnyBranch()
branch.createBranchRevisionFromIDs([])
def test_call_twice_in_one_transaction(self):
# createBranchRevisionFromIDs creates temporary tables, but cleans
# after itself so that it can safely be called twice in one
# transaction.
branch = self.factory.makeAnyBranch()
rev = self.factory.makeRevision()
revision_number = self.factory.getUniqueInteger()
branch.createBranchRevisionFromIDs(
[(rev.revision_id, revision_number)])
rev = self.factory.makeRevision()
revision_number = self.factory.getUniqueInteger()
# This is just "assertNotRaises"
branch.createBranchRevisionFromIDs(
[(rev.revision_id, revision_number)])
class TestRevisionHistory(TestCaseWithFactory):
"""Tests for a branch's revision history."""
layer = DatabaseFunctionalLayer
def test_revision_count(self):
# A branch's revision count is equal to the number of revisions that
# are associated with it.
branch = self.factory.makeBranch()
some_number = 6
self.factory.makeRevisionsForBranch(branch, count=some_number)
self.assertEqual(some_number, branch.revision_count)
def test_revision_history_matches_count(self):
branch = self.factory.makeBranch()
some_number = 3
self.factory.makeRevisionsForBranch(branch, count=some_number)
self.assertEqual(
branch.revision_count, branch.revision_history.count())
def test_revision_history_is_made_of_revisions(self):
# Branch.revision_history contains IBranchRevision objects.
branch = self.factory.makeBranch()
some_number = 6
self.factory.makeRevisionsForBranch(branch, count=some_number)
for branch_revision in branch.revision_history:
self.assertProvides(branch_revision, IBranchRevision)
def test_continuous_sequence_numbers(self):
# The revisions in the revision history have sequence numbers which
# start from 1 at the oldest and don't have any gaps.
branch = self.factory.makeBranch()
some_number = 4
self.factory.makeRevisionsForBranch(branch, count=some_number)
self.assertEqual(
[4, 3, 2, 1], [br.sequence for br in branch.revision_history])
def test_most_recent_first(self):
# The revisions in the revision history start with the most recent
# first.
branch = self.factory.makeBranch()
some_number = 4
self.factory.makeRevisionsForBranch(branch, count=some_number)
revision_history = list(branch.revision_history)
sorted_by_date = sorted(
revision_history, key=lambda x: x.revision.revision_date,
reverse=True)
self.assertEqual(sorted_by_date, revision_history)
def test_latest_revisions(self):
# IBranch.latest_revisions gives only the latest revisions.
branch = self.factory.makeBranch()
some_number = 7
self.factory.makeRevisionsForBranch(branch, count=some_number)
smaller_number = some_number / 2
self.assertEqual(
list(branch.revision_history[:smaller_number]),
list(branch.latest_revisions(smaller_number)))
def test_getRevisionsSince(self):
# IBranch.getRevisionsSince gives all the BranchRevisions for
# revisions committed since a given timestamp.
branch = self.factory.makeBranch()
some_number = 7
self.factory.makeRevisionsForBranch(branch, count=some_number)
mid_sequence = some_number - 2
revisions = list(branch.revision_history)
mid_revision = revisions[mid_sequence]
since = branch.getRevisionsSince(mid_revision.revision.revision_date)
self.assertEqual(revisions[:mid_sequence], list(since))
def test_top_ancestor_has_no_parents(self):
# The top-most revision of a branch (i.e. the first one) has no
# parents.
branch = self.factory.makeBranch()
self.factory.makeRevisionsForBranch(branch, count=1)
revision = list(branch.revision_history)[0].revision
self.assertEqual([], revision.parent_ids)
def test_non_first_revisions_have_parents(self):
# Revisions which are not the first revision of the branch have
# parent_ids. When there are no merges present, there is only one
# parent which is the previous revision.
branch = self.factory.makeBranch()
some_number = 5
self.factory.makeRevisionsForBranch(branch, count=some_number)
revisions = list(branch.revision_history)
last_rev = revisions[0].revision
second_last_rev = revisions[1].revision
self.assertEqual(last_rev.parent_ids, [second_last_rev.revision_id])
def test_tip_revision_when_no_bazaar_data(self):
# When a branch has no revisions and no Bazaar data at all, its tip
# revision is None and its last_scanned_id is None.
branch = self.factory.makeBranch()
self.assertIs(None, branch.last_scanned_id)
self.assertIs(None, branch.getTipRevision())
def test_tip_revision_when_no_revisions(self):
# When a branch has no revisions but does have Bazaar data, its tip
# revision is None and its last_scanned_id is
# bzrlib.revision.NULL_REVISION.
branch = self.factory.makeBranch()
branch.updateScannedDetails(None, 0)
self.assertEqual(NULL_REVISION, branch.last_scanned_id)
self.assertIs(None, branch.getTipRevision())
def test_tip_revision_is_updated(self):
branch = self.factory.makeBranch()
revision = self.factory.makeRevision()
branch.updateScannedDetails(revision, 1)
self.assertEqual(revision.revision_id, branch.last_scanned_id)
self.assertEqual(revision, branch.getTipRevision())
class TestCodebrowse(TestCaseWithFactory):
"""Tests for branch codebrowse support."""
layer = DatabaseFunctionalLayer
def test_simple(self):
# The basic codebrowse URL for a public branch is a 'http' url.
branch = self.factory.makeAnyBranch()
self.assertEqual(
'http://bazaar.launchpad.dev/' + branch.unique_name,
branch.codebrowse_url())
def test_private(self):
# The codebrowse URL for a private branch is a 'https' url.
owner = self.factory.makePerson()
branch = self.factory.makeAnyBranch(private=True, owner=owner)
login_person(owner)
self.assertEqual(
'https://bazaar.launchpad.dev/' + branch.unique_name,
branch.codebrowse_url())
def test_extra_args(self):
# Any arguments to codebrowse_url are appended to the URL.
branch = self.factory.makeAnyBranch()
self.assertEqual(
'http://bazaar.launchpad.dev/' + branch.unique_name + '/a/b',
branch.codebrowse_url('a', 'b'))
def test_source_code_url(self):
# The source code URL points to the codebrowse URL where you can
# actually browse the source code.
branch = self.factory.makeAnyBranch()
self.assertEqual(
branch.browse_source_url, branch.codebrowse_url('files'))
def test_no_revisions_not_browseable(self):
# A branch with no revisions is not browseable.
branch = self.factory.makeBranch()
self.assertFalse(branch.code_is_browseable)
def test_revisions_means_browseable(self):
# A branch that has revisions is browseable.
branch = self.factory.makeBranch()
self.factory.makeRevisionsForBranch(branch, count=5)
self.assertTrue(branch.code_is_browseable)
class TestBranchNamespace(TestCaseWithFactory):
"""Tests for `IBranch.namespace`."""
layer = DatabaseFunctionalLayer
def assertNamespaceEqual(self, namespace_one, namespace_two):
"""Assert that `namespace_one` equals `namespace_two`."""
namespace_one = removeSecurityProxy(namespace_one)
namespace_two = removeSecurityProxy(namespace_two)
self.assertEqual(namespace_one.__class__, namespace_two.__class__)
self.assertEqual(namespace_one.owner, namespace_two.owner)
self.assertEqual(
getattr(namespace_one, 'sourcepackage', None),
getattr(namespace_two, 'sourcepackage', None))
self.assertEqual(
getattr(namespace_one, 'product', None),
getattr(namespace_two, 'product', None))
def test_namespace_personal(self):
# The namespace attribute of a personal branch points to the namespace
# that corresponds to ~owner/+junk.
branch = self.factory.makePersonalBranch()
namespace = getUtility(IBranchNamespaceSet).get(person=branch.owner)
self.assertNamespaceEqual(namespace, branch.namespace)
def test_namespace_package(self):
# The namespace attribute of a package branch points to the namespace
# that corresponds to
# ~owner/distribution/distroseries/sourcepackagename.
branch = self.factory.makePackageBranch()
namespace = getUtility(IBranchNamespaceSet).get(
person=branch.owner, distroseries=branch.distroseries,
sourcepackagename=branch.sourcepackagename)
self.assertNamespaceEqual(namespace, branch.namespace)
def test_namespace_product(self):
# The namespace attribute of a product branch points to the namespace
# that corresponds to ~owner/product.
branch = self.factory.makeProductBranch()
namespace = getUtility(IBranchNamespaceSet).get(
person=branch.owner, product=branch.product)
self.assertNamespaceEqual(namespace, branch.namespace)
class TestPendingWrites(TestCaseWithFactory):
"""Are there changes to this branch not reflected in the database?"""
layer = DatabaseFunctionalLayer
def test_new_branch_no_writes(self):
# New branches have no pending writes.
branch = self.factory.makeAnyBranch()
self.assertEqual(False, branch.pending_writes)
def test_branchChanged_for_hosted(self):
# If branchChanged has been called with a new tip revision id, there
# are pending writes.
branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED)
with person_logged_in(branch.owner):
branch.branchChanged('', 'new-tip', None, None, None)
self.assertEqual(True, branch.pending_writes)
def test_requestMirror_for_imported(self):
# If an imported branch has a requested mirror, then we've just
# imported new changes. Therefore, pending writes.
branch = self.factory.makeAnyBranch(branch_type=BranchType.IMPORTED)
branch.requestMirror()
self.assertEqual(True, branch.pending_writes)
def test_requestMirror_for_mirrored(self):
# Mirrored branches *always* have a requested mirror. The fact that a
# mirror is requested has no bearing on whether there are pending
# writes. Thus, pending_writes is False.
branch = self.factory.makeAnyBranch(branch_type=BranchType.MIRRORED)
branch.requestMirror()
self.assertEqual(False, branch.pending_writes)
def test_pulled_but_not_scanned(self):
# If a branch has been pulled (mirrored) but not scanned, then we have
# yet to load the revisions into the database. This means there are
# pending writes.
branch = self.factory.makeAnyBranch(branch_type=BranchType.MIRRORED)
branch.startMirroring()
rev_id = self.factory.getUniqueString('rev-id')
removeSecurityProxy(branch).branchChanged(
'', rev_id, None, None, None)
self.assertEqual(True, branch.pending_writes)
def test_pulled_and_scanned(self):
# If a branch has been pulled and scanned, then there are no pending
# writes.
branch = self.factory.makeAnyBranch(branch_type=BranchType.MIRRORED)
branch.startMirroring()
rev_id = self.factory.getUniqueString('rev-id')
removeSecurityProxy(branch).branchChanged(
'', rev_id, None, None, None)
# Cheat! The actual API for marking a branch as scanned is
# updateScannedDetails. That requires a revision in the database
# though.
removeSecurityProxy(branch).last_scanned_id = rev_id
self.assertEqual(False, branch.pending_writes)
def test_first_mirror_started(self):
# If we have started mirroring the branch for the first time, then
# there are probably pending writes.
branch = self.factory.makeAnyBranch(branch_type=BranchType.MIRRORED)
branch.startMirroring()
self.assertEqual(True, branch.pending_writes)
def test_following_mirror_started(self):
# If we have started mirroring the branch, then there are probably
# pending writes.
branch = self.factory.makeAnyBranch(branch_type=BranchType.MIRRORED)
branch.startMirroring()
rev_id = self.factory.getUniqueString('rev-id')
removeSecurityProxy(branch).branchChanged(
'', rev_id, None, None, None)
# Cheat! The actual API for marking a branch as scanned is
# updateScannedDetails. That requires a revision in the database
# though.
removeSecurityProxy(branch).last_scanned_id = rev_id
# Cheat again! We can only tell if mirroring has started if the last
# mirrored attempt is different from the last mirrored time. To ensure
# this, we start the second mirror in a new transaction.
transaction.commit()
branch.startMirroring()
self.assertEqual(True, branch.pending_writes)
class TestBranchPrivacy(TestCaseWithFactory):
"""Tests for branch privacy."""
layer = DatabaseFunctionalLayer
def setUp(self):
# Use an admin user as we aren't checking edit permissions here.
TestCaseWithFactory.setUp(self, 'admin@canonical.com')
def test_public_stacked_on_private_is_private(self):
# A public branch stacked on a private branch is private.
stacked_on = self.factory.makeBranch(private=True)
branch = self.factory.makeBranch(stacked_on=stacked_on, private=False)
self.assertTrue(branch.private)
self.assertFalse(branch.explicitly_private)
def test_private_stacked_on_public_is_private(self):
# A public branch stacked on a private branch is private.
stacked_on = self.factory.makeBranch(private=False)
branch = self.factory.makeBranch(stacked_on=stacked_on, private=True)
self.assertTrue(branch.private)
self.assertTrue(branch.explicitly_private)
class TestBranchSetPrivate(TestCaseWithFactory):
"""Test IBranch.setPrivate."""
layer = DatabaseFunctionalLayer
def setUp(self):
# Use an admin user as we aren't checking edit permissions here.
TestCaseWithFactory.setUp(self, 'admin@canonical.com')
def test_public_to_public(self):
# Setting a public branch to be public is a no-op.
branch = self.factory.makeProductBranch()
self.assertFalse(branch.private)
branch.setPrivate(False, branch.owner)
self.assertFalse(branch.private)
def test_public_to_private_allowed(self):
# If there is a privacy policy allowing the branch owner to have
# private branches, then setting the branch private is allowed.
branch = self.factory.makeProductBranch()
branch.product.setBranchVisibilityTeamPolicy(
branch.owner, BranchVisibilityRule.PRIVATE)
branch.setPrivate(True, branch.owner)
self.assertTrue(branch.private)
def test_public_to_private_not_allowed(self):
# If there are no privacy policies allowing private branches, then
# BranchCannotBePrivate is rasied.
branch = self.factory.makeProductBranch()
self.assertRaises(
BranchCannotBePrivate,
branch.setPrivate,
True, branch.owner)
def test_public_to_private_for_admins(self):
# Admins can override the default behaviour and make any public branch
# private.
branch = self.factory.makeProductBranch()
# Grab a random admin, the teamowner is good enough here.
admins = getUtility(ILaunchpadCelebrities).admin
branch.setPrivate(True, admins.teamowner)
self.assertTrue(branch.private)
def test_private_to_private(self):
# Setting a private branch to be private is a no-op.
branch = self.factory.makeProductBranch(private=True)
self.assertTrue(branch.private)
branch.setPrivate(True, branch.owner)
self.assertTrue(branch.private)
def test_private_to_public_allowed(self):
# If the namespace policy allows public branches, then changing from
# private to public is allowed.
branch = self.factory.makeProductBranch(private=True)
branch.setPrivate(False, branch.owner)
self.assertFalse(branch.private)
def test_private_to_public_not_allowed(self):
# If the namespace policy does not allow public branches, attempting
# to change the branch to be public raises BranchCannotBePublic.
branch = self.factory.makeProductBranch(private=True)
branch.product.setBranchVisibilityTeamPolicy(
None, BranchVisibilityRule.FORBIDDEN)
branch.product.setBranchVisibilityTeamPolicy(
branch.owner, BranchVisibilityRule.PRIVATE_ONLY)
self.assertRaises(
BranchCannotBePublic,
branch.setPrivate,
False, branch.owner)
class TestBranchCommitsForDays(TestCaseWithFactory):
"""Tests for `Branch.commitsForDays`."""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
# Use a 30 day epoch for the tests.
self.epoch = datetime.now(tz=UTC) - timedelta(days=30)
def date_generator(self, epoch_offset, delta=None):
if delta is None:
delta = timedelta(days=1)
return time_counter(self.epoch + timedelta(days=epoch_offset), delta)
def test_empty_branch(self):
# A branch with no commits returns an empty list.
branch = self.factory.makeAnyBranch()
self.assertEqual([], branch.commitsForDays(self.epoch))
def test_commits_before_epoch_not_returned(self):
# Commits that occur before the epoch are not returned.
branch = self.factory.makeAnyBranch()
self.factory.makeRevisionsForBranch(
branch, date_generator=self.date_generator(-10))
self.assertEqual([], branch.commitsForDays(self.epoch))
def test_commits_after_epoch_are_returned(self):
# Commits that occur after the epoch are returned.
branch = self.factory.makeAnyBranch()
self.factory.makeRevisionsForBranch(
branch, count=5, date_generator=self.date_generator(1))
# There is one commit for each day starting from epoch + 1.
start = self.epoch + timedelta(days=1)
# Clear off the fractional parts of the day.
start = datetime(start.year, start.month, start.day)
commits = []
for count in range(5):
commits.append((start + timedelta(days=count), 1))
self.assertEqual(commits, branch.commitsForDays(self.epoch))
def test_commits_are_grouped(self):
# The commits are grouped to give counts of commits for the days.
branch = self.factory.makeAnyBranch()
start = self.epoch + timedelta(days=1)
# Add 8 commits starting from 5pm (+ whatever minutes).
# 5, 7, 9, 11pm, then 1, 3, 5, 7am for the following day.
start = start.replace(hour=17)
date_generator = time_counter(start, timedelta(hours=2))
self.factory.makeRevisionsForBranch(
branch, count=8, date_generator=date_generator)
# The resulting queries return time zone unaware times.
first_day = datetime(start.year, start.month, start.day)
commits = [(first_day, 4), (first_day + timedelta(days=1), 4)]
self.assertEqual(commits, branch.commitsForDays(self.epoch))
def test_non_mainline_commits_count(self):
# Non-mainline commits are counted too.
branch = self.factory.makeAnyBranch()
start = self.epoch + timedelta(days=1)
revision = self.factory.makeRevision(revision_date=start)
branch.createBranchRevision(None, revision)
day = datetime(start.year, start.month, start.day)
commits = [(day, 1)]
self.assertEqual(commits, branch.commitsForDays(self.epoch))
class TestBranchBugLinks(TestCaseWithFactory):
"""Tests for bug linkages in `Branch`"""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.user = self.factory.makePerson()
login_person(self.user)
def test_bug_link(self):
# Branches can be linked to bugs through the Branch interface.
branch = self.factory.makeAnyBranch()
bug = self.factory.makeBug()
branch.linkBug(bug, self.user)
self.assertEqual(branch.linked_bugs.count(), 1)
linked_bug = branch.linked_bugs[0]
self.assertEqual(linked_bug.id, bug.id)
def test_bug_unlink(self):
# Branches can be unlinked from the bug as well.
branch = self.factory.makeAnyBranch()
bug = self.factory.makeBug()
branch.linkBug(bug, self.user)
self.assertEqual(branch.linked_bugs.count(), 1)
branch.unlinkBug(bug, self.user)
self.assertEqual(branch.linked_bugs.count(), 0)
class TestBranchSpecLinks(TestCaseWithFactory):
"""Tests for bug linkages in `Branch`"""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.user = self.factory.makePerson()
def test_spec_link(self):
# Branches can be linked to specs through the Branch interface.
branch = self.factory.makeAnyBranch()
spec = self.factory.makeSpecification()
branch.linkSpecification(spec, self.user)
self.assertEqual(branch.spec_links.count(), 1)
spec_branch = branch.spec_links[0]
self.assertEqual(spec_branch.specification.id, spec.id)
self.assertEqual(spec_branch.branch.id, branch.id)
def test_spec_unlink(self):
# Branches can be unlinked from the spec as well.
branch = self.factory.makeAnyBranch()
spec = self.factory.makeSpecification()
branch.linkSpecification(spec, self.user)
self.assertEqual(branch.spec_links.count(), 1)
branch.unlinkSpecification(spec, self.user)
self.assertEqual(branch.spec_links.count(), 0)
class TestBranchIsPersonTrustedReviewer(TestCaseWithFactory):
"""Test the `IBranch.isPersonTrustedReviewer` method."""
layer = DatabaseFunctionalLayer
def assertTrustedReviewer(self, branch, person):
"""Assert that `person` is a trusted reviewer for the `branch`."""
self.assertTrue(branch.isPersonTrustedReviewer(person))
def assertNotTrustedReviewer(self, branch, person):
"""Assert that `person` is not a trusted reviewer for the `branch`."""
self.assertFalse(branch.isPersonTrustedReviewer(person))
def test_none_is_not_trusted(self):
# If None is passed in as the person, the method returns false.
branch = self.factory.makeAnyBranch()
self.assertNotTrustedReviewer(branch, None)
def test_branch_owner_is_trusted(self):
# The branch owner is a trusted reviewer.
branch = self.factory.makeAnyBranch()
self.assertTrustedReviewer(branch, branch.owner)
def test_non_branch_owner_is_not_trusted(self):
# Someone other than the branch owner is not a trusted reviewer.
branch = self.factory.makeAnyBranch()
reviewer = self.factory.makePerson()
self.assertNotTrustedReviewer(branch, reviewer)
def test_lp_admins_always_trusted(self):
# Launchpad admins are special, and as such, are trusted.
branch = self.factory.makeAnyBranch()
admins = getUtility(ILaunchpadCelebrities).admin
# Grab a random admin, the teamowner is good enough here.
self.assertTrustedReviewer(branch, admins.teamowner)
def test_member_of_team_owned_branch(self):
# If the branch is owned by a team, any team member is a trusted
# reviewer.
team = self.factory.makeTeam()
branch = self.factory.makeAnyBranch(owner=team)
self.assertTrustedReviewer(branch, team.teamowner)
def test_review_team_member_is_trusted(self):
# If the reviewer is a member of the review team, but not the owner
# they are still trusted.
team = self.factory.makeTeam()
branch = self.factory.makeAnyBranch(reviewer=team)
self.assertTrustedReviewer(branch, team.teamowner)
def test_branch_owner_not_review_team_member_is_trusted(self):
# If the owner of the branch is not in the review team, they are still
# trusted.
team = self.factory.makeTeam()
branch = self.factory.makeAnyBranch(reviewer=team)
self.assertFalse(branch.owner.inTeam(team))
self.assertTrustedReviewer(branch, branch.owner)
def test_community_reviewer(self):
# If the reviewer is not a member of the owner, or the review team,
# they are not trusted reviewers.
team = self.factory.makeTeam()
branch = self.factory.makeAnyBranch(reviewer=team)
reviewer = self.factory.makePerson()
self.assertNotTrustedReviewer(branch, reviewer)
class TestBranchSetOwner(TestCaseWithFactory):
"""Tests for IBranch.setOwner."""
layer = DatabaseFunctionalLayer
def test_owner_sets_team(self):
# The owner of the branch can set the owner of the branch to be a team
# they are a member of.
branch = self.factory.makeAnyBranch()
team = self.factory.makeTeam(owner=branch.owner)
login_person(branch.owner)
branch.setOwner(team, branch.owner)
self.assertEqual(team, branch.owner)
def test_owner_cannot_set_nonmember_team(self):
# The owner of the branch cannot set the owner to be a team they are
# not a member of.
branch = self.factory.makeAnyBranch()
team = self.factory.makeTeam()
login_person(branch.owner)
self.assertRaises(
BranchCreatorNotMemberOfOwnerTeam,
branch.setOwner,
team, branch.owner)
def test_owner_cannot_set_other_user(self):
# The owner of the branch cannot set the new owner to be another
# person.
branch = self.factory.makeAnyBranch()
person = self.factory.makePerson()
login_person(branch.owner)
self.assertRaises(
BranchCreatorNotOwner,
branch.setOwner,
person, branch.owner)
def test_admin_can_set_any_team_or_person(self):
# A Launchpad admin can set the branch to be owned by any team or
# person.
branch = self.factory.makeAnyBranch()
team = self.factory.makeTeam()
# To get a random administrator, choose the admin team owner.
admin = getUtility(ILaunchpadCelebrities).admin.teamowner
login_person(admin)
branch.setOwner(team, admin)
self.assertEqual(team, branch.owner)
person = self.factory.makePerson()
branch.setOwner(person, admin)
self.assertEqual(person, branch.owner)
class TestBranchSetTarget(TestCaseWithFactory):
"""Tests for IBranch.setTarget."""
layer = DatabaseFunctionalLayer
def test_not_both_project_and_source_package(self):
# Only one of project or source_package can be passed in, not both.
branch = self.factory.makePersonalBranch()
project = self.factory.makeProduct()
source_package = self.factory.makeSourcePackage()
login_person(branch.owner)
self.assertRaises(
BranchTargetError,
branch.setTarget,
user=branch.owner, project=project, source_package=source_package)
def test_junk_branch_to_project_branch(self):
# A junk branch can be moved to a project.
branch = self.factory.makePersonalBranch()
project = self.factory.makeProduct()
login_person(branch.owner)
branch.setTarget(user=branch.owner, project=project)
self.assertEqual(project, branch.target.context)
def test_junk_branch_to_package_branch(self):
# A junk branch can be moved to a source package.
branch = self.factory.makePersonalBranch()
source_package = self.factory.makeSourcePackage()
login_person(branch.owner)
branch.setTarget(user=branch.owner, source_package=source_package)
self.assertEqual(source_package, branch.target.context)
def test_project_branch_to_other_project_branch(self):
# Move a branch from one project to another.
branch = self.factory.makeProductBranch()
project = self.factory.makeProduct()
login_person(branch.owner)
branch.setTarget(user=branch.owner, project=project)
self.assertEqual(project, branch.target.context)
def test_project_branch_to_package_branch(self):
# Move a branch from a project to a package.
branch = self.factory.makeProductBranch()
source_package = self.factory.makeSourcePackage()
login_person(branch.owner)
branch.setTarget(user=branch.owner, source_package=source_package)
self.assertEqual(source_package, branch.target.context)
def test_project_branch_to_junk_branch(self):
# Move a branch from a project to junk.
branch = self.factory.makeProductBranch()
login_person(branch.owner)
branch.setTarget(user=branch.owner)
self.assertEqual(branch.owner, branch.target.context)
def test_package_branch_to_other_package_branch(self):
# Move a branch from one package to another.
branch = self.factory.makePackageBranch()
source_package = self.factory.makeSourcePackage()
login_person(branch.owner)
branch.setTarget(user=branch.owner, source_package=source_package)
self.assertEqual(source_package, branch.target.context)
def test_package_branch_to_project_branch(self):
# Move a branch from a package to a project.
branch = self.factory.makePackageBranch()
project = self.factory.makeProduct()
login_person(branch.owner)
branch.setTarget(user=branch.owner, project=project)
self.assertEqual(project, branch.target.context)
def test_package_branch_to_junk_branch(self):
# Move a branch from a package to junk.
branch = self.factory.makePackageBranch()
login_person(branch.owner)
branch.setTarget(user=branch.owner)
self.assertEqual(branch.owner, branch.target.context)
class TestScheduleDiffUpdates(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_scheduleDiffUpdates(self):
"""Create jobs for all merge proposals."""
bmp1 = self.factory.makeBranchMergeProposal()
bmp2 = self.factory.makeBranchMergeProposal(
source_branch=bmp1.source_branch)
removeSecurityProxy(bmp1).target_branch.last_scanned_id = 'rev1'
removeSecurityProxy(bmp2).target_branch.last_scanned_id = 'rev2'
jobs = bmp1.source_branch.scheduleDiffUpdates()
self.assertEqual(2, len(jobs))
bmps_to_update = set(
removeSecurityProxy(job).branch_merge_proposal for job in jobs)
self.assertEqual(set([bmp1, bmp2]), bmps_to_update)
def test_scheduleDiffUpdates_ignores_final(self):
"""Diffs for proposals in final states aren't updated."""
source_branch = self.factory.makeBranch()
for state in FINAL_STATES:
bmp = self.factory.makeBranchMergeProposal(
source_branch=source_branch, set_state=state)
removeSecurityProxy(bmp).target_branch.last_scanned_id = 'rev'
# Creating a superseded proposal has the side effect of creating a
# second proposal. Delete the second proposal.
for bmp in source_branch.landing_targets:
if bmp.queue_status not in FINAL_STATES:
removeSecurityProxy(bmp).deleteProposal()
jobs = source_branch.scheduleDiffUpdates()
self.assertEqual(0, len(jobs))
def test_scheduleDiffUpdates_ignores_unpushed_target(self):
"""Diffs aren't updated if target has no revisions."""
bmp = self.factory.makeBranchMergeProposal()
jobs = bmp.source_branch.scheduleDiffUpdates()
self.assertEqual(0, len(jobs))
class TestBranchGetMainlineBranchRevisions(TestCaseWithFactory):
"""Tests for Branch.getMainlineBranchRevisions."""
layer = DatabaseFunctionalLayer
def test_start_date(self):
# Revisions created before the start date are not returned.
branch = self.factory.makeAnyBranch()
epoch = datetime(2009, 9, 10, tzinfo=UTC)
# Add some revisions before the epoch.
add_revision_to_branch(
self.factory, branch, epoch - timedelta(days=1))
new = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=1))
result = branch.getMainlineBranchRevisions(epoch)
branch_revisions = [br for br, rev in result]
self.assertEqual([new], branch_revisions)
def test_end_date(self):
# Revisions created after the end date are not returned.
branch = self.factory.makeAnyBranch()
epoch = datetime(2009, 9, 10, tzinfo=UTC)
end_date = epoch + timedelta(days=2)
in_range = add_revision_to_branch(
self.factory, branch, end_date - timedelta(days=1))
# Add some revisions after the end_date.
add_revision_to_branch(
self.factory, branch, end_date + timedelta(days=1))
result = branch.getMainlineBranchRevisions(epoch, end_date)
branch_revisions = [br for br, rev in result]
self.assertEqual([in_range], branch_revisions)
def test_newest_first(self):
# If oldest_first is False, the newest are returned first.
branch = self.factory.makeAnyBranch()
epoch = datetime(2009, 9, 10, tzinfo=UTC)
old = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=1))
new = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=2))
result = branch.getMainlineBranchRevisions(epoch, oldest_first=False)
branch_revisions = [br for br, rev in result]
self.assertEqual([new, old], branch_revisions)
def test_oldest_first(self):
# If oldest_first is True, the oldest are returned first.
branch = self.factory.makeAnyBranch()
epoch = datetime(2009, 9, 10, tzinfo=UTC)
old = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=1))
new = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=2))
result = branch.getMainlineBranchRevisions(epoch, oldest_first=True)
branch_revisions = [br for br, rev in result]
self.assertEqual([old, new], branch_revisions)
def test_only_mainline_revisions(self):
# Only mainline revisions are returned.
branch = self.factory.makeAnyBranch()
epoch = datetime(2009, 9, 10, tzinfo=UTC)
old = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=1))
# Add some non mainline revision.
add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=2), mainline=False)
new = add_revision_to_branch(
self.factory, branch, epoch + timedelta(days=3))
result = branch.getMainlineBranchRevisions(epoch)
branch_revisions = [br for br, rev in result]
self.assertEqual([new, old], branch_revisions)
class TestGetBzrBranch(TestCaseWithFactory):
"""Tests for `IBranch.getBzrBranch`."""
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.useBzrBranches(direct_database=True)
def test_simple(self):
# safe_open returns the underlying bzr branch of a database branch in
# the simple, unstacked, case.
db_branch, tree = self.create_branch_and_tree()
# XXX: AaronBentley 2010-08-06 bug=614404: a bzr username is
# required to generate the revision-id.
with override_environ(BZR_EMAIL='me@example.com'):
revid = tree.commit('')
bzr_branch = db_branch.getBzrBranch()
self.assertEqual(revid, bzr_branch.last_revision())
def test_acceptable_stacking(self):
# If the underlying bzr branch of a database branch is stacked on
# another launchpad branch safe_open returns it.
db_stacked_on, stacked_on_tree = self.create_branch_and_tree()
db_stacked, stacked_tree = self.create_branch_and_tree()
stacked_tree.branch.set_stacked_on_url(
'/' + db_stacked_on.unique_name)
bzr_branch = db_stacked.getBzrBranch()
self.assertEqual(
'/' + db_stacked_on.unique_name, bzr_branch.get_stacked_on_url())
def test_unacceptable_stacking(self):
# If the underlying bzr branch of a database branch is stacked on
# a non-Launchpad url, it cannot be opened.
branch = BzrDir.create_branch_convenience('local')
db_stacked, stacked_tree = self.create_branch_and_tree()
stacked_tree.branch.set_stacked_on_url(branch.base)
self.assertRaises(BadUrl, db_stacked.getBzrBranch)
class TestMergeQueue(TestCaseWithFactory):
"""Tests for branch merge queue functionality in branches."""
layer = DatabaseFunctionalLayer
def test_addToQueue(self):
"""Test Branch.addToQueue."""
branch = self.factory.makeBranch()
queue = self.factory.makeBranchMergeQueue()
with person_logged_in(branch.owner):
branch.addToQueue(queue)
self.assertEqual(branch.merge_queue, queue)
def test_setMergeQueueConfig(self):
"""Test Branch.setMergeQueueConfig."""
branch = self.factory.makeBranch()
config = simplejson.dumps({
'path': '/',
'test': 'make test',
})
with person_logged_in(branch.owner):
branch.setMergeQueueConfig(config)
self.assertEqual(branch.merge_queue_config, config)
def test_setMergeQueueConfig_invalid(self):
"""Test that invalid JSON strings aren't added to the database."""
branch = self.factory.makeBranch()
config = 'abc'
with person_logged_in(branch.owner):
self.assertRaises(
InvalidMergeQueueConfig,
branch.setMergeQueueConfig,
config)
class TestWebservice(TestCaseWithFactory):
"""Tests for the webservice."""
layer = AppServerLayer
def test_set_merge_queue(self):
"""Test that the merge queue can be set properly."""
with person_logged_in(ANONYMOUS):
db_queue = self.factory.makeBranchMergeQueue()
db_branch = self.factory.makeBranch()
launchpad = launchpadlib_for('test', db_branch.owner,
service_root=self.layer.appserver_root_url('api'))
branch = ws_object(launchpad, db_branch)
queue = ws_object(launchpad, db_queue)
branch.merge_queue = queue
branch.lp_save()
branch2 = ws_object(launchpad, db_branch)
self.assertEqual(branch2.merge_queue, queue)
def test_set_configuration(self):
"""Test the mutator for setting configuration."""
with person_logged_in(ANONYMOUS):
db_branch = self.factory.makeBranch()
launchpad = launchpadlib_for('test', db_branch.owner,
service_root=self.layer.appserver_root_url('api'))
configuration = simplejson.dumps({'test': 'make check'})
branch = ws_object(launchpad, db_branch)
branch.merge_queue_config = configuration
branch.lp_save()
branch2 = ws_object(launchpad, db_branch)
self.assertEqual(branch2.merge_queue_config, configuration)
|