def test_create_event_not_unique(self): now = datetime.now() Event.createOne(name="new_event", date_time=now.isoformat(), description="description", image="https://blob.knighthacks.org/somelogo.png", link="https://blob.knighthacks.org/somelogo.png", end_date_time=now.isoformat(), attendees_count=10, event_status="status") res = self.client.post("api/events/create_event/", data=json.dumps({ "name": "new_event", "date_time": now.isoformat(), "description": "description", "image": "https://blob.knighthacks.org/somelogo.png", "link": "https://blob.knighthacks.org/somelogo.png", "end_date_time": now.isoformat(), "attendees_count": 10, "event_status": "status" }), content_type="application/json") data = json.loads(res.data.decode()) self.assertEqual(res.status_code, 409) self.assertEqual(data["name"], "Conflict")
def test_get_all_events(self): now = datetime.now() Event.createOne(name="new_event", date_time=now.isoformat(), description="description", image="https://blob.knighthacks.org/somelogo.png", link="https://blob.knighthacks.org/somelogo.png", end_date_time=now.isoformat(), attendees_count=10, event_status="status") Event.createOne(name="another_new_event", date_time=now.isoformat(), description="description", image="https://blob.knighthacks.org/somelogo.png", link="https://blob.knighthacks.org/somelogo.png", end_date_time=now.isoformat(), attendees_count=10, event_status="status") res = self.client.get("api/events/get_all_events/") data = json.loads(res.data.decode()) self.assertEqual(res.status_code, 201) self.assertEqual(data["events"][0]["name"], "new_event") self.assertEqual(data["events"][1]["name"], "another_new_event")
def test_update_event(self): now = datetime.now() Event.createOne(name="new_event", date_time=now.isoformat(), description="description", image="https://blob.knighthacks.org/somelogo.png", link="https://blob.knighthacks.org/somelogo.png", end_date_time=now.isoformat(), attendees_count=10, event_status="status") res = self.client.put("api/events/update_event/new_event/", data=json.dumps({ "date_time": now.isoformat(), "description": "another_description", "image": "https://blob.knighthacks.org/somelogo.png", "link": "https://blob.knighthacks.org/somelogo.png", "end_date_time": now.isoformat(), "attendees_count": 20, "event_status": "ongoing", }), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertEqual(Event.objects.first().event_status, "ongoing")
def create_event(): """ Creates a new event. --- tags: - event summary: Creates event requestBody: content: application/json: schema: $ref: '#/components/schemas/Event' description: Created event object required: true responses: 201: description: OK 400: description: Bad request. 5XX: description: Unexpected error (the API issue). """ data = request.get_json() if not data: raise BadRequest() if data.get("date_time"): try: data["date_time"] = dateutil.parser.parse(data["date_time"]) except ParserError: raise BadRequest() if data.get("end_date_time"): try: data["end_date_time"] = dateutil.parser.parse( data["end_date_time"]) # noqa: E501 except ParserError: raise BadRequest() new_data = {} for field in EVENT_FIELDS: new_data[field] = data.pop(field, None) try: Event.createOne(**new_data) except NotUniqueError: raise Conflict("The event name already exists.") res = {"status": "success", "message": "Event was created!"} return res, 201
def test_update_event_invalid_datatypes(self): now = datetime.now() Event.createOne(name="new_event", date_time=now.isoformat(), description="description", image="https://blob.knighthacks.org/somelogo.png", link="https://blob.knighthacks.org/somelogo.png", end_date_time=now.isoformat(), attendees_count=10, event_status="status") res = self.client.put("api/events/update_event/new_event/", data=json.dumps({ "date_time": "datetime", "description": "another_description", "image": "https://blob.knighthacks.org/somelogo.png", "link": "https://blob.knighthacks.org/somelogo.png", "end_date_time": now.isoformat(), "attendees_count": 20, "event_status": "ongoing", }), content_type="application/json") data = json.loads(res.data.decode()) self.assertEqual(res.status_code, 400) self.assertEqual(data["name"], "Bad Request") res = self.client.put("api/events/update_event/new_event/", data=json.dumps({ "date_time": now.isoformat(), "description": "another_description", "image": "https://blob.knighthacks.org/somelogo.png", "link": "https://blob.knighthacks.org/somelogo.png", "end_date_time": "end_date_time", "attendees_count": 20, "event_status": "ongoing", }), content_type="application/json") data = json.loads(res.data.decode()) self.assertEqual(res.status_code, 400) self.assertEqual(data["name"], "Bad Request")
def test_create_event(self): now = datetime.now() event = Event.createOne(name="foobar", date_time=now, link="https://foobar.com", end_date_time=now) self.assertTrue(event.id) self.assertEqual(event.name, "foobar") self.assertEqual(event.date_time, now) self.assertEqual(event.link, "https://foobar.com") self.assertEqual(event.end_date_time, now)