-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cropplan.py
1077 lines (850 loc) · 34.9 KB
/
test_cropplan.py
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
# Copyright Jean-Paul Calderone. See LICENSE file for details.
"""
Unit tests for the crop planning and scheduling module, L{cropplan}.
"""
from datetime import date, datetime, timedelta
from zope.interface.verify import verifyObject
from twisted.trial.unittest import TestCase
from twisted.python.filepath import FilePath
from cropplan import (
UnsplittableTask, MissingInformation,
ITask, FinishPlanning, SeedFlats, DirectSeed, BedPreparation, Weed,
Transplant, Harvest, Order, Price, Crop, Seed,
load_crops, load_seeds, create_tasks, schedule_tasks)
# TODO
# Add 30% buffer for failed germination
# Add 30% buffer for failure in the field
# Add 15% extra for CSA share
def dummyCrop(**kw):
args = dict(
name='foo',
fresh_eating_lbs=3, fresh_eating_weeks=5,
storage_eating_lbs=6, storage_eating_weeks=10,
variety=None, harvest_weeks=4, row_feet_per_oz_seed=None,
yield_lbs_per_bed_foot=2, rows_per_bed=4, in_row_spacing=16,
_bed_feet=None)
args.update(kw)
return Crop(**args)
def dummySeed(crop, **kw):
args = dict(
variety='bar', parts_per_crop=1, product_id='1234g',
greenhouse_days=10, beginning_of_season=90, maturity_days=20,
end_of_season=200, seeds_per_packet=20, row_foot_per_packet=10,
seeds_per_oz=500, dollars_per_packet=5.5, dollars_per_hundred=6.5,
dollars_per_two_fifty=7.6, dollars_per_five_hundred=8.25,
dollars_per_thousand=20.5, dollars_per_five_thousand=40.5,
dollars_per_quarter_oz=7.5, dollars_per_half_oz=8.75,
dollars_per_oz=10.25, dollars_per_eighth_lb=50.5,
dollars_per_quarter_lb=100.5, dollars_per_half_lb=25.50,
dollars_per_lb=200.5, row_foot_per_oz=50, dollars_per_mini=25,
seeds_per_mini=3, row_foot_per_mini=4, harvest_duration=14,
notes="hello, world")
args.update(**kw)
return Seed(crop, **args)
class ComparisonTestsMixin(object):
def test_identicalEquality(self):
"""
An instance compares equal to itself.
"""
instance = self.createFirst()
self.assertTrue(instance == instance)
def test_identicalNotUnequality(self):
"""
An instance does not compare not equal to itself.
"""
instance = self.createFirst()
self.assertFalse(instance != instance)
def test_sameAttributesEquality(self):
"""
Two instances with the same attributes compare equal.
"""
first = self.createFirst()
second = self.createFirst()
self.assertTrue(first == second)
def test_sameAttributesNotUnequality(self):
"""
Two instances with the same attributes do not compare not equal.
"""
first = self.createFirst()
second = self.createFirst()
self.assertFalse(first != second)
def test_differentAttributesNotEquality(self):
"""
Two instances with different attributes do not compare equal.
"""
first = self.createFirst()
second = self.createSecond()
self.assertFalse(first == second)
def test_differentAttributesUnequality(self):
"""
Two instances with different attributes compare not equal.
"""
first = self.createFirst()
second = self.createSecond()
self.assertTrue(first != second)
class LoadCropsTests(TestCase):
"""
Tests for L{load_crops} which reads crop data from a CSV file and returns a
C{dict} containing L{Crop} instances.
"""
HEADER = (
"Crop,Eating lb/wk,Fresh Eating Weeks,Storage Pounds Per Week,"
"Storage Eating Weeks,Variety,Harvest wks,Row Feet Per Ounce Seed,"
"Yield Pounds Per Foot,Rows / Bed,Spacing (inches),Bed Feet,equipment")
def _serialize(self, crop):
format = (
"%(name)s,%(fresh_eating_lbs)f,%(fresh_eating_weeks)d,"
"%(storage_eating_lbs)f,%(storage_eating_weeks)d,"
"%(variety)s,%(harvest_weeks)d,%(row_feet_per_oz_seed)f,"
"%(yield_lbs_per_bed_foot)f,%(rows_per_bed)d,%(in_row_spacing)d,"
"%(_bed_feet)s")
values = vars(crop).copy()
if values['_bed_feet'] is None:
values['_bed_feet'] = ''
return format % values
def test_load_crops(self):
"""
Given a L{FilePath} pointing at a CSV file containing a garbage row and
a header row followed by rows containing a field for each attribute of
L{Crop}, L{load_crops} constructs a C{dict} with crop names as the keys
and L{Crop} instances, with fields populated from the file, as values.
"""
apples = Crop(
"apples", 5.5, 3, 10, 5, "", 2, 100, 250, 1, 120, 1000)
path = FilePath(self.mktemp())
path.setContent(
"garbage\n%s\n%s\n" % (self.HEADER, self._serialize(apples)))
crops = load_crops(path)
self.assertEqual({"apples": apples}, crops)
def test_implicit_bed_feet(self):
"""
Rows from the input may omit explicit information about the number of
bed feet planned, allowing the value to be computed from other fields.
"""
apples = Crop(
"apples", 5.5, 3, 10, 5, "", 2, 100, 250, 1, 120, None)
path = FilePath(self.mktemp())
path.setContent(
"garbage\n%s\n%s\n" % (self.HEADER, self._serialize(apples)))
crops = load_crops(path)
self.assertEqual({"apples": apples}, crops)
def test_extra_columns(self):
"""
Extra columns in the input file that are not part of the L{Crop} are
ignored.
"""
apples = Crop(
"apples", 5.5, 3, 10, 5, "", 2, 100, 250, 1, 120, None)
path = FilePath(self.mktemp())
path.setContent(
"garbage\n%s\n%s\n" % (
"mystery," + self.HEADER,
"mystery value," + self._serialize(apples)))
crops = load_crops(path)
self.assertEqual({"apples": apples}, crops)
class LoadSeedsTests(TestCase):
"""
Tests for L{load_seeds} which reads seed variety data from a CSV file and
returns a C{dict} containing L{Seed} instances.
"""
HEADER = (
"Type,Variety,Fresh Eating Generations,Storage Generations,"
"Bed Ft/fresh eating Generation,Bed ft/Storage generation,"
"time between generations,Parts Per Crop,Product ID,Greenhouse (days),"
"Outside,Maturity (total days from seeding),End of season,"
"seeds/packet,row feet/packet,Seeds/oz,$$/packet,$$/100,$$/250,$$/500,"
"$$/M,$$/(M>=5),$$/.25OZ,$$/.5OZ,$$/oz,$$/.125LB,$$/.25LB,$$/.5LB,"
"$$/LB,ft/oz,$$/mini,Seeds/mini,row feet/mini,Harvest Duration (Days),"
"Notes")
def _serialize(self, seed):
format = (
"%(crop)s,%(variety)s,%(fresh_generations)s,%(storage_generations)s,,,"
"%(intergenerational_weeks)s,%(parts_per_crop)d,%(product_id)s,"
"%(greenhouse_days)s,%(beginning_of_season)s,%(maturity_days)d,"
"%(end_of_season)s,%(seeds_per_packet)d,%(row_foot_per_packet)d,"
"%(seeds_per_oz)d,%(dollars_per_packet)f,%(dollars_per_hundred)f,"
"%(dollars_per_two_fifty)f,%(dollars_per_five_hundred)f,"
"%(dollars_per_thousand)f,%(dollars_per_five_thousand)f,"
"%(dollars_per_quarter_oz)f,%(dollars_per_half_oz)f,"
"%(dollars_per_oz)f,%(dollars_per_eighth_lb)f,"
"%(dollars_per_quarter_lb)f,%(dollars_per_half_lb)f,"
"%(dollars_per_lb)f,%(row_foot_per_oz)f,%(dollars_per_mini)f,"
"%(seeds_per_mini)d,%(row_foot_per_mini)f,%(harvest_duration)d,"
"%(notes)s")
values = vars(seed).copy()
dates = ["beginning_of_season", "end_of_season"]
for k in dates:
d = values[k]
d = date(1980, 1, 1) + timedelta(days=d)
values[k] = "%d/%d/%d" % (d.month, d.day, d.year)
values['crop'] = values['crop'].name
nones = [
'greenhouse_days', 'product_id', 'notes', 'intergenerational_weeks',
'fresh_generations', 'storage_generations']
for k in nones:
if values[k] is None:
values[k] = ""
# LA LA
values['dollars_per_five_thousand'] /= 5.0
return format % values
def test_load_seeds(self):
"""
Given a L{FilePath} pointing at a CSV file containing a header row
followed by rows containing a field for each attribute of L{Crop}, and a
C{dict} like the one returned by L{load_crops}, L{load_seeds} constructs
a C{list} of L{Seed} instances, with fields populated from the file.
"""
apples = Crop(
"apples", 5.5, 3, 10, 5, "", 2, 100, 250, 1, 120, 1000)
crops = {'apples': apples}
wealthy = Seed(
apples, 'wealthy', 1, None, None, 91, 25, 150, 100, 10,
1000, 1.50, 2.50, 3.50, 4.50, 5.50, 6.50, 7.50, 8.50, 9.50, 10.50,
11.50, 12.50, 13.50, 25, 0.50, 15, 3, 14, None)
path = FilePath(self.mktemp())
path.setContent(
"%s\n%s\n" % (self.HEADER, self._serialize(wealthy)))
seeds = load_seeds(path, crops)
self.assertEqual([wealthy], seeds)
def test_load_generations(self):
"""
L{Seed.intergenerational_weeks}, L{Seed.fresh_generations}, and
L{Seed.storage_generations} are populated from the C{"time between
generations"}, C{"fresh eating generations"}, and C{"storage
generations"} columns, respectively.
"""
apples = Crop(
"apples", 5.5, 3, 10, 5, "", 2, 100, 250, 1, 120, 1000)
crops = {'apples': apples}
wealthy = Seed(
apples, 'wealthy', 1, None, None, 91, 25, 150, 100, 10,
1000, 1.50, 2.50, 3.50, 4.50, 5.50, 6.50, 7.50, 8.50, 9.50, 10.50,
11.50, 12.50, 13.50, 25, 0.50, 15, 3, 14, None,
intergenerational_weeks=2, fresh_generations=4,
storage_generations=2)
path = FilePath(self.mktemp())
path.setContent(
"%s\n%s\n" % (self.HEADER, self._serialize(wealthy)))
seeds = load_seeds(path, crops)
self.assertEqual([wealthy], seeds)
class CropTests(TestCase, ComparisonTestsMixin):
"""
Tests for L{Crop}, a representation of a particular crop but not any
specific variety of that crop.
"""
def createFirst(self):
return dummyCrop(name='foo')
def createSecond(self):
return dummyCrop(name='bar')
def test_bed_feet(self):
"""
L{Crop.bed_feet} is computed based on the total yield required,
considering the estimated productivity of the crop per bed foot.
"""
crop = dummyCrop(
fresh_eating_weeks=2, fresh_eating_lbs=3,
storage_eating_weeks=4, storage_eating_lbs=5,
yield_lbs_per_bed_foot=0.5)
self.assertEqual((2 * 3 + 4 * 5) * 2.0, crop.bed_feet)
def test_bed_feet_fallback(self):
"""
If a crop's estimated productivity is not supplied, the C{_bed_feet}
attribute can provide a fallback value for the value of
C{Crop.bed_feet}.
"""
crop = dummyCrop(
fresh_eating_weeks=2, fresh_eating_lbs=3,
storage_eating_weeks=4, storage_eating_lbs=5,
yield_lbs_per_bed_foot=None, _bed_feet=4.5)
self.assertEqual(4.5, crop.bed_feet)
def test_invalid_yield_lbs_per_bed_foot(self):
"""
L{Crop.yield_lbs_per_bed_foot} must be specified as C{None} or a
positive number. Otherwise a L{ValueError} is raised.
"""
self.assertRaises(ValueError, dummyCrop, yield_lbs_per_bed_foot=0)
self.assertRaises(ValueError, dummyCrop, yield_lbs_per_bed_foot=-1)
class SeedTests(TestCase, ComparisonTestsMixin):
"""
Tests for L{Seed}, a representation of a particular variety of a particular
crop.
"""
def setUp(self):
self.crop = dummyCrop()
def createFirst(self):
return dummySeed(self.crop, variety='foo')
def createSecond(self):
return dummySeed(self.crop, variety='bar')
def test_unplantedCrop(self):
"""
L{Seed}s associated with a L{Crop} for which no bed feet are being
planted report their own bed feet as C{0}.
"""
# If we have no yield, we need no bed feet.
self.crop.fresh_eating_weeks = 0
self.crop.storage_eating_weeks = 0
seed = dummySeed(self.crop)
self.assertEqual(0, seed.bed_feet)
def test_freshAndStorageBedFeet(self):
"""
The sum of L{Seed.fresh_bed_feet} and L{Seed.storage_bed_feet} equals
L{Seed.bed_feet} and the two values are in the same ratio as
L{Seed.fresh_yield} and L{Seed.storage_yield}.
"""
crop = dummyCrop(
fresh_eating_lbs=2, fresh_eating_weeks=3,
storage_eating_lbs=5, storage_eating_weeks=7,
yield_lbs_per_bed_foot=0.5,
)
# Sanity check
fresh_feet = 2 * 3 / 0.5
storage_feet = 5 * 7 / 0.5
bed_feet = fresh_feet + storage_feet
self.assertEqual(bed_feet, crop.bed_feet)
self.assertEqual(fresh_feet, crop.fresh_bed_feet)
self.assertEqual(storage_feet, crop.storage_bed_feet)
class SeedOrderTests(TestCase):
"""
Tests for L{Seed.order}, a method for determining how much of a seed to buy,
and in what packaging.
"""
def dummySeed(self, crop, **kw):
"""
A version of L{dummySeed} which removes all default price information.
"""
args = dict(
dollars_per_packet=None, dollars_per_hundred=None,
dollars_per_two_fifty=None, dollars_per_five_hundred=None,
dollars_per_thousand=None, dollars_per_five_thousand=None,
dollars_per_quarter_oz=None, dollars_per_half_oz=None,
dollars_per_oz=None, dollars_per_eighth_lb=None,
dollars_per_quarter_lb=None, dollars_per_half_lb=None,
dollars_per_lb=None, dollars_per_mini=None,
)
args.update(kw)
return dummySeed(crop, **args)
def test_missingPrices(self):
"""
L{Seed.order} raises L{MissingInformation} if there is no price
information.
"""
crop = dummyCrop()
seed = self.dummySeed(crop)
self.assertIsInstance(seed.order(100), MissingInformation)
def test_orderMinimumSufficient(self):
"""
L{Seed.order} takes a number of bed feet and returns a list of L{Order}
instances which represent enough seed to plant that number of bed feet
at the lowest price possible given the available price data.
"""
crop = dummyCrop(rows_per_bed=2)
seed = self.dummySeed(
crop, dollars_per_packet=2.0, row_foot_per_packet=10)
order = seed.order(25)
price = Price('packet', 2.0, 10)
self.assertEqual([Order(seed, 70, price)], order)
class PriceTests(TestCase, ComparisonTestsMixin):
"""
Tests for L{Price}, representing a particular item and its cost.
"""
def createFirst(self):
return Price('packet', 3.50, 10)
def createSecond(self):
return Price('mini', 2, 5)
class OrderTests(TestCase, ComparisonTestsMixin):
"""
Tests for L{Order}, representing a quantity of an item to purchase.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
self.price = self.seed.prices[0]
def createFirst(self):
return Order(self.seed, 10, self.price)
def createSecond(self):
return Order(self.seed, 20, self.price)
class TaskTestsMixin(object):
"""
L{TaskTestsMixin} is a mixin for L{TestCase} subclasses which defines
several test methods common to any kind of task.
"""
def createTask(self):
"""
Create a new instance of a kind of task.
Override this in a subclass to create the kind of task that subclass is
testing.
"""
raise NotImplementedError(
"%s did not implement createTask" % (self.__class__,))
def test_interface(self):
"""
All tasks provide L{ITask}.
"""
task = self.createTask()
self.assertTrue(verifyObject(ITask, task))
def test_split(self):
"""
L{ITask.split} returns a tuple of two L{ITask} providers. The sum of
the C{duration} of the two new tasks is the same as the duration of the
original task, but the C{duration} of the first task is no greater than
the duration passed to C{split}.
"""
task = self.createTask()
first, second = task.split(task.duration / 5 * 3)
self.assertEqual(first.duration, task.duration / 5 * 3)
self.assertEqual(second.duration, task.duration / 5 * 2)
# The other attributes should be the same
self.assertEqual(first.when, task.when)
self.assertEqual(second.when, task.when)
self.assertEqual(first.seed, task.seed)
self.assertEqual(second.seed, task.seed)
def test_str(self):
"""
The result of C{str} on a task is the task summary in parenthesis.
"""
task = self.createTask()
self.assertEqual("(%s)" % (task.summarize(),), str(task))
class FinishPlanningTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{FinishPlanning}, representing a task for finishing planning
related to a particular seed.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{FinishPlanning} task using an arbitrary seed variety.
"""
return FinishPlanning(dummySeed(dummyCrop()))
def createFirst(self):
return FinishPlanning(self.seed)
def createSecond(self):
return FinishPlanning(dummySeed(self.crop, variety='quux'))
def test_split(self):
"""
L{FinishPlanning.split} raises L{UnsplittableTask}.
"""
self.assertRaises(
UnsplittableTask,
FinishPlanning(dummySeed(dummyCrop())).split, timedelta())
def test_summarize(self):
"""
L{FinishPlanning.summarize} identifies the task as planning and includes
the name of the seed and crop it is for.
"""
task = self.createTask()
self.assertEqual("Finish planning bar (foo)", task.summarize())
class SeedFlatsTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{SeedFlats}, representing a task for sowing seeds of a particular
variety into flats.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{SeedFlats} task using an arbitrary date, seed variety,
and quantity.
"""
return SeedFlats(datetime(2000, 6, 12, 8, 0, 0), self.seed, 50)
def createFirst(self):
return SeedFlats(datetime(2001, 6, 1), self.seed, 25)
def createSecond(self):
return SeedFlats(datetime(2001, 6, 2), self.seed, 25)
def test_summarize(self):
"""
L{SeedFlats.summarize} identifies the task as seeding flats and includes
the name of the seed and crop it is for as well as the number of bed
feet.
"""
task = self.createTask()
self.assertEqual(
'Seed flats for 50 bed feet of bar (foo)',
task.summarize())
class DirectSeedTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{DirectSeed}, representing a task for sowing seeds of a
particular variety directly into a bed.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{DirectSeed} task using an arbitrary date, seed variety,
and quantity.
"""
return DirectSeed(datetime(2001, 7, 2, 9, 30, 0), self.seed, 10)
def createFirst(self):
return DirectSeed(datetime(2001, 6, 1), self.seed, 25)
def createSecond(self):
return DirectSeed(datetime(2001, 6, 2), self.seed, 25)
def test_summarize(self):
"""
L{DirectSeed.summarize} identifies the task as direct seeding and
includes the name of the seed and crop it is for as well as the number
of bed feet.
"""
task = self.createTask()
self.assertEqual(
"Direct seed 10 bed feet of bar (foo)", task.summarize())
class BedPreparationTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{BedPreparation}, representing a task for preparing bed space for
seeds of a particular variety (without specifying the details of that
preparation; it may be amending, weeding, etc).
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{BedPreparation} task using an arbitrary date, seed
variety, and quantity.
"""
return BedPreparation(datetime(2002, 5, 25, 10, 15, 0), self.seed, 25)
def createFirst(self):
return BedPreparation(datetime(2003, 5, 16, 9, 0, 0), self.seed, 25)
def createSecond(self):
return BedPreparation(datetime(2003, 5, 16, 9, 0, 0), self.seed, 50)
class WeedTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{Weed}, representing a task for weeding bed space already planted
in a particular variety.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{Weed} task using an arbitrary date, seed variety, and
quantity.
"""
return Weed(datetime(2003, 8, 15, 11, 0, 0), self.seed, 50)
def createFirst(self):
return Weed(datetime(2001, 6, 1), self.seed, 25)
def createSecond(self):
return Weed(datetime(2001, 6, 1), self.seed, 75)
class TransplantTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{Transplant}, representing a task for transplanting seedlings of a
particular variety from flats into bed space.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{Transplant} task using an arbitrary date, seed variety,
and quantity.
"""
return Transplant(datetime(2004, 7, 1, 8, 0, 0), self.seed, 15)
def createFirst(self):
return Transplant(datetime(2001, 6, 1), self.seed, 25)
def createSecond(self):
return Transplant(datetime(2001, 6, 1), self.seed, 75)
class HarvestTests(TestCase, TaskTestsMixin, ComparisonTestsMixin):
"""
Tests for L{Harvest}, representing a task for harvesting produce from bed
space planted in particular variety.
"""
def setUp(self):
self.crop = dummyCrop()
self.seed = dummySeed(self.crop)
def createTask(self):
"""
Create a new L{Harvest} task using an arbitrary date, seed variety, and
quantity.
"""
return Harvest(datetime(2005, 9, 15, 9, 0, 0), self.seed, 25)
def createFirst(self):
return Harvest(datetime(2001, 6, 1), self.seed, 25)
def createSecond(self):
return Harvest(datetime(2001, 6, 1), self.seed, 75)
class CreateTasksTests(TestCase):
"""
Tests for L{create_tasks}, a function for creating the list of necessary
tasks from crop and seed information.
"""
def _taskFactory(self, seed, time_base=None, generations=1.0):
epoch = datetime.now().replace(
month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
def day(n):
return epoch + timedelta(days=n)
if time_base is None:
time_base = seed.beginning_of_season
def t(cls, adj):
return cls(
day(time_base + adj), seed, seed.bed_feet / float(generations))
return t
def test_noTasksForNoFeet(self):
"""
L{create_tasks} creates no tasks at all for a seed with C{bed_feet} set
to C{0}.
"""
crop = dummyCrop()
crops = {'foo': crop}
seedA = dummySeed(crop, parts_per_crop=0)
seedB = dummySeed(crop, parts_per_crop=1)
# Just a sanity check
self.assertEqual(0, seedA.bed_feet)
seeds = [seedA, seedB]
tasks = create_tasks(crops, seeds)
for task in tasks:
if task.seed is seedA:
self.fail(
"Created a task for a seed with no bed feet: %r" % (
task,))
def test_noBeginningOfSeason(self):
"""
L{create_tasks} creates a L{FinishPlanning} for a seed with no beginning
of season information.
"""
crop = dummyCrop()
crops = {'foo': crop}
seed = dummySeed(crop, beginning_of_season=None)
seeds = [seed]
tasks = create_tasks(crops, seeds)
self.assertEqual([FinishPlanning(seed)], tasks)
def test_noGreenhouseDays(self):
"""
L{create_tasks} creates a L{FinishPlanning} for a seed with no
greenhouse information.
"""
crop = dummyCrop()
crops = {'foo': crop}
seed = dummySeed(crop, greenhouse_days=None)
seeds = [seed]
tasks = create_tasks(crops, seeds)
self.assertEqual([FinishPlanning(seed)], tasks)
def test_tasksForDirectSeeding(self):
"""
For a seed variety with C{greenhouse_days} set to C{0}, L{create_tasks}
creates L{BedPreparation}, L{DirectSeed}, and L{Harvest} tasks.
"""
crop = dummyCrop()
crops = {'foo': crop}
seed = dummySeed(crop, greenhouse_days=0)
seeds = [seed]
tasks = create_tasks(crops, seeds)
t = self._taskFactory(seed)
expected = [
t(BedPreparation, -14),
t(DirectSeed, 0),
t(Harvest, seed.maturity_days)]
self.assertEqual(expected, tasks)
def test_tasksForFlatSeeding(self):
"""
For a seed variety with C{greenhouse_days} set to a non-zero value,
L{create_tasks} creates L{BedPreparation}, L{SeedFlats}, L{Transplant},
and L{Harvest} tasks.
"""
crop = dummyCrop()
crops = {'foo': crop}
seed = dummySeed(crop)
seeds = [seed]
tasks = create_tasks(crops, seeds)
t = self._taskFactory(seed)
expected = [
t(BedPreparation, -14),
t(SeedFlats, -seed.greenhouse_days),
t(Transplant, 0),
t(Harvest, seed.maturity_days - seed.greenhouse_days)]
self.assertEqual(expected, tasks)
def test_severalFreshGenerations(self):
"""
For a variety with multiple succession plantings planned for fresh
eating, L{create_tasks} creates an instance of each normal cultivation
task for each succession. Tasks for subsequent successions are delayed
the amount of time indicated by C{intergenerational_weeks}.
"""
crop = dummyCrop()
crops = {'foo': crop}
seed = dummySeed(
crop, greenhouse_days=21,
fresh_generations=2, intergenerational_weeks=3)
seeds = [seed]
tasks = create_tasks(crops, seeds)
self.assertEqual(8, len(tasks))
t = self._taskFactory(seed, generations=2.0)
expected = [
# Generation 1
t(BedPreparation, -14),
t(SeedFlats, -seed.greenhouse_days),
t(Transplant, 0),
t(Harvest, seed.maturity_days - seed.greenhouse_days),
# Generation 2
t(BedPreparation, seed.intergenerational_days - 14),
t(SeedFlats, seed.intergenerational_days - seed.greenhouse_days),
t(Transplant, seed.intergenerational_days),
t(Harvest,
seed.intergenerational_days + seed.maturity_days -
seed.greenhouse_days)]
self.assertEqual(
sorted(expected, key=lambda task: task.when), tasks)
def test_severalStorageGenerations(self):
"""
For a variety with multiple succession plantings planned for storage
production, L{create_tasks} creates an instance of each normal
cultivation task for each succession. Tasks are scheduled to cause
harvest of the last succession to coincide with the end of the season
for that crop, with successions separated by the amount of time
indicated by C{intergenerational_weeks}.
"""
crop = dummyCrop()
crops = {'foo': crop}
seed = dummySeed(
crop, greenhouse_days=21,
storage_generations=2, intergenerational_weeks=3)
seeds = [seed]
tasks = create_tasks(crops, seeds)
self.assertEqual(8, len(tasks))
# Find out when the first seeding of the first storage generation should
# happen; the rest of the date math is easier using this as a reference.
time_base = seed.end_of_season
time_base -= seed.maturity_days
for n in range(seed.storage_generations - 1):
time_base -= seed.intergenerational_days
t = self._taskFactory(
seed, time_base=time_base, generations=seed.storage_generations)
expected = [
# Generation 1
t(BedPreparation, -14),
t(SeedFlats, -seed.greenhouse_days),
t(Transplant, 0),
t(Harvest, seed.maturity_days - seed.greenhouse_days),
# Generation 2
t(BedPreparation, seed.intergenerational_days - 14),
t(SeedFlats, seed.intergenerational_days - seed.greenhouse_days),
t(Transplant, seed.intergenerational_days),
t(Harvest,
seed.intergenerational_days + seed.maturity_days -
seed.greenhouse_days)]
self.assertEqual(
sorted(expected, key=lambda task: task.when), tasks)
def test_bedFeetAllocation(self):
"""
Bed feet for fresh produce and storage produce are allocated in the same
ratio as the yield ratio for those uses.
"""
crop = dummyCrop(
fresh_eating_lbs=3, fresh_eating_weeks=1,
storage_eating_lbs=0, storage_eating_weeks=0,
yield_lbs_per_bed_foot=1)
crops = {'foo': crop}
fuyu = dummySeed(
crop,
variety='fuyu',
parts_per_crop=1,
greenhouse_days=0,
fresh_generations=1,
storage_generations=0)
hachiya = dummySeed(
crop,
variety='hachiya',
parts_per_crop=2,
greenhouse_days=0,
fresh_generations=1,
storage_generations=0)
tasks = create_tasks(crops, [fuyu, hachiya])
fuyu_tasks = [
task for task in tasks if task.seed.variety == fuyu.variety]
hachiya_tasks = [
task for task in tasks if task.seed.variety == hachiya.variety]
# Sanity check, we better have some tasks
self.assertEqual(3, len(fuyu_tasks))
self.assertEqual(3, len(hachiya_tasks))
# The bed feet on each task should be the same, since each task applies
# to the entire generation. And the bed feet should be in proportion to
# the yield requirements.
for task in fuyu_tasks:
self.assertEqual(1, task.quantity)
for task in hachiya_tasks:
self.assertEqual(2, task.quantity)
class ScheduleTasksTests(TestCase):
"""
Tests for L{schedule_tasks}
"""
def test_eagerScheduling(self):
"""
When there is no contention amongst necessary tasks, L{schedule_tasks}
leaves the scheduling of tasks alone.
"""
crop = dummyCrop()
seed = dummySeed(crop)
tasks = [SeedFlats(datetime(2012, 5, 1), seed, 10)]
schedule = schedule_tasks(tasks)
# Compare against a new copy, to ensure that no unexpected mutation of
# the SeedFlats instance happened.
self.assertEqual(