From a96ecb4837fc8c034c156b0efc1b651348377893 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 8 Nov 2023 11:13:56 +0200 Subject: [PATCH] Adds some unit test Former-commit-id: b1a59df1c87187a1538f30c447d42bbe96402419 --- go.mod | 6 ++++- go.work | 3 +++ go.work.sum | 15 ++++++++++++ pkg/obiseq/subseq.go | 12 ++++++++-- pkg/obiseq/subseq_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 go.work create mode 100644 go.work.sum create mode 100644 pkg/obiseq/subseq_test.go diff --git a/go.mod b/go.mod index 7e66a37..4f4bf09 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.metabarcoding.org/lecasofts/go/obitools -go 1.20 +go 1.21 require ( github.com/DavidGamba/go-getoptions v0.28.0 @@ -13,6 +13,7 @@ require ( github.com/rrethy/ahocorasick v1.0.0 github.com/schollz/progressbar/v3 v3.13.1 github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.7.0 github.com/tevino/abool/v2 v2.1.0 golang.org/x/exp v0.0.0-20231006140011-7918f672742d gonum.org/v1/gonum v0.14.0 @@ -20,8 +21,11 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.2.1 // indirect github.com/kr/text v0.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) require ( diff --git a/go.work b/go.work new file mode 100644 index 0000000..767c526 --- /dev/null +++ b/go.work @@ -0,0 +1,3 @@ +go 1.21.3 + +use . diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..be488ec --- /dev/null +++ b/go.work.sum @@ -0,0 +1,15 @@ +git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc= +github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM= +github.com/go-fonts/liberation v0.3.0/go.mod h1:jdJ+cqF+F4SUL2V+qxBth8fvBpBDS7yloUL5Fi8GTGY= +github.com/go-latex/latex v0.0.0-20230307184459-12ec69307ad9/go.mod h1:gWuR/CrFDDeVRFQwHPvsv9soJVB/iqymhuZQuJ3a9OM= +github.com/go-pdf/fpdf v0.6.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M= +github.com/goccmack/gocc v0.0.0-20230228185258-2292f9e40198/go.mod h1:DTh/Y2+NbnOVVoypCCQrovMPDKUGp4yZpSbWg5D0XIM= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= +gonum.org/v1/plot v0.10.1/go.mod h1:VZW5OlhkL1mysU9vaqNHnsy86inf6Ot+jB3r+BczCEo= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/pkg/obiseq/subseq.go b/pkg/obiseq/subseq.go index 59c290d..5039e21 100644 --- a/pkg/obiseq/subseq.go +++ b/pkg/obiseq/subseq.go @@ -5,8 +5,16 @@ import ( "fmt" ) -// Returns a sub sequence start from position 'from' included, -// to position 'to' excluded. Coordinates start at position 0. +// Subsequence returns a subsequence of the BioSequence. +// +// Parameters: +// - from: starting position of the subsequence. +// - to: ending position of the subsequence. +// - circular: indicates whether the subsequence should be circular. +// +// Return: +// - *BioSequence: the subsequence of the BioSequence. +// - error: an error if the subsequence parameters are invalid. func (sequence *BioSequence) Subsequence(from, to int, circular bool) (*BioSequence, error) { if from >= to && !circular { return nil, errors.New("from greater than to") diff --git a/pkg/obiseq/subseq_test.go b/pkg/obiseq/subseq_test.go new file mode 100644 index 0000000..ba02bd9 --- /dev/null +++ b/pkg/obiseq/subseq_test.go @@ -0,0 +1,48 @@ +package obiseq + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSubsequence(t *testing.T) { + // Test case 1: Subsequence with valid parameters and non-circular + seq := NewBioSequence("ID1", []byte("ATCG"), "") + sub, err := seq.Subsequence(1, 3, false) + assert.NoError(t, err) + assert.Equal(t, []byte("tc"), sub.Sequence()) + + seq = NewBioSequenceWithQualities("ID1", []byte("ATCG"), "", []byte{40, 30, 20, 10}) + sub, err = seq.Subsequence(1, 3, false) + assert.NoError(t, err) + assert.Equal(t, []byte("tc"), sub.Sequence()) + assert.Equal(t, Quality([]byte{30, 20}), sub.Qualities()) + + // Test case 2: Subsequence with valid parameters and circular + seq2 := NewBioSequence("ID1", []byte("ATCG"), "") + sub2, err2 := seq2.Subsequence(3, 2, true) + assert.NoError(t, err2) + assert.Equal(t, []byte("gat"), sub2.Sequence()) + + seq = NewBioSequenceWithQualities("ID1", []byte("ATCG"), "", []byte{40, 30, 20, 10}) + sub, err = seq.Subsequence(3, 2, true) + assert.NoError(t, err) + assert.Equal(t, []byte("gat"), sub.Sequence()) + assert.Equal(t, Quality([]byte{10, 40, 30}), sub.Qualities()) + + // Test case 3: Subsequence with from greater than to and non-circular + seq3 := NewBioSequence("ID1", []byte("ATCG"), "") + _, err3 := seq3.Subsequence(3, 1, false) + assert.EqualError(t, err3, "from greater than to") + + // Test case 4: Subsequence with from out of bounds + seq4 := NewBioSequence("ID1", []byte("ATCG"), "") + _, err4 := seq4.Subsequence(-1, 2, false) + assert.EqualError(t, err4, "from out of bounds") + + // Test case 5: Subsequence with to out of bounds + seq5 := NewBioSequence("ID1", []byte("ATCG"), "") + _, err5 := seq5.Subsequence(2, 5, false) + assert.EqualError(t, err5, "to out of bounds") +}