blob_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "io/ioutil"
  8. "testing"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. var testBlob = &Blob{
  12. repo: &Repository{},
  13. TreeEntry: &TreeEntry{
  14. ID: MustIDFromString("176d8dfe018c850d01851b05fb8a430096247353"),
  15. ptree: &Tree{
  16. repo: &Repository{},
  17. },
  18. },
  19. }
  20. func Test_Blob_Data(t *testing.T) {
  21. Convey("Get blob data", t, func() {
  22. _output := `Copyright (c) 2015 All Gogs Contributors
  23. Permission is hereby granted, free of charge, to any person obtaining a copy
  24. of this software and associated documentation files (the "Software"), to deal
  25. in the Software without restriction, including without limitation the rights
  26. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  27. copies of the Software, and to permit persons to whom the Software is
  28. furnished to do so, subject to the following conditions:
  29. The above copyright notice and this permission notice shall be included in
  30. all copies or substantial portions of the Software.
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  37. THE SOFTWARE.`
  38. Convey("Get data all at once", func() {
  39. r, err := testBlob.Data()
  40. So(err, ShouldBeNil)
  41. So(r, ShouldNotBeNil)
  42. data, err := ioutil.ReadAll(r)
  43. So(err, ShouldBeNil)
  44. So(string(data), ShouldEqual, _output)
  45. })
  46. Convey("Get blob data with pipeline", func() {
  47. stdout := new(bytes.Buffer)
  48. err := testBlob.DataPipeline(stdout, nil)
  49. So(err, ShouldBeNil)
  50. So(stdout.String(), ShouldEqual, _output)
  51. })
  52. })
  53. }
  54. func Benchmark_Blob_Data(b *testing.B) {
  55. for i := 0; i < b.N; i++ {
  56. r, _ := testBlob.Data()
  57. ioutil.ReadAll(r)
  58. }
  59. }
  60. func Benchmark_Blob_DataPipeline(b *testing.B) {
  61. stdout := new(bytes.Buffer)
  62. for i := 0; i < b.N; i++ {
  63. stdout.Reset()
  64. testBlob.DataPipeline(stdout, nil)
  65. }
  66. }