11# tests/test_blob.py
22from __future__ import annotations
33
4+ import base64
5+ import mimetypes
46from datetime import datetime , timezone
7+ from pathlib import Path
58from typing import Optional
69
710import pandas as pd
811import pytest
912
10- import cwms
13+ import cwms . catalog . blobs as blobs
1114
1215TEST_OFFICE = "MVP"
1316TEST_BLOB_ID = "PYTEST_BLOB_ALPHA"
2528def ensure_clean_slate ():
2629 """Delete the test blob (if it exists) before/after running this module."""
2730 try :
28- cwms .delete_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
31+ blobs .delete_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
2932 except Exception :
3033 pass
3134 yield
3235 try :
33- cwms .delete_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
36+ blobs .delete_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
3437 except Exception :
3538 pass
3639
@@ -44,7 +47,7 @@ def _find_blob_row(office: str, blob_id: str) -> Optional[pd.Series]:
4447 """
4548 Helper: return the row for blob_id from cwms.get_blobs(...).df if present.
4649 """
47- res = cwms .get_blobs (office_id = office , blob_id_like = blob_id )
50+ res = blobs .get_blobs (office_id = office , blob_id_like = blob_id )
4851 df = res if isinstance (res , pd .DataFrame ) else getattr (res , "df" , None )
4952 if df is None or df .empty :
5053 return None
@@ -55,6 +58,28 @@ def _find_blob_row(office: str, blob_id: str) -> Optional[pd.Series]:
5558 return match .iloc [0 ] if not match .empty else None
5659
5760
61+ def test_store_blob_excel ():
62+ excel_file_path = Path (__file__ ).parent .parent / "resources" / "blob_test.xlsx"
63+ with open (excel_file_path , "rb" ) as f :
64+ file_data = f .read ()
65+ mime_type , _ = mimetypes .guess_type (excel_file_path )
66+ excel_blob_id = "TEST_BLOB_EXCEL"
67+ payload = {
68+ "office-id" : TEST_OFFICE ,
69+ "id" : excel_blob_id ,
70+ "description" : "testing excel file" ,
71+ "media-type-id" : mime_type ,
72+ "value" : base64 .b64encode (file_data ).decode ("utf-8" ),
73+ }
74+ blobs .store_blobs (data = payload )
75+ try :
76+ row = _find_blob_row (TEST_OFFICE , excel_blob_id )
77+ assert row is not None , "Stored blob not found in listing"
78+ finally :
79+ # Cleanup excel
80+ blobs .delete_blob (blob_id = excel_blob_id , office_id = TEST_OFFICE )
81+
82+
5883def test_store_blob ():
5984 # Build request JSON for store_blobs
6085 payload = {
@@ -64,7 +89,7 @@ def test_store_blob():
6489 "media-type-id" : TEST_MEDIA_TYPE ,
6590 "value" : TEST_TEXT ,
6691 }
67- cwms .store_blobs (payload , fail_if_exists = True )
92+ blobs .store_blobs (payload , fail_if_exists = True )
6893
6994 # Verify via listing metadata
7095 row = _find_blob_row (TEST_OFFICE , TEST_BLOB_ID )
@@ -76,14 +101,14 @@ def test_store_blob():
76101 assert TEST_DESC in str (row ["description" ])
77102
78103 # Verify content by downloading
79- content = cwms .get_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
104+ content = blobs .get_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
80105 assert isinstance (content , str ) and content , "Empty blob content"
81106 assert TEST_TEXT in content
82107
83108
84109def test_get_blob ():
85110 # Do a simple read of the blob created in test_store_blob
86- content = cwms .get_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
111+ content = blobs .get_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_ID )
87112 assert TEST_TEXT in content
88113 assert len (content ) >= len (TEST_TEXT )
89114
@@ -97,7 +122,7 @@ def test_update_blob():
97122 "media-type-id" : TEST_MEDIA_TYPE ,
98123 "value" : TEST_TEXT_UPDATED ,
99124 }
100- cwms .update_blob (update , fail_if_not_exists = True )
125+ blobs .update_blob (update , fail_if_not_exists = True )
101126
102127 # Confirm updated metadata
103128 row = _find_blob_row (TEST_OFFICE , TEST_BLOB_UPDATED_ID )
@@ -106,5 +131,5 @@ def test_update_blob():
106131 assert TEST_DESC_UPDATED in str (row ["description" ])
107132
108133 # Verify new content
109- content = cwms .get_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_UPDATED_ID )
134+ content = blobs .get_blob (office_id = TEST_OFFICE , blob_id = TEST_BLOB_UPDATED_ID )
110135 assert TEST_TEXT_UPDATED in content
0 commit comments