github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/database/timestamp_test.go (about)

     1  package database_test
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/quickfeed/quickfeed/database"
    11  	"github.com/quickfeed/quickfeed/qf"
    12  	"google.golang.org/protobuf/types/known/timestamppb"
    13  	"gorm.io/gorm/schema"
    14  )
    15  
    16  func TestTimestampSerializer_Value(t *testing.T) {
    17  	ctx := context.Background()
    18  	now := time.Now()
    19  	tests := []struct {
    20  		name       string
    21  		fieldValue interface{}
    22  		want       interface{}
    23  		wantErr    bool
    24  	}{
    25  		{
    26  			name:       "correct timestamp, current time",
    27  			fieldValue: timestamppb.New(now),
    28  			want:       now,
    29  			wantErr:    false,
    30  		},
    31  		{
    32  			name:       "correct timestamp, preset time",
    33  			fieldValue: timestamppb.New(time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC)),
    34  			want:       time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC),
    35  			wantErr:    false,
    36  		},
    37  		{
    38  			name:       "incorrect type: time.Time",
    39  			fieldValue: time.Now(),
    40  			want:       nil,
    41  			wantErr:    true,
    42  		},
    43  		{
    44  			name:       "incorrect type: string",
    45  			fieldValue: "2022-11-11T23:59:00",
    46  			want:       nil,
    47  			wantErr:    true,
    48  		},
    49  		{
    50  			name:       "empty interface",
    51  			fieldValue: nil,
    52  			want:       nil,
    53  			wantErr:    false,
    54  		},
    55  	}
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			ts := database.TimestampSerializer{}
    59  			got, err := ts.Value(ctx, &schema.Field{}, reflect.Value{}, tt.fieldValue)
    60  			if (err != nil) != tt.wantErr {
    61  				t.Errorf("%s: expected error: %v, got = %v, ", tt.name, tt.wantErr, err)
    62  			}
    63  			if diff := cmp.Diff(tt.want, got); diff != "" {
    64  				t.Errorf("mismatch timestamp (-want +got):\n%s", diff)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestTimestampSerializer_Nil_Value(t *testing.T) {
    71  	ctx := context.Background()
    72  	assignment := &qf.Assignment{
    73  		Order:    1,
    74  		CourseID: 1,
    75  		// Deadline is nil
    76  	}
    77  	ts := database.TimestampSerializer{}
    78  	got, _ := ts.Value(ctx, &schema.Field{}, reflect.Value{}, assignment.Deadline)
    79  	if got != nil {
    80  		t.Errorf("expected nil, got = %v, ", got)
    81  	}
    82  }
    83  
    84  func TestTimestampSerializer_Scan(t *testing.T) {
    85  	ctx := context.Background()
    86  	ts := database.TimestampSerializer{}
    87  	tests := []struct {
    88  		name    string
    89  		field   *schema.Field
    90  		dst     reflect.Value
    91  		dbValue interface{}
    92  		wantErr bool
    93  	}{
    94  		{
    95  			name:    "incorrect db type",
    96  			field:   &schema.Field{},
    97  			dst:     reflect.Value{},
    98  			dbValue: "2022-01-24 14:03:00 +0000 UTC",
    99  			wantErr: true,
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			err := ts.Scan(ctx, tt.field, tt.dst, tt.dbValue)
   105  			if (err != nil) != tt.wantErr {
   106  				t.Errorf("%s: expected error: %v, got = %v, ", tt.name, tt.wantErr, err)
   107  			}
   108  		})
   109  	}
   110  }