File size: 1,568 Bytes
31dbadc
685180e
ec4d503
685180e
ec4d503
685180e
 
ec4d503
 
 
 
 
 
685180e
31dbadc
685180e
ec4d503
 
 
 
 
 
685180e
 
 
 
ec4d503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685180e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.cs102.attendance;

import com.cs102.attendance.repository.TestConnectionRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class Cs102AttendanceProjectApplicationTests {

	@Autowired
	private DataSource dataSource;

	@Autowired
	private TestConnectionRepository testConnectionRepository;

	@Test
	void contextLoads() {
	}

	@Test
	void testDatabaseConnection() throws SQLException {
		// Test that we can get a connection from the DataSource
		try (Connection connection = dataSource.getConnection()) {
			assertNotNull(connection, "Database connection should not be null");
			assertTrue(connection.isValid(5), "Database connection should be valid");
			
			// Print connection details for verification
			System.out.println("Database URL: " + connection.getMetaData().getURL());
			System.out.println("Database Driver: " + connection.getMetaData().getDriverName());
			System.out.println("Database Version: " + connection.getMetaData().getDatabaseProductVersion());
		}
	}

	@Test
	void testRepositoryConnection() {
		// Test that the repository can execute queries
		assertDoesNotThrow(() -> {
			Integer result = testConnectionRepository.testQuery();
			assertEquals(1, result, "Test query should return 1");
		}, "Repository test query should not throw an exception");
	}

}