|
16 | 16 | import datetime |
17 | 17 | import unittest |
18 | 18 |
|
| 19 | +import pydantic |
| 20 | +from google.protobuf import json_format |
19 | 21 | from google.protobuf import struct_pb2 as structpb |
20 | 22 |
|
| 23 | +import crossplane.function.proto.v1.run_function_pb2 as fnv1 |
21 | 24 | from crossplane.function import logging, resource |
22 | 25 |
|
| 26 | +from .testdata.models.io.upbound.aws.s3 import v1beta2 |
| 27 | + |
23 | 28 |
|
24 | 29 | class TestResource(unittest.TestCase): |
25 | 30 | def setUp(self) -> None: |
26 | 31 | logging.configure(level=logging.Level.DISABLED) |
27 | 32 |
|
| 33 | + def test_add(self) -> None: |
| 34 | + @dataclasses.dataclass |
| 35 | + class TestCase: |
| 36 | + reason: str |
| 37 | + r: fnv1.Resource |
| 38 | + source: dict | structpb.Struct | pydantic.BaseModel |
| 39 | + want: fnv1.Resource |
| 40 | + |
| 41 | + cases = [ |
| 42 | + TestCase( |
| 43 | + reason="Updating from a dict should work.", |
| 44 | + r=fnv1.Resource(), |
| 45 | + source={"apiVersion": "example.org", "kind": "Resource"}, |
| 46 | + want=fnv1.Resource( |
| 47 | + resource=resource.dict_to_struct( |
| 48 | + {"apiVersion": "example.org", "kind": "Resource"} |
| 49 | + ), |
| 50 | + ), |
| 51 | + ), |
| 52 | + TestCase( |
| 53 | + reason="Updating an existing resource from a dict should work.", |
| 54 | + r=fnv1.Resource( |
| 55 | + resource=resource.dict_to_struct( |
| 56 | + {"apiVersion": "example.org", "kind": "Resource"} |
| 57 | + ), |
| 58 | + ), |
| 59 | + source={ |
| 60 | + "metadata": {"name": "cool"}, |
| 61 | + }, |
| 62 | + want=fnv1.Resource( |
| 63 | + resource=resource.dict_to_struct( |
| 64 | + { |
| 65 | + "apiVersion": "example.org", |
| 66 | + "kind": "Resource", |
| 67 | + "metadata": {"name": "cool"}, |
| 68 | + } |
| 69 | + ), |
| 70 | + ), |
| 71 | + ), |
| 72 | + TestCase( |
| 73 | + reason="Updating from a struct should work.", |
| 74 | + r=fnv1.Resource(), |
| 75 | + source=resource.dict_to_struct( |
| 76 | + {"apiVersion": "example.org", "kind": "Resource"} |
| 77 | + ), |
| 78 | + want=fnv1.Resource( |
| 79 | + resource=resource.dict_to_struct( |
| 80 | + {"apiVersion": "example.org", "kind": "Resource"} |
| 81 | + ), |
| 82 | + ), |
| 83 | + ), |
| 84 | + TestCase( |
| 85 | + reason="Updating from a Pydantic model should work.", |
| 86 | + r=fnv1.Resource(), |
| 87 | + source=v1beta2.Bucket( |
| 88 | + spec=v1beta2.Spec( |
| 89 | + forProvider=v1beta2.ForProvider(region="us-west-2"), |
| 90 | + ), |
| 91 | + ), |
| 92 | + want=fnv1.Resource( |
| 93 | + resource=resource.dict_to_struct( |
| 94 | + {"spec": {"forProvider": {"region": "us-west-2"}}} |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ] |
| 99 | + |
| 100 | + for case in cases: |
| 101 | + resource.update(case.r, case.source) |
| 102 | + self.assertEqual( |
| 103 | + json_format.MessageToDict(case.want), |
| 104 | + json_format.MessageToDict(case.r), |
| 105 | + "-want, +got", |
| 106 | + ) |
| 107 | + |
28 | 108 | def test_get_condition(self) -> None: |
29 | 109 | @dataclasses.dataclass |
30 | 110 | class TestCase: |
|
0 commit comments