def get_by_line_equals(cls, line_number): api = BuslineAPI() try: busline = api.get_by_line_equals(line_number) except: busline = cls.objects.get(line_number=line_number) return busline
def all(cls): r""" If API is up, send requisition and returns all buslines. If API is \ down, searches local database to return all buslines. """ api = BuslineAPI() try: objects = api.all() except ApiException: objects = cls.objects.all() return objects
def filter_by_line_number(cls, line_number): r""" If API is up, send requisition and returns buslines with the \ specified line number. If API is down, searches local database to\ return buslines with the specified line number. """ api = BuslineAPI() try: objects = api.filter_by_line(line_number) except ApiException: objects = cls.objects.filter(line_number__startswith=line_number) return objects
def filter_by_description(cls, description): r""" If API is up, send requisition and returns buslines with the \ specified description. If API is down, searches local database to\ return buslines with the specified description (via). """ api = BuslineAPI() try: objects = api.filter_by_description(description) except ApiException: objects = cls.objects.filter(description__startswith=description) return objects
def filter_by_multiple(cls, line_number, description, terminal__description): r""" Perform an advanced search filtering the results by the line number,\ description and terminal description entered by the user then returns\ the results list. """ api = BuslineAPI() try: objects = api.filter( line_number=line_number, description=description, terminal__description=terminal__description ) except ApiException: objects = cls.objects.filter( description__startswith=description, line_number__startswith=line_number, # terminals__startswith=terminals #FIXME forekey ) return objects
def setUp(self): self.api = BuslineAPI()
class testBusilineAPI(SimpleTestCase): """docstring for testBusilineAPI""" def setUp(self): self.api = BuslineAPI() @patch('api.busline.requests.get') def test_all(self, mock_get): mock_response = Mock() mock_response.json.return_value = self.all_buslines_dict() mock_get.return_value = mock_response all_url = "http://*****:*****@patch('api.busline.requests.get') def test_filter_by_line(self, mock_get): mock_response = Mock() mock_response.json.return_value = self.buslines_by_number() mock_get.return_value = mock_response all_url = "http://*****:*****@patch('api.busline.requests.get') def test_filter_by_description(self, mock_get): mock_response = Mock() mock_response.json.return_value = self.buslines_by_description() mock_get.return_value = mock_response all_url = "http://*****:*****@patch('api.busline.requests.get') def test_filter_by_multiple(self, mock_get): mock_response = Mock() mock_response.json.return_value = self.buslines_by_description() mock_get.return_value = mock_response all_url = "http://*****:*****@patch('api.busline.requests.get') def test_filter(self, mock_get): mock_response = Mock() mock_response.json.return_value = self.buslines_by_description() mock_get.return_value = mock_response all_url = "http://localhost:8080/api/v1/busline/?line_number__contains=917&description__contains=setor&limit=0" self.api.filter(line_number='917', description='setor') mock_get.assert_called_once_with(all_url) mock_response.json.assert_called_once() def all_buslines_dict(self): return { "meta": {"limit": 1000, "next": "null", "offset": 0, "previous": "null", "total_count": 6}, "objects": [ {"company": {"id": 7, "name": "SÃO JOSÉ", "resource_uri": "/api/v1/company/7"}, "description": "Riacho Fundo/Rodoviária do Plano Piloto (Eixo)", "fee": 2.0, "id": 76, "line_number": "0.172", "resource_uri": "/api/v1/busline/76", "route_size": 22.57, "terminals": [ {"address": "null", "description": "RIACHO FUNDO", "id": 6, "resource_uri": "/api/v1/terminal/6"}, {"address": "null", "description": "RODOVIÃRIA DO PLANO PILOTO", "id": 19, "resource_uri": "/api/v1/terminal/19"} ], "via": "AV. RIACHO FUNDO I SUL"}, {"company": {"id": 7, "name": "SÃO JOSÉ", "resource_uri": "/api/v1/company/7"}, "description": "Riacho Fundo/Rodoviária do Plano Piloto (Eixo)", "fee": 2.0, "id": 557, "line_number": "172.1", "resource_uri": "/api/v1/busline/557", "route_size": 26.48, "terminals": [ {"address": "null", "description": "RIACHO FUNDO", "id": 6, "resource_uri": "/api/v1/terminal/6"}, {"address": "null", "description": "RODOVIÃRIA DO PLANO PILOTO", "id": 19, "resource_uri": "/api/v1/terminal/19"} ], "via": "AV. RIACHO FUNDO I SUL"}, {"company": {"id": 62, "name": "RÃPIDO BRASILIA", "resource_uri": "/api/v1/company/62"}, "description": "Escola Classe 405 Sul/ASTECA (ISM)", "fee": 0.0, "id": 558, "line_number": "172.2", "resource_uri": "/api/v1/busline/558", "route_size": 25.84, "terminals": [ {"address": "null", "description": "E.C. 405 SUL", "id": 78, "resource_uri": "/api/v1/terminal/78"}, {"address": "null", "description": "ASTECA (ISM)", "id": 129, "resource_uri": "/api/v1/terminal/129"} ], "via": "VIA EQS 206/205 COMÉRCIO LOCAL SUL"}, {"company": {"id": 7, "name": "SÃO JOSÉ", "resource_uri": "/api/v1/company/7"}, "description": "Riacho Fundo/Rodoviária do Plano Piloto (Eixo-Noturna)", "fee": 2.0, "id": 559, "line_number": "172.4", "resource_uri": "/api/v1/busline/559", "route_size": 22.57, "terminals": [ {"address": "null", "description": "RIACHO FUNDO", "id": 6, "resource_uri": "/api/v1/terminal/6"}, {"address": "null", "description": "RODOVIÃRIA DO PLANO PILOTO", "id": 19, "resource_uri": "/api/v1/terminal/19"} ], "via": "AV. RIACHO FUNDO I SUL"}, {"company": {"id": 29, "name": "VIAÇÃO VIVA BRASÃLIA,VIAÇÃO PLANETA", "resource_uri": "/api/v1/company/29"}, "description": "Taguatinga Sul/CEET 01/APAED", "fee": 0.0, "id": 151, "line_number": "0.309", "resource_uri": "/api/v1/busline/151", "route_size": 35.08, "terminals": [ {"address": "null", "description": "AREAL - QS 10", "id": 37, "resource_uri": "/api/v1/terminal/37"}, {"address": "null", "description": "APAED", "id": 59, "resource_uri": "/api/v1/terminal/59"} ], "via": "AV. ÃGUAS CLARAS"}, {"company": {"id": 29, "name": "VIAÇÃO VIVA BRASÃLIA,VIAÇÃO PLANETA", "resource_uri": "/api/v1/company/29"}, "description": "APAED/Taguatinga Sul/CEET 01", "fee": 0.0, "id": 690, "line_number": "309.1", "resource_uri": "/api/v1/busline/690", "route_size": 50.6, "terminals": [ {"address": "null", "description": "APAED", "id": 59, "resource_uri": "/api/v1/terminal/59"} ], "via": "VIA M3 - CEILÂNDIA"}, {"company": {"id": 10, "name": "VIAÇÃO CIDADE BRASÃLIA", "resource_uri": "/api/v1/company/10"}, "description": "Setor \"O\" (via M-2)/SAAN(Estrutural)", "fee": 3.0, "id": 380, "line_number": "0.917", "resource_uri": "/api/v1/busline/380", "route_size": 32.17, "terminals": [ {"address": "null", "description": "SETOR \"O\"", "id": 8, "resource_uri": "/api/v1/terminal/8"}, {"address": "null", "description": "FINAL DA QUADRA 02 DO SAAN", "id": 117, "resource_uri": "/api/v1/terminal/117"} ], "via": "VIA O4 - CEILÂNDIA"} ]} def buslines_by_number(self): return { "meta": {"limit": 1000, "next": "null", "offset": 0, "previous": "null", "total_count": 6}, "objects": [ {"company": {"id": 7, "name": "SÃO JOSÉ", "resource_uri": "/api/v1/company/7"}, "description": "Riacho Fundo/Rodoviária do Plano Piloto (Eixo)", "fee": 2.0, "id": 76, "line_number": "0.172", "resource_uri": "/api/v1/busline/76", "route_size": 22.57, "terminals": [ {"address": "null", "description": "RIACHO FUNDO", "id": 6, "resource_uri": "/api/v1/terminal/6"}, {"address": "null", "description": "RODOVIÃRIA DO PLANO PILOTO", "id": 19, "resource_uri": "/api/v1/terminal/19"} ], "via": "AV. RIACHO FUNDO I SUL"}, {"company": {"id": 7, "name": "SÃO JOSÉ", "resource_uri": "/api/v1/company/7"}, "description": "Riacho Fundo/Rodoviária do Plano Piloto (Eixo)", "fee": 2.0, "id": 557, "line_number": "172.1", "resource_uri": "/api/v1/busline/557", "route_size": 26.48, "terminals": [ {"address": "null", "description": "RIACHO FUNDO", "id": 6, "resource_uri": "/api/v1/terminal/6"}, {"address": "null", "description": "RODOVIÃRIA DO PLANO PILOTO", "id": 19, "resource_uri": "/api/v1/terminal/19"} ], "via": "AV. RIACHO FUNDO I SUL"}, {"company": {"id": 62, "name": "RÃPIDO BRASILIA", "resource_uri": "/api/v1/company/62"}, "description": "Escola Classe 405 Sul/ASTECA (ISM)", "fee": 0.0, "id": 558, "line_number": "172.2", "resource_uri": "/api/v1/busline/558", "route_size": 25.84, "terminals": [ {"address": "null", "description": "E.C. 405 SUL", "id": 78, "resource_uri": "/api/v1/terminal/78"}, {"address": "null", "description": "ASTECA (ISM)", "id": 129, "resource_uri": "/api/v1/terminal/129"} ], "via": "VIA EQS 206/205 COMÉRCIO LOCAL SUL"}, {"company": {"id": 7, "name": "SÃO JOSÉ", "resource_uri": "/api/v1/company/7"}, "description": "Riacho Fundo/Rodoviária do Plano Piloto (Eixo-Noturna)", "fee": 2.0, "id": 559, "line_number": "172.4", "resource_uri": "/api/v1/busline/559", "route_size": 22.57, "terminals": [ {"address": "null", "description": "RIACHO FUNDO", "id": 6, "resource_uri": "/api/v1/terminal/6"}, {"address": "null", "description": "RODOVIÃRIA DO PLANO PILOTO", "id": 19, "resource_uri": "/api/v1/terminal/19"} ], "via": "AV. RIACHO FUNDO I SUL"} ]} def buslines_by_description(self): return { "meta": {"limit": 1000, "next": "null", "offset": 0, "previous": "null", "total_count": 6}, "objects": [ {"company": {"id": 10, "name": "VIAÇÃO CIDADE BRASÃLIA", "resource_uri": "/api/v1/company/10"}, "description": "Setor \"O\" (via M-2)/SAAN(Estrutural)", "fee": 3.0, "id": 380, "line_number": "0.917", "resource_uri": "/api/v1/busline/380", "route_size": 32.17, "terminals": [ {"address": "null", "description": "SETOR \"O\"", "id": 8, "resource_uri": "/api/v1/terminal/8"}, {"address": "null", "description": "FINAL DA QUADRA 02 DO SAAN", "id": 117, "resource_uri": "/api/v1/terminal/117"} ], "via": "VIA O4 - CEILÂNDIA"} ]}
def setUp(self): self.flush_buslines() self.api = BuslineAPI()