import json

import responses
from conftest import Mock

from upcloud_api import IPAddress, Server, Storage, login_user_block


class TestCreateServer:
    def test_storage_prepare_post_body(self, manager):
        s1 = Storage(os='01000000-0000-4000-8000-000030200200', size=10)
        body1 = s1.to_dict()
        assert body1['tier'] == 'maxiops'
        assert body1['size'] == 10

        s2 = Storage(size=100)
        body2 = s2.to_dict()
        assert body2['tier'] == 'maxiops'
        assert body2['size'] == 100

    def test_storage_prepare_post_body_optional_attributes(self, manager):
        s2 = Storage(size=100, address='virtio:0')
        body2 = s2.to_dict()

        assert body2['tier'] == 'maxiops'
        assert body2['size'] == 100
        assert body2['address'] == 'virtio:0'

    def test_server_init(self, manager):
        server1 = Server(
            core_number=2,
            memory_amount=1024,
            hostname='my.example.com',
            zone='us-chi1',
            storage_devices=[
                Storage(os='01000000-0000-4000-8000-000030200200', size=10),
                Storage(size=100, title='storage disk 1'),
            ],
        )

        assert server1.title == 'my.example.com'
        assert server1.core_number == 2
        assert server1.memory_amount == 1024
        assert server1.hostname == server1.title
        assert server1.zone == 'us-chi1'

    def test_server_prepare_post_body(self):
        server = Server(
            core_number=2,
            memory_amount=1024,
            hostname='my.example.com',
            zone='us-chi1',
            storage_devices=[
                Storage(os='01000000-0000-4000-8000-000030200200', size=10),
                Storage(),
            ],
        )
        body = server.prepare_post_body()

        s1 = body['server']['storage_devices']['storage_device'][0]
        assert s1['title'] == 'my.example.com OS disk'