def test_not_create_energy_transductor_wrong_serial_number(self): size = len(EnergyTransductor.objects.all()) transductor = EnergyTransductor() transductor.serial_number = '123456789' transductor.ip_address = '1.1.1.1' transductor.broken = False transductor.active = True transductor.model = "EnergyTransductor" transductor.installation_date = datetime.now() with self.assertRaises(DataError): transductor.save()
def setUp(self): HOST, PORT = "localhost", 9999 # Creating Transductor Model and Energy Transductor t_model = TransductorModel() t_model.name = "TR 4020" t_model.transport_protocol = "UDP" t_model.serial_protocol = "Modbus RTU" t_model.register_addresses = [[4, 0], [68, 1]] t_model.save() transductor = EnergyTransductor() transductor.serie_number = "1" transductor.description = "Test" transductor.model = t_model transductor.ip_address = HOST transductor.save() # Setting instance attributes self.t_model = t_model self.transductor = transductor self.modbus_rtu = ModbusRTU(self.transductor) self.udp_protocol = UdpProtocol(serial_protocol=self.modbus_rtu, timeout=0.5, port=9999) # Starting UDP server via thread self.server = SocketServer.UDPServer((HOST, PORT), UDPHandler) self.server_thread = threading.Thread(target=self.server.serve_forever) self.server_thread.start()
def create_energy_transductor(self, serie_number, description, ip_address, t_model): transductor = EnergyTransductor() transductor.serie_number = serie_number transductor.description = description transductor.creation_date = timezone.now() transductor.ip_address = ip_address transductor.model = t_model transductor.save() return transductor
def test_not_create_energy_transductor_no_transductor_model(self): size = len(EnergyTransductor.objects.all()) energy_transductor = EnergyTransductor() energy_transductor.serial_number = '12345678' energy_transductor.ip_address = '' energy_transductor.broken = False energy_transductor.active = True energy_transductor.installation_date = datetime.now() with self.assertRaises(IntegrityError): energy_transductor.save()
def setUp(self): t_model = TransductorModel() t_model.name = "TR 4020" t_model.internet_protocol = "UDP" t_model.serial_protocol = "Modbus RTU" t_model.register_addresses = [68, 70, 72, 74, 76, 78, 80, 82, 84, 88, 90, 92] t_model.save() transductor = EnergyTransductor() transductor.serie_number = "1" transductor.description = "Test" transductor.creation_date = timezone.now() transductor.model = t_model transductor.ip_address = "111.111.111.111" transductor.save()
def test_should_not_create_transductor_with_same_ip_address(self): t_model = self.t_model transductor = EnergyTransductor() transductor.serie_number = "2" transductor.description = "Test 2" transductor.creation_date = timezone.now() transductor.model = t_model transductor.ip_address = "111.111.111.111" self.assertRaises(IntegrityError, transductor.save)
def test_should_not_create_transductor_with_wrong_ip_address(self): t_model = self.t_model transductor = EnergyTransductor() transductor.serie_number = "1" transductor.description = "Test" transductor.creation_date = timezone.now() transductor.model = t_model transductor.ip_address = "1" self.assertRaises(ValidationError, transductor.full_clean)
def setUp(self): self.client = Client() address = [[68, 0], [70, 1]] self.t_model = TransductorModel(name="Test Name", transport_protocol="UDP", serial_protocol="Modbus RTU", register_addresses=address) self.t_model.save() self.e_transductor = EnergyTransductor( model=self.t_model, serie_number=1, ip_address="1.1.1.1", description="Energy Transductor Test", creation_date=timezone.now()) self.e_transductor.save() self.e_measurements = EnergyMeasurements( transductor=self.e_transductor, voltage_a=122.875, voltage_b=122.784, voltage_c=121.611, current_a=22.831, current_b=17.187, current_c=3.950, active_power_a=2.794, active_power_b=1.972, active_power_c=3.950, reactive_power_a=-0.251, reactive_power_b=-0.752, reactive_power_c=-1.251, apparent_power_a=2.805, apparent_power_b=2.110, apparent_power_c=4.144, collection_minute=timezone.now().minute) self.e_measurements.save()
def setUp(self): t_model = TransductorModel() t_model.name = "TR 4020" t_model.transport_protocol = "UdpProtocol" t_model.serial_protocol = "ModbusRTU" t_model.register_addresses = [[4, 0], [68, 1]] t_model.measurements_type = "EnergyMeasurements" t_model.save() self.t_model = t_model transductor = EnergyTransductor() transductor.serie_number = "1" transductor.description = "Test" transductor.model = t_model transductor.ip_address = "111.111.111.111" transductor.broken = False transductor.save() self.transductor = transductor self.modbus_rtu = ModbusRTU(self.transductor)
def test_raise_exception_on_create_messages_with_wrong_address(self): wrong_address = [[4, 2]] t_model = TransductorModel() t_model.name = "Test Model" t_model.transport_protocol = "UDP" t_model.serial_protocol = "Modbus RTU" t_model.register_addresses = wrong_address t_model.save() transductor = EnergyTransductor() transductor.serie_number = "2" transductor.description = "Test 2" transductor.creation_date = timezone.now() transductor.model = t_model transductor.ip_address = "222.222.222.222" transductor.save() modbus = ModbusRTU(transductor) with self.assertRaises(RegisterAddressException): modbus.create_messages()
def setUp(self): t_model = TransductorModel() t_model.name = "TR 4020" t_model.transport_protocol = "UDP" t_model.serial_protocol = "Modbus RTU" t_model.register_addresses = [[68, 0], [70, 1]] t_model.save() self.t_model = t_model transductor = EnergyTransductor() transductor.serie_number = "1" transductor.description = "Test" transductor.model = t_model transductor.ip_address = "111.111.111.111" transductor.save() self.transductor = transductor self.create_energy_measurements()
class TestReportView(unittest.TestCase): def setUp(self): self.client = Client() address = [[68, 0], [70, 1]] self.t_model = TransductorModel(name="Test Name", transport_protocol="UDP", serial_protocol="Modbus RTU", register_addresses=address) self.t_model.save() self.e_transductor = EnergyTransductor( model=self.t_model, serie_number=1, ip_address="1.1.1.1", description="Energy Transductor Test", creation_date=timezone.now()) self.e_transductor.save() self.e_measurements = EnergyMeasurements( transductor=self.e_transductor, voltage_a=122.875, voltage_b=122.784, voltage_c=121.611, current_a=22.831, current_b=17.187, current_c=3.950, active_power_a=2.794, active_power_b=1.972, active_power_c=3.950, reactive_power_a=-0.251, reactive_power_b=-0.752, reactive_power_c=-1.251, apparent_power_a=2.805, apparent_power_b=2.110, apparent_power_c=4.144, collection_minute=timezone.now().minute) self.e_measurements.save() # self.assertEqual(1, 2) def tearDown(self): self.t_model.delete() self.e_transductor.delete() #self.e_measurements.delete() def test_getting_page_report(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/report/' + str(self.e_transductor.id) + '/') print 'aqui' print(EnergyMeasurements.objects.all()) self.assertEqual(200, response.status_code) def test_getting_wrong_page_report(self): response = self.client.post('/reports/reports/', { "username": '******', 'password': '******' }) self.assertEqual(404, response.status_code) def test_getting_pdf(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/open_pdf/1/') self.assertEqual(200, response.status_code) def test_getting_wrong_page_filter(self): response = self.client.post('/reports/transductors_filt/', { "username": '******', 'password': '******' }) self.assertEqual(404, response.status_code) def test_getting_page_transductor_list(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/transductor_list/') self.assertEqual(200, response.status_code) def test_getting_wrong_page_transductor_list(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/transductores_list/') self.assertEqual(404, response.status_code) def test_getting_page_transductor_filter(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/transductors_filter/') self.assertEqual(200, response.status_code) def test_getting_wrong_page_transductor_filter(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/transductores_filter/') self.assertEqual(404, response.status_code) def test_getting_page_invoice(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/invoice/' + str(self.e_transductor.id)) self.assertEqual(301, response.status_code) def test_getting_wrong_page_invoice(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/invoice/') self.assertEqual(404, response.status_code) def test_getting_page_charts(self): logged_in = self.client.login(username='******', password='******') response = self.client.get('/reports/return_chart/') self.assertEqual(200, response.status_code) def test_create_graphic(self): now = datetime.datetime.now() delta = datetime.timedelta(days=1) a = datetime.datetime.today() numdays = 3 dateList = [] for x in range(0, numdays): dateList.append(a - datetime.timedelta(days=x)) self.graphic = views.create_graphic( path='src/report/static/currentGraphic1.png', array_date=[12, 13, 14], array_dateb=[56, 67, 78], array_datec=[34, 45, 56], array_data=dateList, label='Current') image = ("src/report/static/currentGraphic1.png") self.assertTrue(os.path.isfile(image)) self.assertEqual(self.graphic, 'src/report/static/currentGraphic1.png') def test_create_generatePdf(self): views.generatePdf('1') pdf = ("src/report/static/Relatorio1.pdf") self.assertTrue(os.path.isfile(pdf))
def test_not_create_energy_transductor_no_geolocation_longitude(self): size = len(EnergyTransductor.objects.all()) transductor = EnergyTransductor() transductor.serial_number = '123456789' transductor.ip_address = '1.1.1.1' transductor.broken = False transductor.active = True transductor.model = "EnergyTransductor" transductor.firmware_version = '12.1.3215' transductor.physical_location = 'predio 2 sala 44' transductor.geolocation_latitude = -24.4556 transductor.installation_date = datetime.now() with self.assertRaises(DataError): transductor.save()
def test_create_energy_transductor(self): size = len(EnergyTransductor.objects.all()) energy_transductor = EnergyTransductor() energy_transductor.serial_number = '12345678' energy_transductor.ip_address = '1.1.1.1' energy_transductor.broken = False energy_transductor.active = True energy_transductor.model = "EnergyTransductor" energy_transductor.firmware_version = '12.1.3215' energy_transductor.physical_location = 'predio 2 sala 44' energy_transductor.geolocation_longitude = -24.4556 energy_transductor.geolocation_latitude = -24.45996 energy_transductor.installation_date = datetime.now() self.assertIsNone(energy_transductor.save()) self.assertEqual(size + 1, len(EnergyTransductor.objects.all()))