Example #1
0
    def test_get_sows_at_date(self):
        tour = Tour.objects.get_or_create_by_week_in_current_year(week_number=10)
        location = Location.objects.filter(pigletsGroupCell__isnull=False).first()
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=tour, location=location, quantity=10)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=tour, location=location, quantity=15)

        date0 = date(2020, 6, 5)
        event = PigletsToSowsEvent.objects.create_event(piglets=piglets1, date=date0)

        date01 = date(2020, 6, 25)
        event = PigletsToSowsEvent.objects.create_event(piglets=piglets2, date=date01)

        date02 = date(2020, 7, 1)
        sow1 = Sow.objects.all().first()
        CullingSow.objects.create_culling(sow=sow1, culling_type='padej', date=date02)

        date03 = date(2020, 7, 5)
        sow2 = Sow.objects.all().first()
        CullingSow.objects.create_culling(sow=sow2, culling_type='padej', date=date03)

        with self.assertNumQueries(3):
            date1 = date(2020, 8, 5)
            sows = Sow.objects.get_sows_at_date(date=date1)
            self.assertEqual(len(sows), 23)

        date2 = date(2020, 7, 2)
        sows = Sow.objects.get_sows_at_date(date=date2)
        self.assertEqual(len(sows), 24)
Example #2
0
    def test_add_ws_weighing_out(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[0], quantity=10)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[1], quantity=10)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[2], quantity=10)

        with freeze_time("2020-06-3"):
            for p in Piglets.objects.all():
                PigletsTransaction.objects.create_transaction(
                    piglets_group=p, to_location=self.loc_ws4)
                WeighingPiglets.objects.create_weighing(piglets_group=p,
                                                        total_weight=100,
                                                        place='3/4')

        with freeze_time("2020-06-6"):
            for p in Piglets.objects.all():
                PigletsTransaction.objects.create_transaction(
                    piglets_group=p, to_location=self.loc_ws8)
                WeighingPiglets.objects.create_weighing(piglets_group=p,
                                                        total_weight=150,
                                                        place='4/8')

        rds = ReportDate.objects.all().add_ws_weighing_out(ws_number=4)

        day0_rd = rds.filter(date=date(2020, 6, 3)).first()
        self.assertEqual(day0_rd.tr_out_aka_weight_in_qnty, None)
        self.assertEqual(day0_rd.tr_out_aka_weight_in_total, None)
        self.assertEqual(day0_rd.tr_out_aka_weight_in_avg, None)

        day1_rd = rds.filter(date=date(2020, 6, 6)).first()
        self.assertEqual(day1_rd.tr_out_aka_weight_in_qnty, 30)
        self.assertEqual(day1_rd.tr_out_aka_weight_in_total, 450)
        self.assertEqual(day1_rd.tr_out_aka_weight_in_avg, 15)
Example #3
0
    def test_create_from_merging_list_v3(self):
        # with gilts quantity
        self.client.force_authenticate(user=self.brig3)
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3_sec1, 10)
        piglets1.add_gilts_without_increase_quantity(2)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3_sec1, 10)
        piglets2.add_gilts_without_increase_quantity(2)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3_sec1, 10)
        piglets3.add_gilts_without_increase_quantity(2)

        response = self.client.post('/api/piglets/create_from_merging_list_and_move_to_ws4/', \
            {'records': [
                {'piglets_id': piglets1.pk, 'quantity': 8, 'changed': True, 
                    'gilts_contains': True, 'gilts_quantity': 8},
                {'piglets_id': piglets2.pk, 'quantity': piglets2.quantity, 'changed': False, 
                    'gilts_contains': False,}
                ],
            },
            format='json')
        
        self.assertEqual(response.data['message'], 'Партия создана и перемещена в Цех4.')
        self.assertEqual(response.data['piglets']['quantity'], 18)
        self.assertEqual(response.data['piglets']['gilts_quantity'], 10)
        self.client.logout()
Example #4
0
    def test_sum_balances_by_locations(self):
        #section 1
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            quantity=10, tour=self.tour1, location=self.ws3_cells[0])
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            quantity=15, tour=self.tour1, location=self.ws3_cells[1])

        # section 2
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            quantity=11, tour=self.tour1, location=self.ws3_cells[50])

        recount1 = Recount.objects.create_recount(piglets=piglets1,
                                                  new_quantity=9)
        recount2 = Recount.objects.create_recount(piglets=piglets2,
                                                  new_quantity=13)
        recount3 = Recount.objects.create_recount(piglets=piglets3,
                                                  new_quantity=14)

        ws3_locs = Location.objects.all().get_workshop_location_by_number(
            workshop_number=3)
        self.assertEqual(
            Recount.objects.sum_balances_by_locations(locations=ws3_locs), 0)

        sec1 = Location.objects.filter(
            section__number=1, section__workshop__number=3).first().section
        sec1_locs = Location.objects.all().get_locations_in_section(sec1)
        self.assertEqual(
            Recount.objects.sum_balances_by_locations(locations=sec1_locs), -3)

        sec2 = Location.objects.filter(
            section__number=2, section__workshop__number=3).first().section
        sec2_locs = Location.objects.all().get_locations_in_section(sec2)
        self.assertEqual(
            Recount.objects.sum_balances_by_locations(locations=sec2_locs), 3)
