diff --git a/README.md b/README.md index ab507d0..6fb22bb 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,9 @@ go test ./... -v ```shell go build ``` + +# How to add dependencies + +```shell +go get -t ./... +``` diff --git a/server/db/db_test.go b/server/db/db_test.go index 0cfc251..b31d317 100644 --- a/server/db/db_test.go +++ b/server/db/db_test.go @@ -81,6 +81,7 @@ func setup() error { // Create counter table _, err = db.Exec(`CREATE TABLE IF NOT EXISTS counter ( current_value INT NOT NULL, + is_locked BOOLEAN NOT NULL DEFAULT FALSE, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, reseted_at TIMESTAMP NULL DEFAULT NULL )`) @@ -113,12 +114,17 @@ func teardown() error { func TestGetCounter(t *testing.T) { // Insert a row into the counter table - _, err := db.Exec(`INSERT INTO counter (current_value, updated_at, reseted_at) VALUES (42, '2024-05-30 12:34:56', '2024-05-01 12:00:00')`) + _, err := db.Exec(` + INSERT INTO + counter (current_value, is_locked, updated_at, reseted_at) + VALUES + (42, false, '2024-05-30 12:34:56', '2024-05-01 12:00:00') + `) if err != nil { t.Fatalf("failed to insert into table: %s", err) } - counter, err := GetCounter() + counter, err := GetCounter("counter") if err != nil { t.Fatalf("failed to get counter: %s", err) } @@ -126,6 +132,9 @@ func TestGetCounter(t *testing.T) { if counter.CurrentValue != 42 { t.Errorf("expected current_value to be 42, got %d", counter.CurrentValue) } + if counter.IsLocked != false { + t.Errorf("expected isLocked to be true, got %v", counter.IsLocked) + } if counter.UpdatedAt != "2024-05-30T12:34:56Z" { t.Errorf("expected updated_at to be '2024-05-30T12:34:56Z', got %s", counter.UpdatedAt) } @@ -146,7 +155,7 @@ func TestGetCounterEmpty(t *testing.T) { t.Fatalf("failed to clean up table: %s", err) } - counter, err := GetCounter() + counter, err := GetCounter("counter") if err != nil { t.Fatalf("failed to get counter: %s", err) } @@ -154,6 +163,9 @@ func TestGetCounterEmpty(t *testing.T) { if counter.CurrentValue != 0 { t.Errorf("expected current_value to be 0, got %d", counter.CurrentValue) } + if counter.IsLocked != false { + t.Errorf("expected isLocked to be true, got %v", counter.IsLocked) + } if counter.UpdatedAt != "" { t.Errorf("expected updated_at to be '', got %s", counter.UpdatedAt) } @@ -172,13 +184,14 @@ func TestUpdateCounter(t *testing.T) { } // Call the UpdateCounter function - err := UpdateCounter() - if err != nil { - t.Fatalf("failed to upsert counter data: %s", err) + isUpdated := UpdateCounter() + + if !isUpdated { + t.Errorf("expected isUpdated to be true, got %v", isUpdated) } // Test GetCounter function - counter, err := GetCounter() + counter, err := GetCounter("counter") if err != nil { t.Fatalf("failed to get counter: %s", err) } @@ -205,6 +218,10 @@ func TestUpdateCounter(t *testing.T) { t.Errorf("expected reseted_at to be null, got %v", counter.ResetedAt) } + if counter.IsLocked != false { + t.Errorf("expected isLocked to be true, got %v", counter.IsLocked) + } + // Cleanup table after test if _, err = db.Exec(`DELETE FROM counter`); err != nil { t.Fatalf("failed to clean up table: %s", err) @@ -216,7 +233,12 @@ func TestUpdateCounter(t *testing.T) { // one and update the updated_at field to NOW(). func TestUpdateCounterTypicalCase(t *testing.T) { // Insert a row into the counter table - _, ierr := db.Exec(`INSERT INTO counter (current_value, updated_at) VALUES (42, '2024-05-28 12:34:56')`) + _, ierr := db.Exec(` + INSERT INTO + counter (current_value, is_locked, updated_at) + VALUES + (42, false, '2024-05-28 12:34:56') + `) if ierr != nil { t.Fatalf("failed to insert into table: %s", ierr) } @@ -227,12 +249,13 @@ func TestUpdateCounterTypicalCase(t *testing.T) { } // Test UpdateCounter - err := UpdateCounter() - if err != nil { - t.Fatalf("failed to upsert counter data: %s", err) + isUpdated := UpdateCounter() + + if !isUpdated { + t.Errorf("expected isUpdated to be true, got %v", isUpdated) } - counter, err := GetCounter() + counter, err := GetCounter("counter") if err != nil { t.Fatalf("failed to get counter: %s", err) } @@ -257,6 +280,10 @@ func TestUpdateCounterTypicalCase(t *testing.T) { t.Errorf("expected reseted_at to be null, got %v", counter.ResetedAt) } + if counter.IsLocked != false { + t.Errorf("expected isLocked to be true, got %v", counter.IsLocked) + } + // Cleanup table after test if _, err = db.Exec(`DELETE FROM counter`); err != nil { t.Fatalf("failed to clean up table: %s", err) @@ -272,7 +299,11 @@ func TestUpdateCounterTimeDidNotPass(t *testing.T) { parsedUpdatedLessThan24hAgo := updatedLessThan24hAgo.Format(time.RFC3339) // Insert a row into the counter table - _, ierr := db.Exec(`INSERT INTO counter (current_value, updated_at) VALUES (42, $1)`, parsedUpdatedLessThan24hAgo) + _, ierr := db.Exec(` + INSERT INTO + counter (current_value, is_locked, updated_at) + VALUES + (42, false, $1)`, parsedUpdatedLessThan24hAgo) if ierr != nil { t.Fatalf("failed to insert into table: %s", ierr) } @@ -283,12 +314,13 @@ func TestUpdateCounterTimeDidNotPass(t *testing.T) { } // Test UpdateCounter - err := UpdateCounter() - if err != nil { - t.Fatalf("failed to upsert counter data: %s", err) + isUpdated := UpdateCounter() + + if isUpdated { + t.Errorf("expected isUpdated to be false, got %v", isUpdated) } - counter, err := GetCounter() + counter, err := GetCounter("counter") if err != nil { t.Fatalf("failed to get counter: %s", err) } @@ -297,6 +329,10 @@ func TestUpdateCounterTimeDidNotPass(t *testing.T) { t.Errorf("expected current_value to be 42, got %d", counter.CurrentValue) } + if counter.IsLocked != false { + t.Errorf("expected isLocked to be true, got %v", counter.IsLocked) + } + // Check updated_at (should be intact) if counter.UpdatedAt != parsedUpdatedLessThan24hAgo { t.Errorf("expected updated_at: '%s' to not change, got '%s'", parsedUpdatedLessThan24hAgo, counter.UpdatedAt) @@ -315,7 +351,10 @@ func TestUpdateCounterTimeDidNotPass(t *testing.T) { // NOTE: Counter has some value, resetting, should be zero now. func TestResetCounter(t *testing.T) { - _, err := db.Exec(`INSERT INTO counter (current_value, updated_at, reseted_at) VALUES (42, '2024-05-30 12:34:56', '2024-05-01 12:00:00')`) + _, err := db.Exec(` + INSERT INTO counter (current_value, is_locked, updated_at, reseted_at) + VALUES (42, false, '2024-05-30 12:34:56', '2024-05-01 12:00:00') + `) if err != nil { t.Fatalf("failed to insert into table: %s", err) } @@ -335,7 +374,7 @@ func TestResetCounter(t *testing.T) { t.Errorf("expected last value to be 42, got %d", lastValue) } - counter, err := GetCounter() + counter, err := GetCounter("counter") if err != nil { t.Fatalf("failed to get counter: %s", err) } @@ -344,6 +383,10 @@ func TestResetCounter(t *testing.T) { t.Errorf("expected current_value to be 0, got %d", counter.CurrentValue) } + if counter.IsLocked != false { + t.Errorf("expected isLocked to be true, got %v", counter.IsLocked) + } + // Check updated_at (should be close to current time) expectedTime := time.Now().UTC() parsedUpdatedAt, err := time.Parse(time.RFC3339, counter.UpdatedAt) diff --git a/server/db/update_counter.go b/server/db/update_counter.go index d5aa9c0..8c91491 100644 --- a/server/db/update_counter.go +++ b/server/db/update_counter.go @@ -60,9 +60,11 @@ func upsertCounterData(tableName string) (bool, error) { `, tableName) _, err = tx.Exec(insertCounterQuery) + if err != nil { return false, fmt.Errorf("❌ Error inserting new counter row.\n %s", err) } + return true, nil } else { return false, fmt.Errorf("❌ Error querying %s table.\n %s", tableName, err) @@ -147,11 +149,9 @@ func SetCounter(value int) error { func UpdateCounter() bool { isUpdated, err := upsertCounterData("counter") - log.Printf("isUpdated: %v", isUpdated) - log.Printf("err: %v", err) if err != nil { - log.Println("❌ Error updating counter.\n %s", err) + log.Printf("❌ Error updating counter.\n %s", err) } if !isUpdated { @@ -165,7 +165,7 @@ func UpdateOhnoCounter() bool { isUpdated, err := upsertCounterData("ohno_counter") if err != nil { - log.Println("❌ Error updating counter.\n %s", err) + log.Printf("❌ Error updating counter.\n %s", err) } if !isUpdated { diff --git a/server/go.sum b/server/go.sum index c9ad829..6b99c7e 100644 --- a/server/go.sum +++ b/server/go.sum @@ -4,6 +4,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= @@ -124,6 +125,7 @@ github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= @@ -295,9 +297,11 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/rpc v0.0.0-20230731190214-cbb8c96f2d6d h1:pgIUhmqwKOUlnKna4r6amKdUngdL8DrkpFeV8+VBElY= google.golang.org/genproto/googleapis/rpc v0.0.0-20230731190214-cbb8c96f2d6d/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 h1:AB/lmRny7e2pLhFEYIbl5qkDAUt2h0ZRO4wGPhZf+ik= google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405/go.mod h1:67X1fPuzjcrkymZzZV1vvkFeTn2Rvc6lYF9MYFGCcwE= google.golang.org/grpc v1.58.3 h1:BjnpXut1btbtgN/6sp+brB2Kbm2LjNXnidYujAVbSoQ= google.golang.org/grpc v1.58.3/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/server/handlers/counter.go b/server/handlers/counter.go index 99ef079..8b99c14 100644 --- a/server/handlers/counter.go +++ b/server/handlers/counter.go @@ -64,7 +64,6 @@ func IncrementCounter(w http.ResponseWriter, r *http.Request) { if IsOhnoCounterLocked() { log.Printf("😀 Ohno Counter is locked. Proceeding with incrementing counter. Another happy day.") isUpdated := db.UpdateCounter() - log.Printf("isUpdated: %v", isUpdated) if !isUpdated { errResponse := ServerResponse{Message: "Counter not incremented. Conditions not met."}