示例#1
0
def send_power_off():
    if request.json:
        description = request.json.get('description') or 'website off'
    else:
        description = 'website off'
    resp = arduino.turn_off()
    if resp.get('return_value', 0) == 1:
        Event.create_event(EventType.off, description)
        return 'OK', 200
    return 'Error', 500
示例#2
0
def monitor_ac_usage(minutes=30):
    """ Checks how long AC has been running and shuts it off after max is reached.
    :param minutes: How many minutes to let the AC run continuously.
    :return:
    """
    with app.app_context():
        event = Event.last_event()
        if event and event.event != EventType.off:
            off_time = event.timestamp.replace(minutes=+minutes)
            now = arrow.utcnow()

            if off_time < now:
                if arduino.turn_off():
                    Event.create_event(EventType.off, "AutoOff Timer")
示例#3
0
    def test_get_all_event_liked_by_user_1(self):
        """
        Test get_all_event_liked_by_user
        Case1: user exists, and user has liked some events
        """

        latitude = 40.730610
        longitude = -73.935242
        for i in range(10):
            time = datetime.strptime("2020-12-12 12:12:12", "%Y-%m-%d %H:%M:%S")
            tmp_event = Event(user=self.useremail,
                              name="testevent" + str(i),
                              address="address" + str(i),
                              zipcode=10025,
                              event_time=time,
                              longitude=longitude + i,
                              latitude=latitude + i)
            tmp_event.category = "test"
            tmp_event_id = Event.create_event(tmp_event)
            self.event_ids.append(tmp_event_id)
            like = Like(self.useremail, tmp_event_id)
            Like.create_like(like)
        result = Event.get_all_event_liked_by_user(self.useremail)
        for res in result:
            self.assertTrue(res.event_id in self.event_ids)
        self.assertEqual(10, len(result))
示例#4
0
    def test_get_all_ongoing_events_3(self):
        """
        Test get_all_ongoing_events
        Test case3: user is provided, but not exists
        """

        latitude = 40.730610
        longitude = -73.935242
        dif = 0.001
        User.create_user(self.user2)
        for i in range(5):
            time = datetime.strptime("2023-12-12 12:12:12", "%Y-%m-%d %H:%M:%S")
            tmp_event = Event(user=self.useremail,
                              name="testevent" + str(i),
                              address="address" + str(i),
                              zipcode=10025,
                              event_time=time,
                              longitude=longitude + dif,
                              latitude=latitude + dif)
            if i % 2 == 0:
                tmp_event.user_email = self.user2.email
            dif *= 10
            tmp_event.category = "test"
            tmp_event_id = Event.create_event(tmp_event)
            self.event_ids.append(tmp_event_id)

        result = Event.get_all_ongoing_events("*****@*****.**")
        self.assertEqual(5, len(result))
        for i, _ in enumerate(result):
            self.assertEqual(result[i].event_id, self.event_ids[i])
示例#5
0
    def setUp(self) -> None:
        self.app = app
        self.app.config['TESTING'] = True

        self.user = "******"
        User.create_user(User(self.user, "test"))
        self.event = Event.create_event(create_event())
示例#6
0
    def test_get_all_event_joined_by_user_1(self):
        """
        Test get_all_ongoing_event_by_user
        Case1: user has joined some events
        """
        latitude = 40.730610
        longitude = -73.935242
        for i in range(5):
            latitude += 0.01
            longitude += 0.01
            time = datetime.strptime("2020-12-12 12:12:12", "%Y-%m-%d %H:%M:%S")
            tmp_event = Event(user=self.useremail,
                              name="testevent" + str(i),
                              address="address" + str(i),
                              zipcode=10025,
                              event_time=time,
                              longitude=longitude,
                              latitude=latitude)
            tmp_event.category = "test"
            tmp_event_id = Event.create_event(tmp_event)
            self.event_ids.append(tmp_event_id)

            join = Join(self.useremail, tmp_event_id)
            Join.create_join(join)
            if i % 2 == 0:
                like = Like(self.useremail, tmp_event_id)
                Like.create_like(like)

        result = Event.get_all_event_joined_by_user(self.useremail)
        for res in result:
            self.assertTrue(res.event_id in self.event_ids)
        self.assertEqual(5, len(result))
 def setUp(self) -> None:
     self.app = app
     self.app.config['TESTING'] = True
     email = "*****@*****.**"
     user = User(email=email, username="******")
     User.create_user(user)
     event = create_event()
     self.event_id = Event.create_event(event)
示例#8
0
 def test_get_event_id_2(self):
     """
     Test get_event_id
     Test case2: get event exists
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     event = Event.get_event_by_id(event_id)
     self.assertEqual(event.event_id, event_id)
示例#9
0
def create_api_body(reading_schedule):
    for book in reading_schedule.keys():
        for i in reading_schedule[book]:
            book_pages = i['pages']
            book_date = i['date']
            summ = "Read pages {} in: [{}]".format(book_pages, book)
            event = Event(summ, book_date, book_date)
            body = event.create_event()
            requests.append(body)
    return requests
示例#10
0
 def test_update_event_4(self):
     """
     Test update_event
     The new description is too long
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     description = "new desc" * 600
     self.assertRaises(mysql.connector.Error, Event.update_event,
                       {"description": description}, event_id)