Example #5
0
    def test_add_ws3_piglets_trs_out_aka_weighing(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[0], quantity=10)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[1], quantity=10)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[2], quantity=10)

        with freeze_time("2020-06-3"):
            for p in Piglets.objects.all():
                WeighingPiglets.objects.create_weighing(piglets_group=p,
                                                        total_weight=100,
                                                        place='3/4')

        rds = ReportDate.objects.all().add_ws3_piglets_trs_out_aka_weighing()

        day1_rd = rds.filter(date=date(2020, 6, 3)).first()
        self.assertEqual(day1_rd.tr_out_aka_weight_qnty, 30)
        self.assertEqual(day1_rd.tr_out_aka_weight_total, 300)
        self.assertEqual(day1_rd.tr_out_aka_weight_avg, 10)

        day0_rd = rds.filter(date=date(2020, 6, 1)).first()
        self.assertEqual(day0_rd.tr_out_aka_weight_qnty, None)
        self.assertEqual(day0_rd.tr_out_aka_weight_total, None)
        self.assertEqual(day0_rd.tr_out_aka_weight_avg, None)
Example #6
0
    def test_gen_anf_birthday(self):
        now = datetime.today()

        piglets1_born_date = now - timedelta(10)
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour2,
            location=self.loc_ws5,
            quantity=15,
            birthday=piglets1_born_date)

        piglets2_born_date = now - timedelta(15)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1,
            location=self.loc_ws5,
            quantity=4,
            birthday=piglets2_born_date)

        piglets3_born_date = now - timedelta(20)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1,
            location=self.loc_ws5,
            quantity=8,
            birthday=piglets3_born_date)

        piglets_qs = Piglets.objects.filter(pk__in=[
            piglets1.pk,
            piglets2.pk,
            piglets3.pk,
        ])

        avg_birthday = piglets_qs.gen_avg_birthday()
        self.assertEqual((now - avg_birthday).days, 13)
Example #7
0
    def test_queryset_get_tour_data_by_place(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1,
            location=self.loc_ws4,
            quantity=100,
            birthday=datetime.datetime(2020, 5, 5, 0, 0))
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1,
            location=self.loc_ws4,
            quantity=100,
            birthday=datetime.datetime(2020, 5, 8, 0, 0))

        WeighingPiglets.objects.create_weighing(piglets_group=piglets1,
                                                total_weight=120,
                                                place='3/4',
                                                date=datetime.datetime.today())
        WeighingPiglets.objects.create_weighing(piglets_group=piglets1,
                                                total_weight=150,
                                                place='3/4',
                                                date=datetime.datetime(
                                                    2020, 9, 25, 0, 0))

        WeighingPiglets.objects.create_weighing(piglets_group=piglets2,
                                                total_weight=360,
                                                place='4/8',
                                                date=datetime.datetime(
                                                    2020, 9, 15, 0, 0))

        data = WeighingPiglets.objects.all().get_tour_data_by_place(
            tour=self.tour1, place='3/4')
        self.assertEqual(data['place'], '3/4')
        self.assertEqual(data['total']['total_quantity'], 200)
Example #8
0
    def test_transaction_with_split_and_merge_v3(self):
        # transaction with merge
        piglets = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3, 10)

        piglets_in_cell = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4_cell1, 10)

        transaction, moved_merged_piglets, stayed_piglets, split_event, merge_event = PigletsTransaction.objects. \
            transaction_with_split_and_merge(piglets=piglets, to_location=self.loc_ws4_cell1, merge=True)

        piglets.refresh_from_db()
        piglets_in_cell.refresh_from_db()
        # active piglets should be 1
        self.assertEqual(Piglets.objects.all().count(), 1)
        self.assertEqual(piglets.active, False)
        self.assertEqual(piglets_in_cell.active, False)

        moved_merged_piglets.refresh_from_db()
        self.assertEqual(moved_merged_piglets.quantity, 20)
        self.assertEqual(moved_merged_piglets.active, True)
        self.assertEqual(moved_merged_piglets.location, self.loc_ws4_cell1)
        self.assertEqual(moved_merged_piglets.metatour.records.all().first().percentage, 100)
        self.assertEqual(moved_merged_piglets.metatour.records.all().first().quantity, 20)
        self.assertEqual(moved_merged_piglets.metatour.records.all().count(), 1)

        self.assertEqual(Piglets.objects.get_all().filter(merger_as_parent=moved_merged_piglets.merger_as_child). \
            first(), piglets)

        self.assertEqual(Piglets.objects.get_all().filter(merger_as_parent=moved_merged_piglets.merger_as_child)[1], \
            piglets_in_cell)

        self.assertEqual(transaction.from_location.workshop.number, 3)
        self.assertEqual(transaction.to_location, self.loc_ws4_cell1)
        self.assertEqual(transaction.piglets_group, piglets)
