Exemplo n.º 1
0
func RunTableDiscriminator(TM ITransactionManager, t *testing.T) {
	ResetDB3(TM)

	store := TM.Store()
	var statuses []*Status
	err := store.Query(STATUS).
		All().
		List(&statuses)

	if err != nil {
		t.Fatalf("Failed Query in TestTableDiscriminator: %s", err)
	}

	if len(statuses) != 4 {
		t.Fatalf("Expected 4 Statuses, but got %v", len(statuses))
	}

	for k, v := range statuses {
		logger.Debugf("Statuss[%v] = %s", k, v.String())
		if v.Id == nil {
			t.Fatal("Expected a valid Id, but got nil")
		}
	}

	var tmp int64
	status := statuses[0]
	status.Code = ext.String("X")
	status.Description = ext.String("Unknown")
	tmp, err = store.Update(STATUS).Submit(status)
	if err != nil {
		t.Fatalf("Failed Update in TestTableDiscriminator: %s", err)
	}
	if tmp != 1 {
		t.Fatalf("Expected 1 rows updates, but got %v", tmp)
	}

	tmp, err = store.Delete(STATUS).Execute()
	if err != nil {
		t.Fatalf("Failed Delete in TestTableDiscriminator: %s", err)
	}
	if tmp != 4 {
		t.Fatalf("Expected 4 rows deleted, but got %v", tmp)
	}

	status = new(Status)
	status.Code = ext.String("X")
	status.Description = ext.String("Unknown")
	tmp, err = store.Insert(STATUS).Submit(status)
	if err != nil {
		t.Fatalf("Failed Insert in TestTableDiscriminator: %s", err)
	}
	if tmp == 0 {
		t.Fatal("Expected Id different of 0")
	}

}
Exemplo n.º 2
0
func RunInsertStructReturningKey(TM ITransactionManager, t *testing.T) {
	ResetDB(TM)

	var err error
	if err = TM.Transaction(func(store IDb) error {
		var pub Publisher
		pub.Name = ext.String("Untited Editors")
		key, err := store.Insert(PUBLISHER).Submit(&pub) // passing as a pointer
		if err != nil {
			return err
		}

		if key == 0 {
			t.Fatal("The Auto Insert Key for the ID column was not retrived")
		}

		if key != *pub.Id {
			t.Fatal("The Auto Insert Key for the ID field was not set")
		}

		var pubPtr = new(Publisher)
		pubPtr.Name = ext.String("Untited Editors")
		key, err = store.Insert(PUBLISHER).Submit(pubPtr)
		if err != nil {
			return err
		}

		if key == 0 {
			t.Fatal("The Auto Insert Key for the ID column was not retrived")
		}

		pub = Publisher{}
		pubPtr.Name = ext.String("Untited Editors")
		err = store.Create(&pub)
		if err != nil {
			return err
		}

		if pub.Id == nil || *pub.Id == 0 {
			t.Fatal("The Auto Insert Key for the ID column was not set")
		}

		if pub.Version == 0 {
			t.Fatal("Version column was not set")
		}

		return nil
	}); err != nil {
		t.Fatalf("Failed Struct Insert Return Key: %s", err)
	}
}
Exemplo n.º 3
0
func RunStructUpdate(TM ITransactionManager, t *testing.T) {
	ResetDB(TM)

	var err error
	if err = TM.Transaction(func(store IDb) error {
		var publisher Publisher
		publisher.Name = ext.String("Untited Editors")
		publisher.Id = ext.Int64(1)
		publisher.Version = 1
		affectedRows, err := store.Update(PUBLISHER).Submit(&publisher) // passing as a pointer
		if err != nil {
			t.Fatalf("Failed RunStructUpdate: %s", err)
		}

		if affectedRows != 1 {
			t.Fatal("The record was not updated")
		}

		if publisher.Version != 2 {
			t.Fatalf("Expected Version = 2, got %v", publisher.Version)
		}

		publisher.Name = ext.String("Super Duper Test")
		ok, err := store.Modify(&publisher)
		if err != nil {
			t.Fatalf("Failed RunStructUpdate: %s", err)
		}

		if !ok {
			t.Fatal("The record was not Modifyied")
		}

		if publisher.Version != 3 {
			t.Fatalf("Expected Version = 3, got %v", publisher.Version)
		}

		return nil
	}); err != nil {
		t.Fatalf("Failed Update Test: %s", err)
	}
}
Exemplo n.º 4
0
func RunRemoveAll(TM ITransactionManager, t *testing.T) {
	ResetDB2(TM)

	// get the database context
	store := TM.Store()
	// the target entity
	affected, err := store.RemoveAll(Project{StatusCod: ext.String("DEV")})
	if err != nil {
		t.Fatalf("Failed RunRemoveAll: %s", err)
	}
	if affected != 2 {
		t.Fatalf("Failed RunRemoveAll: Expected 2 deleted Projects, got %s", affected)
	}
}
Exemplo n.º 5
0
func RunStructSaveAndRetrive(TM ITransactionManager, t *testing.T) {
	ResetDB(TM)

	var err error
	if err = TM.Transaction(func(store IDb) error {
		var publisher Publisher
		// === save insert ===
		publisher.Name = ext.String("Super Duper Test")
		ok, err := store.Save(&publisher)
		if err != nil {
			t.Fatalf("Failed RunStructSaveAndRetrive: %s", err)
		}

		if !ok {
			t.Fatal("The record was not Saved")
		}

		if publisher.Version != 1 {
			t.Fatalf("Expected Version = 1, got %v", publisher.Version)
		}

		// === check insert ===
		var oldPub Publisher
		ok, err = store.Retrive(&oldPub, publisher.Id)
		if err != nil {
			t.Fatalf("Failed RunStructSaveAndRetrive: %s", err)
		}

		if !ok {
			t.Fatal("The record was not Saved")
		}

		if publisher.Version != oldPub.Version {
			t.Fatalf("Expected same Version = 1, got %v", oldPub.Version)
		}

		// === save update ===
		publisher.Name = ext.String("UPDDATE: Super Duper Test")
		ok, err = store.Save(&publisher)
		if err != nil {
			t.Fatalf("Failed RunStructSaveAndRetrive: %s", err)
		}

		if !ok {
			t.Fatal("The record was not Saved")
		}

		if publisher.Version != 2 {
			t.Fatalf("Expected Version = 2, got %v", publisher.Version)
		}

		// === check update ===
		oldPub = Publisher{}
		ok, err = store.Retrive(&oldPub, publisher.Id)
		if err != nil {
			t.Fatalf("Failed RunStructSaveAndRetrive: %s", err)
		}

		if !ok {
			t.Fatal("The record was not Saved")
		}

		if publisher.Version != oldPub.Version {
			t.Fatalf("Expected same Version, got %v", oldPub.Version)
		}

		// === check optimistic lock ===
		publisher.Version = 1 // invalid version
		ok, err = store.Save(&publisher)
		fail, _ := err.(*dbx.OptimisticLockFail)
		if fail == nil {
			t.Fatalf("Failed RunStructSaveAndRetrive: %s", err)
		}

		if ok {
			t.Fatal("The record was Saved, without an optimistic lock fail")
		}

		return nil
	}); err != nil {
		t.Fatalf("Failed Update Test: %s", err)
	}
}