|
5 | 5 | updateSchema, |
6 | 6 | } from './schema-traversal'; |
7 | 7 | import Sinon from 'sinon'; |
8 | | -import { update } from 'lodash'; |
9 | 8 |
|
10 | 9 | describe('traverseSchema', function () { |
11 | 10 | let sandbox: Sinon.SinonSandbox; |
@@ -732,3 +731,293 @@ describe('removeField', function () { |
732 | 731 | }); |
733 | 732 | }); |
734 | 733 | }); |
| 734 | + |
| 735 | +describe('renameField', function () { |
| 736 | + describe('field not found', function () { |
| 737 | + it('empty schema', function () { |
| 738 | + const result = updateSchema({ |
| 739 | + fieldPath: ['name'], |
| 740 | + jsonSchema: {}, |
| 741 | + update: 'renameField', |
| 742 | + newFieldName: 'newName', |
| 743 | + }); |
| 744 | + expect(result).to.deep.equal({}); |
| 745 | + }); |
| 746 | + |
| 747 | + it('wrong path', function () { |
| 748 | + const schema = { |
| 749 | + bsonType: 'object', |
| 750 | + properties: { |
| 751 | + person: { |
| 752 | + bsonType: 'object', |
| 753 | + properties: { |
| 754 | + age: { bsonType: 'int' }, |
| 755 | + name: { bsonType: 'string' }, |
| 756 | + }, |
| 757 | + }, |
| 758 | + address: { |
| 759 | + bsonType: 'object', |
| 760 | + properties: { |
| 761 | + street: { bsonType: 'string' }, |
| 762 | + city: { bsonType: 'string' }, |
| 763 | + }, |
| 764 | + }, |
| 765 | + }, |
| 766 | + }; |
| 767 | + const result = updateSchema({ |
| 768 | + fieldPath: ['address', 'age'], |
| 769 | + jsonSchema: schema, |
| 770 | + update: 'renameField', |
| 771 | + newFieldName: 'newName', |
| 772 | + }); |
| 773 | + expect(result).to.deep.equal(schema); |
| 774 | + }); |
| 775 | + }); |
| 776 | + |
| 777 | + describe('flat schema', function () { |
| 778 | + it('rename top level field', function () { |
| 779 | + const schema = { |
| 780 | + bsonType: 'object', |
| 781 | + properties: { |
| 782 | + name: { bsonType: 'string' }, |
| 783 | + age: { bsonType: ['string', 'int'] }, |
| 784 | + }, |
| 785 | + }; |
| 786 | + const result = updateSchema({ |
| 787 | + fieldPath: ['name'], |
| 788 | + jsonSchema: schema, |
| 789 | + update: 'renameField', |
| 790 | + newFieldName: 'newName', |
| 791 | + }); |
| 792 | + expect(result).to.deep.equal({ |
| 793 | + ...schema, |
| 794 | + properties: { |
| 795 | + newName: schema.properties.name, |
| 796 | + age: schema.properties.age, |
| 797 | + }, |
| 798 | + }); |
| 799 | + }); |
| 800 | + }); |
| 801 | + |
| 802 | + describe('nested schema', function () { |
| 803 | + it('rename a field from the middle level', function () { |
| 804 | + const schema = { |
| 805 | + bsonType: 'object', |
| 806 | + properties: { |
| 807 | + person: { |
| 808 | + bsonType: 'object', |
| 809 | + properties: { |
| 810 | + name: { bsonType: 'string' }, |
| 811 | + address: { |
| 812 | + bsonType: 'object', |
| 813 | + properties: { |
| 814 | + street: { bsonType: 'string' }, |
| 815 | + city: { bsonType: 'string' }, |
| 816 | + }, |
| 817 | + }, |
| 818 | + }, |
| 819 | + }, |
| 820 | + }, |
| 821 | + }; |
| 822 | + const result = updateSchema({ |
| 823 | + fieldPath: ['person', 'address'], |
| 824 | + jsonSchema: schema, |
| 825 | + update: 'renameField', |
| 826 | + newFieldName: 'location', |
| 827 | + }); |
| 828 | + expect(result).to.deep.equal({ |
| 829 | + ...schema, |
| 830 | + properties: { |
| 831 | + person: { |
| 832 | + ...schema.properties.person, |
| 833 | + properties: { |
| 834 | + name: schema.properties.person.properties.name, |
| 835 | + location: schema.properties.person.properties.address, |
| 836 | + }, |
| 837 | + }, |
| 838 | + }, |
| 839 | + }); |
| 840 | + }); |
| 841 | + |
| 842 | + it('rename a deeply nested field', function () { |
| 843 | + const schema = { |
| 844 | + bsonType: 'object', |
| 845 | + properties: { |
| 846 | + person: { |
| 847 | + bsonType: 'object', |
| 848 | + properties: { |
| 849 | + name: { bsonType: 'string' }, |
| 850 | + address: { |
| 851 | + bsonType: 'object', |
| 852 | + properties: { |
| 853 | + street: { bsonType: 'string' }, |
| 854 | + city: { bsonType: 'string' }, |
| 855 | + }, |
| 856 | + }, |
| 857 | + }, |
| 858 | + }, |
| 859 | + }, |
| 860 | + }; |
| 861 | + const result = updateSchema({ |
| 862 | + fieldPath: ['person', 'address', 'city'], |
| 863 | + jsonSchema: schema, |
| 864 | + update: 'renameField', |
| 865 | + newFieldName: 'town', |
| 866 | + }); |
| 867 | + expect(result).to.deep.equal({ |
| 868 | + ...schema, |
| 869 | + properties: { |
| 870 | + person: { |
| 871 | + ...schema.properties.person, |
| 872 | + properties: { |
| 873 | + name: schema.properties.person.properties.name, |
| 874 | + address: { |
| 875 | + ...schema.properties.person.properties.address, |
| 876 | + properties: { |
| 877 | + street: |
| 878 | + schema.properties.person.properties.address.properties |
| 879 | + .street, |
| 880 | + town: schema.properties.person.properties.address.properties |
| 881 | + .city, |
| 882 | + }, |
| 883 | + }, |
| 884 | + }, |
| 885 | + }, |
| 886 | + }, |
| 887 | + }); |
| 888 | + }); |
| 889 | + |
| 890 | + it('rename field nested in a mixed type', function () { |
| 891 | + const schema = { |
| 892 | + bsonType: 'object', |
| 893 | + properties: { |
| 894 | + names: { |
| 895 | + anyOf: [ |
| 896 | + { bsonType: 'string' }, |
| 897 | + { |
| 898 | + bsonType: 'object', |
| 899 | + properties: { |
| 900 | + first: { bsonType: 'string' }, |
| 901 | + last: { bsonType: 'string' }, |
| 902 | + }, |
| 903 | + }, |
| 904 | + ], |
| 905 | + }, |
| 906 | + }, |
| 907 | + }; |
| 908 | + const result = updateSchema({ |
| 909 | + fieldPath: ['names', 'first'], |
| 910 | + jsonSchema: schema, |
| 911 | + update: 'renameField', |
| 912 | + newFieldName: 'given', |
| 913 | + }); |
| 914 | + expect(result).to.deep.equal({ |
| 915 | + ...schema, |
| 916 | + properties: { |
| 917 | + names: { |
| 918 | + anyOf: [ |
| 919 | + { bsonType: 'string' }, |
| 920 | + { |
| 921 | + bsonType: 'object', |
| 922 | + properties: { |
| 923 | + last: { bsonType: 'string' }, |
| 924 | + given: { bsonType: 'string' }, |
| 925 | + }, |
| 926 | + }, |
| 927 | + ], |
| 928 | + }, |
| 929 | + }, |
| 930 | + }); |
| 931 | + }); |
| 932 | + |
| 933 | + it('nested in an array of objects', function () { |
| 934 | + const schema = { |
| 935 | + bsonType: 'object', |
| 936 | + properties: { |
| 937 | + addresses: { |
| 938 | + bsonType: 'array', |
| 939 | + items: { |
| 940 | + bsonType: 'object', |
| 941 | + properties: { |
| 942 | + street: { bsonType: 'string' }, |
| 943 | + streetNumber: { bsonType: ['int', 'string'] }, |
| 944 | + city: { bsonType: 'string' }, |
| 945 | + }, |
| 946 | + }, |
| 947 | + }, |
| 948 | + }, |
| 949 | + }; |
| 950 | + const result = updateSchema({ |
| 951 | + fieldPath: ['addresses', 'streetNumber'], |
| 952 | + jsonSchema: schema, |
| 953 | + update: 'renameField', |
| 954 | + newFieldName: 'street_num', |
| 955 | + }); |
| 956 | + expect(result).to.deep.equal({ |
| 957 | + ...schema, |
| 958 | + properties: { |
| 959 | + addresses: { |
| 960 | + ...schema.properties.addresses, |
| 961 | + items: { |
| 962 | + ...schema.properties.addresses.items, |
| 963 | + properties: { |
| 964 | + street: { bsonType: 'string' }, |
| 965 | + city: { bsonType: 'string' }, |
| 966 | + street_num: { bsonType: ['int', 'string'] }, |
| 967 | + }, |
| 968 | + }, |
| 969 | + }, |
| 970 | + }, |
| 971 | + }); |
| 972 | + }); |
| 973 | + |
| 974 | + it('nested in an array of mixed items (including objects)', function () { |
| 975 | + const schema = { |
| 976 | + bsonType: 'object', |
| 977 | + properties: { |
| 978 | + todos: { |
| 979 | + bsonType: 'array', |
| 980 | + items: { |
| 981 | + anyOf: [ |
| 982 | + { |
| 983 | + bsonType: 'object', |
| 984 | + properties: { |
| 985 | + title: { bsonType: 'string' }, |
| 986 | + completed: { bsonType: 'bool' }, |
| 987 | + }, |
| 988 | + }, |
| 989 | + { bsonType: 'string' }, |
| 990 | + ], |
| 991 | + }, |
| 992 | + }, |
| 993 | + }, |
| 994 | + }; |
| 995 | + const result = updateSchema({ |
| 996 | + fieldPath: ['todos', 'completed'], |
| 997 | + jsonSchema: schema, |
| 998 | + update: 'renameField', |
| 999 | + newFieldName: 'done', |
| 1000 | + }); |
| 1001 | + expect(result).to.deep.equal({ |
| 1002 | + ...schema, |
| 1003 | + properties: { |
| 1004 | + todos: { |
| 1005 | + bsonType: 'array', |
| 1006 | + items: { |
| 1007 | + anyOf: [ |
| 1008 | + { |
| 1009 | + bsonType: 'object', |
| 1010 | + properties: { |
| 1011 | + title: { bsonType: 'string' }, |
| 1012 | + done: { bsonType: 'bool' }, |
| 1013 | + }, |
| 1014 | + }, |
| 1015 | + { bsonType: 'string' }, |
| 1016 | + ], |
| 1017 | + }, |
| 1018 | + }, |
| 1019 | + }, |
| 1020 | + }); |
| 1021 | + }); |
| 1022 | + }); |
| 1023 | +}); |
0 commit comments