Example #9
0
    def test_add_piglets_today_quantity_v2(self):
        # cullings and farrows today
        for i in range(0, 20):
            piglets_testing.create_new_group_with_metatour_by_one_tour(
                self.tour1, self.loc_ws1, 10)

        piglets = Piglets.objects.all()
        for i in range(0, 5):
            CullingPiglets.objects.create_culling_piglets(
                piglets_group=piglets[i], culling_type='padej', quantity=2)

        self.assertEqual(CullingPiglets.objects.all().count(), 5)
        self.assertEqual(CullingPiglets.objects.all() \
            .aggregate(qnty=models.Sum('quantity'))['qnty'], 10)

        sow = sows_testings.create_sow_with_semination_usound(
            self.loc_ws3_cells[0])
        SowFarrow.objects.create_sow_farrow(sow=sow, alive_quantity=15)

        self.assertEqual(
            piglets.aggregate(qnty=models.Sum('quantity'))['qnty'], 205)

        with self.assertNumQueries(1):
            rds = ReportDate.objects.all() \
                    .add_piglets_today_quantity()
            bool(rds)
            # 205 + 10 - 15
            self.assertEqual(rds[0].piglets_today_qnty, 200)
Example #10
0
    def test_add_piglets_target_date_quantity_v4(self):
        # calc at date
        for i in range(0, 20):
            piglets_testing.create_new_group_with_metatour_by_one_tour(
                self.tour1, self.loc_ws1, 10)

        piglets = Piglets.objects.all()
        for i in range(0, 5):
            CullingPiglets.objects.create_culling_piglets(
                piglets_group=piglets[i], culling_type='padej', quantity=2)

        sow = sows_testings.create_sow_with_semination_usound(
            self.loc_ws3_cells[0])
        SowFarrow.objects.create_sow_farrow(sow=sow, alive_quantity=15)

        past_date = timezone.now().date() - timedelta(10)
        for i in range(6, 9):
            CullingPiglets.objects.create_culling_piglets(
                piglets_group=piglets[i],
                culling_type='padej',
                quantity=2,
                date=past_date)

        target_date = timezone.now().date() - timedelta(5)
        sow2 = sows_testings.create_sow_with_semination_usound(
            self.loc_ws3_cells[1])
        SowFarrow.objects.create_sow_farrow(sow=sow2,
                                            alive_quantity=13,
                                            date=target_date)

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets[0],
                                                      culling_type='spec',
                                                      quantity=4,
                                                      date=target_date,
                                                      total_weight=43)
        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets[1],
                                                      culling_type='spec',
                                                      quantity=3,
                                                      date=target_date,
                                                      total_weight=32)

        with self.assertNumQueries(1):
            rds = ReportDate.objects.all() \
                    .add_piglets_padej_qnty() \
                    .add_piglets_prirezka_qnty() \
                    .add_piglets_vinuzhd_qnty() \
                    .add_piglets_spec_qnty() \
                    .add_piglets_spec_total_weight() \
                    .add_born_alive() \
                    .add_piglets_today_quantity() \
                    .add_piglets_quantity_at_date_start() \
                    .add_piglets_quantity_at_date_end() \
                    .filter(date=target_date)
            bool(rds)
            self.assertEqual(rds[0].piglets_today_qnty, 200)
            self.assertEqual(rds[0].piglets_qnty_start_date, 194)
            self.assertEqual(rds[0].piglets_qnty_start_end, 200)
            self.assertEqual(rds[0].piglets_spec_total_weight, 75)
            self.assertEqual(rds[0].piglets_spec_qnty, 7)
Example #11
0
    def test_add_piglets_today_quantity_v1(self):
        # not cullings and farrows today
        for i in range(0, 20):
            piglets_testing.create_new_group_with_metatour_by_one_tour(
                self.tour1, self.loc_ws1, 10)

        with self.assertNumQueries(1):
            rds = ReportDate.objects.all() \
                    .add_piglets_today_quantity()
            bool(rds)
            self.assertEqual(rds[0].piglets_today_qnty, 200)
