def test_calculate_service_fee(self):
        with app.app_context():
            User.query.filter_by(public_id=self.scheduled_user).update({
                'is_premium':
                True,
                'five_min_price':
                100.0
            })

            db.session.commit()

            new_event = Event(
                datetime.utcnow(),
                datetime.utcnow() + timedelta(minutes=5),
                self.scheduling_user,
                self.scheduled_user,
            )

            db.session.add(new_event)
            db.session.commit()

            self.assertAlmostEqual(float(new_event.total_price),
                                   102.7,
                                   delta=0.2)
            self.assertAlmostEqual(float(new_event.service_fee),
                                   2.5,
                                   delta=0.2)
    def test_get_events_with_offset(self):
        with app.app_context():
            new_event = Event(
                datetime.utcnow() + timedelta(days=-90, minutes=-60),
                datetime.utcnow() + timedelta(days=-90, minutes=0),
                self.scheduling_uid,
                self.scheduled_uid
            )
            new_event.total_price = 5.00
            db.session.add(new_event)
            db.session.commit()

            response = self.test_client.get(
                '/event/{0}/{1}?time_period_offset=3'.format(
                    self.scheduling_uid,
                    str(new_event.public_id)
                ),
                headers={'jwt': create_token(self.scheduling_uid, app.config)}
            )

            self.assertEqual(response.status_code, HTTPStatus.OK)
            self.assertIsNotNone(response)
            response = json.loads(str(response.data.decode('utf-8')))
            self.assertEqual(
                new_event.public_id,
                response['event']['public_id']
            )
    def test_calculate_prices(self):
        with app.app_context():
            User.query.filter_by(public_id=self.scheduled_user).update(
                {'sixty_min_price': 15.0})

            new_event = Event(datetime.utcnow(),
                              datetime.utcnow() + timedelta(minutes=60),
                              self.scheduling_user, self.scheduled_user)

            db.session.add(new_event)
            db.session.commit()

            self.assertAlmostEqual(float(15.575),
                                   float(new_event.total_price),
                                   delta=float(0.2))
Exemple #4
0
    def create_new_event(self,
                         scheduling_user_id,
                         scheduled_user_id,
                         localized_start_time,
                         localized_end_time,
                         local_tz,
                         notes=None,
                         *,
                         skip_commit=False):
        """
        Creates a new event for the scheduled user.

        :param str scheduling_user_id: The user creating the event.
        :param str scheduled_user_id: The user who's time is being purchased
        :param datetime.datetime localized_start_time: The localized start.
          Converted to UTC for insert.
        :param datetime.datetime localized_end_time: The localized end
        :param str local_tz: The timezone in which the event was created.
        :param str notes: Any notes written by the scheduling user.
        :param bool skip_commit: An optional flag used to indicate if we want
        to create the object and add to DB delta, but not commit it just yet.
        Used mostly for facade methods which roll back if the event doesn't
        fully complete (transaction doesn't finalize, &c.).
        :rtype: EventTable
        :return: The newly created event.
        """
        start_time = pytz.timezone(local_tz).localize(
            dateutil.parser.parse(localized_start_time))
        end_time = pytz.timezone(local_tz).localize(
            dateutil.parser.parse(localized_end_time))

        start_time = start_time.astimezone(pytz.timezone('UTC'))
        end_time = end_time.astimezone(pytz.timezone('UTC'))

        self._assert_not_in_past(start_time, end_time)

        self._assert_schedule_exists(start_time, end_time, scheduled_user_id)

        self._assert_valid_duration(start_time, end_time)

        new_event = Event(start_time, end_time, scheduling_user_id,
                          scheduled_user_id, notes)

        exec_and_commit(db.session.add, new_event, skip_commit=skip_commit)

        return new_event
    def setUpClass(cls):
        with app.app_context():
            db.drop_all()
            db.create_all()

            cls.test_dao = EventDAO()

            cls.scheduling_user = User(
                "*****@*****.**",
                "testpw",
                'US/Central',
                'scheduling_user',
            )

            cls.scheduled_user = User(
                "*****@*****.**",
                "testpw",
                'US/Central',
                'scheduled_user',
            )

            db.session.add(cls.scheduling_user)
            db.session.add(cls.scheduled_user)

            db.session.commit()

            cls.scheduled_user = User.query.filter_by(
                public_id=cls.scheduled_user.public_id).first().public_id

            cls.scheduling_user = User.query.filter_by(
                public_id=cls.scheduling_user.public_id).first().public_id

            User.query.filter_by(public_id=cls.scheduled_user).update(
                {'sixty_min_price': 15})

            db.session.add(
                Submerchant(
                    cls.scheduled_user,
                    'testaccountid',
                    'firstName',
                    'LastName',
                    'email',
                    datetime.utcnow() + timedelta(days=-365 * 20),
                    'address_street',
                    'address_locality',
                    'address_region',
                    'address_zip',
                ))

            db.session.commit()

            cls.eids = []
            for i in range(3):
                new_event = Event(
                    datetime.utcnow(),
                    datetime.utcnow() + timedelta(minutes=60),
                    cls.scheduling_user,
                    cls.scheduled_user,
                )

                db.session.add(new_event)

                db.session.commit()

                cls.eids.append(new_event.public_id)