def test_parse_rkey(self): self.__parse( 'Location,Frequency\n3,100\n1,101', { 3: db.normalize_record({'freq': 100e6}), 1: db.normalize_record({'freq': 101e6}), }, [])
def test_normalize_float(self): r = db.normalize_record({ 'lowerFreq': 1, 'upperFreq': 2 }) self.assertIsInstance(r['lowerFreq'], float) self.assertIsInstance(r['upperFreq'], float)
def test_freq_shorthand(self): r = db.normalize_record({ 'freq': 1, }) self.assertEqual(r['lowerFreq'], 1) self.assertEqual(r['upperFreq'], 1) self.assertNotIn('freq', r)
def test_update_good(self): new_data = { u'type': u'channel', u'lowerFreq': 20e6, u'upperFreq': 20e6, u'label': u'modified', } index = u'1' modified = dict(self.response_json[u'records']) modified[index] = db.normalize_record(new_data) d = testutil.http_post(reactor, self.__url('/' + str(index)), { 'old': self.response_json[u'records'][index], 'new': new_data }) def proceed((response, data)): if response.code >= 300: print data self.assertEqual(response.code, http.NO_CONTENT) def check(s): j = json.loads(s) self.assertEqual(j[u'records'], modified) return client.getPage(self.__url('/')).addCallback(check) d.addCallback(proceed) return d
def test_normalize_complete_result(self): self.assertEqual( { u'type': u'channel', u'lowerFreq': 1e6, u'upperFreq': 2e6, u'mode': u'', u'label': u'', u'notes': u'', u'location': None }, db.normalize_record({ 'lowerFreq': 1e6, 'upperFreq': 2e6, }))
def f(): db.normalize_record({ 'label': 'foo', })
def f(): db.normalize_record({ 'lowerFreq': 1, 'upperFreq': 1, 'foo': 'bar', })
def check(s): j = json.loads(s) self.assertEqual(j[u'records'][u'3'], db.normalize_record(new_record))
class TestDBWeb(unittest.TestCase): test_records = { 1: db.normalize_record({ u'type': u'channel', u'lowerFreq': 10e6, u'upperFreq': 10e6, u'mode': u'AM', u'label': u'chname', u'notes': u'comment', u'location': [0, 90], }), 2: db.normalize_record({ u'type': u'band', u'lowerFreq': 10e6, u'upperFreq': 20e6, u'mode': u'AM', u'label': u'bandname', u'notes': u'comment', u'location': None, }), } response_json = { u'records': {unicode(k): v for k, v in test_records.iteritems()}, u'writable': True, } def setUp(self): db_model = db.DatabaseModel(reactor, dict(self.test_records), writable=True) dbResource = db.DatabaseResource(db_model) self.port = reactor.listenTCP(0, server.Site(dbResource), interface="127.0.0.1") def tearDown(self): return self.port.stopListening() def __url(self, path): return 'http://127.0.0.1:%i%s' % (self.port.getHost().port, path) def test_index_response(self): def callback((response, data)): self.assertEqual(response.headers.getRawHeaders('Content-Type'), ['application/json']) j = json.loads(data) self.assertEqual(j, self.response_json) return testutil.http_get(reactor, self.__url('/')).addCallback(callback) def test_record_response(self): def callback((response, data)): self.assertEqual(response.headers.getRawHeaders('Content-Type'), ['application/json']) j = json.loads(data) self.assertEqual(j, self.test_records[1]) return testutil.http_get(reactor, self.__url('/1')).addCallback(callback) def test_update_good(self): new_data = { u'type': u'channel', u'lowerFreq': 20e6, u'upperFreq': 20e6, u'label': u'modified', } index = u'1' modified = dict(self.response_json[u'records']) modified[index] = db.normalize_record(new_data) d = testutil.http_post(reactor, self.__url('/' + str(index)), { 'old': self.response_json[u'records'][index], 'new': new_data }) def proceed((response, data)): if response.code >= 300: print data self.assertEqual(response.code, http.NO_CONTENT) def check(s): j = json.loads(s) self.assertEqual(j[u'records'], modified) return client.getPage(self.__url('/')).addCallback(check) d.addCallback(proceed) return d def test_create(self): new_record = { u'type': u'channel', u'lowerFreq': 20e6, u'upperFreq': 20e6, } d = testutil.http_post(reactor, self.__url('/'), { 'new': new_record }) def proceed((response, data)): if response.code >= 300: print data self.assertEqual(response.code, http.CREATED) url = 'ONLYONE'.join(response.headers.getRawHeaders('Location')) self.assertEqual(url, self.__url('/3')) # URL of new entry def check(s): j = json.loads(s) self.assertEqual(j[u'records'][u'3'], db.normalize_record(new_record)) return client.getPage(self.__url('/')).addCallback(check) d.addCallback(proceed) return d
def test_long_line(self): self.__parse( 'Frequency,Name\n1,a,boom', {1: db.normalize_record({'freq': 1e6, 'label': 'a'})}, [(2, Warning, 'Record contains extra columns; data discarded.')])
def test_short_line(self): self.__parse( 'Frequency,Name,Comment\n1,a', {1: db.normalize_record({'freq': 1e6, 'label': 'a'})}, [])
def test_no_frequency(self): self.__parse( 'Name,Frequency\na,1\nb', {1: db.normalize_record({'freq': 1e6, 'label': 'a'})}, [(3, Warning, 'Record contains no value for Frequency column; line discarded.')])
def add_record(record): records.append(normalize_record(record))