Example #12
0
    def test_manager_get_culling_data_by_piglets(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws4, 101)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws4, 99)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws4, 105)

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets1,
                                                      culling_type='padej',
                                                      reason='xz',
                                                      quantity=10,
                                                      total_weight=100,
                                                      date='2020-03-09')
        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets1,
                                                      culling_type='padej',
                                                      reason='xz',
                                                      quantity=1,
                                                      total_weight=9.5,
                                                      date='2020-03-09')
        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets2,
                                                      culling_type='padej',
                                                      reason='xz',
                                                      quantity=2,
                                                      total_weight=18,
                                                      date='2020-03-09')

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets3,
                                                      culling_type='spec',
                                                      reason='xz',
                                                      quantity=2,
                                                      total_weight=18,
                                                      date='2020-03-09')

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets2,
                                                      culling_type='spec',
                                                      reason='xz',
                                                      quantity=5,
                                                      total_weight=53,
                                                      date='2020-03-09')

        piglets = Piglets.objects.all().with_tour(week_number=1)

        culling_total_data1 = CullingPiglets.objects.get_culling_by_piglets(
            culling_type='spec', piglets=piglets)
        self.assertEqual(culling_total_data1['total_quantity'], 7)
        self.assertEqual(culling_total_data1['total_weight'], 71)
        self.assertEqual(culling_total_data1['avg_weight'], 9.8)

        culling_total_data2 = CullingPiglets.objects.get_culling_by_piglets(
            culling_type='padej', piglets=piglets)
        self.assertEqual(culling_total_data2['total_quantity'], 13)
        self.assertEqual(culling_total_data2['total_weight'], 127.5)
        self.assertEqual(culling_total_data2['avg_weight'], 9.5)
Example #13
0
    def test_transaction_with_split_and_merge_v5(self):
        # transaction with merge and split. diff tours
        piglets = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3, 10)

        piglets_in_cell = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour2,
            self.loc_ws4_cell1, 10)

        # split piglets = 6 and 4. thane merge 4 with piglets_in_cell, return merged piglets 14
        transaction, moved_merged_piglets, stayed_piglets, split_event, merge_event = PigletsTransaction.objects. \
            transaction_with_split_and_merge(piglets=piglets, to_location=self.loc_ws4_cell1, \
                new_amount=4, merge=True)

        piglets.refresh_from_db()
        piglets_in_cell.refresh_from_db()
        self.assertEqual(Piglets.objects.all().count(), 2)
        self.assertEqual(piglets.active, False)
        self.assertEqual(piglets_in_cell.active, False)

        moved_merged_piglets.refresh_from_db()
        self.assertEqual(moved_merged_piglets.quantity, 14)
        self.assertEqual(moved_merged_piglets.active, True)
        self.assertEqual(moved_merged_piglets.location, self.loc_ws4_cell1)
        self.assertEqual(moved_merged_piglets.metatour.records.all().count(), 2)

        self.assertEqual(round(moved_merged_piglets.metatour.records_repr()[0]['percentage'], 2), 28.57)
        self.assertEqual(moved_merged_piglets.metatour.records.all()[0].quantity, 4)

        self.assertEqual(round(moved_merged_piglets.metatour.records_repr()[1]['percentage'], 2), 71.43)
        self.assertEqual(moved_merged_piglets.metatour.records.all()[1].quantity, 10)

        stayed_piglets.refresh_from_db()
        self.assertEqual(stayed_piglets.quantity, 6)
        self.assertEqual(stayed_piglets.active, True)
        self.assertEqual(stayed_piglets.location, self.loc_ws3)
        self.assertEqual(stayed_piglets.metatour.records.all().count(), 1)
        self.assertEqual(round(stayed_piglets.metatour.records_repr()[0]['percentage'], 2), 100.00)
        self.assertEqual(stayed_piglets.metatour.records.all()[0].quantity, 6)
       
        child_split_piglets1 = Piglets.objects.get_all() \
            .filter(split_as_child=piglets.split_as_parent, quantity=6).first()
        child_split_piglets2 = Piglets.objects.get_all() \
            .filter(split_as_child=piglets.split_as_parent, quantity=4).first()

        self.assertEqual(child_split_piglets1.active, True)
        self.assertEqual(child_split_piglets1.location, self.loc_ws3)

        self.assertEqual(child_split_piglets2.active, False)
        self.assertEqual(child_split_piglets2.location, self.loc_ws4_cell1)

        self.assertEqual(transaction.from_location.workshop.number, 3)
        self.assertEqual(transaction.to_location, self.loc_ws4_cell1)
        self.assertEqual(transaction.piglets_group, child_split_piglets2)
