def test_read_tcx_correction(self): """ read garmin tcx format """ gfile = GarminParse( filename=TCXFILE, corr_list={'2012-11-05T11:52:21Z': { 0: [4.0, 1050] }}) gfile.read_file() tmp = '%s' % gfile test = 'GarminFile<filename=test.tcx, filetype=tcx, ' + \ 'begin_datetime=2012-11-05 11:52:21, sport=biking, ' + \ 'total_calories=285, total_distance=6437.376, ' + \ 'total_duration=1050, total_hr_dur=0, total_hr_dis=0>' self.assertTrue(gfile.filetype == 'tcx') self.assertEqual(gfile.begin_datetime.date(), datetime.date(year=2012, month=11, day=5)) self.assertEqual(tmp, test) gsum = GarminSummary( filename=TCXFILE, corr_list={'2012-11-05T11:52:21Z': { 0: [4.0, 1050] }}) gsum.read_file() tmp = '%s' % gsum test = 'GarminSummary<filename=test.tcx, begin_datetime=' + \ '2012-11-05 06:52:21-05:00, sport=biking, total_calories=285, ' + \ 'total_distance=6437.376, total_duration=1050, ' + \ 'total_hr_dur=0, total_hr_dis=0, number_of_items=1, ' + \ 'md5sum=eaa1e1a2bc26b1145a046c39f31b4024>' # tmp = tmp.replace('begin_datetime=2012-11-05 11:52:21+00:00', # 'begin_datetime=2012-11-05 06:52:21-05:00') print(tmp) print(test) self.assertEqual(tmp, test)
def test_cache_dataframe_xml(self): """ test cache dump xml to dataframe """ if datetime.datetime.now(dateutil.tz.tzlocal()).tzname() == 'EST': gsum = GarminSummary(GMNFILE) gfile = gsum.read_file() gdf = GarminDataFrame(garmin_class=GarminPoint, garmin_list=gfile.points).dataframe gdf.to_csv('temp.xml.point.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.xml.point.csv | md5sum') self.assertIn(md5, [ '4f574433d75f4c5406babc81997f719c', 'f57722c0aabd0e87bf10ae3d9aabb707' ]) gdf = GarminDataFrame(garmin_class=GarminLap, garmin_list=gfile.laps).dataframe gdf.to_csv('temp.xml.lap.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.xml.lap.csv | md5sum') self.assertIn(md5, [ '1a18d3b5b06368a13efb6e00dd0a718c', '60d445adb1f0586fda32b3166f44a18f' ]) gdf = GarminDataFrame(garmin_class=GarminSummary, garmin_list=[gsum]).dataframe gdf.to_csv('temp.fit.sum.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.fit.sum.csv | md5sum') self.assertIn(md5, [ '4d58abbb5cd604582782595c99f83757', '4aed7300dae1ebc400642cbf87dceac8', '4bf114c418e0bf66ead4c31b3a464073' ])
def _read_file_in_par(init, cache_directory): reduced_gmn_filename, gmn_filename, gmn_md5sum, corr_list = init gsum = GarminSummary(gmn_filename, md5sum=gmn_md5sum, corr_list=corr_list) gfile = gsum.read_file() if gfile: _write_cached_file(gfile, cache_directory) else: print('no gfile?', gmn_filename) outit = (reduced_gmn_filename, gmn_filename, gmn_md5sum, gsum) return outit
def test_garmin_total_summary_report_txt(self): """ test GarminReport.total_summary_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() output = gr_.total_summary_report_txt(gsum, sport='running') mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), '9f5aa437a6e8bbe6ecd25a088b634018')
def test_garmin_week_average_report_txt(self): """ test GarminReport.week_average_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() output = gr_.week_average_report_txt(gsum, sport='running', number_of_weeks=1) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), '4b649d4c617128138a1540c9dc2e1a09')
def test_read_cached_gfile(mock_func): mock_func.side_effect = ValueError() gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, cache_directory='%s/run/cache' % CURDIR, use_sql=False) gsum = GarminSummary(FITFILE) gfile = gsum.read_file() gc_.write_cached_gfile(gfile) gfile_new = gc_.read_cached_gfile(gfbname=gfile.filename) assert gfile_new is not False
def test_garmin_day_summary_report_txt(self): """ test GarminReport.day_summary_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() output = gr_.day_summary_report_txt( gsum, sport='running', cur_date=gsum.begin_datetime.date()) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), 'b05ddcddc03a64b650a75ad397037d00')
def test_garmin_month_average_report_txt(self): """ test GarminReport.month_average_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() output = gr_.month_average_report_txt(gsum, sport='running', number_of_months=1) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), '08a90d3c2e16ba2aafb4403e3ed69824')
def test_garmin_summary(self): """ test GarminSummary.__repr__ """ gsum = GarminSummary(FITFILE) gsum.read_file() output = '%s' % gsum # output = output.replace('begin_datetime=2014-01-12 16:00:05+00:00', # 'begin_datetime=2014-01-12 11:00:05-05:00') print(output) print(GARMINSUMMARYSTR) self.assertEqual(output, GARMINSUMMARYSTR) mstr = hashlib.md5() mstr.update(output.encode())
def test_garmin_year_summary_report_txt(self): """ test GarminReport.year_summary_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() dt_ = gsum.begin_datetime output = gr_.year_summary_report_txt(gsum, sport='running', year=dt_.year, number_in_year=1) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), 'b7ab537ea3090fc44276b15bc61577b5')
def test_garmin_month_summary_report_txt(self): """ test GarminReport.month_summary_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() output = gr_.month_summary_report_txt(gsum, sport='running', year=gsum.begin_datetime.year, month=gsum.begin_datetime.month, number_in_month=1) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), 'd2a1204e0374c7e83c450a1ae4ce3981')
def test_garmin_day_average_report_txt(self): """ test GarminReport.day_average_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() self.assertFalse(gr_.day_average_report_txt(gsum, number_days=0)) output = gr_.day_average_report_txt(gsum, sport='running', number_days=1) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), '59c50d1eff34a78619e9f37e45445535')
def test_cache_dataframe_fit_fill_list(self): """ test GarminDataFrame.fill_list """ gsum = GarminSummary(FITFILE) gfile = gsum.read_file() gdf = GarminDataFrame(garmin_class=GarminPoint, garmin_list=gfile.points) output = '%s' % gdf.fill_list()[0] mstr = hashlib.md5() mstr.update(output.encode()) self.assertIn(mstr.hexdigest(), [ '73c52b6753bc841dc09936dadac33c9c', '53087d6c0777c42c9ff06326ad52ab3c', '7c67d4fb98b12129b4878d11a2af35ee' ])
def test_garmin_week_summary_report_txt(self): """ test GarminReport.week_summary_report_txt """ gsum = GarminSummary(FITFILE) gsum.read_file() gr_ = GarminReport() ic_ = gsum.begin_datetime.isocalendar() output = gr_.week_summary_report_txt(gsum, sport='running', isoyear=ic_[0], isoweek=ic_[1], number_in_week=1, date=gsum.begin_datetime) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), 'e538e1b1abd954083c9cc19e14d04315')
def test_read_xml_correction(self): """ read garmin xml format """ gfile = GarminParse( filename=GMNFILE, corr_list={'2011-05-07T15:43:08Z': { 0: [1.1, 300] }}) gfile.read_file() tmp = '%s' % gfile test0 = 'GarminFile<filename=test.gmn, filetype=gmn, ' + \ 'begin_datetime=2011-05-07 15:43:08, sport=biking, ' + \ 'total_calories=61, total_distance=1770.2784, ' + \ 'total_duration=300, total_hr_dur=0, total_hr_dis=0>' test1 = test0.replace('total_distance=1770.2784', 'total_distance=1770.2784000000001') self.assertTrue(gfile.filetype == 'gmn') self.assertEqual(gfile.begin_datetime.date(), datetime.date(year=2011, month=5, day=7)) self.assertIn(tmp, [test0, test1]) gsum = GarminSummary( filename=GMNFILE, corr_list={'2011-05-07T15:43:08Z': { 0: [1.1, 300] }}) gsum.read_file() tmp = '%s' % gsum test0 = 'GarminSummary<filename=test.gmn, begin_datetime=' \ '2011-05-07 10:43:08-05:00, sport=biking, ' \ 'total_calories=61, total_distance=1770.2784, ' \ 'total_duration=300, total_hr_dur=0, total_hr_dis=0, ' \ 'number_of_items=1, md5sum=af6f79ef18f4ec5526d3f987b6f00f9b>' test1 = test0.replace('total_distance=1770.2784', 'total_distance=1770.2784000000001') test2 = test0.replace('10:43:08-05:00', '11:43:08-04:00') test3 = test1.replace('10:43:08-05:00', '11:43:08-04:00') print(tmp) print(test0) print(test1) print(test2) self.assertIn(tmp, [test0, test1, test2, test3])
def test_cache_dataframe_fit(self): """ test cache dump fit to dataframe """ gsum = GarminSummary(FITFILE) gfile = gsum.read_file() gdf = GarminDataFrame(garmin_class=GarminPoint, garmin_list=gfile.points).dataframe gdf.to_csv('temp.fit.point.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.fit.point.csv | md5sum') self.assertEqual(md5, '9b5dd53949c7f9555d97c4a95be1934e') gdf = GarminDataFrame(garmin_class=GarminLap, garmin_list=gfile.laps).dataframe gdf.to_csv('temp.fit.lap.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.fit.lap.csv | md5sum') #self.assertEqual(md5, 'c3c3ee7f75d11b7c64fa3b854602cdaf') gdf = GarminDataFrame(garmin_class=GarminSummary, garmin_list=[gsum]).dataframe gdf.to_csv('temp.fit.sum.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.fit.sum.csv | md5sum') self.assertIn(md5, [ '2cbb419260abb688a930065f7d192186', '2ab4f377f398b9f7db9132bd6e40f02c' ])
def test_cache_dataframe_tcx(self): """ test cache dump tcx to dataframe """ gsum = GarminSummary(TCXFILE) gfile = gsum.read_file() gdf = GarminDataFrame(garmin_class=GarminPoint, garmin_list=gfile.points).dataframe gdf.to_csv('temp.tcx.point.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.tcx.point.csv | md5sum') self.assertEqual(md5, '5ebd5307817c251ae58e48862a633d47') gdf = GarminDataFrame(garmin_class=GarminLap, garmin_list=gfile.laps).dataframe gdf.to_csv('temp.tcx.lap.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.tcx.lap.csv | md5sum') #self.assertEqual(md5, '90402b951f4e677a07b9d04e4af94a18') gdf = GarminDataFrame(garmin_class=GarminSummary, garmin_list=[gsum]).dataframe gdf.to_csv('temp.fit.sum.csv', index=False, float_format='%.4f') md5 = md5_command('cat temp.fit.sum.csv | md5sum') self.assertIn(md5, [ 'ac5e35fea996f62757b6c25d21f543f4', '9de73ef049c7c6c5287bc1fe5c35d501' ])
def read_sql_table(self): """ deserialize from database """ session = sessionmaker(bind=self.engine) session = session() for row in session.query(GarminSummaryTable).all(): gsum = GarminSummary() for sl_ in DB_ENTRIES: if sl_ == 'begin_datetime': tmp = getattr(row, sl_) if 'txt' in row.filename: tmp = parse(tmp) tmp = tmp.astimezone(est) else: tmp = parse(tmp) tmp = tmp.astimezone(est) setattr(gsum, sl_, tmp) else: setattr(gsum, sl_, getattr(row, sl_)) self.summary_list[gsum.filename] = gsum session.commit() session.close() return self.summary_list
def test_garmin_cache(self): """ test GarminCache.get_cache_summary_list """ gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, cache_directory='%s/run/cache' % CURDIR, cache_read_fn=read_pickle_object_in_file, cache_write_fn=write_pickle_object_to_file, use_sql=False) sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR) output = '\n'.join( '%s' % s for s in sorted(sl_.values(), key=lambda x: x.filename)) test_output = open('tests/test_cache_summary.out', 'rt').read().strip() test_output0 = test_output.replace('10:43:08-05:00', '11:43:08-04:00') mstr = hashlib.md5() mstr.update(output.encode()) print(output) # print(test_output) self.assertIn(output, [test_output, test_output0]) sqlite_str = 'sqlite:///%s/run/cache/test.db' % CURDIR gc_ = GarminCacheSQL(sql_string=sqlite_str) sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR) output = '\n'.join( '%s' % s for s in sorted(sl_.values(), key=lambda x: x.filename)) mstr = hashlib.md5() mstr.update(output.encode()) self.assertIn(mstr.hexdigest(), [ 'c06f13236f9abed0723e4af7537ca3d4', 'a59c8ee120e789eda36e0cc8592ffce1', '35475bfdd07e72c9cd3988c83a07b083', 'f1749a2ec48d1ca814b570d2bf36d587' ]) gc0 = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, cache_directory='%s/run/cache' % CURDIR, use_sql=False) sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR) sqlite_str = 'sqlite:///%s/run/cache/test.db' % CURDIR gc1 = GarminCacheSQL(sql_string=sqlite_str, garmin_cache=gc0, summary_list=sl_) output = '\n'.join('%s' % s for s in sorted(gc1.summary_list.values(), key=lambda x: x.filename)) mstr = hashlib.md5() mstr.update(output.encode()) # self.assertIn(mstr.hexdigest(), [ # '06465ba08d19d59c963e542bc19f12b7', 'a59c8ee120e789eda36e0cc8592ffce1', # '34605a1d755eda499022946e46d46c1a', '9fbf84e57a513d875f471fbcabe20e22', # '9e23c7a7bc3c436ef319a5a3d1003264' # ]) with OpenPostgreSQLsshTunnel(port=5435, do_tunnel=True) as pport: postgre_str = '%s:%d/test_garmin_summary' % (POSTGRESTRING, pport) gc_ = GarminCacheSQL(sql_string=postgre_str) sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR) output = '\n'.join( '%s' % s for s in sorted(sl_.values(), key=lambda x: x.filename)) print(output) mstr = hashlib.md5() mstr.update(output.encode()) #self.assertIn(mstr.hexdigest(), [ #'c06f13236f9abed0723e4af7537ca3d4', 'a59c8ee120e789eda36e0cc8592ffce1', #'35475bfdd07e72c9cd3988c83a07b083', '34605a1d755eda499022946e46d46c1a', #'9fbf84e57a513d875f471fbcabe20e22', 'f1749a2ec48d1ca814b570d2bf36d587', #'9e23c7a7bc3c436ef319a5a3d1003264' #]) with OpenPostgreSQLsshTunnel(port=5436, do_tunnel=True) as pport: postgre_str = '%s:%d/test_garmin_summary' % (POSTGRESTRING, pport) gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, cache_directory='%s/run/cache' % CURDIR, cache_read_fn=read_pickle_object_in_file, cache_write_fn=write_pickle_object_to_file, use_sql=False) sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR) sl_ = _write_postgresql_table(sl_, get_summary_list=True, dbname='test_garmin_summary', port=pport) sl_ = _write_postgresql_table(sl_, dbname='test_garmin_summary', port=pport) print(len(sl_)) output = '\n'.join( '%s' % s for s in sorted(sl_.values(), key=lambda x: x.filename)) gc_ = GarminCacheSQL(sql_string=postgre_str) gc_.delete_table() mstr = hashlib.md5() mstr.update(output.encode()) # self.assertIn(mstr.hexdigest(), [ # '06465ba08d19d59c963e542bc19f12b7', '34605a1d755eda499022946e46d46c1a', # '9fbf84e57a513d875f471fbcabe20e22', '9e23c7a7bc3c436ef319a5a3d1003264' # ]) gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, cache_directory='%s/run/cache' % CURDIR, use_sql=False) gsum = GarminSummary(FITFILE) gfile = gsum.read_file() gc_.write_cached_gfile(gfile) gfile_new = gc_.read_cached_gfile(gfbname=gfile.filename) self.assertEqual(gfile, gfile_new) gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, use_sql=False) gfile_new = gc_.read_cached_gfile(gfbname=gfile.filename) self.assertEqual(gfile_new, False) gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, cache_read_fn=read_pickle_object_in_file, cache_write_fn=write_pickle_object_to_file, cache_directory='%s/run/cache' % CURDIR, use_sql=False) sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR) rp_ = GarminReport(cache_obj=gc_) options = { 'do_plot': False, 'do_year': False, 'do_month': False, 'do_week': False, 'do_day': False, 'do_file': False, 'do_sport': None, 'do_update': False, 'do_average': False } script_path = CURDIR options['script_path'] = '%s/garmin_app' % script_path options['cache_dir'] = script_path output = rp_.summary_report(sl_, copy_to_public_html=False, options=options) mstr = hashlib.md5() mstr.update(output.encode()) self.assertEqual(mstr.hexdigest(), 'dd7cc23be0f6f21a6d05782e506cb647')