def test_return_attribute_of_subtheme(self): """ Adding a theme and subtheme to the database and then testing to see if it's data is retrieved correctly """ theme = Theme("Test_Theme") theme.save() theme.commit() sub_theme = SubTheme(theme.id, "Test_Sub_Theme") sub_theme.save() sub_theme.commit() attributes = Attributes("1234567890-123456789-123456789", "_test_attribute_", "_table_name_", sub_theme.id, 1) attributes.save() attributes.commit() response = self.testing_client.get('/data', data=dict(subtheme=theme.id)) self.assertEqual(theme.json(), response.get_json()) attributes.delete() attributes.commit() sub_theme.delete() sub_theme.commit() theme.delete() theme.commit()
def create_subtheme(self, theme_id: str, name: str) -> db.Model: """ Create new SubTheme :param theme_id: Theme id :param name: SubTheme name :return: Subtheme """ sub_theme = SubTheme(t_id=theme_id, name=name) sub_theme.save() return sub_theme
def create_dummy_subtheme(self) -> SubTheme: """ Create SubTheme :return: SubTheme """ subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_') if not subtheme: subtheme = SubTheme(self.theme.id, '_TEST_SUB_THEME_') subtheme.save() subtheme.commit() subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_') return subtheme
def create_sub_theme(theme: Theme, name: str = 'Airquality') -> None: """ Create a SubTheme :param theme: Parent Theme :param name: SubThemes name :raises ValueError: If the new SubTheme is not persisted to the dB """ sub_theme = SubTheme.get_by_name(name) if not sub_theme: sub_theme = SubTheme(theme.id, name) sub_theme.save() sub_theme.commit() if not sub_theme: logger.critical('ValueError raised while creating SubTheme') raise ValueError
def test_submit(): u = Unit('kg', 'Kilogram') u2 = Unit('g', 'Grams') u3 = Unit('km', 'KiloMeter') u.save() u2.save() u3.save() t = Theme('Environment') t2 = Theme('Transport') t.save() t2.save() st = SubTheme(t.id, 'Airquality') st2 = SubTheme(t2.id, 'Traffic') st.save() st2.save() db.session.commit()
def create_sub_themes(self, count: int) -> None: """ Create SubThemes :param count: The number of SubThemes to create. """ if self.theme: theme_id = self.theme.id for num in range(0, count): sub_theme = SubTheme(theme_id, "_test_sub_theme_{}".format(num)) if sub_theme: try: sub_theme.save() sub_theme.commit() self.sub_themes.append(sub_theme) except Exception as exp: logger.error(exp) self.tearDown()
def test_return_subthemes_of_theme(self): """ Adding a theme and subtheme (linked to to that theme) to the database and then testing to see if it is retrieved correctly """ theme = Theme(name='Test_Theme') theme.save() theme.commit() sub_theme = SubTheme(theme.id, "Test_Sub_Theme") sub_theme.save() sub_theme.commit() response = self.testing_client.get('/data', data=dict(theme=theme.id)) self.assertEqual(sub_theme.json(), response.get_json()) sub_theme.delete() sub_theme.commit() theme.delete() theme.commit()
def post(self) -> (dict, HTTPStatus): """ Get dummy data from CSV. Store dummy data in database :param file_name: File name to extract data from. :return: A Status Report detailing the dB Entries created and an HTTP Status code 200 on success otherwise, a JSON error message is returned with the appropriate HTTPStatus code """ args = self.reqparser.parse_args() # Get size of tables before import self.loc_stats["before"] = db.session.query(func.count( LocationData.id)).scalar() self.tracker_stats["before"] = db.session.query(func.count( Tracker.id)).scalar() # Fetch Data from CSV try: df = pd.read_csv(args['file_name']) except IOError as ioe: logger.error("Unable to parse CSV data to dataframe", ioe.with_traceback(ioe.__traceback__)) return dict(error="Unable to parse CSV data to dataframe", trackback=ioe.with_traceback( ioe.__traceback__)), HTTPStatus.BAD_REQUEST moving_theme = Theme.get_by_name("Moving_Sensors") if moving_theme: moving_sensor_theme_id = moving_theme.id else: moving_sensor_theme = Theme("Moving_Sensors") moving_sensor_theme.save() moving_sensor_theme.commit() moving_sensor_theme_id = moving_sensor_theme.id moving_subtheme = SubTheme.get_by_name("Moving_Airquality") if moving_subtheme: moving_sensor_subtheme_id = moving_subtheme.id else: moving_sensor_subtheme = SubTheme(moving_sensor_theme_id, "Moving_Airquality") moving_sensor_subtheme.save() moving_sensor_subtheme.commit() moving_sensor_subtheme_id = moving_sensor_subtheme.id # Trackers must be unique, Fetch trackers and make dB entries for # each unique tracker unique_tracker_ids = df["tracker"].unique() for tracker_id in unique_tracker_ids: self.t_ids[self.create_trackers(str(tracker_id), moving_sensor_subtheme_id)] = 0 # Define Location data Pandas DataFrame Column names loc_df = df[[ 'tracker', 'datetime', 'latitude', 'longitude', 'speed', 'heading', 'elevation', 'charger', 'battery', 'signalquality', 'satcnt' ]] # Drop all entries that are incomplete have NaN or None/ Null values loc_df = loc_df.dropna() # Store Location Data in the dB for index, row in loc_df.iterrows(): self.add_location_data(row['tracker'], row['datetime'], row['latitude'], row['longitude'], row['speed'], row['heading'], row['elevation'], row['charger'], row['battery'], row['signalquality'], row['satcnt']) self.loc_stats["after"] = db.session.query(func.count( LocationData.id)).scalar() self.tracker_stats["after"] = db.session.query(func.count( Tracker.id)).scalar() return self.status_report(), 200
def create_subtheme(self, theme_id, name): sub_theme = SubTheme(t_id=theme_id, name=name) sub_theme.save() return sub_theme
def post(self) -> ({str: str}, HTTPStatus): """ Create new SubTheme :param theme: the name of the parent theme :param theme_id: the identification number of the parent theme :param subtheme: the name of the sub theme :type theme: str :type theme_id: str :type subtheme: str :returns: A JSON of the new SubTheme with a http status code of 200, otherwise a JSON of the error details and the appropriate http status code """ if not get_jwt_claims()['admin']: return { "error": "administration privileges required" }, HTTPStatus.FORBIDDEN args = self.reqparser.parse_args() if "theme" not in args and "theme_id" not in args: return { "error": "theme or theme_id required" }, HTTPStatus.BAD_REQUEST theme = None if "theme_id" in args: theme = Theme.get_by_id(args["theme_id"]) elif "theme" in args: theme = Theme.get_by_theme(args["theme"]) if not theme: return ({ "error": "Theme not found", "Theme": args["theme_id"] if args["theme_id"] else args["theme"] }, HTTPStatus.NOT_FOUND) sub_theme = SubTheme.get_by(name=args["subtheme"], t_id=theme.id) # Avoid duplicating sub themes if sub_theme: return { "error": "sub theme already exists", "theme_id": theme.id, "sub_theme_id": sub_theme.id, "Theme": theme.name, "subtheme": sub_theme.name }, HTTPStatus.BAD_REQUEST sub_theme = SubTheme(theme.id, args["subtheme"]) sub_theme.save() sub_theme.commit() return { "message": "sub theme created", "theme_id": theme.id, "sub_theme_id": sub_theme.id, "Theme": theme.name, "subtheme": sub_theme.name }, HTTPStatus.OK