Example #14
0
    def test_move_piglets_v4(self):
        # transaction with merge and spli
        self.client.force_authenticate(user=self.brig3)
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3, 10)

        piglets_in_cell = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4_cell1, 10)

        response = self.client.post('/api/piglets/%s/move_piglets/' %
          piglets1.pk, {'to_location': self.loc_ws4.pk, 'merge': True, 'new_amount': 3 })

        self.assertEqual(response.data['message'], 'Перевод прошел успешно.')
        self.client.logout()
Example #15
0
    def test_weighing_piglets_split_return_permissions(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3, 10)

        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4, 10)

        self.client.force_authenticate(user=self.brig3)
        response = self.client.post('/api/piglets/%s/weighing_piglets_split_return/' %
          piglets2.pk, {'to_location': self.loc_ws3.pk, 'new_amount': 8, 'total_weight': 80,
           'place': '3/4' })

        self.assertEqual(response.status_code, 403)
        self.client.logout()
Example #16
0
    def test_add_ws_piglets_trs_in_out(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[0], quantity=10)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[1], quantity=10)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[2], quantity=10)
        piglets4 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[3], quantity=20)

        with freeze_time("2020-06-3"):
            for p in Piglets.objects.filter(
                    pk__in=[piglets1.pk, piglets2.pk, piglets3.pk]):
                PigletsTransaction.objects.create_transaction(
                    piglets_group=p, to_location=self.loc_ws4)
                WeighingPiglets.objects.create_weighing(piglets_group=p,
                                                        total_weight=100,
                                                        place='3/4')

        with freeze_time("2020-06-6"):
            PigletsTransaction.objects.create_transaction(
                piglets_group=piglets4, to_location=self.loc_ws4)

        with freeze_time("2020-06-10"):
            piglets1.refresh_from_db()
            piglets2.refresh_from_db()
            PigletsTransaction.objects.create_transaction(
                piglets_group=piglets1, to_location=self.loc_ws8)
            PigletsTransaction.objects.create_transaction(
                piglets_group=piglets2, to_location=self.loc_ws8)

        rds = ReportDate.objects.all().add_ws_piglets_trs_in_out(
            ws_locs=self.ws4_locs)

        day0_rd = rds.filter(date=date(2020, 6, 3)).first()
        self.assertEqual(day0_rd.tr_in_qnty, 30)
        self.assertEqual(day0_rd.tr_out_qnty, None)

        day1_rd = rds.filter(date=date(2020, 6, 6)).first()
        self.assertEqual(day1_rd.tr_in_qnty, 20)
        self.assertEqual(day1_rd.tr_out_qnty, None)

        day2_rd = rds.filter(date=date(2020, 6, 10)).first()
        self.assertEqual(day2_rd.tr_in_qnty, None)
        self.assertEqual(day2_rd.tr_out_qnty, 20)

        day3_rd = rds.filter(date=date(2020, 6, 12)).first()
        self.assertEqual(day3_rd.tr_in_qnty, None)
        self.assertEqual(day3_rd.tr_out_qnty, None)
Example #17
0
    def test_add_ws_piglets_culling_data(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[0], quantity=10)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[1], quantity=10)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=self.tour1, location=self.loc_ws3_cells[2], quantity=10)

        with freeze_time("2020-06-3"):
            for p in Piglets.objects.all():
                PigletsTransaction.objects.create_transaction(
                    piglets_group=p, to_location=self.loc_ws4)
                WeighingPiglets.objects.create_weighing(piglets_group=p,
                                                        total_weight=100,
                                                        place='3/4')

        with freeze_time("2020-06-5"):
            for p in Piglets.objects.all():
                CullingPiglets.objects.create_culling_piglets(
                    piglets_group=p, culling_type='padej', total_weight=10)

        with freeze_time("2020-06-7"):
            for p in Piglets.objects.all():
                CullingPiglets.objects.create_culling_piglets(
                    piglets_group=p, culling_type='prirezka', total_weight=10)

        with freeze_time("2020-06-10"):
            for p in Piglets.objects.all():
                CullingPiglets.objects.create_culling_piglets(
                    piglets_group=p, culling_type='vinuzhd', total_weight=10)

        rds = ReportDate.objects.all().add_ws_piglets_culling_data(
            ws_locs=self.ws4_locs)

        day0_rd = rds.filter(date=date(2020, 6, 3)).first()
        self.assertEqual(day0_rd.padej_qnty, None)
        self.assertEqual(day0_rd.padej_total_weight, None)
        self.assertEqual(day0_rd.prirezka_qnty, None)
        self.assertEqual(day0_rd.prirezka_total_weight, None)
        self.assertEqual(day0_rd.vinuzhd_qnty, None)
        self.assertEqual(day0_rd.vinuzhd_total_weight, None)

        day1_rd = rds.filter(date=date(2020, 6, 5)).first()
        self.assertEqual(day1_rd.padej_qnty, 3)
        self.assertEqual(day1_rd.padej_total_weight, 30)
        self.assertEqual(day1_rd.prirezka_qnty, None)
        self.assertEqual(day1_rd.prirezka_total_weight, None)
        self.assertEqual(day1_rd.vinuzhd_qnty, None)
        self.assertEqual(day1_rd.vinuzhd_total_weight, None)
