package model

import (
	"testing"

	"github.com/onsi/gomega"

	"github.com/pingcap-inc/clinic/pkg/db"
)

func TestGetOrgQuotaByOrgID(t *testing.T) {
	g := gomega.NewGomegaWithT(t)
	orgLimit := db.OrgQuota{
		OrgID:                       1,
		DataSizeBytesPerCluster:     2,
		RebuildDataTTL:              3,
		DownloadCntPerClusterPerDay: 4,
		PackageTTL:                  5,
		FileLenBytes:                6,
		UploadCntPerClusterPerDay:   7,
		ClusterCnt:                  8,
		ReportCntPerClusterPerDay:   9,
	}
	err := UpsertOrgQuota(&orgLimit)
	g.Expect(err).To(gomega.Succeed())
	err = UpsertOrgQuota(&db.OrgQuota{OrgID: 2})
	g.Expect(err).To(gomega.Succeed())
	{
		ol, found, err := FindOrgQuotaByOrgID(1)
		g.Expect(err).To(gomega.Succeed())
		g.Expect(found).To(gomega.BeTrue())
		g.Expect(ol).NotTo(gomega.BeNil())
		g.Expect(ol.DataSizeBytesPerCluster == orgLimit.DataSizeBytesPerCluster).To(gomega.BeTrue())
	}
	{
		ol, found, err := FindOrgQuotaByOrgID(2)
		g.Expect(err).To(gomega.Succeed())
		g.Expect(found).To(gomega.BeTrue())
		g.Expect(ol).NotTo(gomega.BeNil())
		g.Expect(ol.DataSizeBytesPerCluster == 53687091200).To(gomega.BeTrue())
		g.Expect(ol.ClusterCnt == 10).To(gomega.BeTrue())
	}
	{
		ol, found, err := FindOrgQuotaByOrgID(3)
		g.Expect(err).To(gomega.Succeed())
		g.Expect(found).To(gomega.BeFalse())
		g.Expect(ol).To(gomega.BeNil())
	}
}
