File: test_ce.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (133 lines) | stat: -rw-r--r-- 4,481 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from unittest import mock

import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID

# See our Development Tips on writing tests for hints on how to write good tests:
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html


@mock_aws
@mock.patch.dict("os.environ", {"MOTO_ENABLE_ISO_REGIONS": "true"})
@pytest.mark.parametrize(
    "region,partition",
    [("us-east-1", "aws"), ("cn-north-1", "aws-cn"), ("us-isob-east-1", "aws-iso-b")],
)
def test_create_cost_category_definition(region, partition):
    client = boto3.client("ce", region_name=region)
    resp = client.create_cost_category_definition(
        Name="ccd",
        RuleVersion="CostCategoryExpression.v1",
        Rules=[
            {"Value": "v", "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}}}
        ],
    )
    assert resp["CostCategoryArn"].startswith(
        f"arn:{partition}:ce::{ACCOUNT_ID}:costcategory/"
    )
    assert "EffectiveStart" in resp


@mock_aws
def test_create_cost_category_definition_with_effective_start():
    client = boto3.client("ce", region_name="ap-southeast-1")
    resp = client.create_cost_category_definition(
        Name="ccd",
        RuleVersion="CostCategoryExpression.v1",
        Rules=[
            {"Value": "v", "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}}}
        ],
        EffectiveStart="2022-11-01T00:00:00Z",
    )
    assert resp["CostCategoryArn"].startswith(f"arn:aws:ce::{ACCOUNT_ID}:costcategory/")
    assert resp["EffectiveStart"] == "2022-11-01T00:00:00Z"


@mock_aws
def test_describe_cost_category_definition():
    client = boto3.client("ce", region_name="us-east-2")
    ccd_arn = client.create_cost_category_definition(
        Name="ccd",
        RuleVersion="CostCategoryExpression.v1",
        Rules=[
            {"Value": "v", "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}}}
        ],
    )["CostCategoryArn"]

    resp = client.describe_cost_category_definition(CostCategoryArn=ccd_arn)[
        "CostCategory"
    ]
    assert resp["Name"] == "ccd"
    assert resp["CostCategoryArn"] == ccd_arn
    assert resp["RuleVersion"] == "CostCategoryExpression.v1"
    assert len(resp["Rules"]) == 1
    assert resp["Rules"][0] == {
        "Value": "v",
        "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}},
    }


@mock_aws
def test_delete_cost_category_definition():
    client = boto3.client("ce", region_name="ap-southeast-1")
    ccd_arn = client.create_cost_category_definition(
        Name="ccd",
        RuleVersion="CostCategoryExpression.v1",
        Rules=[
            {"Value": "v", "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}}}
        ],
    )["CostCategoryArn"]
    ccd_id = ccd_arn.split("/")[-1]

    client.delete_cost_category_definition(CostCategoryArn=ccd_arn)

    with pytest.raises(ClientError) as exc:
        client.describe_cost_category_definition(CostCategoryArn=ccd_arn)
    err = exc.value.response["Error"]
    assert err["Code"] == "ResourceNotFoundException"
    assert err["Message"] == f"No Cost Categories found with ID {ccd_id}"


@mock_aws
def test_update_cost_category_definition():
    client = boto3.client("ce", region_name="us-east-2")
    ccd_arn = client.create_cost_category_definition(
        Name="ccd",
        RuleVersion="CostCategoryExpression.v1",
        Rules=[
            {"Value": "v", "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}}}
        ],
    )["CostCategoryArn"]

    client.update_cost_category_definition(
        CostCategoryArn=ccd_arn,
        RuleVersion="CostCategoryExpression.v1",
        Rules=[
            {"Value": "v", "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}}}
        ],
        SplitChargeRules=[{"Source": "s", "Targets": ["t1"], "Method": "EVEN"}],
    )

    resp = client.describe_cost_category_definition(CostCategoryArn=ccd_arn)[
        "CostCategory"
    ]
    assert resp["Name"] == "ccd"
    assert resp["CostCategoryArn"] == ccd_arn
    assert resp["RuleVersion"] == "CostCategoryExpression.v1"

    assert len(resp["Rules"]) == 1
    assert resp["Rules"][0] == {
        "Value": "v",
        "Rule": {"CostCategories": {"Key": "k", "Values": ["v"]}},
    }

    assert len(resp["SplitChargeRules"]) == 1
    assert resp["SplitChargeRules"][0] == {
        "Source": "s",
        "Targets": ["t1"],
        "Method": "EVEN",
    }