Example #18
0
    def test_weighing_piglets_split_return(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3, 10)

        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4, 10)

        self.client.force_authenticate(user=self.brig4)
        response = self.client.post('/api/piglets/%s/weighing_piglets_split_return/' %
          piglets2.pk, {'to_location': self.loc_ws3.pk, 'new_amount': 8, 'total_weight': 80,
           'place': '3/4' })

        self.assertEqual(response.data['message'],
             'Взвешивание прошло успешно. Возврат поросят прошел успешно.')
        self.client.logout()
Example #19
0
    def test_transaction_with_split_and_merge_v4(self):
        # transaction with merge and split
        piglets = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws3, 10)

        piglets_in_cell = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4_cell1, 10)

        transaction, moved_merged_piglets, stayed_piglets, split_event, merge_event = PigletsTransaction.objects. \
            transaction_with_split_and_merge(piglets=piglets, to_location=self.loc_ws4_cell1, \
                new_amount=4, merge=True)

        piglets.refresh_from_db()
        piglets_in_cell.refresh_from_db()
        self.assertEqual(Piglets.objects.all().count(), 2)
        self.assertEqual(piglets.active, False)
        self.assertEqual(piglets_in_cell.active, False)

        moved_merged_piglets.refresh_from_db()
        self.assertEqual(moved_merged_piglets.quantity, 14)
        self.assertEqual(moved_merged_piglets.active, True)
        self.assertEqual(moved_merged_piglets.location, self.loc_ws4_cell1)
        self.assertEqual(moved_merged_piglets.metatour.records.all().first().percentage, 100)
        self.assertEqual(moved_merged_piglets.metatour.records.all().first().quantity, 14)
        self.assertEqual(moved_merged_piglets.metatour.records.all().count(), 1)

        stayed_piglets.refresh_from_db()
        self.assertEqual(stayed_piglets.quantity, 6)
        self.assertEqual(stayed_piglets.active, True)
        self.assertEqual(stayed_piglets.location, self.loc_ws3)
        self.assertEqual(stayed_piglets.metatour.records.all().first().percentage, 100)
        self.assertEqual(stayed_piglets.metatour.records.all().first().quantity, 6)
        self.assertEqual(stayed_piglets.metatour.records.all().count(), 1)

        child_split_piglets1 = Piglets.objects.get_all() \
            .filter(split_as_child=piglets.split_as_parent, quantity=6).first()
        child_split_piglets2 = Piglets.objects.get_all() \
            .filter(split_as_child= piglets.split_as_parent, quantity=4).first()

        self.assertEqual(child_split_piglets1.active, True)
        self.assertEqual(child_split_piglets1.location, self.loc_ws3)

        self.assertEqual(child_split_piglets2.active, False)
        self.assertEqual(child_split_piglets2.location, self.loc_ws4_cell1)

        self.assertEqual(transaction.from_location.workshop.number, 3)
        self.assertEqual(transaction.to_location, self.loc_ws4_cell1)
        self.assertEqual(transaction.piglets_group, child_split_piglets2)
Example #20
0
    def test_create_culling_piglets(self):
        piglets = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws3, 101)

        culling = CullingPiglets.objects.create_culling_piglets(
            piglets_group=piglets,
            culling_type='padej',
            reason='xz',
            quantity=10,
            total_weight=100,
            date='2020-03-09')

        self.piglets.refresh_from_db()
        self.assertEqual(piglets.quantity, 91)
        self.assertEqual(culling.piglets_group, piglets)
        self.assertEqual(culling.culling_type, 'padej')
        self.assertEqual(culling.reason, 'xz')
        self.assertEqual(culling.total_weight, 100)
        self.assertEqual(culling.avg_weight, 10)
        self.assertEqual(culling.date.day, 9)
        self.assertEqual(culling.location, self.loc_ws3)

        piglets.location = self.loc_ws4
        piglets.save()

        culling.refresh_from_db()
        self.assertEqual(culling.location, self.loc_ws3)
