<commit_before>package mcstore

import (
	"fmt"
	"net/http/httptest"
	"time"

	"github.com/emicklei/go-restful"
	"github.com/materials-commons/config"
	"github.com/materials-commons/gohandy/ezhttp"
	c "github.com/materials-commons/mcstore/cmd/pkg/client"
	"github.com/materials-commons/mcstore/pkg/db/dai"
	"github.com/materials-commons/mcstore/pkg/testdb"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/parnurzeal/gorequest"
	"github.com/willf/bitset"
	"net/http"
)

var _ = fmt.Println

var _ = Describe("UploadResource", func() {
	Describe("findStartingBlock method tests", func() {
		var (
			blocks *bitset.BitSet
		)

		BeforeEach(func() {
			blocks = bitset.New(10)
		})

		It("Should return 1 if no blocks have been set", func() {
			block := findStartingBlock(blocks)
			Expect(block).To(BeNumerically("==", 1))
		})

		It("Should return 2 if the first block has been uploaded", func() {
			// BitSet starts a zero. Flowjs starts at 1. So we have to adjust.
			blocks.Set(0)
			block := findStartingBlock(blocks)
			Expect(block).To(BeNumerically("==", 2))
		})

		It("Should return 1 if only the last block as been uploaded", func() {
			blocks.Set(9)
			block := findStartingBlock(blocks)
			Expect(block).To(BeNumerically("==", 1))
		})

		It("Should return 2 if only 2 has not been set (all others set)", func() {
			complement := blocks.Complement()
			complement.Clear(1) // second block
			block := findStartingBlock(complement)
			Expect(block).To(BeNumerically("==", 2))
		})
	})

	Describe("Upload REST API method tests", func() {
		var (
			client        *gorequest.SuperAgent
			server        *httptest.Server
			container     *restful.Container
			rr            *httptest.ResponseRecorder
			uploadRequest CreateUploadRequest
			uploads       dai.Uploads
		)

		BeforeEach(func() {
			client = c.NewGoRequest()
			container = NewServicesContainer(testdb.Sessions)
			server = httptest.NewServer(container)
			rr = httptest.NewRecorder()
			config.Set("mcurl", server.URL)
			uploadRequest = CreateUploadRequest{
				ProjectID:     "test",
				DirectoryID:   "test",
				DirectoryPath: "test/test",
				FileName:      "testreq.txt",
				FileSize:      4,
				ChunkSize:     2,
				FileMTime:     time.Now().Format(time.RFC1123),
				Checksum:      "abc123456",
			}
			uploads = dai.NewRUploads(testdb.RSessionMust())
		})

		var (
			createUploadRequest = func(req CreateUploadRequest) (*CreateUploadResponse, error) {
				r, body, errs := client.Post(Url("/upload")).Send(req).End()
				if err := ToError(r, errs); err != nil {
					return nil, err
				}

				var uploadResponse CreateUploadResponse
				if err := ToJSON(body, &uploadResponse); err != nil {
					return nil, err
				}
				return &uploadResponse, nil<commit_msg>Get rid of removed structure member DirectoryPath.<commit_after>package mcstore

import (
	"fmt"
	"net/http/httptest"
	"time"

	"github.com/emicklei/go-restful"
	"github.com/materials-commons/config"
	"github.com/materials-commons/gohandy/ezhttp"
	c "github.com/materials-commons/mcstore/cmd/pkg/client"
	"github.com/materials-commons/mcstore/pkg/db/dai"
	"github.com/materials-commons/mcstore/pkg/testdb"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/parnurzeal/gorequest"
	"github.com/willf/bitset"
	"net/http"
)

var _ = fmt.Println

var _ = Describe("UploadResource", func() {
	Describe("findStartingBlock method tests", func() {
		var (
			blocks *bitset.BitSet
		)

		BeforeEach(func() {
			blocks = bitset.New(10)
		})

		It("Should return 1 if no blocks have been set", func() {
			block := findStartingBlock(blocks)
			Expect(block).To(BeNumerically("==", 1))
		})

		It("Should return 2 if the first block has been uploaded", func() {
			// BitSet starts a zero. Flowjs starts at 1. So we have to adjust.
			blocks.Set(0)
			block := findStartingBlock(blocks)
			Expect(block).To(BeNumerically("==", 2))
		})

		It("Should return 1 if only the last block as been uploaded", func() {
			blocks.Set(9)
			block := findStartingBlock(blocks)
			Expect(block).To(BeNumerically("==", 1))
		})

		It("Should return 2 if only 2 has not been set (all others set)", func() {
			complement := blocks.Complement()
			complement.Clear(1) // second block
			block := findStartingBlock(complement)
			Expect(block).To(BeNumerically("==", 2))
		})
	})

	Describe("Upload REST API method tests", func() {
		var (
			client        *gorequest.SuperAgent
			server        *httptest.Server
			container     *restful.Container
			rr            *httptest.ResponseRecorder
			uploadRequest CreateUploadRequest
			uploads       dai.Uploads
		)

		BeforeEach(func() {
			client = c.NewGoRequest()
			container = NewServicesContainer(testdb.Sessions)
			server = httptest.NewServer(container)
			rr = httptest.NewRecorder()
			config.Set("mcurl", server.URL)
			uploadRequest = CreateUploadRequest{
				ProjectID:     "test",
				DirectoryID:   "test",
				FileName:      "testreq.txt",
				FileSize:      4,
				ChunkSize:     2,
				FileMTime:     time.Now().Format(time.RFC1123),
				Checksum:      "abc123456",
			}
			uploads = dai.NewRUploads(testdb.RSessionMust())
		})

		var (
			createUploadRequest = func(req CreateUploadRequest) (*CreateUploadResponse, error) {
				r, body, errs := client.Post(Url("/upload")).Send(req).End()
				if err := ToError(r, errs); err != nil {
					return nil, err
				}

				var uploadResponse CreateUploadResponse
				if err := ToJSON(body, &uploadResponse); err != nil {
					return nil, err
				}
				return &uploadResponse, nil
{'fraction_non_alphanumeric': 0.1372725635020717, 'fraction_numerical': 0.009727976941091695, 'mean_word_length': 3.3889328063241106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218638', 'n_tokens_mistral': 2200, 'n_tokens_neox': 1859, 'n_words': 403}