Skip to content
Snippets Groups Projects
Commit 81f1dc80 authored by alita's avatar alita Committed by Tahir, Ali (UG - Comp Sci & Elec Eng)
Browse files

Changed test

parent 82d8f95f
No related branches found
No related tags found
No related merge requests found
package com.com1028.cw.mt00969;
import static org.junit.Assert.assertEquals;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class RequirementTests {
private Connection connect;
private Statement statement;
@Before
public void setUp() throws Exception {
String db = "jdbc:mysql://localhost:3306/classicmodels?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
try {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
connect = DriverManager.getConnection(db, "root", "1234");
} catch (Exception e) {
System.out.println(e);
}
}
@After
public void tearDown() throws Exception {
connect.close();
}
@Test
public void TestReq1() {
String Actual = "";
String Result = "";
String Expected = "";
int counter = 0;
String print = "";
try {
// This is our prepared query, that selects everything from book
// table
String query = "select * from customers where salesRepEmployeeNumber is null ORDER BY customerNumber asc";
statement = connect.createStatement();
// Executes the query and stores the results.
ResultSet results = statement.executeQuery(query);
while (results.next()) {
/*
* Assign results from query to their own variable. We can reference columns by
* their name of index value e.g. 0
*/
Result += "\ncustomerName: " + results.getString("customerName") + "\n" + "contactFullName: "
+ results.getString("contactFirstName") + results.getString("contactLastName") + "\n" + "City: "
+ results.getString("city") + "\n";
counter++;
print = "\n" + counter + " records";
Expected = Result.concat(print);
}
} catch (SQLException e) {
e.printStackTrace();
}
// Prints results to console
BaseQuery.getData();
Actual = BaseQuery.showAllCustomers();
System.out.println(Actual);
assertEquals(Expected, Actual);
}
@Test
public void TestReq2() {
String Actual = "";
String Result = "";
String Expected = "";
int counter = 0;
String print = "";
String pattern = "###,###.00";
DecimalFormat df = new DecimalFormat(pattern);
try {
// This is our prepared query, that selects everything from book
// table
String query = "select customers.customerNumber, customerName, contactFirstName,contactLastName, payments.amount from customers inner join payments on payments.customerNumber=customers.customerNumber where payments.amount > 100000 order by amount desc";
statement = connect.createStatement();
// Executes the query and stores the results.
ResultSet results = statement.executeQuery(query);
while (results.next()) {
/*
* Assign results from query to their own variable. We can reference columns by
* their name of index value e.g. 0
*/
Result += "customerNumber: " + results.getInt("customerNumber") + "\n" + "customerName: "
+ results.getString("customerName") + "\n" + "contactFullName: "
+ results.getString("contactFirstName") + " " + results.getString("contactLastName") + "\n"
+ "amount: $" + df.format(results.getDouble("amount")) + "\n\n";
counter++;
print = counter + " records";
Expected = Result.concat(print);
}
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
Actual = BaseQuery.showAllPayments();
System.out.println(Actual);
assertEquals(Expected, Actual);
}
@Test
public void TestReq3() {
String Actual = "";
String Result = "";
String Expected = "";
int counter = 0;
String print = "";
String pattern = "###,###.00";
DecimalFormat df = new DecimalFormat(pattern);
try {
// This is our prepared query, that selects everything from book
// table
String query = "select orderNumber, orderDate, shippedDate, customers.customerNumber, customers.customerName, avg(TIMESTAMPDIFF(DAY,orderDate,shippedDate)) as'AverageDeliveryTimeInDays' from orders inner join customers on customers.customerNumber = orders.customerNumber where shippedDate is not null group by orders.customerNumber order by AverageDeliveryTimeInDays desc, customerNumber asc";
statement = connect.createStatement();
// Executes the query and stores the results.
ResultSet results = statement.executeQuery(query);
while (results.next()) {
/*
* Assign results from query to their own variable. We can reference columns by
* their name of index value e.g. 0
*/
Result += "CustomerName: " + results.getString("customerName") + "\n" + "CustomerNumber: "
+ results.getString("customerNumber") + "\n" + "AverageDeliveryTime: "
+ df.format(results.getDouble("AverageDeliveryTimeInDays")) + " days\n" + "\n";
counter++;
print = counter + " records\n";
Expected = Result.concat(print);
}
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
Actual = BaseQuery.showAllOrders();
System.out.println(Actual);
assertEquals(Expected, Actual);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment