feat(store): sqlc-запросы, dto TemplateDoc, Repository, интеграционные тесты CRUD
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package dto
|
||||
|
||||
import "github.com/vasyakrg/dns-autoresolver/internal/model"
|
||||
|
||||
// RecordDTO is the JSONB representation of one DNS record in a template.
|
||||
type RecordDTO struct {
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
TTL int `json:"ttl"`
|
||||
Values []string `json:"values"`
|
||||
}
|
||||
|
||||
// TemplateDoc is stored in templates.doc (jsonb).
|
||||
type TemplateDoc struct {
|
||||
Records []RecordDTO `json:"records"`
|
||||
}
|
||||
|
||||
func FromModel(recs []model.Record) TemplateDoc {
|
||||
out := TemplateDoc{Records: make([]RecordDTO, 0, len(recs))}
|
||||
for _, r := range recs {
|
||||
out.Records = append(out.Records, RecordDTO{
|
||||
Type: string(r.Type), Name: r.Name, TTL: r.TTL, Values: r.Values,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (d TemplateDoc) ToModel() []model.Record {
|
||||
out := make([]model.Record, 0, len(d.Records))
|
||||
for _, r := range d.Records {
|
||||
out = append(out, model.Record{
|
||||
Type: model.RecordType(r.Type), Name: r.Name, TTL: r.TTL, Values: r.Values,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/vasyakrg/dns-autoresolver/internal/model"
|
||||
)
|
||||
|
||||
func TestTemplateDocRoundTrip(t *testing.T) {
|
||||
recs := []model.Record{
|
||||
{Type: model.A, Name: "www.example.com.", TTL: 300, Values: []string{"1.2.3.4"}},
|
||||
{Type: model.MX, Name: "example.com.", TTL: 3600, Values: []string{"10 mx1.example.com."}},
|
||||
}
|
||||
doc := FromModel(recs)
|
||||
if len(doc.Records) != 2 {
|
||||
t.Fatalf("want 2 records, got %d", len(doc.Records))
|
||||
}
|
||||
back := doc.ToModel()
|
||||
if len(back) != 2 || back[0].Type != model.A || back[1].Type != model.MX {
|
||||
t.Fatalf("round-trip mismatch: %+v", back)
|
||||
}
|
||||
if back[0].Values[0] != "1.2.3.4" || back[1].TTL != 3600 {
|
||||
t.Fatalf("field mismatch: %+v", back)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user