lookup_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package filex
  2. import (
  3. "os"
  4. "testing"
  5. "git.i2edu.net/i2/go-zero/core/fs"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestSplitLineChunks(t *testing.T) {
  9. const text = `first line
  10. second line
  11. third line
  12. fourth line
  13. fifth line
  14. sixth line
  15. seventh line
  16. `
  17. fp, err := fs.TempFileWithText(text)
  18. assert.Nil(t, err)
  19. defer func() {
  20. fp.Close()
  21. os.Remove(fp.Name())
  22. }()
  23. offsets, err := SplitLineChunks(fp.Name(), 3)
  24. assert.Nil(t, err)
  25. body := make([]byte, 512)
  26. for _, offset := range offsets {
  27. reader := NewRangeReader(fp, offset.Start, offset.Stop)
  28. n, err := reader.Read(body)
  29. assert.Nil(t, err)
  30. assert.Equal(t, uint8('\n'), body[n-1])
  31. }
  32. }
  33. func TestSplitLineChunksNoFile(t *testing.T) {
  34. _, err := SplitLineChunks("nosuchfile", 2)
  35. assert.NotNil(t, err)
  36. }
  37. func TestSplitLineChunksFull(t *testing.T) {
  38. const text = `first line
  39. second line
  40. third line
  41. fourth line
  42. fifth line
  43. sixth line
  44. `
  45. fp, err := fs.TempFileWithText(text)
  46. assert.Nil(t, err)
  47. defer func() {
  48. fp.Close()
  49. os.Remove(fp.Name())
  50. }()
  51. offsets, err := SplitLineChunks(fp.Name(), 1)
  52. assert.Nil(t, err)
  53. body := make([]byte, 512)
  54. for _, offset := range offsets {
  55. reader := NewRangeReader(fp, offset.Start, offset.Stop)
  56. n, err := reader.Read(body)
  57. assert.Nil(t, err)
  58. assert.Equal(t, []byte(text), body[:n])
  59. }
  60. }