Example #21
0
    def test_create_event(self):
        sow = Sow.objects.create_new_and_put_in_workshop_one(farm_id=123)

        tour = Tour.objects.get_or_create_by_week_in_current_year(
            week_number=10)
        location = Location.objects.filter(
            pigletsGroupCell__isnull=False).first()
        piglets = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=tour, location=location, quantity=50)

        event = PigletsToSowsEvent.objects.create_event(piglets=piglets)

        self.assertEqual(event.piglets, piglets)
        self.assertEqual(event.metatour, piglets.metatour)
        self.assertEqual(event.sows.all().count(), 50)
        self.assertEqual(Sow.objects.all().count(), 51)

        sow1 = Sow.objects.all()[1]
        self.assertEqual(sow1.status.title, 'Ремонтная')
        self.assertEqual(sow1.location.workshop.number, 1)
        self.assertEqual(sow1.sow_group.title, 'Ремонтная')

        self.assertEqual(SowStatusRecord.objects.all().count(), 50)
        self.assertEqual(SowGroupRecord.objects.all().count(), 50)
        piglets.refresh_from_db()
        self.assertEqual(piglets.active, False)

        sows = Sow.objects.all().add_status_at_date(date=datetime.today())
        self.assertEqual(sows[1].status_at_date, 'Ремонтная')
Example #22
0
    def setUp(self):
        locations_testing.create_workshops_sections_and_cells()
        piglets_testing.create_piglets_statuses()

        self.tour1 = Tour.objects.get_or_create_by_week_in_current_year(
            week_number=1)
        self.tour2 = Tour.objects.get_or_create_by_week_in_current_year(
            week_number=2)
        self.loc_ws3 = Location.objects.get(workshop__number=3)
        self.piglets = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws3, 101)
        self.piglets.gilts_quantity = 10
        self.piglets.save()

        self.piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour2, self.loc_ws3, 232)
Example #23
0
    def test_merge_piglets_in_location(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws3_sec1_cell1, 10)
        piglets1.add_gilts_without_increase_quantity(3)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour2, self.loc_ws3_sec1_cell1, 10)
        piglets2.add_gilts_without_increase_quantity(2)

        merged_piglets = PigletsMerger.objects.merge_piglets_in_location(
            self.loc_ws3_sec1_cell1)
        self.assertEqual(merged_piglets.quantity, 20)
        self.assertEqual(merged_piglets.gilts_quantity, 5)

        piglets1.refresh_from_db()
        piglets2.refresh_from_db()
        self.assertEqual(piglets1.active, False)
Example #24
0
    def test_add_piglets_qnty_in_transactions(self):
        for i in range(0, 20):
            piglets_testing.create_new_group_with_metatour_by_one_tour(
                self.tour1, self.loc_ws1, 10)

        piglets = Piglets.objects.all()
        for i in range(0, 10):
            PigletsTransaction.objects.create_transaction(
                self.loc_ws3, piglets[i])

        with self.assertNumQueries(1):
            rds = ReportDate.objects.all() \
                    .add_piglets_qnty_in_transactions() \
                    .order_by('-date')
            bool(rds)
            self.assertEqual(rds[0].piglets_transfered, 100)
Example #25
0
    def test_filter_piglets_without_weighing_record(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4, 10)

        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4, 10)

        WeighingPiglets.objects.create_weighing(piglets1, 240, '3/4')

        response = self.client.get('/api/piglets/?piglets_without_weighing_record=3/4')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['id'], piglets2.pk)

        response = self.client.get('/api/piglets/?piglets_with_weighing_record=3/4')
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(response.data['results'][0]['id'], piglets1.pk)
Example #26
0
    def test_qs_add_label_is_oporos_before(self):
        tour = Tour.objects.get_or_create_by_week_in_current_year(week_number=10)
        location = Location.objects.filter(pigletsGroupCell__isnull=False).first()
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            tour=tour, location=location, quantity=10)

        date0 = date(2020, 6, 5)
        event = PigletsToSowsEvent.objects.create_event(piglets=piglets1, date=date0)

        date01 = date(2020, 6, 25)
        sow1 = Sow.objects.all().first()
        sow1.farm_id = '123'
        sow1.tour = tour
        sow1.location = Location.objects.filter(sowAndPigletsCell__isnull=False).first()
        sow1.save()
        event = SowFarrow.objects.create_sow_farrow(sow=sow1, alive_quantity=11, date=date01)

        date2 = date(2020, 7, 2)
        sows = Sow.objects.get_sows_at_date(date=date2)
        self.assertEqual(len(sows), 10)

        sows = sows.add_label_is_oporos_before(date=date2).add_status_at_date(date=date2)

        sow1  = sows.get(farm_id=123)
        self.assertEqual(sow1.is_oporos_before, True)
        self.assertEqual(sow1.status_at_date, 'Опоросилась')

        sow2  = sows.filter(farm_id__isnull=True).first()
        self.assertEqual(sow2.is_oporos_before, False)
        self.assertEqual(sow2.status_at_date, 'Ремонтная')
