This project demonstrates a Golang application utilizing GORM for ORM and implementing tests for the functionalities.
GORM is a powerful ORM library for Go, designed to simplify database operations and streamline development workflows. This project demonstrates various GORM features and best practices through a simple yet comprehensive example application.
The project consists of the following files:
db.go: Contains the implementation of the database connection using the singleton design pattern.main.go: Defines the main functionalities of the application.main_test.go: Contains test cases for the implemented functionalities.go.modandgo.sum: Files generated by Go modules to manage dependencies.
The project connects to a PostgreSQL database. Modify the connection parameters (user, password, host, port, dbname) in db.go according to your PostgreSQL setup.
GetConnection(): Establishes a connection to the database using the singleton pattern.RefreshDatabase(): Refreshes the database by dropping existing tables and recreating them.SeedUser(): Seeds the database with user data, including calendars and appointments.UserWithRangeAppointmentScope(): Defines a scope to retrieve users with appointments within a specified date range and calendar name.UpdateAppointment(): Updates appointment start time and description based on calendar name and keyword.
TestDatabaseConnectionAndAutoMigrate(): Tests the database connection and auto-migration of tables.TestUserTable(),TestCalendarTable(),TestAppointmentTable(),TestTaskListTable(): Tests the structure of each database table.TestUserCalendarRelationship(),TestCalendarAppointmentRelationship(),TestTaskListAppointmentRelationship(),TestUserAppointmentRelationship(): Tests the relationships between database tables.TestRefreshDatabase(): Tests the refresh database functionality.TestSeedUser(): Tests the seed user functionality.TestUserWithRangeAppointmentScope(): Tests the user appointment scope functionality.TestUpdateAppointment(): Tests the update appointment functionality.
- Each user has a calendar, and each calendar belongs to a user.
- Each user can have several appointments, and each appointment can have several users (through the
appointment_usertable). - Each calendar can have several appointments.
- Each task list can have several appointments.
- Whenever an appointment belongs to a calendar or task list.
- Clone the repository:
git clone <repository-url> - Navigate to the project directory:
cd <project-directory> - Modify the database connection parameters in
db.goif necessary. - Run the tests:
go test -v
- Go 1.16 or higher
- PostgreSQL database
This project was inspired by the need for a practical guide to working with GORM in Golang. it's a good starting point for learning about GORM and getting familiar with its features.