Skip to content

Commit

Permalink
mock data (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Apr 14, 2024
1 parent 450323d commit 32292b4
Show file tree
Hide file tree
Showing 27 changed files with 1,084 additions and 1,101 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
name: Club Scraper
name: Mock Data

permissions: read-all

on:
push:
paths:
- scraper/club/**
- .github/workflows/club_scraper.yml
- mock_data/**
- .github/workflows/mock_data.yml
pull_request:
types: [opened]
paths:
- scraper/club/**
- .github/workflows/club_scraper.yml
- mock_data/**
- .github/workflows/mock_data.yml

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always
MANIFEST_PATH: ./scraper/clubs/Cargo.toml
MANIFEST_PATH: ./mock_data/Cargo.toml

jobs:
test:
Expand Down
2 changes: 2 additions & 0 deletions backend/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func main() {
configPath := flag.String("config", filepath.Join("..", "..", "config"), "Specify the path to the config directory")
useDevDotEnv := flag.Bool("use-dev-dot-env", true, "Specify if you want to use the .env.dev file")

fmt.Println("foo", *useDevDotEnv)

flag.Parse()

config, err := config.GetConfiguration(*configPath, *useDevDotEnv)
Expand Down
1,989 changes: 1,009 additions & 980 deletions backend/src/migrations/data.sql

Large diffs are not rendered by default.

28 changes: 8 additions & 20 deletions backend/src/models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@ type Event struct {
Waitlist []User `gorm:"many2many:user_event_waitlists;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Clubs []Club `gorm:"many2many:club_events;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Tag []Tag `gorm:"many2many:event_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Notification []Notification `gorm:"polymorphic:Reference;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;;" json:"-" validate:"-"`
Notification []Notification `gorm:"polymorphic:Reference;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
}

type Series struct {
Model
RecurringType RecurringType `gorm:"type:varchar(255);default:open" json:"recurring_type" validate:"max=255"`
SeparationCount int `gorm:"type:int" json:"separation_count" validate:"min=0"`
MaxOccurrences int `gorm:"type:int" json:"max_occurrences" validate:"min=1"`
DayOfWeek int `gorm:"type:int" json:"day_of_week" validate:"min=1,max=7"`
WeekOfMonth int `gorm:"type:int" json:"week_of_month" validate:"min=1,max=5"`
DayOfMonth int `gorm:"type:int" json:"day_of_month" validate:"min=1,max=31"`
Events []Event `gorm:"many2many:event_series;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"events" validate:"-"`
RecurringType RecurringType `gorm:"type:varchar(255);default:open" json:"recurring_type" validate:"max=255"`
MaxOccurrences int `gorm:"type:int" json:"max_occurrences" validate:"min=1"`
Events []Event `gorm:"many2many:event_series;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"events" validate:"-"`
}

// TODO: add not null to required fields on all gorm models
Expand Down Expand Up @@ -93,12 +89,8 @@ type CreateEventRequestBody struct {
}

type CreateSeriesRequestBody struct {
RecurringType RecurringType `json:"recurring_type" validate:"required,max=255,oneof=daily weekly monthly"`
SeparationCount int `json:"separation_count" validate:"required,min=0"`
MaxOccurrences int `json:"max_occurrences" validate:"required,min=2"`
DayOfWeek int `json:"day_of_week" validate:"required,min=1,max=7"`
WeekOfMonth int `json:"week_of_month" validate:"required,min=1,max=5"`
DayOfMonth int `json:"day_of_month" validate:"required,min=1,max=31"`
RecurringType RecurringType `json:"recurring_type" validate:"required,max=255,oneof=daily weekly monthly"`
MaxOccurrences int `json:"max_occurrences" validate:"required,min=2"`
}

type UpdateEventRequestBody struct {
Expand All @@ -119,12 +111,8 @@ type UpdateEventRequestBody struct {

// TODO: probably need to make changes to this to update the events as well
type UpdateSeriesRequestBody struct {
RecurringType RecurringType `json:"recurring_type" validate:"omitempty,max=255,oneof=daily weekly monthly"`
SeparationCount int `json:"separation_count" validate:"omitempty,min=0"`
MaxOccurrences int `json:"max_occurrences" validate:"omitempty,min=2"`
DayOfWeek int `json:"day_of_week" validate:"omitempty,min=1,max=7"`
WeekOfMonth int `json:"week_of_month" validate:"omitempty,min=1,max=5"`
DayOfMonth int `json:"day_of_month" validate:"omitempty,min=1,max=31"`
RecurringType RecurringType `json:"recurring_type" validate:"omitempty,max=255,oneof=daily weekly monthly"`
MaxOccurrences int `json:"max_occurrences" validate:"omitempty,min=2"`

EventDetails UpdateEventRequestBody `json:"event_details" validate:"omitempty"`
}
6 changes: 3 additions & 3 deletions backend/src/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ func createEventSlice(firstEvent *models.Event, series models.Series) []models.E

switch series.RecurringType {
case "daily":
days = series.SeparationCount + 1
days = 1
case "weekly":
days = 7 * (series.SeparationCount + 1)
days = 7
case "monthly":
months = series.SeparationCount + 1
months = 1
}

for i := 1; i < series.MaxOccurrences; i++ {
Expand Down
10 changes: 1 addition & 9 deletions backend/src/transactions/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,7 @@ func CreateEvent(db *gorm.DB, event models.Event) ([]models.Event, *errors.Error
}

func CreateEventSeries(db *gorm.DB, series models.Series) ([]models.Event, *errors.Error) {
tx := db.Begin()

if err := tx.Create(&series).Error; err != nil {
tx.Rollback()
return nil, &errors.FailedToCreateEventSeries
}

if err := tx.Commit().Error; err != nil {
tx.Rollback()
if err := db.Create(&series).Error; err != nil {
return nil, &errors.FailedToCreateEventSeries
}

Expand Down
56 changes: 14 additions & 42 deletions backend/tests/api/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ func SampleEventFactory() *map[string]interface{} {
func SampleSeriesFactory() *map[string]interface{} {
return CustomSampleSeriesFactory(
models.CreateSeriesRequestBody{
RecurringType: "daily",
MaxOccurrences: 10,
SeparationCount: 4,
DayOfWeek: 3,
WeekOfMonth: 2,
DayOfMonth: 1,
RecurringType: "daily",
MaxOccurrences: 10,
},
)
}
Expand Down Expand Up @@ -347,12 +343,8 @@ func AssertCreateBadEventSeriesDataFails(t *testing.T, badSeries models.CreateSe
func TestCreateSeriesFailsOnInvalidRecurringType(t *testing.T) {
AssertCreateBadEventSeriesDataFails(t,
models.CreateSeriesRequestBody{
RecurringType: "annually",
MaxOccurrences: 10,
SeparationCount: 0,
DayOfWeek: 3,
WeekOfMonth: 2,
DayOfMonth: 1,
RecurringType: "annually",
MaxOccurrences: 10,
},
errors.FailedToValidateEventSeries,
)
Expand All @@ -361,12 +353,8 @@ func TestCreateSeriesFailsOnInvalidRecurringType(t *testing.T) {
func TestCreateSeriesFailsOnInvalidMaxOccurrences(t *testing.T) {
AssertCreateBadEventSeriesDataFails(t,
models.CreateSeriesRequestBody{
RecurringType: "weekly",
MaxOccurrences: -1,
SeparationCount: 0,
DayOfWeek: 3,
WeekOfMonth: 2,
DayOfMonth: 1,
RecurringType: "weekly",
MaxOccurrences: -1,
},
errors.FailedToValidateEventSeries,
)
Expand All @@ -375,12 +363,8 @@ func TestCreateSeriesFailsOnInvalidMaxOccurrences(t *testing.T) {
func TestCreateSeriesFailsOnInvalidSeparationCount(t *testing.T) {
AssertCreateBadEventSeriesDataFails(t,
models.CreateSeriesRequestBody{
RecurringType: "weekly",
MaxOccurrences: 10,
SeparationCount: -1,
DayOfWeek: 3,
WeekOfMonth: 2,
DayOfMonth: 1,
RecurringType: "weekly",
MaxOccurrences: 10,
},
errors.FailedToValidateEventSeries,
)
Expand All @@ -389,12 +373,8 @@ func TestCreateSeriesFailsOnInvalidSeparationCount(t *testing.T) {
func TestCreateSeriesFailsOnInvalidDayOfWeek(t *testing.T) {
AssertCreateBadEventSeriesDataFails(t,
models.CreateSeriesRequestBody{
RecurringType: "weekly",
MaxOccurrences: 10,
SeparationCount: 0,
DayOfWeek: 8,
WeekOfMonth: 2,
DayOfMonth: 1,
RecurringType: "weekly",
MaxOccurrences: 10,
},
errors.FailedToValidateEventSeries,
)
Expand All @@ -403,12 +383,8 @@ func TestCreateSeriesFailsOnInvalidDayOfWeek(t *testing.T) {
func TestCreateSeriesFailsOnInvalidWeekOfMonth(t *testing.T) {
AssertCreateBadEventSeriesDataFails(t,
models.CreateSeriesRequestBody{
RecurringType: "weekly",
MaxOccurrences: 10,
SeparationCount: 0,
DayOfWeek: 5,
WeekOfMonth: -5,
DayOfMonth: 1,
RecurringType: "weekly",
MaxOccurrences: 10,
},
errors.FailedToValidateEventSeries,
)
Expand All @@ -417,12 +393,8 @@ func TestCreateSeriesFailsOnInvalidWeekOfMonth(t *testing.T) {
func TestCreateSeriesFailsOnInvalidDayOfMonth(t *testing.T) {
AssertCreateBadEventSeriesDataFails(t,
models.CreateSeriesRequestBody{
RecurringType: "weekly",
MaxOccurrences: 10,
SeparationCount: 0,
DayOfWeek: 5,
WeekOfMonth: 2,
DayOfMonth: 42,
RecurringType: "weekly",
MaxOccurrences: 10,
},
errors.FailedToValidateEventSeries,
)
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func MigrateCommand() *cli.Command {
func Migrate() error {
fmt.Println("Migrating database")

goCmd := exec.Command("go", "run", "main.go", "--only-migrate")
goCmd := exec.Command("go", "run", "main.go", "--only-migrate", "--use-dev-dot-env=false")
goCmd.Dir = BACKEND_SRC_DIR

output, err := goCmd.CombinedOutput()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{error::Error, fs::File, io::Write};

use crate::domain::Category;

pub fn dump(categories: &Vec<Category>, file: &mut File) -> Result<(), Box<dyn Error>> {
pub fn dump(categories: &[Category], file: &mut File) -> Result<(), Box<dyn Error>> {
for category in categories {
writeln!(
file,
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions mock_data/src/dumper/dump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use chrono::Local;
use std::{error::Error, fs::File, io::Write};

use crate::domain::{club::Club, Category, Tag};

fn autogen_commenter<F>(file: &mut File, func: F) -> Result<(), Box<dyn Error>>
where
F: FnOnce(&mut File) -> Result<(), Box<dyn Error>>,
{
writeln!(file, "-- AUTOGENERATED MOCK DATA, DO NOT MODIFY")?;
writeln!(
file,
"-- GENERATED AT {}",
Local::now().format("%Y-%m-%d %H:%M:%S")
)?;
func(file)?;
writeln!(file, "-- END AUTOGENERATED MOCK DATA")?;
Ok(())
}

pub fn dump_all(
categories: &[Category],
tags: &[&Tag],
clubs: Vec<Club>,
club_parent: uuid::Uuid,
file: &mut File,
) -> Result<(), Box<dyn Error>> {
autogen_commenter(file, |file| {
writeln!(file, "BEGIN;")?;
crate::dumper::category::dump(categories, file)?;
crate::dumper::tag::dump(tags, file)?;
crate::dumper::club::dump(clubs, file, club_parent)?;
writeln!(file, "COMMIT;")?;
Ok(())
})?;

Ok(())
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{error::Error, fs::File, io::Write};

use crate::domain::tag::Tag;

pub fn dump(tags: &Vec<&'static Tag>, file: &mut File) -> Result<(), Box<dyn Error>> {
pub fn dump(tags: &[&Tag], file: &mut File) -> Result<(), Box<dyn Error>> {
for tag in tags {
writeln!(
file,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 0 additions & 38 deletions scraper/clubs/src/dumper/dump.rs

This file was deleted.

0 comments on commit 32292b4

Please sign in to comment.