MCPcopy
hub / github.com/qax-os/excelize / TestGetPicture

Function TestGetPicture

picture_test.go:220–358  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

218}
219
220func TestGetPicture(t *testing.T) {
221 f := NewFile()
222 assert.NoError(t, f.AddPicture("Sheet1", "A1", filepath.Join("test", "images", "excel.png"), nil))
223 pics, err := f.GetPictures("Sheet1", "A1")
224 assert.NoError(t, err)
225 assert.Len(t, pics[0].File, 4718)
226 assert.Empty(t, pics[0].Format.AltText)
227 assert.Equal(t, "Picture 2", pics[0].Format.Name)
228 assert.Equal(t, PictureInsertTypePlaceOverCells, pics[0].InsertType)
229
230 f, err = prepareTestBook1()
231 if !assert.NoError(t, err) {
232 t.FailNow()
233 }
234
235 pics, err = f.GetPictures("Sheet1", "F21")
236 assert.NoError(t, err)
237 if !assert.NotEmpty(t, filepath.Join("test", fmt.Sprintf("image1%s", pics[0].Extension))) || !assert.NotEmpty(t, pics[0].File) ||
238 !assert.NoError(t, os.WriteFile(filepath.Join("test", fmt.Sprintf("image1%s", pics[0].Extension)), pics[0].File, 0o644)) {
239 t.FailNow()
240 }
241
242 // Try to get picture from a worksheet with illegal cell reference
243 _, err = f.GetPictures("Sheet1", "A")
244 assert.Equal(t, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")), err)
245
246 // Try to get picture from a worksheet that doesn't contain any images
247 pics, err = f.GetPictures("Sheet3", "I9")
248 assert.EqualError(t, err, "sheet Sheet3 does not exist")
249 assert.Len(t, pics, 0)
250
251 // Try to get picture from a cell that doesn't contain an image
252 pics, err = f.GetPictures("Sheet2", "A2")
253 assert.NoError(t, err)
254 assert.Len(t, pics, 0)
255
256 // Test get picture with invalid sheet name
257 _, err = f.GetPictures("Sheet:1", "A2")
258 assert.EqualError(t, err, ErrSheetNameInvalid.Error())
259
260 f.getDrawingRelationships("xl/worksheets/_rels/sheet1.xml.rels", "rId8")
261 f.getDrawingRelationships("", "")
262 f.getSheetRelationshipsTargetByID("", "")
263 f.deleteSheetRelationships("", "")
264
265 // Try to get picture from a local storage file.
266 assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGetPicture.xlsx")))
267
268 f, err = OpenFile(filepath.Join("test", "TestGetPicture.xlsx"))
269 assert.NoError(t, err)
270
271 pics, err = f.GetPictures("Sheet1", "F21")
272 assert.NoError(t, err)
273 if !assert.NotEmpty(t, filepath.Join("test", fmt.Sprintf("image1%s", pics[0].Extension))) || !assert.NotEmpty(t, pics[0].File) ||
274 !assert.NoError(t, os.WriteFile(filepath.Join("test", fmt.Sprintf("image1%s", pics[0].Extension)), pics[0].File, 0o644)) {
275 t.FailNow()
276 }
277

Callers

nothing calls this directly

Calls 15

AddPictureMethod · 0.95
GetPicturesMethod · 0.95
SaveAsMethod · 0.95
CloseMethod · 0.95
GetPictureCellsMethod · 0.95
relsReaderMethod · 0.95
getPictureMethod · 0.95
SetCellFormulaMethod · 0.95
NewFileFunction · 0.85

Tested by

no test coverage detected