Example #27
0
    def test_create_merger_return_group_v2(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws3, 10)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour2, self.loc_ws3, 10)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour2, self.loc_ws3, 10)
        piglets_qs1 = Piglets.objects.filter(
            pk__in=[piglets1.pk, piglets2.pk, piglets3.pk])
        child_piglets1 = PigletsMerger.objects.create_merger_return_group(
            parent_piglets=piglets_qs1, new_location=self.loc_ws3)

        piglets4 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour3, self.loc_ws3, 10)
        piglets5 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour4, self.loc_ws3, 10)
        piglets_qs2 = Piglets.objects.filter(pk__in=[piglets4.pk, piglets5.pk])
        child_piglets2 = PigletsMerger.objects.create_merger_return_group(
            parent_piglets=piglets_qs2, new_location=self.loc_ws3)

        piglets_qs3 = Piglets.objects.filter(
            pk__in=[child_piglets1.pk, child_piglets2.pk])
        child_piglets3 = PigletsMerger.objects.create_merger_return_group(
            parent_piglets=piglets_qs3, new_location=self.loc_ws3)

        mtTourRecords = child_piglets3.metatour.records.all()

        self.assertEqual(mtTourRecords.count(), 4)

        self.assertEqual(mtTourRecords[0].tour, self.tour1)
        self.assertEqual(mtTourRecords[0].quantity, 10)
        self.assertEqual(round(mtTourRecords[0].percentage), 20)

        self.assertEqual(mtTourRecords[1].tour, self.tour2)
        self.assertEqual(mtTourRecords[1].quantity, 20)
        self.assertEqual(round(mtTourRecords[1].percentage), 40)

        self.assertEqual(mtTourRecords[2].tour, self.tour3)
        self.assertEqual(mtTourRecords[2].quantity, 10)
        self.assertEqual(round(mtTourRecords[2].percentage), 20)

        self.assertEqual(mtTourRecords[3].tour, self.tour4)
        self.assertEqual(mtTourRecords[3].quantity, 10)
        self.assertEqual(round(mtTourRecords[3].percentage), 20)

        piglets1.refresh_from_db()
        self.assertEqual(piglets1.active, False)
Example #28
0
    def test_get_by_tour_and_ws_number(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws5_cells[0], 101)
        piglets2 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws5_cells[1], 99)
        piglets3 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws5_cells[2], 105)

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets1,
                                                      culling_type='padej',
                                                      reason='xz',
                                                      quantity=10,
                                                      total_weight=100,
                                                      date='2020-12-09')
        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets1,
                                                      culling_type='padej',
                                                      reason='xz',
                                                      quantity=1,
                                                      total_weight=9.5,
                                                      date='2020-12-09')
        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets2,
                                                      culling_type='padej',
                                                      reason='xz',
                                                      quantity=2,
                                                      total_weight=18,
                                                      date='2020-12-09')

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets3,
                                                      culling_type='spec',
                                                      reason='xz',
                                                      quantity=2,
                                                      total_weight=18,
                                                      date='2020-12-09')

        CullingPiglets.objects.create_culling_piglets(piglets_group=piglets2,
                                                      culling_type='spec',
                                                      reason='xz',
                                                      quantity=5,
                                                      total_weight=53,
                                                      date='2020-12-09')

        qs, total = CullingPiglets.objects\
            .get_by_tour_and_ws_number(tour=self.tour1, ws_number=5)

        self.assertEqual(qs.count(), 5)
        self.assertEqual(total['total_quantity'], 20)
        self.assertEqual(total['total_total_weight'], 198.5)
Example #29
0
    def test_split_with_gilts_validate2(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(
            self.tour1, self.loc_ws3, 100)
        piglets1.add_gilts_without_increase_quantity(10)

        with self.assertRaises(ValidationError):
            child_piglets1, child_piglets2 = PigletsSplit.objects.split_return_groups(
                parent_piglets=piglets1, new_amount=91, gilts_to_new=False)
Example #30
0
    def test_weighing_piglets_permissions(self):
        piglets1 = piglets_testing.create_new_group_with_metatour_by_one_tour(self.tour1,
            self.loc_ws4_cell1, 10)

        self.client.force_authenticate(user=self.brig3)
        response = self.client.post('/api/piglets/%s/weighing_piglets/' %
          piglets1.pk, {'total_weight': 670, 'place': '3/4'})
        self.assertEqual(response.status_code, 403)      
        self.client.logout()