import os
import requests
import unittest
from django.test import TestCase
from products.aliyun import *

BASE_DIR = os.path.abspath(os.path.dirname(__file__) + "/..")

class OSSTestCase(TestCase):

    def test_oss_get(self):
        img = open(os.path.join(BASE_DIR, "tests/data/qr.jpg"), 'rb')
        oss_put("test.jpg", img)
        r = oss_get("test.jpg")
        self.assertEqual(r, open(os.path.join(BASE_DIR, "tests/data/qr.jpg"), 'rb').read())

    def test_oss_put(self):
        img = open(os.path.join(BASE_DIR, "tests/data/qr.jpg"), 'rb')
        oss_put("test.jpg", img)

    def test_oss_sign_url(self):
        name = 'test.jpg'
        img = open(os.path.join(BASE_DIR, "tests/data/qr.jpg"), 'rb')
        data = img.read()
        oss_put(name, data)
        url = oss_sign_url(name)
        r = requests.get(url)
        self.assertEqual(data, r.content)

    def test_oss_stat(self):
        r = oss_stat()
        print("object count and bucket size", r['objects'], r['size'])

if __name__ == "__main__":
    unittest.main()
