Пример #1
0
func (test *TestAreaRepository) TestCreateArea() {
	t := test.T()

	resource.Require(t, resource.Database)

	repo := area.NewAreaRepository(test.DB)

	name := "Area 21"

	i := area.Area{
		Name:    name,
		SpaceID: uuid.NewV4(),
	}

	repo.Create(context.Background(), &i)
	if i.ID == uuid.Nil {
		t.Errorf("Area was not created, ID nil")
	}

	if i.CreatedAt.After(time.Now()) {
		t.Errorf("Area was not created, CreatedAt after Now()?")
	}

	assert.Equal(t, name, i.Name)
}
Пример #2
0
func (test *TestAreaRepository) TestCreateChildArea() {
	t := test.T()

	resource.Require(t, resource.Database)

	repo := area.NewAreaRepository(test.DB)

	name := "Area #24"
	name2 := "Area #24.1"

	i := area.Area{
		Name:    name,
		SpaceID: uuid.NewV4(),
	}
	err := repo.Create(context.Background(), &i)
	assert.Nil(t, err)

	// ltree field doesnt accept "-" , so we will save them as "_"
	expectedPath := strings.Replace((i.ID).String(), "-", "_", -1)
	area2 := area.Area{
		Name:    name2,
		SpaceID: uuid.NewV4(),
		Path:    expectedPath,
	}
	err = repo.Create(context.Background(), &area2)
	assert.Nil(t, err)

	actualArea, err := repo.Load(context.Background(), area2.ID)
	actualPath := actualArea.Path
	require.Nil(t, err)
	require.NotNil(t, actualArea)
	assert.Equal(t, expectedPath, actualPath)

}
Пример #3
0
func (test *TestAreaRepository) TestListImmediateChildrenOfGrandParents() {
	t := test.T()

	resource.Require(t, resource.Database)
	repo := area.NewAreaRepository(test.DB)

	name := "Area #240"
	name2 := "Area #240.1"
	name3 := "Area #240.1.3"

	// *** Create Parent Area ***

	i := area.Area{
		Name:    name,
		SpaceID: uuid.NewV4(),
	}
	err := repo.Create(context.Background(), &i)
	assert.Nil(t, err)

	// *** Create 'son' area ***

	expectedPath := strings.Replace((i.ID).String(), "-", "_", -1)
	area2 := area.Area{
		Name:    name2,
		SpaceID: uuid.NewV4(),
		Path:    expectedPath,
	}
	err = repo.Create(context.Background(), &area2)
	require.Nil(t, err)

	childAreaList, err := repo.ListChildren(context.Background(), &i)
	assert.Equal(t, 1, len(childAreaList))
	require.Nil(t, err)

	// *** Create 'grandson' area ***

	expectedPath = strings.Replace((i.ID).String()+"."+(area2.ID.String()), "-", "_", -1)
	area4 := area.Area{
		Name:    name3,
		SpaceID: uuid.NewV4(),
		Path:    expectedPath,
	}
	err = repo.Create(context.Background(), &area4)
	require.Nil(t, err)

	childAreaList, err = repo.ListChildren(context.Background(), &i)

	// But , There is only 1 'son' .

	require.Nil(t, err)
	assert.Equal(t, 1, len(childAreaList))
	assert.Equal(t, area2.ID, childAreaList[0].ID)

	// *** Confirm the grandson has no son

	childAreaList, err = repo.ListChildren(context.Background(), &area4)
	assert.Equal(t, 0, len(childAreaList))
}
Пример #4
0
func (test *TestAreaRepository) TestListParentTree() {
	t := test.T()

	resource.Require(t, resource.Database)
	repo := area.NewAreaRepository(test.DB)

	name := "Area #240"
	name2 := "Area #240.1"

	// *** Create Parent Area ***

	i := area.Area{
		Name:    name,
		SpaceID: uuid.NewV4(),
	}
	err := repo.Create(context.Background(), &i)
	assert.Nil(t, err)

	// *** Create 'son' area ***

	expectedPath := strings.Replace((i.ID).String(), "-", "_", -1)
	area2 := area.Area{
		Name:    name2,
		SpaceID: uuid.NewV4(),
		Path:    expectedPath,
	}
	err = repo.Create(context.Background(), &area2)
	require.Nil(t, err)

	listOfCreatedID := []uuid.UUID{i.ID, area2.ID}
	listOfCreatedAreas, err := repo.LoadMultiple(context.Background(), listOfCreatedID)

	require.Nil(t, err)
	assert.Equal(t, 2, len(listOfCreatedAreas))

	for i := 0; i < 2; i++ {
		assert.NotNil(t, searchInAreaSlice(listOfCreatedID[i], listOfCreatedAreas))
	}

}
Пример #5
0
func (test *TestAreaRepository) TestListAreaBySpace() {
	t := test.T()

	resource.Require(t, resource.Database)

	repo := area.NewAreaRepository(test.DB)

	spaceID := uuid.NewV4()

	var createdAreaIds []uuid.UUID
	for i := 0; i < 3; i++ {
		name := "Test Area #20" + strconv.Itoa(i)

		a := area.Area{
			Name:    name,
			SpaceID: spaceID,
		}
		err := repo.Create(context.Background(), &a)
		assert.Equal(t, nil, err)
		createdAreaIds = append(createdAreaIds, a.ID)
		t.Log(a.ID)
	}
	err := repo.Create(context.Background(), &area.Area{
		Name:    "Other Test area #20",
		SpaceID: uuid.NewV4(),
	})
	assert.Equal(t, nil, err)

	its, err := repo.List(context.Background(), spaceID)
	assert.Nil(t, err)
	assert.Len(t, its, 3)

	for i := 0; i < 3; i++ {
		assert.NotNil(t, searchInAreaSlice(createdAreaIds[i], its))
	}
}
Пример #6
0
func (test *TestAreaRepository) TestListChildrenOfParents() {
	t := test.T()
	resource.Require(t, resource.Database)
	//test.DBTestSuite.DB = test.DBTestSuite.DB.Debug()
	repo := area.NewAreaRepository(test.DB)

	name := "Area #240"
	name2 := "Area #240.1"
	name3 := "Area #240.2"
	var createdAreaIDs []uuid.UUID
	// *** Create Parent Area ***

	i := area.Area{
		Name:    name,
		SpaceID: uuid.NewV4(),
	}
	err := repo.Create(context.Background(), &i)
	require.Nil(t, err)

	// *** Create 1st child area ***

	// ltree field doesnt accept "-" , so we will save them as "_"
	expectedPath := strings.Replace((i.ID).String(), "-", "_", -1)
	area2 := area.Area{
		Name:    name2,
		SpaceID: uuid.NewV4(),
		Path:    expectedPath,
	}
	err = repo.Create(context.Background(), &area2)
	require.Nil(t, err)
	createdAreaIDs = append(createdAreaIDs, area2.ID)

	actualArea, err := repo.Load(context.Background(), area2.ID)
	actualPath := actualArea.Path
	require.Nil(t, err)
	assert.NotEqual(t, uuid.Nil, area2.Path)
	assert.Equal(t, expectedPath, actualPath) // check that path ( an ltree field ) was populated.

	// *** Create 2nd child area ***

	expectedPath = strings.Replace((i.ID).String(), "-", "_", -1)
	area3 := area.Area{
		Name:    name3,
		SpaceID: uuid.NewV4(),
		Path:    expectedPath,
	}
	err = repo.Create(context.Background(), &area3)
	require.Nil(t, err)
	createdAreaIDs = append(createdAreaIDs, area3.ID)

	actualArea, err = repo.Load(context.Background(), area3.ID)
	require.Nil(t, err)

	actualPath = actualArea.Path
	assert.Equal(t, expectedPath, actualPath)

	// *** Validate that there are 2 children

	childAreaList, err := repo.ListChildren(context.Background(), &i)
	require.Nil(t, err)

	assert.Equal(t, 2, len(childAreaList))

	for i := 0; i < len(createdAreaIDs); i++ {
		assert.NotNil(t, createdAreaIDs[i], childAreaList[i].ID)
	}
}
Пример #7
0
// Areas returns a area repository
func (g *GormBase) Areas() area.Repository {
	return area.NewAreaRepository(g.db)
}