示例#11
0
 def test_update_event_2(self):
     """
     Test update_event, update time
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     time = datetime.strptime("2022-12-12 12:12:12", "%Y-%m-%d %H:%M:%S")
     Event.update_event({"time": time}, event_id)
     ret = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret.time, time)
示例#12
0
 def test_update_event_1(self):
     """
     Test update_event, update description
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     description = "new desc"
     Event.update_event({"description": description}, event_id)
     ret = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret.description, description)
示例#13
0
 def test_delete_event_1(self):
     """
     Test delete_event
     The event exists
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     Event.delete_event_by_id(event_id)
     event = Event.get_event_by_id(event_id, event.user_email)
     self.assertIsNone(event)
示例#14
0
 def test_get_attendees_by_event_1(self):
     """
     Test function get_attendees_by_event
     Event exist
     """
     user = "******"
     event = create_event()
     event.category = "test"
     event_id = Event.create_event(event)
     Join.create_join(Join(user, event_id))
     users = User.get_attendees_by_event(event_id)
     self.assertEqual(users[0].email, user)
     Join.delete_join(Join(user, event_id))
     Event.delete_event_by_id(event_id)
示例#15
0
 def test_update_event_5(self):
     """
     Test update_event, update address, longitude, latitude
     New address length is too long
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     address = "412 E, 110th St, New york" * 200
     longitude = Decimal(22.1111)
     latitude = Decimal(33.2222)
     self.assertRaises(mysql.connector.Error, Event.update_event, {"address": address,
                                                                   "longitude": longitude,
                                                                   "latitude": latitude}, event_id)
示例#16
0
 def test_create_event_1(self):
     """
     Test create_event
     All columns are valid
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     ret_event = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret_event.event_id, event_id)
     self.assertEqual(ret_event.user_email, event.user_email)
     self.assertEqual(ret_event.name, event.name)
     self.assertEqual(ret_event.address, event.address)
     self.assertEqual(ret_event.time, event.time)
     self.assertAlmostEqual(ret_event.longitude, event.longitude)
     self.assertAlmostEqual(ret_event.latitude, event.latitude)
示例#17
0
 def test_create_event_5_boundary(self):
     """
     Test create_event
     Description length at boundary 600
     """
     event = create_event()
     event.description = "aaabbb" * 100
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     ret_event = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret_event.event_id, event_id)
     self.assertEqual(ret_event.user_email, event.user_email)
     self.assertEqual(ret_event.name, event.name)
     self.assertEqual(ret_event.address, event.address)
     self.assertEqual(ret_event.time, event.time)
     self.assertAlmostEqual(ret_event.longitude, event.longitude)
     self.assertAlmostEqual(ret_event.latitude, event.latitude)
示例#18
0
 def test_create_event_6_boundary(self):
     """
     Test create_event
     Image path length at boundary 200
     """
     event = create_event()
     event.image = "http://path.com/1234" * 10
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     ret_event = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret_event.event_id, event_id)
     self.assertEqual(ret_event.user_email, event.user_email)
     self.assertEqual(ret_event.name, event.name)
     self.assertEqual(ret_event.address, event.address)
     self.assertEqual(ret_event.time, event.time)
     self.assertAlmostEqual(ret_event.longitude, event.longitude)
     self.assertAlmostEqual(ret_event.latitude, event.latitude)
示例#19
0
 def test_create_event_7_boundary(self):
     """
     Test create_event
     Category length at boundary 20
     """
     event = create_event()
     event.category = "aa" * 10
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     ret_event = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret_event.event_id, event_id)
     self.assertEqual(ret_event.user_email, event.user_email)
     self.assertEqual(ret_event.name, event.name)
     self.assertEqual(ret_event.address, event.address)
     self.assertEqual(ret_event.time, event.time)
     self.assertAlmostEqual(ret_event.longitude, event.longitude)
     self.assertAlmostEqual(ret_event.latitude, event.latitude)
示例#20
0
 def test_create_event_3_boundary(self):
     """
     Test create_event
     Event name at boundary 50
     """
     event = create_event()
     event.name = "event" * 10 # length=50
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     ret_event = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret_event.event_id, event_id)
     self.assertEqual(ret_event.user_email, event.user_email)
     self.assertEqual(ret_event.name, event.name)
     self.assertEqual(ret_event.address, event.address)
     self.assertEqual(ret_event.time, event.time)
     self.assertAlmostEqual(ret_event.longitude, event.longitude)
     self.assertAlmostEqual(ret_event.latitude, event.latitude)
示例#21
0
 def test_update_event_3(self):
     """
     Test update_event, update address, longitude, latitude
     """
     event = create_event()
     event_id = Event.create_event(event)
     self.event_ids.append(event_id)
     address = "412 E, 110th St, New york"
     longitude = Decimal(22.1111)
     latitude = Decimal(33.2222)
     Event.update_event({"address": address,
                         "longitude": longitude,
                         "latitude": latitude}, event_id)
     ret = Event.get_event_by_id(event_id, event.user_email)
     self.assertEqual(ret.address, address)
     self.assertAlmostEqual(ret.longitude, longitude)
     self.assertAlmostEqual(ret.latitude, latitude)