-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(backend): fix test for nocodb related
- Loading branch information
Showing
16 changed files
with
397 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package nocodb_test | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"net/http/httptest" | ||
"os" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"conf/nocodb" | ||
"conf/nocodb/nocodbmock" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var client *nocodb.Client | ||
var tableId string | ||
|
||
func TestMain(m *testing.M) { | ||
baseUrl := os.Getenv("NOCODB_BASE_URL") | ||
apiToken := os.Getenv("NOCODB_API_KEY") | ||
tableId = os.Getenv("NOCODB_TABLE_ID") | ||
if tableId == "" { | ||
tableId = "aabbcc" | ||
} | ||
|
||
var err error = nil | ||
var mockServer *httptest.Server = nil | ||
if baseUrl != "" && apiToken != "" { | ||
client, err = nocodb.NewClient(nocodb.ClientOptions{ | ||
ApiToken: apiToken, | ||
BaseUrl: baseUrl, | ||
}) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("creating nocodb client") | ||
return | ||
} | ||
} else { | ||
mockServer, err = nocodbmock.NewNocoDBMockServer() | ||
if err != nil { | ||
mockServer.Close() | ||
log.Fatal().Err(err).Msg("creating mock server") | ||
return | ||
} | ||
|
||
client, err = nocodb.NewClient(nocodb.ClientOptions{ | ||
ApiToken: "testing", | ||
BaseUrl: mockServer.URL, | ||
HttpClient: mockServer.Client(), | ||
}) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("creating nocodb client") | ||
return | ||
} | ||
} | ||
|
||
exitCode := m.Run() | ||
|
||
if mockServer != nil { | ||
mockServer.Close() | ||
} | ||
|
||
os.Exit(exitCode) | ||
} | ||
|
||
type testBody struct { | ||
Id int64 `json:"Id,omitempty"` | ||
Title string | ||
Age int | ||
RandomText string | ||
} | ||
|
||
func TestIntegration(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
randomBytes := make([]byte, 16) | ||
rand.Read(randomBytes) | ||
randomText := base64.StdEncoding.EncodeToString(randomBytes) | ||
payload := testBody{ | ||
Title: "John Doe", | ||
Age: 49, | ||
RandomText: randomText, | ||
} | ||
|
||
err := client.CreateTableRecords(ctx, tableId, []any{payload}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
var outPayload []testBody | ||
pageInfo, err := client.ListTableRecords(ctx, tableId, &outPayload, nocodb.ListTableRecordOptions{}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
found := false | ||
foundPayload := testBody{} | ||
for _, out := range outPayload { | ||
if out.RandomText == randomText { | ||
found = true | ||
foundPayload = out | ||
} | ||
} | ||
if !found { | ||
t.Errorf("expecting just inserted entry to be found, got not found") | ||
} | ||
if pageInfo.TotalRows <= 0 { | ||
t.Errorf("expecting pageInfo.TotalRows to be a positive number greater than one, got %d", pageInfo.TotalRows) | ||
} | ||
|
||
err = client.UpdateTableRecords(ctx, tableId, []any{map[string]any{"Id": foundPayload.Id, "Age": 320}}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
var anotherOutPayload testBody | ||
err = client.ReadTableRecords(ctx, tableId, strconv.FormatInt(foundPayload.Id, 10), &anotherOutPayload, nocodb.ReadTableRecordsOptions{}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
if anotherOutPayload.Age != 320 { | ||
t.Errorf("expecting Age to be updated to 320, got %d", anotherOutPayload.Age) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package ticketing_test | ||
|
||
import ( | ||
"context" | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"errors" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"conf/ticketing" | ||
"conf/user" | ||
) | ||
|
||
func TestTicketDomain_StorePaymentReceipt(t *testing.T) { | ||
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("generating new ed25519 key: %s", err.Error()) | ||
return | ||
} | ||
|
||
ticketDomain, err := ticketing.NewTicketDomain(database, bucket, privateKey, publicKey, mailSender, tableId) | ||
if err != nil { | ||
t.Fatalf("creating a ticket domain instance: %s", err.Error()) | ||
} | ||
|
||
userDomain, err := user.NewUserDomain(database, "testing") | ||
if err != nil { | ||
t.Fatalf("creating user domain instance: %s", err.Error()) | ||
} | ||
|
||
t.Run("Invalid photo", func(t *testing.T) { | ||
err := ticketDomain.StorePaymentReceipt(context.Background(), user.User{}, nil, "") | ||
if err == nil { | ||
t.Error("expecting an error, got nil instead") | ||
} | ||
|
||
var validationError *ticketing.ValidationError | ||
if errors.As(err, &validationError) { | ||
if len(validationError.Errors) != 2 { | ||
t.Errorf("expecting two errors, got %d", len(validationError.Errors)) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Happy scenario", func(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancel() | ||
|
||
email := "[email protected]" | ||
err := userDomain.CreateParticipant(ctx, user.CreateParticipantRequest{ | ||
Name: "John Doe", | ||
Email: email, | ||
}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
err = ticketDomain.StorePaymentReceipt(ctx, user.User{Email: email}, strings.NewReader("Hello world! This is not a photo. Yet this will be a text file."), "text/plain") | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
}) | ||
|
||
t.Run("Update data if email already exists", func(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancel() | ||
|
||
email := "[email protected]" | ||
err := userDomain.CreateParticipant(ctx, user.CreateParticipantRequest{ | ||
Name: "John Doe", | ||
Email: email, | ||
}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
user := user.User{ | ||
Email: email, | ||
} | ||
|
||
// First attempt | ||
err = ticketDomain.StorePaymentReceipt(ctx, user, strings.NewReader("Hello world! This is not a photo. Yet this will be a text file."), "text/plain") | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
|
||
// Second attempt, should not return error | ||
err = ticketDomain.StorePaymentReceipt(ctx, user, strings.NewReader("Hello world! This is not a photo. Yet this will be a text file."), "text/plain") | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.