def test_default_parser(self): p = NMEAParser() logging.debug('\n\nMessages: %s', pprint.pformat(p.messages)) logging.debug('\n\nSensor Models: %s', pprint.pformat(p.sensor_models)) logging.debug('\n\nMessages: %s', pprint.pformat(p.sensors)) for records in [ GYR1_RECORDS, GRV1_RECORDS, SEAP_RECORDS, S330_RECORDS, PCOD_RECORDS, GP02_RECORDS, ADCP_RECORDS, ENG1_RECORDS, KNUD_RECORDS, CWNC_RECORDS, PGUV_RECORDS, SVP1_RECORDS, TSG1_RECORDS, HDAS_RECORDS, MBDP_RECORDS, PCO2_RECORDS, RTMP_RECORDS, ]: for line in records: logging.info('line: %s', line) record = p.parse_record(line) logging.info('record: %s', str(record))
def test_default_parser(self): p = NMEAParser() t = RecordScreenWriter() for line in RECORDS: record = p.parse_record(line) t.write(record) time.sleep(0.1)
def test_read(self): # Create records using synthetic, randomized data id and write to db test_num = random.randint(0,100000) parser = NMEAParser() writer = DatabaseWriter(database='test', host='localhost', user='******', password='******') writer.db.exec_sql_command('truncate table data') reader = DatabaseReader(database='test', host='localhost', user='******', password='******') # Write to database, automatically creating table records = [parser.parse_record(s) for s in SAMPLE_DATA] index = 0 for record in records: logging.debug('Writing record "%s"', str(record)) writer.write(record) result = True while result: result = writer.db.read() logging.info('Read %d: %s', index, result) if result: self.assertEqual(result, SAMPLE_RESULTS[index]) index += 1 # Test range: read a range that should include 3 records results = reader.read_range(start=2, stop=5) self.assertEqual(results, {'S330GPSDay': [(1509778447.17313, 7)], 'S330GPSMonth': [(1509778447.17313, 8)], 'S330GPSYear': [(1509778447.17313, 2014)]}) # Next record should be one after that result = reader.read() self.assertEqual(result, {'S330GPSTime': [(1509778449.210395, 2034.17)]}) # Test time_range: read a range that should include 3 records results = reader.read_time_range(start_time=1509778449.210395, stop_time=1509778453.290818) self.assertEqual(results, {'S330GPSTime': [(1509778451.248784, 2035.17), (1509778453.290817, 2036.17)], 'S330GPSDay': [(1509778451.248784, 7), (1509778453.290817, 7)], 'S330GPSMonth': [(1509778451.248784, 8), (1509778453.290817, 8)], 'S330GPSYear': [(1509778451.248784, 2014), (1509778453.290817, 2014)]}) # Next record should be one after that result = reader.read() self.assertEqual(result, {'S330GPSTime': [(1509778455.328116, 2037.17)]}) writer.db.close() reader.db.close()
def test_parse_nmea(self): p = NMEAParser() (nmea, msg_type) = p.parse_nmea('Gyroscope', GYR1_RECORDS[0].split(' ')[2]) logging.info('NMEA: %s: %s', msg_type, nmea) self.assertEqual(msg_type, '$HEHDT') self.assertDictEqual(nmea, {'HeadingTrue': 143.7, 'TrueConst': 'T'}) (nmea, msg_type) = p.parse_nmea('Gravimeter', GRV1_RECORDS[0].split(' ', maxsplit=2)[2]) self.assertEqual(msg_type, '') self.assertDictEqual(nmea, {'CounterUnits': 1, 'GravityError': 0, 'GravityValueMg': 24557}) (nmea, msg_type) = p.parse_nmea('Seapath200', SEAP_RECORDS[0].split(' ')[2]) logging.info('NMEA: %s: %s', msg_type, nmea) self.assertEqual(msg_type, '$PSXN-20') self.assertDictEqual(nmea, {'HeightQual': 0, 'RollPitchQual': 0, 'HorizQual': 1, 'HeadingQual': 0}) (nmea, msg_type) = p.parse_nmea('Seapath200', SEAP_RECORDS[1].split(' ')[2]) logging.info('NMEA: %s: %s', msg_type, nmea) self.assertEqual(msg_type, '$PSXN-22') self.assertDictEqual(nmea, {'GyroCal': 0.44, 'GyroOffset': 0.74}) (nmea, msg_type) = p.parse_nmea('Seapath200', SEAP_RECORDS[2].split(' ')[2]) logging.info('NMEA: %s: %s', msg_type, nmea) self.assertEqual(msg_type, '$PSXN-23') self.assertDictEqual(nmea, {'Roll': -1.47, 'HeadingTrue': 235.77, 'Pitch': 0.01, 'Heave': -0.38})
def test_mongo_connector(self): parser = NMEAParser() try: db = MongoConnector(database='test', host='localhost', user='******', password='******') # db.exec_sql_command('truncate table data') except Exception as e: self.assertTrue( False, 'Unable to create database connection. Have you ' 'set up the appropriate setup script in database/setup?') records = [parser.parse_record(s) for s in SAMPLE_DATA] for record in records: db.write_record(record) for r in SINGLE_RESULTS: result = db.read() self.assertEqual(result, r) logging.info('Read record: %s', str(result)) self.assertEqual(db.read(), {}) logging.info('###### Resetting') db.seek(0, 'start') for r in RESET_RESULTS: result = db.read('S330CourseTrue,S330CourseMag') self.assertEqual(result, r) logging.info('Read record: %s', str(result)) self.assertEqual(db.read('S330CourseTrue,S330CourseMag'), {}) logging.info('###### Resetting') db.seek(0, 'start') for r in BATCH_RESULTS: result = db.read('S330CourseTrue,S330CourseMag', num_records=None) self.assertEqual(result, r) logging.info('Read record: %s', str(result)) self.assertEqual( db.read('S330CourseTrue,S330CourseMag', num_records=None), {}) logging.info('Cleaning up test database') db.delete_table("data") db.delete_table("source") db.close()
def test_database_writer(self): parser = NMEAParser() writer = DatabaseWriter(database='test', host='localhost', user='******', password='******') writer.db.exec_sql_command('truncate table data') test_num = random.randint(0, 100000) records = [parser.parse_record(s) for s in SAMPLE_DATA] index = 0 for record in records: writer.write(record) result = True while result: result = writer.db.read() logging.debug('Read %d: %s', index, result) if result: self.assertEqual(result, SINGLE_RESULTS[index]) index += 1
def test_parse_records(self): p = NMEAParser() r = p.parse_record(GYR1_RECORDS[0]) self.assertEqual(r.data_id, 'gyr1') self.assertEqual(r.message_type, '$HEHDT') self.assertAlmostEqual(r.timestamp, 1510275606.739) self.assertDictEqual(r.fields, {'Gyro1HeadingTrue': 143.7}) r = p.parse_record(GRV1_RECORDS[0]) self.assertEqual(r.data_id, 'grv1') self.assertEqual(r.message_type, '') self.assertAlmostEqual(r.timestamp, 1510275606.572) self.assertDictEqual(r.fields, { 'Grav1Error': 0, 'Grav1ValueMg': 24557 }) r = p.parse_record(SEAP_RECORDS[0]) self.assertEqual(r.data_id, 'seap') self.assertEqual(r.message_type, '$PSXN-20') self.assertAlmostEqual(r.timestamp, 1509778839.291859) self.assertEqual( r.fields, { 'Seap200HeightQual': 0, 'Seap200RollPitchQual': 0, 'Seap200HorizQual': 1, 'Seap200HeadingQual': 0 }) r = p.parse_record(SEAP_RECORDS[1]) self.assertEqual(r.data_id, 'seap') self.assertEqual(r.message_type, '$PSXN-22') self.assertAlmostEqual(r.timestamp, 1509778839.547251) self.assertEqual(r.fields, { 'Seap200GyroOffset': 0.74, 'Seap200GyroCal': 0.44 }) r = p.parse_record(SEAP_RECORDS[2]) self.assertEqual(r.data_id, 'seap') self.assertEqual(r.message_type, '$PSXN-23') self.assertAlmostEqual(r.timestamp, 1509778839.802690) self.assertEqual( r.fields, { 'Seap200Roll': -1.47, 'Seap200HeadingTrue': 235.77, 'Seap200Pitch': 0.01 })
def test_mysql_connector(self): parser = NMEAParser() try: db = MySQLRecordConnector(database='test', host='localhost', user='******', password='******') except Exception as e: self.assertTrue(False,'Unable to create database connection. Have you ' 'set up the appropriate setup script in database/setup?') test_num = random.randint(0,100000) records = [parser.parse_record(s) for s in SAMPLE_DATA] for i in range(len(records)): records[i].data_id = '%d_%s' % (test_num, records[i].data_id) table_name = db.table_name_from_record(records[i]) logging.info('Deleting table %s', table_name) if db.table_exists(table_name): db.delete_table(table_name) self.assertFalse(db.table_exists(table_name)) # Delete the mapping table so that we can test its automatic creation if db.table_exists(db.FIELD_NAME_MAPPING_TABLE): try: db.delete_table(db.FIELD_NAME_MAPPING_TABLE) except ProgrammingError: pass for record in records: table_name = db.table_name_from_record(record) self.assertFalse(db.table_exists(table_name)) db.create_table_from_record(record) self.assertTrue(db.table_exists(table_name)) db.write_record(record) result = db.read(table_name) logging.info('Read record: %s', str(result)) self.assertEqual(record, result) # Some fields can come from more than one record, and we only # map to the first such record/table_name #for field in record.fields: # self.assertEqual(table_name, db.table_name_from_field(field)) # Make sure we don't get anything when we try a second read self.assertFalse(db.read(table_name)) for record in records: table_name = db.table_name_from_record(record) db.write_record(record) results = db.read_range(table_name, start=1) logging.debug('Read records: %s', [str(r) for r in results]) self.assertEqual(len(results), 2) table_name = db.table_name_from_record(records[0]) db.seek(table_name, 0, 'start') self.assertEqual(db.read(table_name), records[0]) self.assertEqual(db.read(table_name), records[0]) self.assertEqual(db.read(table_name), None) db.seek(table_name, -2, 'current') self.assertEqual(db.read(table_name), records[0]) self.assertEqual(db.read(table_name), records[0]) self.assertEqual(db.read(table_name), None) db.seek(table_name, -1, 'end') self.assertEqual(db.read(table_name), records[0]) self.assertEqual(db.read(table_name), None) # Finally, clean up for record in records: table_name = db.table_name_from_record(record) db.delete_table(table_name) self.assertFalse(db.table_exists(table_name)) db.close()