Exemplo n.º 1
0
def populate_db(csv_data, city, date_format, verbose=False):
	"""
	Inserts csv data into the DB.
	`csv_data` is list representing that.
	- The fields must be in the following index order:
		0) incident; 1) neighborhood; 2) venue; 3) number; 4) time;
		5) date
	- If no date or time are available, the fields must be empty.
	- hour must be in hh:mm format
	- date must be in dd/mm/yyyy format
	"""
	result = []
	with transaction.atomic():
		for row in csv_data:
			natureza = Natureza.objects.get(nome=row[0])
			obj = Ocorrencia(
				naturezas=natureza,
				bairro=val_or_none(row[1]),
				via=val_or_none(row[2]),
				numero=val_or_none(row[3]),
				hora=resolve_time(row[4]),
				data=resolve_date(row[5], date_format),
				cidade=city,
			)
			obj.save()
			if verbose:
				result.append("Ocorrência %s de %s inserida com sucesso." % (
					row[0], row[1]))
	return result
Exemplo n.º 2
0
 def test_can_save_new_ocorrencia(self):
     ocorr = Ocorrencia(data=datetime.date.today(),
                        local='some place',
                        bairro='some hood',
                        via='some street',
                        numero='some #',
                        latitude=0.0,
                        longitude=0.0,
                        natureza='some nat',
                        hora=datetime.time(11, 11, 11))
     ocorr.save()
     self.assertEqual(Ocorrencia.objects.count(), 1)
Exemplo n.º 3
0
	def test_can_save_new_ocorrencia(self):
		ocorr = Ocorrencia(
			data=datetime.date.today(),
			local='some place',
			bairro='some hood',
			via='some street',
			numero='some #',
			latitude=0.0,
			longitude=0.0,
			natureza='some nat',
			hora=datetime.time(11, 11, 11))
		ocorr.save()
		self.assertEqual(Ocorrencia.objects.count(), 1)
Exemplo n.º 4
0
def insert_data(filename, date_format, verbose=False):
    """
	Fetches the data from a csv file and inserts it to the DB.
	- If no date or time are available, the field must be empty.
	"""
    with open(filename, 'rt', encoding='utf-8') as fin:
        table = [row for row in csv.reader(fin)]

    for row in table:
        obj = Ocorrencia(data=resolve_date(row[1], date_format),
                         bairro=if_exists(row[2]),
                         via=if_exists(row[3]),
                         numero=if_exists(row[4]),
                         natureza=if_exists(row[0]),
                         hora=resolve_time(row[5]))
        obj.save()
        if verbose:
            print("Ocorrência %s inserida, de %s com sucesso." %
                  (row[0], row[1]))
Exemplo n.º 5
0
 def test_default_data(self):
     ocorr = Ocorrencia()
     self.assertEqual(ocorr.data, None)
     self.assertEqual(ocorr.local, None)
     self.assertEqual(ocorr.bairro, None)
     self.assertEqual(ocorr.via, None)
     self.assertEqual(ocorr.numero, None)
     self.assertEqual(ocorr.latitude, 0)
     self.assertEqual(ocorr.longitude, 0)
     self.assertEqual(ocorr.natureza, '')
     self.assertEqual(ocorr.hora, None)
Exemplo n.º 6
0
 def test_returns_portuguese_weekday(self):
     ocorr = Ocorrencia(data=datetime.date(day=1, month=1, year=2000))
     self.assertEqual(ocorr.weekday(), 'Sábado')
Exemplo n.º 7
0
 def test_date_brazilian_representation(self):
     ocorr = Ocorrencia(data=datetime.date(day=1, month=1, year=2000))
     self.assertEqual(str(ocorr.date2string()), '01/01/2000')
Exemplo n.º 8
0
 def test_string_representations(self):
     ocorr = Ocorrencia(natureza="I'm string")
     self.assertEqual(str(ocorr), "I'm string")
Exemplo n.º 9
0
 def test_cannot_save_empty_item(self):
     ocorr = Ocorrencia()
     with self.assertRaises(IntegrityError):
         ocorr.save()
Exemplo n.º 10
0
	def test_returns_portuguese_weekday(self):
		ocorr = Ocorrencia(data=datetime.date(day=1, month=1, year=2000))
		self.assertEqual(ocorr.weekday(), 'Sábado')
Exemplo n.º 11
0
	def test_date_brazilian_representation(self):
		ocorr = Ocorrencia(data=datetime.date(day=1, month=1, year=2000))
		self.assertEqual(str(ocorr.date2string()), '01/01/2000')
Exemplo n.º 12
0
	def test_cannot_save_empty_item(self):
		ocorr = Ocorrencia()
		with self.assertRaises(IntegrityError):
			ocorr.save()